Coder Social home page Coder Social logo

koszeggy / kgysoft.drawing Goto Github PK

View Code? Open in Web Editor NEW
58.0 58.0 8.0 124.01 MB

KGy SOFT Drawing is a library for advanced image, icon and graphics handling.

Home Page: https://kgysoft.net/drawing

License: Other

C# 99.92% Smalltalk 0.07% Batchfile 0.01%
animated-gif animgif bitmaps c-sharp dithering drawing gif gif-animation graphics icons images kgysoft libraries metafiles quantization quantizing

kgysoft.drawing's People

Contributors

koszeggy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

kgysoft.drawing's Issues

Question

KGySoft.Drawing is REALLY GREAT! I wish I'd know about it years ago. It has already solved several issues I've been working on for weeks. The code is really clean and well thought out.

I don't see any support for .webp images, though. Am I missing something? Is webp support planned as a future enhancement? I've been using WebP-wrapper from JosePineiro, but it requires an unmanaged dll and doesn't support animated webp images. This is my only imaging issue that KGySoft.Drawing doesn't seem to solve.

Also, I try to support open source projects when I find them useful. I'm retired and on a fixed income, but I would donate a few dollars to your project if you had a way to accept PayPal donations on your website. Thanks again for your really useful code.

ImageExtensions.ConvertPixelFormat() fails for images with PixelFormat == Cmyk32

ConvertPixelFormat() throws: System.InvalidOperationException: 'Internal Error: Unexpected pixel format 8207'
The failing line is: var newImage = original.ConvertPixelFormat(PixelFormat.Format8bppIndexed, PredefinedColorsQuantizer.FromCustomFunction(c => c.ToGray()));

The failing image is attached. Paint, Paint.net, and Photos can all open the image
https://user-images.githubusercontent.com/79710713/174828173-be47e13d-a8e5-42ea-97d8-9be913ebde9c.jpg
.

Ditherer parameter not optional?

I'm using your library to dither an image in VB.net. That works fine as long as the second parameter is passed with the Dither function.

This works:
bmp.Dither(quantizer, ditherer)

But when I don't want to dither, this doesn't seem to work:
bmp.Dither(quantizer, Nothing)

I get anArgumentNullException error. According to your documentation the second parameter is optional.

Can't update to System.Drawing.Common version 6.0.0

When I try to upgrade the System.Drawing.Common package from version 5.0.3 to 6.0.0, it says it's incompatible with KGySoft.Drawing version 6.3.2.0 . Reading the MS doco for the new System.Drawing.Common package implies that it is now 'Windows Only' and no longer cross platform.

My usage of KGySoft.Drawing and System.Drawing.Common IS 'Windows Only'. Is it possible to use them together in a 'Windows Only' app? My app currently uses .NET Framework 4.8 but will soon be migrated to .NET 6.0.

FWIW, I downloaded the KGySoft.Drawing source code and am compiling it into my app ( to make it easier to step through the code in the debugger). I haven't modifed the code or .csproj file.

Already quantized frames are processed again and not dithered on save

Thank you so much for providing this wonderful tool for free! I'm making a small program to export frames to animation. I already generated preview of the bitmaps and want to export that result directly bit it seems it is being qunatized and not dithered even tho the original preview frames are already processed? What I'm doing wrong? If I use the default windows encoder saving is two times faster so there is something happening under the hood here...

  protected void SaveGifAnimation()
        {       
            var imageArray = previewImages.Select(i => BitmapExtensions.GetReadableBitmapData((Bitmap)i));
            double time = 1000 / (double)nudFPS.Value;
            double percentMultiplier = 100 / previewImages.Count;
            var agf = new AnimatedGifConfiguration(imageArray, TimeSpan.FromMilliseconds(time));
            agf.AnimationMode = (AnimationMode) nudRepeat.Value;
            agf.SizeHandling = AnimationFramesSizeHandling.Resize;
            agf.ReportOverallProgress = true;

            using (var stream = new MemoryStream())
            {
                var progresseReporter = new TaskConfig();
                progresseReporter.Progress = new SaveProgress(saveGIF.FileName, UpdateTitleBar);
                
                GifEncoder.EncodeAnimationAsync(agf, stream, progresseReporter).Wait();

                stream.Position = 0;
                using (var fileStream = new FileStream(saveGIF.FileName, FileMode.Create, FileAccess.Write, FileShare.None, 81920, false))
                {
                    stream.WriteTo(fileStream);
                }
            }
        }

