1 module CPUblit.colorlookup; 2 /** 3 * CPUblit 4 * Color look-up and planar to chunky (coming soon) conversion functions. 5 */ 6 7 8 public import CPUblit.colorspaces; 9 /** 10 * Converts an indexed image of type T (eg. ubyte, ushort) into an unindexed type of U (eg. Pixel16Bit, Pixel32Bit). 11 */ 12 public @nogc void colorLookup(T, U)(T* src, U* dest, U* palette, size_t length){ 13 while(length){ 14 *dest = palette[*src]; 15 src++; 16 dest++; 17 length--; 18 } 19 } 20 /** 21 * Converts a 4 Bit indexed image into an unindexed type of U (eg. Pixel16Bit, Pixel32Bit). 22 * Word order is: 0xF0 even, 0x0F odd. 23 */ 24 public @nogc void colorLookup4Bit(U)(ubyte* src, U* dest, U* palette, size_t length, int offset = 0){ 25 for(; offset < length; offset++){ 26 if(offset & 1){ 27 *dest = palette[(*src) & 0x0F]; 28 offset = 0; 29 src++; 30 }else{ 31 *dest = palette[(*src) & 0xF0]; 32 offset++; 33 } 34 dest++; 35 } 36 } 37 38 /** 39 * Converts a 2 Bit indexed image into an unindexed type of U (eg. Pixel16Bit, Pixel32Bit). 40 * Word order is: 0: 0b11_00_00_00 1: 0b00_11_00_00 2: 0b00_00_11_00 3: 0b00_00_00_11 41 */ 42 public @nogc void colorLookup2Bit(U)(ubyte* src, U* dest, U* palette, size_t length, int offset = 0){ 43 for(; offset < length; offset++){ 44 switch(offset & 3){ 45 case 3: 46 *dest = palette[(*src) & 0b00_00_00_11]; 47 offset = 0; 48 src++; 49 break; 50 case 2: 51 *dest = palette[(*src) & 0b00_00_11_00]; 52 offset++; 53 break; 54 case 1: 55 *dest = palette[(*src) & 0b00_11_00_00]; 56 offset++; 57 break; 58 default: 59 *dest = palette[(*src) & 0b11_00_00_00]; 60 offset++; 61 break; 62 } 63 dest++; 64 } 65 } 66 67 /*public @nogc void convPlanarToChunky(int planes, U)(ubyte* src, U* dest, size_t length){ 68 69 }*/