0

I'm trying to use Magick.NET to save palettized images generated by my program. Currently I'm using the following process:

  1. Create the image with MagickImage img = new(MagickColors.Green, (uint)Width, (uint)Height);
  2. Set img.ColorType = ColorType.Palette; and img.ColormapSize = 256;. Note that for the purpose of my issue (slow), it does not matter if step 2 is merged into the construction step 1.
  3. Copy the pixel data from my buffer to the image with SetByteArea, passing 4 identical bytes of the palette index for each pixel (so I don't have to care which byte is used to store the palette index).
  4. Set up the palette with SetColormapColor

This works, but step 2 (specifically setting ColorType is EXTREMELY slow; for the image I'm working with (8192x7680) it takes 18 seconds. Step 1 is also slower than I'd expect, taking about 2 seconds. How can I drastically speed up this process?

4
  • Manually constructing a TGA file in a buffer (almost instantaneous) then passing it to the MagickImage constructor reduces the construction time to about 2 sec; not great, but tolerable. However this really isn't an answer to the question as the entire point of using Magick.NET was so that I wouldn't need to write any file format code... Commented Apr 16 at 6:42
  • Avoid ColorType.Palette Set the image to palettized mode during creation or use a direct pixel-writing method that assumes an indexed format. Write 1 byte per pixel (palette index) instead of 4, using a method designed for indexed images. Define the colormap in a single operation or during initialization. Use Magick.NET’s pixel cache or direct buffer manipulation to reduce copying. Use MagickFormat.Png8 to ensure the image is treated as 8-bit indexed from the start. Commented Apr 16 at 9:36
  • @SzymonRoziewski ...how do I do any of that? I tried doing MagickImage img = new(MagickColors.Green, (uint)Width, (uint)Height) { ColorType = ColorType.Palette, ColormapSize = 256 }; but this was not any faster than doing it in separate steps. Commented Apr 16 at 21:20
  • Can you post here a working example? Commented Apr 17 at 10:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.