EDIT:
Actually I figured half of the story. If I change the (i => BitmapExtensions.GetReadableBitmapData((Bitmap)i)); to i => BitmapExtensions.GetReadWriteBitmapData((Bitmap)i.Clone())); the indexed format is preserved to the encoder but then the progress report starts at 50% and finishes 100 but getting exponentially slower. Overall time seems comparable or a hair faster.

Conversion of Format8bppIndexed image to gray fails (rather subtly)

I am having an issue when converting 8bppIndexed images to grayscale. The attached image is a png image with Pixelformat.Format8bppIndexed https://user-images.githubusercontent.com/79710713/177016592-a2410453-908f-4914-9c25-c526d80c836d.png and exhibits the problem. If I use:

        imageCI.Image = original.ConvertPixelFormat(PixelFormat.Format8bppIndexed, 
            PredefinedColorsQuantizer.FromCustomFunction(c => c.ToGray()));

to convert it to a gray image, some pixels are not gray. The problem also occurs with 8bppIndexed gif and bmp images that start out with a color (not all-gray) Palette.
FWIW, if I use:

        imageCI.Image = original.ConvertPixelFormat(PixelFormat.Format24bppRgb, 
            PredefinedColorsQuantizer.FromCustomFunction(c => c.ToGray()));

it works fine; a completely gray image is returned. The code I'm using to test that an image is gray is:

public static bool IsGray(this Bitmap image) {
    Bitmap img = null;
    try {
        if (((int) image.PixelFormat) == 8207) {  //8207 == PixelFormat.Cmyk32
            img = image;
            image = image.To24bppRgb();
        }
        BitmapData imageData = null;
        try {
            Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
            imageData = image.LockBits(rect, ImageLockMode.ReadOnly, image.PixelFormat);
            int imageBytesperPixel = Image.GetPixelFormatSize(imageData.PixelFormat) / 8;
            int imageRowPad = Math.Abs(imageData.Stride) - (imageData.Width * imageBytesperPixel);
            int imageSize = imageData.Height * imageData.Stride;
            int imageEnd = (int) IntPtr.Add(imageData.Scan0, imageSize);
            bool is8bppIndexed = image.PixelFormat == PixelFormat.Format8bppIndexed;
            Assert.True((imageBytesperPixel == 1) || (imageBytesperPixel >= 3), 
                    $"Unsupported PixelFormat = {imageData.PixelFormat}");
            unsafe {
                byte* imagePtr = (byte*) imageData.Scan0;
                for (int i = 0; i < imageData.Height; i++) {
                    for (int j = 0; j < imageData.Width; j++) {
                        if (is8bppIndexed) {
                            if ((int) imagePtr >= imageEnd) {
                                continue;
                            }
                            byte index = imagePtr[0];
                            var color = image.Palette.Entries[index];
                            if ((color.R != color.G) || (color.R != color.B)) {
                                return false;
                            }
                        } else {
                            if ((((int) imagePtr) + 2) >= imageEnd) {
                                continue;
                            }
                            byte blue = imagePtr[0];
                            byte green = imagePtr[1];
                            byte red = imagePtr[2];
                            if (red != blue || (blue != green)) {
                                return false;
                            }
                        }
                        imagePtr += imageBytesperPixel;
                    }
                    imagePtr += imageRowPad;
                }
            }
        } finally {
            image?.UnlockBits(imageData);
        }
        return true;
    } finally {
        if (img != null) {
            img.Dispose();
        }
    }
}

I'm using KGySoft.Drawing version 6.3.2

Fast image width and height

I'm looking for a FAST way to determine the width and height of an image (which may be of type jpeg, png, or gif). Loading the entire image to retrieve its size is taking way too long. I think this info is all in the image header, so it should be possible to get it by reading just the first few dozen bytes of the image.
I've used (and donated to) KGySoft.Drawing in the past, and this seems like an extension it might support, but I haven't found a method that quickly returns the image size. Have I just missed something, or is this a "new feature" request?

Either way, thanks for a great toolkit!
David Ei

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.