void PNGLoader::grayscale_to_4ub(int count) { unsigned char *input = scanline; if (bit_depth == 1) { if (!has_colorkey) { for (int i = 0; i < count; i++) { int shift = i % 8; unsigned char value = (input[i / 8] >> shift) & 1; value = static_cast<int>(value)* 255; scanline_4ub[i] = Vec4ub(value, value, value, 255); } } else { for (int i = 0; i < count; i++) { int shift = i % 8; unsigned char value = (input[i / 8] >> shift) & 1; unsigned char alpha = (value != colorkey.r) ? 255 : 0; value = static_cast<int>(value)* 255; scanline_4ub[i] = Vec4ub(value, value, value, alpha); } } }
void PNGLoader::decode_palette() { if (color_type == 3) // indexed { if (plte.is_null() || plte.get_size() % 3 != 0) throw Exception("Invalid PNG image file"); int num_entries = plte.get_size() / 3; if (num_entries > 256) throw Exception("Invalid PNG image file"); unsigned char *entries = reinterpret_cast<unsigned char*>(plte.get_data()); palette = static_cast<Vec4ub *>(System::aligned_alloc(256 * sizeof(Vec4ub))); for (int i = 0; i < num_entries; i++) palette[i] = Vec4ub(entries[i * 3], entries[i * 3 + 1], entries[i * 3 + 2], 255); if (!trns.is_null()) { int num_alpha_entries = trns.get_size(); if (num_alpha_entries > num_entries) throw Exception("Invalid PNG image file"); unsigned char *alpha_entries = reinterpret_cast<unsigned char*>(trns.get_data()); for (int i = 0; i < num_alpha_entries; i++) palette[i].a = alpha_entries[i]; } } }
FontPixelBuffer FontEngine_Win32::get_font_glyph_gray8(int glyph) { DataBuffer glyph_bitmap; GLYPHMETRICS glyph_metrics = { 0 }; MAT2 matrix = { 0 }; matrix.eM11.value = 1; matrix.eM22.value = 1; if (try_load_glyph_bitmap(glyph, GGO_GRAY8_BITMAP, matrix, glyph_bitmap, glyph_metrics)) { PixelBuffer pixelbuffer(glyph_metrics.gmBlackBoxX, glyph_metrics.gmBlackBoxY, tf_rgba8); DWORD s_pitch = (glyph_metrics.gmBlackBoxX + 3) / 4 * 4; unsigned char *s = (unsigned char *) glyph_bitmap.get_data(); DWORD d_width = glyph_metrics.gmBlackBoxX; Vec4ub *d = pixelbuffer.get_data<Vec4ub>(); for (DWORD py = 0; py < glyph_metrics.gmBlackBoxY; py++) { for (DWORD px = 0; px < glyph_metrics.gmBlackBoxX; px++) { DWORD gray = s[px + py*s_pitch]; d[px + py*d_width] = Vec4ub(255, 255, 255, (gray * 255 + 32) / 64); } } FontPixelBuffer font_buffer; font_buffer.glyph = glyph; font_buffer.buffer = pixelbuffer; font_buffer.buffer_rect = pixelbuffer.get_size(); font_buffer.offset.x = glyph_metrics.gmptGlyphOrigin.x; font_buffer.offset.y = -glyph_metrics.gmptGlyphOrigin.y; font_buffer.empty_buffer = false; font_buffer.metrics.advance.width = glyph_metrics.gmCellIncX; font_buffer.metrics.advance.height = glyph_metrics.gmCellIncY; font_buffer.metrics.bbox_offset.x = glyph_metrics.gmptGlyphOrigin.x; font_buffer.metrics.bbox_offset.y = -glyph_metrics.gmptGlyphOrigin.y; font_buffer.metrics.bbox_size.width = glyph_metrics.gmBlackBoxX; font_buffer.metrics.bbox_size.height = glyph_metrics.gmBlackBoxY; return font_buffer; } else { return get_empty_font_glyph(glyph); } }