Writing pixels to a pimage (positioning pixels right)
in
Programming Questions
•
9 months ago
Hi,
I am trying to display an image I receive via serial. I submit different color depths.
The code is below reacts on:
g_grayscaleImage == 0 // 16 bit rgb
g_grayscaleImage == 1 // 8 bit grayscale
g_grayscaleImage == 2 // 4 bit grayscale
The code works fine for 16 and 8 bit. With 4 bit I don’t get the pixels to the right position. (so the part from "else if (g_grayscaleImage==2)" doesn't work)
The “best” result I got with the code below. It looks like this:
http://www3.picturepush.com/photo/a/11925461/img/11925461.jpg
Does someone see the mistake and can help me out?
Here the code:
- for (int row = imageh-1; row >= 0; --row)
- {
- for (int col = 0; col < imagew; ++col)
- {
- int loc = imagew*row+col;
- if (g_grayscaleImage==0) {
- int msb = packedbuf[2*loc] & 0xff;
- int lsb = packedbuf[2*loc+1] & 0xff;
- int r = (lsb >>> 3);
- int g = ((lsb & 0x7) << 3);
- g |= (msb >>> 5);
- int b = (msb & 0x1f);
- r = (r << 3)|(r >>> 2);
- g = (g << 2)|(g >>> 4);
- b = (b << 3)|(b >>> 2);
- img.pixels[loc] = color(r, g, b);
- }
- else if (g_grayscaleImage==1) {
- img.pixels[loc] = color(packedbuf[loc], packedbuf[loc], packedbuf[loc]);
- }
- else if (g_grayscaleImage==2) {
- int LeftPixel = packedbuf[loc/2] & 0XF0;
- int RightPixel = packedbuf[loc/2] << 4;
- img.pixels[loc] = color(LeftPixel, LeftPixel, LeftPixel);
- img.pixels[loc+1] = color(RightPixel, RightPixel, RightPixel);
- ++col;
- }
- }
- }
- img.updatePixels();
- return img;
- }
Thanks
Robert
1