Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello, i am not an software developer but need a way to change this code with a much more faster one. Maybe in Libtiff or something else. This code converting a tiff image to 2 bpp int array for a printing machine. But its too slow especially in larger images like 30000 width, 100000 height. Is there anybody can help me how can i make it faster. Tyvm.






C#
public static string[] imagePaths = new string[16];
public static int LoadedPlaneCount = 0;
public static int Bpp = 2;

public static BitmapSource bitmap;
public static int StartLine { get; set; }
public static int EndLine { get; set; }

public static int DocWidth { get { return bitmap.PixelWidth; } }
public static int DocHeight { get { return bitmap.PixelHeight; } }

public static bool LoadImage(string Path)
{
    try
    {
        bitmap = new BitmapImage(new Uri(Path));
        if (bitmap.Format.BitsPerPixel != 32)
        {
            bitmap = new FormatConvertedBitmap(bitmap, PixelFormats.Bgra32, BitmapPalettes.Gray256, 0);
        }
        StartLine = 0;
        EndLine = bitmap.PixelHeight;
        return true;
    }
    catch (Exception)
    {
        return false;
    }
}

public static int[] GetImageBuffer(int yTop, int trueBpp)
{
    int outbpp = (trueBpp == 3) ? 4 : trueBpp;
    int width = bitmap.PixelWidth;
    int height = bitmap.PixelHeight;
    int bufwidthDWORDs = (((width * outbpp) + 31) >> 5);
    int isize = bufwidthDWORDs * height;
    int[] buff = new int[isize];
    if (null == buff)
    {
        return null;
    }
    int dp = 0; // Destination pointer (offset).

    byte[] lineData = new byte[bufwidthDWORDs * (32 / outbpp) * 4];

    for (int y = StartLine; y < EndLine; y++)
    {
        bitmap.CopyPixels(new System.Windows.Int32Rect(0, y, width, 1), lineData, width * 4, 0);

            for (int x = 0; x < width; x += 2)
            {
                int byteoff = x >> 1;
                int shift = 4 * (7 - (byteoff & 7));    // Bit count to shift left
                // Get first source pixel
                byte b = ArgbToGrey(lineData, x * 4);
                b ^= 0xFF;      // Invert for printer
                b >>= 4;        // Only interested in 2 msbs
                // Get second source pixel
                byte b2 = ArgbToGrey(lineData, (x + 1) * 4);
                b2 ^= 0xFF;     // Invert for printer
                b2 >>= 6;       // Only interested in 2 msbs

                b = (byte)((b & 0x0C) | (b2 & 0x3));    // Align pixels
                buff[dp] |= ((int)b) << shift;          // Write both pixels
                if ((x >= width - 2) || ((byteoff & 7) == 7))
                {
                    dp++;
                }
        }
        
    }
    return buff;
}

public static byte ArgbToGrey(byte[] image, int offset)
{
   return (byte)((float)image[offset + 2] * 0.3 + (float)image[offset + 1] * 0.59 + (float)image[offset + 0] * 0.11);
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900