fast convert any bmp to rgb byte array - C#

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
eitanmiz
Posts: 7
Joined: 2018-11-12T15:06:01-07:00
Authentication code: 1152

fast convert any bmp to rgb byte array - C#

Post by eitanmiz » 2018-11-12T15:29:38-07:00

Hello,
In my code I did:
MagickImage im = new MagickImage(<my file name>);
IPixelCollection pc = im.GetPixelsUnsafe();

...
The image is grayscale so it has 2 channels,
but I want to fast convert it to 3 colors (and save the binary data - only the byte array of rgb to file).
Also convert to 1 color.
How can I do that?

For grayscale image I did:
byte[] b = pc.GetPixelArea(...)
but b got values of "R" and "G" (but not value for "B").

Also, I want to write a small program in C#, that create a binary file for grayscale (or anything), and load the binary again - save part of it or load part of the file.

Maybe I can do as following (for loading), but I didn't see PixelMapping with two colors or one channel.

MagickReadSettings mr = new MagickReadSettings();
PixelStorageSettings ps = new PixelStorageSettings(imgWidth, imgHeight, StorageType.Char, PixelMapping.RGB);

Is there any good way to save to just binary file, and load it quickly (from/to any image format).
Need to resolve very big image files, even 100k x 1000k pixels (maybe jp2 is good for that), and program will not be out of memory.
I need also to load part of the big image (and also save part of the big file).

Thanks.

User avatar
dlemstra
Posts: 1513
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: fast convert any bmp to rgb byte array - C#

Post by dlemstra » 2018-11-13T10:19:55-07:00

You will run into memory issues when you try to allocate a byte array for images that are 100000 x 10000000 pixels. You will need a machine that has 3000 GB memory available. What is the problem that you are trying to solve? It looks like you want multiple things?
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate

eitanmiz
Posts: 7
Joined: 2018-11-12T15:06:01-07:00
Authentication code: 1152

Re: fast convert any bmp to rgb byte array - C#

Post by eitanmiz » 2018-11-14T01:44:26-07:00

For very big image - I assume that "tiles" (area of the picture) can solve the problem, and I can handle and save each tile separately.
(maybe that is TIF and JP2 for).

If that solve the issue, I also assume that for 100000 x 10000000 pixels ImageMagick doesn't encounter memory overflow when creating the image.

How doing partial load and partial save - I don't know.

For the other issue - I have already solve it (creating image from binary data, but not RGB binary data - I found that I can do something like:
byte[] pixels;
// load the pixels of data as byte values of "RRRRR" or "RGRG" or "RGB" etc.
PixelStorageSettings ps = new PixelStorageSettings(width, height, StorageType.Char, "R");
MagickImage mi = new MagickImage(pixels, ps);
)

For the code above - I want that image magic represent a specific tile, so I can save it directly to the tif file (only a specific part).

Thanks.

Post Reply