|
| 1 | +#include <vector> |
| 2 | +#include <osg/Image> |
| 3 | +using namespace std; |
| 4 | + |
| 5 | +struct Color { |
| 6 | + int r; |
| 7 | + int g; |
| 8 | + int b; |
| 9 | +}; |
| 10 | + |
| 11 | +Color RGB565_RGB(unsigned short color0) { |
| 12 | + unsigned long temp; |
| 13 | + temp = (color0 >> 11) * 255 + 16; |
| 14 | + unsigned char r0 = (unsigned char)((temp / 32 + temp) / 32); |
| 15 | + temp = ((color0 & 0x07E0) >> 5) * 255 + 32; |
| 16 | + unsigned char g0 = (unsigned char)((temp / 64 + temp) / 64); |
| 17 | + temp = (color0 & 0x001F) * 255 + 16; |
| 18 | + unsigned char b0 = (unsigned char)((temp / 32 + temp) / 32); |
| 19 | + return Color{ r0,g0,b0 }; |
| 20 | +} |
| 21 | + |
| 22 | +Color Mix_Color( |
| 23 | + unsigned short color0, unsigned short color1, |
| 24 | + Color c0, Color c1, int idx) { |
| 25 | + Color finalColor; |
| 26 | + if (color0 > color1) |
| 27 | + { |
| 28 | + switch (idx) |
| 29 | + { |
| 30 | + case 0: |
| 31 | + finalColor = Color{ c0.r, c0.g, c0.b }; |
| 32 | + break; |
| 33 | + case 1: |
| 34 | + finalColor = Color{ c1.r, c1.g, c1.b }; |
| 35 | + break; |
| 36 | + case 2: |
| 37 | + finalColor = Color{ |
| 38 | + (2 * c0.r + c1.r) / 3, |
| 39 | + (2 * c0.g + c1.g) / 3, |
| 40 | + (2 * c0.b + c1.b) / 3}; |
| 41 | + break; |
| 42 | + case 3: |
| 43 | + finalColor = Color{ |
| 44 | + (c0.r + 2 * c1.r) / 3, |
| 45 | + (c0.g + 2 * c1.g) / 3, |
| 46 | + (c0.b + 2 * c1.b) / 3 }; |
| 47 | + break; |
| 48 | + } |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + switch (idx) |
| 53 | + { |
| 54 | + case 0: |
| 55 | + finalColor = Color{ c0.r, c0.g, c0.b }; |
| 56 | + break; |
| 57 | + case 1: |
| 58 | + finalColor = Color{ c1.r, c1.g, c1.b }; |
| 59 | + break; |
| 60 | + case 2: |
| 61 | + finalColor = Color{ (c0.r + c1.r) / 2, (c0.g + c1.g) / 2, (c0.b + c1.b) / 2 }; |
| 62 | + break; |
| 63 | + case 3: |
| 64 | + finalColor = Color{ 0, 0, 0 }; |
| 65 | + break; |
| 66 | + } |
| 67 | + } |
| 68 | + return finalColor; |
| 69 | +} |
| 70 | + |
| 71 | +void resize_Image(vector<unsigned char>& jpeg_buf, int width, int height, int new_w, int new_h) { |
| 72 | + vector<unsigned char> new_buf(new_w * new_h * 3); |
| 73 | + int scale = width / new_w; |
| 74 | + for (int row = 0; row < new_h ; row++) |
| 75 | + { |
| 76 | + for(int col = 0; col < new_w; col++) { |
| 77 | + int pos = row * new_w + col; |
| 78 | + int old_pos = (row * width + col) * scale; |
| 79 | + for (int i = 0; i < 3 ; i++) |
| 80 | + { |
| 81 | + new_buf[3 * pos + i] = jpeg_buf[3 * old_pos + i]; |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + jpeg_buf = new_buf; |
| 86 | +} |
| 87 | + |
| 88 | +void fill_4BitImage(vector<unsigned char>& jpeg_buf, osg::Image* img, int& width, int& height) { |
| 89 | + jpeg_buf.resize(width * height * 3); |
| 90 | + unsigned char* pData = img->data(); |
| 91 | + int imgSize = img->getImageSizeInBytes(); |
| 92 | + int x_pos = 0; |
| 93 | + int y_pos = 0; |
| 94 | + for (size_t i = 0; i < imgSize; i += 8) |
| 95 | + { |
| 96 | + // 64 bit matrix |
| 97 | + unsigned short color0, color1; |
| 98 | + memcpy(&color0, pData, 2); |
| 99 | + pData += 2; |
| 100 | + memcpy(&color1, pData, 2); |
| 101 | + pData += 2; |
| 102 | + Color c0 = RGB565_RGB(color0); |
| 103 | + Color c1 = RGB565_RGB(color1); |
| 104 | + for (size_t i = 0; i < 4; i++) |
| 105 | + { |
| 106 | + unsigned char idx[4]; |
| 107 | + idx[0] = (*pData >> 6) & 0x03; |
| 108 | + idx[1] = (*pData >> 4) & 0x03; |
| 109 | + idx[2] = (*pData >> 2) & 0x03; |
| 110 | + idx[3] = (*pData) & 0x03; |
| 111 | + // 4 pixel color |
| 112 | + for (size_t pixel_idx = 0; pixel_idx < 4; pixel_idx++) |
| 113 | + { |
| 114 | + Color cf = Mix_Color(color0, color1, c0, c1, idx[pixel_idx]); |
| 115 | + int cell_x_pos = x_pos + pixel_idx; |
| 116 | + int cell_y_pos = y_pos + i; |
| 117 | + int byte_pos = (cell_x_pos + cell_y_pos * width) * 3; |
| 118 | + jpeg_buf[byte_pos] = cf.r; |
| 119 | + jpeg_buf[byte_pos + 1] = cf.g; |
| 120 | + jpeg_buf[byte_pos + 2] = cf.b; |
| 121 | + } |
| 122 | + pData++; |
| 123 | + } |
| 124 | + x_pos += 4; |
| 125 | + if (x_pos >= width) { |
| 126 | + x_pos = 0; |
| 127 | + y_pos += 4; |
| 128 | + } |
| 129 | + } |
| 130 | + int max_size = 512; |
| 131 | + if (width > max_size || height > max_size) { |
| 132 | + int new_w = width, new_h = height; |
| 133 | + while (new_w > max_size || new_h > max_size) |
| 134 | + { |
| 135 | + new_w /= 2; |
| 136 | + new_h /= 2; |
| 137 | + } |
| 138 | + resize_Image(jpeg_buf, width, height, new_w, new_h); |
| 139 | + width = new_w; |
| 140 | + height = new_h; |
| 141 | + } |
| 142 | +} |
0 commit comments