Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warning: invalid resolution fix. #399

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Warning: invalid resolution fix. #399

wants to merge 2 commits into from

Conversation

tdhintz
Copy link
Contributor

@tdhintz tdhintz commented Mar 9, 2018

Fix for #397

@charlesw
Copy link
Owner

charlesw commented Mar 12, 2018 via email

@Shazter
Copy link

Shazter commented May 13, 2018

I'm a beginner, but the fix doesnt work for me, because Bitmap is unknown in PIX.cs. For Net Standard 2.0 compability I made this:

Changes Pix.cs:

public static Pix Create(int width, int height, int depth, int xres, int yres)
       {
           if (!AllowedDepths.Contains(depth))
               throw new ArgumentException("Depth must be 1, 2, 4, 8, 16, or 32 bits.", "depth");

           if (width <= 0) throw new ArgumentException("Width must be greater than zero", "width");
           if (height <= 0) throw new ArgumentException("Height must be greater than zero", "height");

           var handle = Interop.LeptonicaApi.Native.pixCreate(width, height, depth);
           if (handle == IntPtr.Zero) throw new InvalidOperationException("Failed to create pix, this normally occurs because the requested image size is too large, please check Standard Error Output.");
           var pix = Create(handle);
           Interop.LeptonicaApi.Native.pixSetResolution(pix.Handle, xres, yres);

           return pix;
       }

Changes BitmapToPixConverter.cs:

public Pix Convert(Bitmap img)
        {
            var pixDepth = GetPixDepth(img.PixelFormat);
            var xres = (int)img.HorizontalResolution;
            var yres = (int)img.VerticalResolution;
            var pix = Pix.Create(img.Width, img.Height, pixDepth, xres,yres);
           
            BitmapData imgData = null;
            PixData pixData = null;
            try
            { 

                if ((img.PixelFormat & PixelFormat.Indexed) == PixelFormat.Indexed) {
                    CopyColormap(img, pix);
                }

                // transfer data
                imgData = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly, img.PixelFormat);
                pixData = pix.GetData();

                if (imgData.PixelFormat == PixelFormat.Format32bppArgb) {
                    TransferDataFormat32bppArgb(imgData, pixData);
                } else if (imgData.PixelFormat == PixelFormat.Format24bppRgb) {
                    TransferDataFormat24bppRgb(imgData, pixData);
                } else if (imgData.PixelFormat == PixelFormat.Format8bppIndexed) {
                    TransferDataFormat8bppIndexed(imgData, pixData);
                } else if (imgData.PixelFormat == PixelFormat.Format1bppIndexed) {
                    TransferDataFormat1bppIndexed(imgData, pixData);
                }
                return pix;
            } catch (Exception) {
                pix.Dispose();
                throw;
            } finally {
                if (imgData != null) {
                    img.UnlockBits(imgData);
                }
            }
        }

@Shazter
Copy link

Shazter commented May 14, 2018

Converting issue between float and int.

Fixed:

public Pix Convert(Bitmap img)
        {
            var pixDepth = GetPixDepth(img.PixelFormat);
            var xres = Convert.ToInt32(img.HorizontalResolution);
            var yres = Convert.ToInt32(img.VerticalResolution);
            var pix = Pix.Create(img.Width, img.Height, pixDepth, xres,yres);
           
            BitmapData imgData = null;
            PixData pixData = null;
            try
            { 

                if ((img.PixelFormat & PixelFormat.Indexed) == PixelFormat.Indexed) {
                    CopyColormap(img, pix);
                }

                // transfer data
                imgData = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly, img.PixelFormat);
                pixData = pix.GetData();

                if (imgData.PixelFormat == PixelFormat.Format32bppArgb) {
                    TransferDataFormat32bppArgb(imgData, pixData);
                } else if (imgData.PixelFormat == PixelFormat.Format24bppRgb) {
                    TransferDataFormat24bppRgb(imgData, pixData);
                } else if (imgData.PixelFormat == PixelFormat.Format8bppIndexed) {
                    TransferDataFormat8bppIndexed(imgData, pixData);
                } else if (imgData.PixelFormat == PixelFormat.Format1bppIndexed) {
                    TransferDataFormat1bppIndexed(imgData, pixData);
                }
                return pix;
            } catch (Exception) {
                pix.Dispose();
                throw;
            } finally {
                if (imgData != null) {
                    img.UnlockBits(imgData);
                }
            }
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants