SHOW:
|
|
- or go back to the newest paste.
| 1 | |=================================================Pixels.h=================================================| | |
| 2 | ||
| 3 | #define RED_3_3_2 0xE0 | |
| 4 | #define GRN_3_3_2 0x1C | |
| 5 | #define BLU_3_3_2 0x03 | |
| 6 | ||
| 7 | #define RED_5_6_5 0xF800 | |
| 8 | #define GRN_5_6_5 0x07E0 | |
| 9 | #define BLU_5_6_5 0x001F | |
| 10 | ||
| 11 | #define RED_4_4_4_4 0xF000 | |
| 12 | #define GRN_4_4_4_4 0x0F00 | |
| 13 | #define BLU_4_4_4_4 0x00F0 | |
| 14 | #define ALP_4_4_4_4 0x000F | |
| 15 | ||
| 16 | #define RED_8_8_8_8 0xFF000000 | |
| 17 | #define GRN_8_8_8_8 0x00FF0000 | |
| 18 | #define BLU_8_8_8_8 0x0000FF00 | |
| 19 | #define ALP_8_8_8_8 0x000000FF | |
| 20 | ||
| 21 | #define RED_8_8_8 RED_8_8_8_8 | |
| 22 | #define GRN_8_8_8 GRN_8_8_8_8 | |
| 23 | #define BLU_8_8_8 BLU_8_8_8_8 | |
| 24 | ||
| 25 | #define RED_10_10_10 0xFFC00000 | |
| 26 | #define GRN_10_10_10 0x003FF000 | |
| 27 | #define BLU_10_10_10 0x00000FFC | |
| 28 | ||
| 29 | #define RED_16_16_16_16 0xFFFF000000000000 //some people use 64-bit color | |
| 30 | #define GRN_16_16_16_16 0x0000FFFF00000000 | |
| 31 | #define BLU_16_16_16_16 0x00000000FFFF0000 | |
| 32 | #define ALP_16_16_16_16 0x000000000000FFFF | |
| 33 | ||
| 34 | enum class PIXEL_FORMAT : char {
| |
| 35 | PALETTE, | |
| 36 | RGB, | |
| 37 | RGBA | |
| 38 | }; | |
| 39 | ||
| 40 | template<unsigned bit_depth, PIXEL_FORMAT fmt> struct Pixel {};
| |
| 41 | ||
| 42 | template<> struct Pixel<8, PIXEL_FORMAT::RGB> {
| |
| 43 | unsigned char color; | |
| 44 | Pixel(unsigned char red, unsigned char green, unsigned char blue) {
| |
| 45 | color = 0xFF & (((red << 5) & RED_3_3_2) & ((green << 2) & GRN_3_3_2) & (blue & BLU_3_3_2)); | |
| 46 | } | |
| 47 | ||
| 48 | Pixel(unsigned char clr) { color = clr; }
| |
| 49 | ||
| 50 | Pixel<16, PIXEL_FORMAT::RGB> convert_to_rgb16() {
| |
| 51 | unsigned char red, grn, blu; | |
| 52 | red = ((color & RED_3_3_2) >> 5); | |
| 53 | grn = ((color & GRN_3_3_2) >> 2); | |
| 54 | blu = (color & BLU_3_3_2); | |
| 55 | ||
| 56 | red = ((red/7) * 31); //5 | |
| 57 | grn = ((grn/7) * 63); //6 | |
| 58 | blu = ((blu/3) * 31); //5 | |
| 59 | ||
| 60 | Pixel<16, PIXEL_FORMAT::RGB> _16rgb(red, grn, blu); //compiler throws an error here | |
| 61 | return _16rgb; | |
| 62 | } | |
| 63 | }; | |
| 64 | ||
| 65 | template<> struct Pixel<16, PIXEL_FORMAT::RGB> {
| |
| 66 | unsigned short color; | |
| 67 | Pixel(unsigned char red, unsigned char green, unsigned char blue) {
| |
| 68 | color = (0xFFFF & (((red << 11) & RED_5_6_5) & ((green << 5) & GRN_5_6_5) & (blue & BLU_5_6_5))); | |
| 69 | } | |
| 70 | Pixel(short clr) { color = clr; }
| |
| 71 | }; | |
| 72 | ||
| 73 | ||
| 74 | |=================================================Canvas.h=================================================| | |
| 75 | ||
| 76 | #include "Pixels.h" | |
| 77 | ||
| 78 | template< typename PixelType > struct Layer { //will be a base for Canvas
| |
| 79 | unsigned int width, height; | |
| 80 | PixelType** pixels; | |
| 81 | ||
| 82 | Layer(unsigned int width, unsigned int height, unsigned int init_color = 0xFFFFFFFF); | |
| 83 | Layer(PixelType** pixels); | |
| 84 | Layer(); | |
| 85 | ~Layer(); | |
| 86 | Layer(const Layer& layer); | |
| 87 | ||
| 88 | PixelType pixel_at(unsigned int x, unsigned int y); | |
| 89 | void modify_pixel(unsigned int x, unsigned int y, bool add, PixelType pix); | |
| 90 | void replace_pixel(unsigned int x, unsigned int y, PixelType pix); | |
| 91 | - | template<unsigned int bit_depth, PIXEL_FORMAT fmt> Layer<bit_depth, fmt> convert_pixels() {
|
| 91 | + | template<unsigned int bit_depth, PIXEL_FORMAT fmt> Layer<Pixel<bit_depth, fmt>> convert_pixels() {
|
| 92 | Pixel<bit_depth, fmt>** new_pixels; | |
| 93 | new_pixels = new Pixel<bit_depth, fmt>*[height]; | |
| 94 | for(int y = 0; y < height; y++) | |
| 95 | new_pixels[y] = new Pixel<bit_depth, fmt>[width]; | |
| 96 | for(int y = 0; y < height; y++ {
| |
| 97 | for(int x = 0; x < width; x++) {
| |
| 98 | new_pixels[y][x] = pixels[y][x].convert_to<bit_depth, fmt>(); //make a template wrapper for the conversion methods? | |
| 99 | } | |
| 100 | } | |
| 101 | return Layer(new_pixels); | |
| 102 | }; |