How is a sepia tone created?

后端 未结 4 1066
独厮守ぢ
独厮守ぢ 2020-12-28 20:24

What are the basic operations needed to create a sepia tone? My reference point is the perl imagemagick library, so I can easily use any basic operation. I\'ve tried to quan

4条回答
  •  忘掉有多难
    2020-12-28 20:38

    This is in C#, however, the basic concepts are the same. You will likely be able to convert this into perl.

      private void SepiaBitmap(Bitmap bmp)
    {
        Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
        System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
            System.Drawing.Imaging.PixelFormat.Format32bppRgb);
    
        IntPtr ptr = bmpData.Scan0;
    
        int numPixels = bmpData.Width * bmp.Height;
        int numBytes = numPixels * 4;
        byte[] rgbValues = new byte[numBytes];
    
        System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, numBytes);
        for (int i = 0; i < rgbValues.Length; i += 4)
        {
            rgbValues[i + 2] = (byte)Math.Min((.393 * red) + (.769 * green) + (.189 * (blue)), 255.0); //red
            rgbValues[i + 1] = (byte)Math.Min((.349 * red) + (.686 * green) + (.168 * (blue)), 255.0); //green
            rgbValues[i + 0] = (byte)Math.Min((.272 * red) + (.534 * green) + (.131 * (blue)), 255.0); //blue
            if ((rgbValues[i + 2]) > 255)
            {
                rgbValues[i + 2] = 255; 
            }
    
            if ((rgbValues[i + 1]) > 255)
            {
                rgbValues[i + 1] = 255;
            }
            if ((rgbValues[i + 0]) > 255)
            {
                rgbValues[i + 0] = 255;
            }
        }
    
        System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, numBytes);
        this.Invalidate();
        bmp.UnlockBits(bmpData);
    
    }
    

提交回复
热议问题