#include #include #include #include /**********************************************************/ void rgb16convert(BITMAP *bmp) { int color, r, g, b; for (int y = 0; y < bmp->h; y++) { for (int x = 0; x < bmp->w; x++) { color = _getpixel32(bmp, x, y); r = getr32(color); g = getg32(color); b = getb32(color); r = ((r + 4) >> 3) << 3; g = ((g + 2) >> 2) << 2; b = ((b + 4) >> 3) << 3; if (r > 255) r = 255; if (g > 255) g = 255; if (b > 255) b = 255; _putpixel32(bmp, x, y, makecol32(r, g, b)); } } } void hsv16convert(BITMAP *bmp) { int color, r, g, b; float h, s, v; for (int y = 0; y < bmp->h; y++) { for (int x = 0; x < bmp->w; x++) { color = _getpixel32(bmp, x, y); r = getr32(color); g = getg32(color); b = getb32(color); rgb_to_hsv(r, g, b, &h, &s, &v); h = std::floor((h * (16.0 / 360.0)) + 0.5) * (360.0 / 16.0); s = std::floor(s * 16.0 + 0.5) / 16.0; v = std::floor(v * 256.0 + 0.5) / 256.0; if (h > 360.0) h = 0.0; if (s > 1.0) s = 1.0; if (v > 1.0) v = 1.0; hsv_to_rgb(h, s, v, &r, &g, &b); _putpixel32(bmp, x, y, makecol32(r, g, b)); } } } void hsv547convert(BITMAP *bmp) { int color, r, g, b; float h, s, v; for (int y = 0; y < bmp->h; y++) { for (int x = 0; x < bmp->w; x++) { color = _getpixel32(bmp, x, y); r = getr32(color); g = getg32(color); b = getb32(color); rgb_to_hsv(r, g, b, &h, &s, &v); h = std::floor((h * (32.0 / 360.0)) + 0.5) * (360.0 / 32.0); s = std::floor(s * 16.0 + 0.5) / 16.0; v = std::floor(v * 128.0 + 0.5) / 128.0; if (h > 360.0) h = 0.0; if (s > 1.0) s = 1.0; if (v > 1.0) v = 1.0; hsv_to_rgb(h, s, v, &r, &g, &b); _putpixel32(bmp, x, y, makecol32(r, g, b)); } } } void hsv646convert(BITMAP *bmp) { int color, r, g, b; float h, s, v; for (int y = 0; y < bmp->h; y++) { for (int x = 0; x < bmp->w; x++) { color = _getpixel32(bmp, x, y); r = getr32(color); g = getg32(color); b = getb32(color); rgb_to_hsv(r, g, b, &h, &s, &v); h = std::floor((h * (64.0 / 360.0)) + 0.5) * (360.0 / 64.0); s = std::floor(s * 16.0 + 0.5) / 16.0; v = std::floor(v * 64.0 + 0.5) / 64.0; if (h > 360.0) h = 0.0; if (s > 1.0) s = 1.0; if (v > 1.0) v = 1.0; hsv_to_rgb(h, s, v, &r, &g, &b); _putpixel32(bmp, x, y, makecol32(r, g, b)); } } } /**********************************************************/ int main(int argc, char **argv) { if (argc != 4) { std::cerr << "Usage: cvt rgb565/hsv448/hsv547/hsv646 " << std::endl; return -1; } allegro_init(); set_color_depth(32); set_gfx_mode(GFX_AUTODETECT_WINDOWED, 160, 160, 0, 0); BITMAP *bmp = load_bitmap(argv[2], NULL); if (!bmp) { std::cerr << "Couldn't load bitmap" << std::endl; return -1; } if (strcmp(argv[1], "rgb565") == 0) rgb16convert(bmp); else if (strcmp(argv[1], "hsv448") == 0) hsv16convert(bmp); else if (strcmp(argv[1], "hsv547") == 0) hsv547convert(bmp); else if (strcmp(argv[1], "hsv646") == 0) hsv646convert(bmp); else { std::cerr << "I don't know this format: " << argv[1] << std::endl; return -1; } save_bitmap(argv[3], bmp, NULL); return 0; } END_OF_MAIN()