void test_bitfield(Bits bits, int x) { int* ip = bitfields(bits.int_bitfield, 0); float* fp = bitfields(bits.uint_bitfield, 0u); }
bool ReadPalette(File* file, Header& h) { // initialize bit masks/shifts... just in case h.bf_red_mask = h.bf_red_shift = h.bf_red_rshift = 0; h.bf_green_mask = h.bf_green_shift = h.bf_green_rshift = 0; h.bf_blue_mask = h.bf_blue_shift = h.bf_blue_rshift = 0; // if we're not a palettized image, don't even read a palette if (h.bpp > 8) { h.palette_size = 0; // do we have bitfields? if (h.compression == 3) { auto_array<byte> bitfields(new byte[12]); if (file->read(bitfields, 12) != 12) { return false; } h.bf_red_mask = read32_le((byte*)bitfields); h.bf_green_mask = read32_le((byte*)bitfields + 4); h.bf_blue_mask = read32_le((byte*)bitfields + 8); // calculate shifts h.bf_red_shift = count_right_zeroes(h.bf_red_mask); h.bf_green_shift = count_right_zeroes(h.bf_green_mask); h.bf_blue_shift = count_right_zeroes(h.bf_blue_mask); h.bf_red_rshift = 8 - count_ones(h.bf_red_mask); h.bf_green_rshift = 8 - count_ones(h.bf_green_mask); h.bf_blue_rshift = 8 - count_ones(h.bf_blue_mask); // otherwise, set default bitfield entries } else { if (h.bpp == 16) { h.bf_red_mask = 0x7C00; h.bf_red_shift = 10; h.bf_red_rshift = 3; h.bf_green_mask = 0x03E0; h.bf_green_shift = 5; h.bf_green_rshift = 3; h.bf_blue_mask = 0x001F; h.bf_blue_shift = 0; h.bf_blue_rshift = 3; } else if (h.bpp == 32) { // these don't need any rshift h.bf_red_mask = 0x00FF0000; h.bf_red_shift = 16; h.bf_green_mask = 0x0000FF00; h.bf_green_shift = 8; h.bf_blue_mask = 0x000000FF; h.bf_blue_shift = 0; } } return true; } if (h.bpp <= 8) { h.palette_size = 1 << h.bpp; } else { h.palette_size = 0; return true; } h.palette = new BGR[h.palette_size]; // read the BMP color table const int buffer_size = h.palette_size * (h.os2 ? 3 : 4); auto_array<byte> buffer(new byte[buffer_size]); if (file->read(buffer, buffer_size) != buffer_size) { return false; } byte* in = buffer; BGR* out = h.palette; for (int i = 0; i < h.palette_size; ++i) { out->blue = *in++; out->green = *in++; out->red = *in++; if (!h.os2) { ++in; // skip alpha } ++out; } return true; }