Leon
Leon

Reputation: 1031

How to resize 8bpp bitmap image in c#?

This is for duplication recognition.

I can covert a image into 8bpp bitmap but since it's an indexed pixel format, I don't know how to resize it smaller. Is there anyway to do it without changing it to a higher pixel format (which will introduce more unwanted color)?

Thanks

Upvotes: 0

Views: 1399

Answers (2)

Ted Spence
Ted Spence

Reputation: 2668

This is a common problem in 2D computer graphics. The overall question that needs to be asked is what you want to accomplish. Is it:

  1. Do you want a smaller 8 bits per pixel image that most closely represents the original image just in a smaller form?
  2. Do you want a smaller 8 bits per pixel image that has the same identical indexed color map (palette) as the original image?

For example, when I was designing 2D games that operated on 8bpp displays, I asked the artists to produce all original source images in 24-bit color at full resolution. Then, I created a workflow process that unified their color palettes all at once, so they all used an optimized palette and were represented as accurately as possible. If this is your case, I'd suggest you go back to the original source image and reconvert it to the smaller size.

Sometimes, I'm doing a one off experiment, or working with art on the fly, and I don't care about making sure all images use an identical palette. In this case, I would convert the image to 24bpp in memory, resize it using bicubic interpolation (which is a high quality resizing algorithm), and then reconvert it back to 8bpp. The downside is that doing it this way generates a new and unique palette for the resized image.

But if you really want to resize the image without changing the palette and without upconverting to 24bpp internally, you have to use one of two really bad choices: you can either use dithering to attempt to pick the best color according to the size maps - a process that is tricky and involves carefully tracking your color error - or you can simply drop scan lines until the image matches the correct size you want.

For reference, traditional 2D consoles like the Neo Geo followed the "line drop" approach, which results in their quaint and jagged graphics appearance today. Modern consoles always resample images in 16, 24, or 32 bit color, sometimes even using floating point to represent color values.

Upvotes: 1

0xFE
0xFE

Reputation: 344

Normally, when you shrink a picture, you do some sort of interpolation on the colors. Imagine you have a 2x2 picture and the top two pixels are black, and the bottom 2 are white. You want to shrink this into a 1x2 picture. The two pixels would be a mixture of black and white, which is gray.

BB  -->   GG
WW

where G = gray (50% black, 50% white), B = Black, and W = white.
However, the gray color that ends up in the final image was not present in the original image. For indexed images, this is a problem. You can either pick one of the pixels (randomly or using some algorithm):

BB  -->   BW
WW

or interpolate and then choose the color in the index that is closest to the result of the interpolation. So maybe you don't have the 50% gray in your index, but you do have a 40%. That would be pretty close, so you could choose that instead. If there aren't any colors that are close enough, you could fall back to the first method.

Thirdly, you could convert to a normal bitmap, resize, then convert back to indexed, but based on how you worded your question, it seems like you want to avoid that.

Upvotes: 0

Related Questions