diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 16:29:51 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 16:29:51 +0000 |
commit | 6e7a315eb67cb6c113cf37e1d66c4f11a51a2b3e (patch) | |
tree | 32451fa3cdd9321fb2591fada9891b2cb70a9cd1 /grub-core/video/readers | |
parent | Initial commit. (diff) | |
download | grub2-6e7a315eb67cb6c113cf37e1d66c4f11a51a2b3e.tar.xz grub2-6e7a315eb67cb6c113cf37e1d66c4f11a51a2b3e.zip |
Adding upstream version 2.06.upstream/2.06upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'grub-core/video/readers')
-rw-r--r-- | grub-core/video/readers/jpeg.c | 879 | ||||
-rw-r--r-- | grub-core/video/readers/png.c | 1166 | ||||
-rw-r--r-- | grub-core/video/readers/tga.c | 511 |
3 files changed, 2556 insertions, 0 deletions
diff --git a/grub-core/video/readers/jpeg.c b/grub-core/video/readers/jpeg.c new file mode 100644 index 0000000..e31602f --- /dev/null +++ b/grub-core/video/readers/jpeg.c @@ -0,0 +1,879 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2008 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <grub/bitmap.h> +#include <grub/types.h> +#include <grub/normal.h> +#include <grub/dl.h> +#include <grub/mm.h> +#include <grub/misc.h> +#include <grub/bufio.h> + +GRUB_MOD_LICENSE ("GPLv3+"); + +/* Uncomment following define to enable JPEG debug. */ +//#define JPEG_DEBUG + +#define JPEG_ESC_CHAR 0xFF + +#define JPEG_SAMPLING_1x1 0x11 + +enum + { + JPEG_MARKER_SOF0 = 0xc0, + JPEG_MARKER_DHT = 0xc4, + JPEG_MARKER_SOI = 0xd8, + JPEG_MARKER_EOI = 0xd9, + JPEG_MARKER_RST0 = 0xd0, + JPEG_MARKER_RST1 = 0xd1, + JPEG_MARKER_RST2 = 0xd2, + JPEG_MARKER_RST3 = 0xd3, + JPEG_MARKER_RST4 = 0xd4, + JPEG_MARKER_RST5 = 0xd5, + JPEG_MARKER_RST6 = 0xd6, + JPEG_MARKER_RST7 = 0xd7, + JPEG_MARKER_SOS = 0xda, + JPEG_MARKER_DQT = 0xdb, + JPEG_MARKER_DRI = 0xdd, + }; + +#define SHIFT_BITS 8 +#define CONST(x) ((int) ((x) * (1L << SHIFT_BITS) + 0.5)) + +#define JPEG_UNIT_SIZE 8 + +static const grub_uint8_t jpeg_zigzag_order[64] = { + 0, 1, 8, 16, 9, 2, 3, 10, + 17, 24, 32, 25, 18, 11, 4, 5, + 12, 19, 26, 33, 40, 48, 41, 34, + 27, 20, 13, 6, 7, 14, 21, 28, + 35, 42, 49, 56, 57, 50, 43, 36, + 29, 22, 15, 23, 30, 37, 44, 51, + 58, 59, 52, 45, 38, 31, 39, 46, + 53, 60, 61, 54, 47, 55, 62, 63 +}; + +#ifdef JPEG_DEBUG +static grub_command_t cmd; +#endif + +typedef int jpeg_data_unit_t[64]; + +struct grub_jpeg_data +{ + grub_file_t file; + struct grub_video_bitmap **bitmap; + grub_uint8_t *bitmap_ptr; + + unsigned image_width; + unsigned image_height; + + grub_uint8_t *huff_value[4]; + int huff_offset[4][16]; + int huff_maxval[4][16]; + + grub_uint8_t quan_table[2][64]; + int comp_index[3][3]; + + jpeg_data_unit_t ydu[4]; + jpeg_data_unit_t crdu; + jpeg_data_unit_t cbdu; + + unsigned log_vs, log_hs; + int dri; + unsigned r1; + + int dc_value[3]; + + int color_components; + + int bit_mask, bit_save; +}; + +static grub_uint8_t +grub_jpeg_get_byte (struct grub_jpeg_data *data) +{ + grub_uint8_t r; + + r = 0; + grub_file_read (data->file, &r, 1); + + return r; +} + +static grub_uint16_t +grub_jpeg_get_word (struct grub_jpeg_data *data) +{ + grub_uint16_t r; + + r = 0; + grub_file_read (data->file, &r, sizeof (grub_uint16_t)); + + return grub_be_to_cpu16 (r); +} + +static int +grub_jpeg_get_bit (struct grub_jpeg_data *data) +{ + int ret; + + if (data->bit_mask == 0) + { + data->bit_save = grub_jpeg_get_byte (data); + if (data->bit_save == JPEG_ESC_CHAR) + { + if (grub_jpeg_get_byte (data) != 0) + { + grub_error (GRUB_ERR_BAD_FILE_TYPE, + "jpeg: invalid 0xFF in data stream"); + return 0; + } + } + data->bit_mask = 0x80; + } + + ret = ((data->bit_save & data->bit_mask) != 0); + data->bit_mask >>= 1; + return ret; +} + +static int +grub_jpeg_get_number (struct grub_jpeg_data *data, int num) +{ + int value, i, msb; + + if (num == 0) + return 0; + + msb = value = grub_jpeg_get_bit (data); + for (i = 1; i < num; i++) + value = (value << 1) + (grub_jpeg_get_bit (data) != 0); + if (!msb) + value += 1 - (1 << num); + + return value; +} + +static int +grub_jpeg_get_huff_code (struct grub_jpeg_data *data, int id) +{ + int code; + unsigned i; + + code = 0; + for (i = 0; i < ARRAY_SIZE (data->huff_maxval[id]); i++) + { + code <<= 1; + if (grub_jpeg_get_bit (data)) + code++; + if (code < data->huff_maxval[id][i]) + return data->huff_value[id][code + data->huff_offset[id][i]]; + } + grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: huffman decode fails"); + return 0; +} + +static grub_err_t +grub_jpeg_decode_huff_table (struct grub_jpeg_data *data) +{ + int id, ac, n, base, ofs; + grub_uint32_t next_marker; + grub_uint8_t count[16]; + unsigned i; + + next_marker = data->file->offset; + next_marker += grub_jpeg_get_word (data); + + while (data->file->offset + sizeof (count) + 1 <= next_marker) + { + id = grub_jpeg_get_byte (data); + ac = (id >> 4) & 1; + id &= 0xF; + if (id > 1) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, + "jpeg: too many huffman tables"); + + if (grub_file_read (data->file, &count, sizeof (count)) != + sizeof (count)) + return grub_errno; + + n = 0; + for (i = 0; i < ARRAY_SIZE (count); i++) + n += count[i]; + + id += ac * 2; + data->huff_value[id] = grub_malloc (n); + if (grub_errno) + return grub_errno; + + if (grub_file_read (data->file, data->huff_value[id], n) != n) + return grub_errno; + + base = 0; + ofs = 0; + for (i = 0; i < ARRAY_SIZE (count); i++) + { + base += count[i]; + ofs += count[i]; + + data->huff_maxval[id][i] = base; + data->huff_offset[id][i] = ofs - base; + + base <<= 1; + } + } + + if (data->file->offset != next_marker) + grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: extra byte in huffman table"); + + return grub_errno; +} + +static grub_err_t +grub_jpeg_decode_quan_table (struct grub_jpeg_data *data) +{ + int id; + grub_uint32_t next_marker; + + next_marker = data->file->offset; + next_marker += grub_jpeg_get_word (data); + + if (next_marker > data->file->size) + { + /* Should never be set beyond the size of the file. */ + return grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: invalid next reference"); + } + + while (data->file->offset + sizeof (data->quan_table[id]) + 1 + <= next_marker) + { + id = grub_jpeg_get_byte (data); + if (id >= 0x10) /* Upper 4-bit is precision. */ + return grub_error (GRUB_ERR_BAD_FILE_TYPE, + "jpeg: only 8-bit precision is supported"); + + if (id > 1) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, + "jpeg: too many quantization tables"); + + if (grub_file_read (data->file, &data->quan_table[id], + sizeof (data->quan_table[id])) + != sizeof (data->quan_table[id])) + return grub_errno; + + } + + if (data->file->offset != next_marker) + grub_error (GRUB_ERR_BAD_FILE_TYPE, + "jpeg: extra byte in quantization table"); + + return grub_errno; +} + +static grub_err_t +grub_jpeg_decode_sof (struct grub_jpeg_data *data) +{ + int i, cc; + grub_uint32_t next_marker; + + next_marker = data->file->offset; + next_marker += grub_jpeg_get_word (data); + + if (grub_jpeg_get_byte (data) != 8) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, + "jpeg: only 8-bit precision is supported"); + + data->image_height = grub_jpeg_get_word (data); + data->image_width = grub_jpeg_get_word (data); + + if ((!data->image_height) || (!data->image_width)) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: invalid image size"); + + cc = grub_jpeg_get_byte (data); + if (cc != 1 && cc != 3) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, + "jpeg: component count must be 1 or 3"); + data->color_components = cc; + + for (i = 0; i < cc; i++) + { + int id, ss; + + id = grub_jpeg_get_byte (data) - 1; + if ((id < 0) || (id >= 3)) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: invalid index"); + + ss = grub_jpeg_get_byte (data); /* Sampling factor. */ + if (!id) + { + grub_uint8_t vs, hs; + vs = ss & 0xF; /* Vertical sampling. */ + hs = ss >> 4; /* Horizontal sampling. */ + if ((vs > 2) || (hs > 2) || (vs == 0) || (hs == 0)) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, + "jpeg: sampling method not supported"); + data->log_vs = (vs == 2); + data->log_hs = (hs == 2); + } + else if (ss != JPEG_SAMPLING_1x1) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, + "jpeg: sampling method not supported"); + + data->comp_index[id][0] = grub_jpeg_get_byte (data); + if (data->comp_index[id][0] > 1) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, + "jpeg: too many quantization tables"); + } + + if (data->file->offset != next_marker) + grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: extra byte in sof"); + + return grub_errno; +} + +static grub_err_t +grub_jpeg_decode_dri (struct grub_jpeg_data *data) +{ + if (grub_jpeg_get_word (data) != 4) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, + "jpeg: DRI marker length must be 4"); + + data->dri = grub_jpeg_get_word (data); + + return grub_errno; +} + +static void +grub_jpeg_idct_transform (jpeg_data_unit_t du) +{ + int *pd; + int i; + int t0, t1, t2, t3, t4, t5, t6, t7; + int v0, v1, v2, v3, v4; + + pd = du; + for (i = 0; i < JPEG_UNIT_SIZE; i++, pd++) + { + if ((pd[JPEG_UNIT_SIZE * 1] | pd[JPEG_UNIT_SIZE * 2] | + pd[JPEG_UNIT_SIZE * 3] | pd[JPEG_UNIT_SIZE * 4] | + pd[JPEG_UNIT_SIZE * 5] | pd[JPEG_UNIT_SIZE * 6] | + pd[JPEG_UNIT_SIZE * 7]) == 0) + { + pd[JPEG_UNIT_SIZE * 0] <<= SHIFT_BITS; + + pd[JPEG_UNIT_SIZE * 1] = pd[JPEG_UNIT_SIZE * 2] + = pd[JPEG_UNIT_SIZE * 3] = pd[JPEG_UNIT_SIZE * 4] + = pd[JPEG_UNIT_SIZE * 5] = pd[JPEG_UNIT_SIZE * 6] + = pd[JPEG_UNIT_SIZE * 7] = pd[JPEG_UNIT_SIZE * 0]; + + continue; + } + + t0 = pd[JPEG_UNIT_SIZE * 0]; + t1 = pd[JPEG_UNIT_SIZE * 2]; + t2 = pd[JPEG_UNIT_SIZE * 4]; + t3 = pd[JPEG_UNIT_SIZE * 6]; + + v4 = (t1 + t3) * CONST (0.541196100); + + v0 = ((t0 + t2) << SHIFT_BITS); + v1 = ((t0 - t2) << SHIFT_BITS); + v2 = v4 - t3 * CONST (1.847759065); + v3 = v4 + t1 * CONST (0.765366865); + + t0 = v0 + v3; + t3 = v0 - v3; + t1 = v1 + v2; + t2 = v1 - v2; + + t4 = pd[JPEG_UNIT_SIZE * 7]; + t5 = pd[JPEG_UNIT_SIZE * 5]; + t6 = pd[JPEG_UNIT_SIZE * 3]; + t7 = pd[JPEG_UNIT_SIZE * 1]; + + v0 = t4 + t7; + v1 = t5 + t6; + v2 = t4 + t6; + v3 = t5 + t7; + + v4 = (v2 + v3) * CONST (1.175875602); + + v0 *= CONST (0.899976223); + v1 *= CONST (2.562915447); + v2 = v2 * CONST (1.961570560) - v4; + v3 = v3 * CONST (0.390180644) - v4; + + t4 = t4 * CONST (0.298631336) - v0 - v2; + t5 = t5 * CONST (2.053119869) - v1 - v3; + t6 = t6 * CONST (3.072711026) - v1 - v2; + t7 = t7 * CONST (1.501321110) - v0 - v3; + + pd[JPEG_UNIT_SIZE * 0] = t0 + t7; + pd[JPEG_UNIT_SIZE * 7] = t0 - t7; + pd[JPEG_UNIT_SIZE * 1] = t1 + t6; + pd[JPEG_UNIT_SIZE * 6] = t1 - t6; + pd[JPEG_UNIT_SIZE * 2] = t2 + t5; + pd[JPEG_UNIT_SIZE * 5] = t2 - t5; + pd[JPEG_UNIT_SIZE * 3] = t3 + t4; + pd[JPEG_UNIT_SIZE * 4] = t3 - t4; + } + + pd = du; + for (i = 0; i < JPEG_UNIT_SIZE; i++, pd += JPEG_UNIT_SIZE) + { + if ((pd[1] | pd[2] | pd[3] | pd[4] | pd[5] | pd[6] | pd[7]) == 0) + { + pd[0] >>= (SHIFT_BITS + 3); + pd[1] = pd[2] = pd[3] = pd[4] = pd[5] = pd[6] = pd[7] = pd[0]; + continue; + } + + v4 = (pd[2] + pd[6]) * CONST (0.541196100); + + v0 = (pd[0] + pd[4]) << SHIFT_BITS; + v1 = (pd[0] - pd[4]) << SHIFT_BITS; + v2 = v4 - pd[6] * CONST (1.847759065); + v3 = v4 + pd[2] * CONST (0.765366865); + + t0 = v0 + v3; + t3 = v0 - v3; + t1 = v1 + v2; + t2 = v1 - v2; + + t4 = pd[7]; + t5 = pd[5]; + t6 = pd[3]; + t7 = pd[1]; + + v0 = t4 + t7; + v1 = t5 + t6; + v2 = t4 + t6; + v3 = t5 + t7; + + v4 = (v2 + v3) * CONST (1.175875602); + + v0 *= CONST (0.899976223); + v1 *= CONST (2.562915447); + v2 = v2 * CONST (1.961570560) - v4; + v3 = v3 * CONST (0.390180644) - v4; + + t4 = t4 * CONST (0.298631336) - v0 - v2; + t5 = t5 * CONST (2.053119869) - v1 - v3; + t6 = t6 * CONST (3.072711026) - v1 - v2; + t7 = t7 * CONST (1.501321110) - v0 - v3; + + pd[0] = (t0 + t7) >> (SHIFT_BITS * 2 + 3); + pd[7] = (t0 - t7) >> (SHIFT_BITS * 2 + 3); + pd[1] = (t1 + t6) >> (SHIFT_BITS * 2 + 3); + pd[6] = (t1 - t6) >> (SHIFT_BITS * 2 + 3); + pd[2] = (t2 + t5) >> (SHIFT_BITS * 2 + 3); + pd[5] = (t2 - t5) >> (SHIFT_BITS * 2 + 3); + pd[3] = (t3 + t4) >> (SHIFT_BITS * 2 + 3); + pd[4] = (t3 - t4) >> (SHIFT_BITS * 2 + 3); + } + + for (i = 0; i < JPEG_UNIT_SIZE * JPEG_UNIT_SIZE; i++) + { + du[i] += 128; + + if (du[i] < 0) + du[i] = 0; + if (du[i] > 255) + du[i] = 255; + } +} + +static void +grub_jpeg_decode_du (struct grub_jpeg_data *data, int id, jpeg_data_unit_t du) +{ + int h1, h2, qt; + unsigned pos; + + grub_memset (du, 0, sizeof (jpeg_data_unit_t)); + + qt = data->comp_index[id][0]; + h1 = data->comp_index[id][1]; + h2 = data->comp_index[id][2]; + + data->dc_value[id] += + grub_jpeg_get_number (data, grub_jpeg_get_huff_code (data, h1)); + + du[0] = data->dc_value[id] * (int) data->quan_table[qt][0]; + pos = 1; + while (pos < ARRAY_SIZE (data->quan_table[qt])) + { + int num, val; + + num = grub_jpeg_get_huff_code (data, h2); + if (!num) + break; + + val = grub_jpeg_get_number (data, num & 0xF); + num >>= 4; + pos += num; + + if (pos >= ARRAY_SIZE (jpeg_zigzag_order)) + { + grub_error (GRUB_ERR_BAD_FILE_TYPE, + "jpeg: invalid position in zigzag order!?"); + return; + } + + du[jpeg_zigzag_order[pos]] = val * (int) data->quan_table[qt][pos]; + pos++; + } + + grub_jpeg_idct_transform (du); +} + +static void +grub_jpeg_ycrcb_to_rgb (int yy, int cr, int cb, grub_uint8_t * rgb) +{ + int dd; + + cr -= 128; + cb -= 128; + + /* Red */ + dd = yy + ((cr * CONST (1.402)) >> SHIFT_BITS); + if (dd < 0) + dd = 0; + if (dd > 255) + dd = 255; +#ifdef GRUB_CPU_WORDS_BIGENDIAN + rgb[2] = dd; +#else + *(rgb++) = dd; +#endif + + /* Green */ + dd = yy - ((cb * CONST (0.34414) + cr * CONST (0.71414)) >> SHIFT_BITS); + if (dd < 0) + dd = 0; + if (dd > 255) + dd = 255; +#ifdef GRUB_CPU_WORDS_BIGENDIAN + rgb[1] = dd; +#else + *(rgb++) = dd; +#endif + + /* Blue */ + dd = yy + ((cb * CONST (1.772)) >> SHIFT_BITS); + if (dd < 0) + dd = 0; + if (dd > 255) + dd = 255; +#ifdef GRUB_CPU_WORDS_BIGENDIAN + rgb[0] = dd; + rgb += 3; +#else + *(rgb++) = dd; +#endif +} + +static grub_err_t +grub_jpeg_decode_sos (struct grub_jpeg_data *data) +{ + int i, cc; + grub_uint32_t data_offset; + + data_offset = data->file->offset; + data_offset += grub_jpeg_get_word (data); + + cc = grub_jpeg_get_byte (data); + + if (cc != 3 && cc != 1) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, + "jpeg: component count must be 1 or 3"); + data->color_components = cc; + + for (i = 0; i < cc; i++) + { + int id, ht; + + id = grub_jpeg_get_byte (data) - 1; + if ((id < 0) || (id >= 3)) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: invalid index"); + + ht = grub_jpeg_get_byte (data); + data->comp_index[id][1] = (ht >> 4); + data->comp_index[id][2] = (ht & 0xF) + 2; + + if ((data->comp_index[id][1] < 0) || (data->comp_index[id][1] > 3) || + (data->comp_index[id][2] < 0) || (data->comp_index[id][2] > 3)) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: invalid hufftable index"); + } + + grub_jpeg_get_byte (data); /* Skip 3 unused bytes. */ + grub_jpeg_get_word (data); + + if (data->file->offset != data_offset) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: extra byte in sos"); + + if (grub_video_bitmap_create (data->bitmap, data->image_width, + data->image_height, + GRUB_VIDEO_BLIT_FORMAT_RGB_888)) + return grub_errno; + + data->bitmap_ptr = (*data->bitmap)->data; + return GRUB_ERR_NONE; +} + +static grub_err_t +grub_jpeg_decode_data (struct grub_jpeg_data *data) +{ + unsigned c1, vb, hb, nr1, nc1; + int rst = data->dri; + + vb = 8 << data->log_vs; + hb = 8 << data->log_hs; + nr1 = (data->image_height + vb - 1) >> (3 + data->log_vs); + nc1 = (data->image_width + hb - 1) >> (3 + data->log_hs); + + if (data->bitmap_ptr == NULL) + return grub_error(GRUB_ERR_BAD_FILE_TYPE, + "jpeg: attempted to decode data before start of stream"); + + for (; data->r1 < nr1 && (!data->dri || rst); + data->r1++, data->bitmap_ptr += (vb * data->image_width - hb * nc1) * 3) + for (c1 = 0; c1 < nc1 && (!data->dri || rst); + c1++, rst--, data->bitmap_ptr += hb * 3) + { + unsigned r2, c2, nr2, nc2; + grub_uint8_t *ptr2; + + for (r2 = 0; r2 < (1U << data->log_vs); r2++) + for (c2 = 0; c2 < (1U << data->log_hs); c2++) + grub_jpeg_decode_du (data, 0, data->ydu[r2 * 2 + c2]); + + if (data->color_components >= 3) + { + grub_jpeg_decode_du (data, 1, data->cbdu); + grub_jpeg_decode_du (data, 2, data->crdu); + } + + if (grub_errno) + return grub_errno; + + nr2 = (data->r1 == nr1 - 1) ? (data->image_height - data->r1 * vb) : vb; + nc2 = (c1 == nc1 - 1) ? (data->image_width - c1 * hb) : hb; + + ptr2 = data->bitmap_ptr; + for (r2 = 0; r2 < nr2; r2++, ptr2 += (data->image_width - nc2) * 3) + for (c2 = 0; c2 < nc2; c2++, ptr2 += 3) + { + unsigned i0; + int yy; + + i0 = (r2 >> data->log_vs) * 8 + (c2 >> data->log_hs); + yy = data->ydu[(r2 / 8) * 2 + (c2 / 8)][(r2 % 8) * 8 + (c2 % 8)]; + + if (data->color_components >= 3) + { + int cr, cb; + cr = data->crdu[i0]; + cb = data->cbdu[i0]; + grub_jpeg_ycrcb_to_rgb (yy, cr, cb, ptr2); + } + else + { + ptr2[0] = yy; + ptr2[1] = yy; + ptr2[2] = yy; + } + } + } + + return grub_errno; +} + +static void +grub_jpeg_reset (struct grub_jpeg_data *data) +{ + data->bit_mask = 0x0; + + data->dc_value[0] = 0; + data->dc_value[1] = 0; + data->dc_value[2] = 0; +} + +static grub_uint8_t +grub_jpeg_get_marker (struct grub_jpeg_data *data) +{ + grub_uint8_t r; + + r = grub_jpeg_get_byte (data); + + if (r != JPEG_ESC_CHAR) + { + grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: invalid maker"); + return 0; + } + + return grub_jpeg_get_byte (data); +} + +static grub_err_t +grub_jpeg_decode_jpeg (struct grub_jpeg_data *data) +{ + if (grub_jpeg_get_marker (data) != JPEG_MARKER_SOI) /* Start Of Image. */ + return grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: invalid jpeg file"); + + while (grub_errno == 0) + { + grub_uint8_t marker; + + marker = grub_jpeg_get_marker (data); + if (grub_errno) + break; + + grub_dprintf ("jpeg", "jpeg marker: %x\n", marker); + + switch (marker) + { + case JPEG_MARKER_DHT: /* Define Huffman Table. */ + grub_jpeg_decode_huff_table (data); + break; + case JPEG_MARKER_DQT: /* Define Quantization Table. */ + grub_jpeg_decode_quan_table (data); + break; + case JPEG_MARKER_SOF0: /* Start Of Frame 0. */ + grub_jpeg_decode_sof (data); + break; + case JPEG_MARKER_DRI: /* Define Restart Interval. */ + grub_jpeg_decode_dri (data); + break; + case JPEG_MARKER_SOS: /* Start Of Scan. */ + if (grub_jpeg_decode_sos (data)) + break; + /* FALLTHROUGH */ + case JPEG_MARKER_RST0: /* Restart. */ + case JPEG_MARKER_RST1: + case JPEG_MARKER_RST2: + case JPEG_MARKER_RST3: + case JPEG_MARKER_RST4: + case JPEG_MARKER_RST5: + case JPEG_MARKER_RST6: + case JPEG_MARKER_RST7: + grub_jpeg_decode_data (data); + grub_jpeg_reset (data); + break; + case JPEG_MARKER_EOI: /* End Of Image. */ + return grub_errno; + default: /* Skip unrecognized marker. */ + { + grub_uint16_t sz; + + sz = grub_jpeg_get_word (data); + if (grub_errno) + return (grub_errno); + grub_file_seek (data->file, data->file->offset + sz - 2); + } + } + } + + return grub_errno; +} + +static grub_err_t +grub_video_reader_jpeg (struct grub_video_bitmap **bitmap, + const char *filename) +{ + grub_file_t file; + struct grub_jpeg_data *data; + + file = grub_buffile_open (filename, GRUB_FILE_TYPE_PIXMAP, 0); + if (!file) + return grub_errno; + + data = grub_zalloc (sizeof (*data)); + if (data != NULL) + { + int i; + + data->file = file; + data->bitmap = bitmap; + grub_jpeg_decode_jpeg (data); + + for (i = 0; i < 4; i++) + grub_free (data->huff_value[i]); + + grub_free (data); + } + + if (grub_errno != GRUB_ERR_NONE) + { + grub_video_bitmap_destroy (*bitmap); + *bitmap = 0; + } + + grub_file_close (file); + return grub_errno; +} + +#if defined(JPEG_DEBUG) +static grub_err_t +grub_cmd_jpegtest (grub_command_t cmdd __attribute__ ((unused)), + int argc, char **args) +{ + struct grub_video_bitmap *bitmap = 0; + + if (argc != 1) + return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected")); + + grub_video_reader_jpeg (&bitmap, args[0]); + if (grub_errno != GRUB_ERR_NONE) + return grub_errno; + + grub_video_bitmap_destroy (bitmap); + + return GRUB_ERR_NONE; +} +#endif + +static struct grub_video_bitmap_reader jpg_reader = { + .extension = ".jpg", + .reader = grub_video_reader_jpeg, + .next = 0 +}; + +static struct grub_video_bitmap_reader jpeg_reader = { + .extension = ".jpeg", + .reader = grub_video_reader_jpeg, + .next = 0 +}; + +GRUB_MOD_INIT (jpeg) +{ + grub_video_bitmap_reader_register (&jpg_reader); + grub_video_bitmap_reader_register (&jpeg_reader); +#if defined(JPEG_DEBUG) + cmd = grub_register_command ("jpegtest", grub_cmd_jpegtest, + "FILE", "Tests loading of JPEG bitmap."); +#endif +} + +GRUB_MOD_FINI (jpeg) +{ +#if defined(JPEG_DEBUG) + grub_unregister_command (cmd); +#endif + grub_video_bitmap_reader_unregister (&jpeg_reader); + grub_video_bitmap_reader_unregister (&jpg_reader); +} diff --git a/grub-core/video/readers/png.c b/grub-core/video/readers/png.c new file mode 100644 index 0000000..0157ff7 --- /dev/null +++ b/grub-core/video/readers/png.c @@ -0,0 +1,1166 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2008,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <grub/bitmap.h> +#include <grub/types.h> +#include <grub/normal.h> +#include <grub/dl.h> +#include <grub/mm.h> +#include <grub/misc.h> +#include <grub/bufio.h> +#include <grub/safemath.h> + +GRUB_MOD_LICENSE ("GPLv3+"); + +/* Uncomment following define to enable PNG debug. */ +//#define PNG_DEBUG + +enum + { + PNG_COLOR_TYPE_GRAY = 0, + PNG_COLOR_MASK_PALETTE = 1, + PNG_COLOR_MASK_COLOR = 2, + PNG_COLOR_MASK_ALPHA = 4, + PNG_COLOR_TYPE_PALETTE = (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE), + }; + +#define PNG_COMPRESSION_BASE 0 + +#define PNG_INTERLACE_NONE 0 +#define PNG_INTERLACE_ADAM7 1 + +#define PNG_FILTER_TYPE_BASE 0 + +#define PNG_FILTER_VALUE_NONE 0 +#define PNG_FILTER_VALUE_SUB 1 +#define PNG_FILTER_VALUE_UP 2 +#define PNG_FILTER_VALUE_AVG 3 +#define PNG_FILTER_VALUE_PAETH 4 +#define PNG_FILTER_VALUE_LAST 5 + +enum + { + PNG_CHUNK_IHDR = 0x49484452, + PNG_CHUNK_IDAT = 0x49444154, + PNG_CHUNK_IEND = 0x49454e44, + PNG_CHUNK_PLTE = 0x504c5445 + }; + +#define Z_DEFLATED 8 +#define Z_FLAG_DICT 32 + +#define INFLATE_STORED 0 +#define INFLATE_FIXED 1 +#define INFLATE_DYNAMIC 2 + +#define WSIZE 0x8000 + +#define DEFLATE_HCLEN_BASE 4 +#define DEFLATE_HCLEN_MAX 19 +#define DEFLATE_HLIT_BASE 257 +#define DEFLATE_HLIT_MAX 288 +#define DEFLATE_HDIST_BASE 1 +#define DEFLATE_HDIST_MAX 30 + +#define DEFLATE_HUFF_LEN 16 + +#ifdef PNG_DEBUG +static grub_command_t cmd; +#endif + +struct huff_table +{ + int *values, *maxval, *offset; + int num_values, max_length; +}; + +struct grub_png_data +{ + grub_file_t file; + struct grub_video_bitmap **bitmap; + + int bit_count, bit_save; + + grub_uint32_t next_offset; + + unsigned image_width, image_height; + int bpp, is_16bit; + int raw_bytes, is_gray, is_alpha, is_palette; + int row_bytes, color_bits; + grub_uint8_t *image_data; + + int inside_idat, idat_remain; + + int code_values[DEFLATE_HLIT_MAX]; + int code_maxval[DEFLATE_HUFF_LEN]; + int code_offset[DEFLATE_HUFF_LEN]; + + int dist_values[DEFLATE_HDIST_MAX]; + int dist_maxval[DEFLATE_HUFF_LEN]; + int dist_offset[DEFLATE_HUFF_LEN]; + + grub_uint8_t palette[256][3]; + + struct huff_table code_table; + struct huff_table dist_table; + + grub_uint8_t slide[WSIZE]; + int wp; + + grub_uint8_t *cur_rgb; + + int cur_column, cur_filter, first_line; +}; + +static grub_uint32_t +grub_png_get_dword (struct grub_png_data *data) +{ + grub_uint32_t r; + + r = 0; + grub_file_read (data->file, &r, sizeof (grub_uint32_t)); + + return grub_be_to_cpu32 (r); +} + +static grub_uint8_t +grub_png_get_byte (struct grub_png_data *data) +{ + grub_uint8_t r; + + if ((data->inside_idat) && (data->idat_remain == 0)) + { + grub_uint32_t len, type; + + do + { + /* Skip crc checksum. */ + grub_png_get_dword (data); + + if (data->file->offset != data->next_offset) + { + grub_error (GRUB_ERR_BAD_FILE_TYPE, + "png: chunk size error"); + return 0; + } + + len = grub_png_get_dword (data); + type = grub_png_get_dword (data); + if (type != PNG_CHUNK_IDAT) + { + grub_error (GRUB_ERR_BAD_FILE_TYPE, + "png: unexpected end of data"); + return 0; + } + + data->next_offset = data->file->offset + len + 4; + } + while (len == 0); + data->idat_remain = len; + } + + r = 0; + grub_file_read (data->file, &r, 1); + + if (data->inside_idat) + data->idat_remain--; + + return r; +} + +static int +grub_png_get_bits (struct grub_png_data *data, int num) +{ + int code, shift; + + if (data->bit_count == 0) + { + data->bit_save = grub_png_get_byte (data); + data->bit_count = 8; + } + + code = 0; + shift = 0; + while (grub_errno == 0) + { + int n; + + n = data->bit_count; + if (n > num) + n = num; + + code += (int) (data->bit_save & ((1 << n) - 1)) << shift; + num -= n; + if (!num) + { + data->bit_count -= n; + data->bit_save >>= n; + break; + } + + shift += n; + + data->bit_save = grub_png_get_byte (data); + data->bit_count = 8; + } + + return code; +} + +static grub_err_t +grub_png_decode_image_palette (struct grub_png_data *data, + unsigned len) +{ + unsigned i = 0, j; + + if (len == 0) + return GRUB_ERR_NONE; + + for (i = 0; 3 * i < len && i < 256; i++) + for (j = 0; j < 3; j++) + data->palette[i][j] = grub_png_get_byte (data); + for (i *= 3; i < len; i++) + grub_png_get_byte (data); + + grub_png_get_dword (data); + + return GRUB_ERR_NONE; +} + +static grub_err_t +grub_png_decode_image_header (struct grub_png_data *data) +{ + int color_type; + int color_bits; + enum grub_video_blit_format blt; + + data->image_width = grub_png_get_dword (data); + data->image_height = grub_png_get_dword (data); + + if ((!data->image_height) || (!data->image_width)) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, "png: invalid image size"); + + color_bits = grub_png_get_byte (data); + data->is_16bit = (color_bits == 16); + + color_type = grub_png_get_byte (data); + + /* According to PNG spec, no other types are valid. */ + if ((color_type & ~(PNG_COLOR_MASK_ALPHA | PNG_COLOR_MASK_COLOR)) + && (color_type != PNG_COLOR_TYPE_PALETTE)) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, + "png: color type not supported"); + if (color_type == PNG_COLOR_TYPE_PALETTE) + data->is_palette = 1; + if (data->is_16bit && data->is_palette) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, + "png: color type not supported"); + if (color_type & PNG_COLOR_MASK_ALPHA) + blt = GRUB_VIDEO_BLIT_FORMAT_RGBA_8888; + else + blt = GRUB_VIDEO_BLIT_FORMAT_RGB_888; + if (data->is_palette) + data->bpp = 1; + else if (color_type & PNG_COLOR_MASK_COLOR) + data->bpp = 3; + else + { + data->is_gray = 1; + data->bpp = 1; + } + + if ((color_bits != 8) && (color_bits != 16) + && (color_bits != 4 + || !(data->is_gray || data->is_palette))) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, + "png: bit depth must be 8 or 16"); + + if (color_type & PNG_COLOR_MASK_ALPHA) + data->bpp++; + + if (grub_video_bitmap_create (data->bitmap, data->image_width, + data->image_height, + blt)) + return grub_errno; + + if (data->is_16bit) + data->bpp <<= 1; + + data->color_bits = color_bits; + + if (grub_mul (data->image_width, data->bpp, &data->row_bytes)) + return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected")); + + if (data->color_bits <= 4) + { + if (grub_mul (data->image_width, data->color_bits + 7, &data->row_bytes)) + return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected")); + + data->row_bytes >>= 3; + } + +#ifndef GRUB_CPU_WORDS_BIGENDIAN + if (data->is_16bit || data->is_gray || data->is_palette) +#endif + { + data->image_data = grub_calloc (data->image_height, data->row_bytes); + if (grub_errno) + return grub_errno; + + data->cur_rgb = data->image_data; + } +#ifndef GRUB_CPU_WORDS_BIGENDIAN + else + { + data->image_data = 0; + data->cur_rgb = (*data->bitmap)->data; + } +#endif + + data->raw_bytes = data->image_height * (data->row_bytes + 1); + + data->cur_column = 0; + data->first_line = 1; + + if (grub_png_get_byte (data) != PNG_COMPRESSION_BASE) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, + "png: compression method not supported"); + + if (grub_png_get_byte (data) != PNG_FILTER_TYPE_BASE) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, + "png: filter method not supported"); + + if (grub_png_get_byte (data) != PNG_INTERLACE_NONE) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, + "png: interlace method not supported"); + + /* Skip crc checksum. */ + grub_png_get_dword (data); + + return grub_errno; +} + +/* Order of the bit length code lengths. */ +static const grub_uint8_t bitorder[] = { + 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 +}; + +/* Copy lengths for literal codes 257..285. */ +static const int cplens[] = { + 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, + 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 +}; + +/* Extra bits for literal codes 257..285. */ +static const grub_uint8_t cplext[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, + 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99 +}; /* 99==invalid */ + +/* Copy offsets for distance codes 0..29. */ +static const int cpdist[] = { + 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, + 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, + 8193, 12289, 16385, 24577 +}; + +/* Extra bits for distance codes. */ +static const grub_uint8_t cpdext[] = { + 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, + 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, + 12, 12, 13, 13 +}; + +static void +grub_png_init_huff_table (struct huff_table *ht, int cur_maxlen, + int *cur_values, int *cur_maxval, int *cur_offset) +{ + ht->values = cur_values; + ht->maxval = cur_maxval; + ht->offset = cur_offset; + ht->num_values = 0; + ht->max_length = cur_maxlen; + grub_memset (cur_maxval, 0, sizeof (int) * cur_maxlen); +} + +static void +grub_png_insert_huff_item (struct huff_table *ht, int code, int len) +{ + int i, n; + + if (len == 0) + return; + + if (len > ht->max_length) + { + grub_error (GRUB_ERR_BAD_FILE_TYPE, "png: invalid code length"); + return; + } + + n = 0; + for (i = len; i < ht->max_length; i++) + n += ht->maxval[i]; + + for (i = 0; i < n; i++) + ht->values[ht->num_values - i] = ht->values[ht->num_values - i - 1]; + + ht->values[ht->num_values - n] = code; + ht->num_values++; + ht->maxval[len - 1]++; +} + +static void +grub_png_build_huff_table (struct huff_table *ht) +{ + int base, ofs, i; + + base = 0; + ofs = 0; + for (i = 0; i < ht->max_length; i++) + { + base += ht->maxval[i]; + ofs += ht->maxval[i]; + + ht->maxval[i] = base; + ht->offset[i] = ofs - base; + + base <<= 1; + } +} + +static int +grub_png_get_huff_code (struct grub_png_data *data, struct huff_table *ht) +{ + int code, i; + + code = 0; + for (i = 0; i < ht->max_length; i++) + { + code = (code << 1) + grub_png_get_bits (data, 1); + if (code < ht->maxval[i]) + return ht->values[code + ht->offset[i]]; + } + return 0; +} + +static grub_err_t +grub_png_init_fixed_block (struct grub_png_data *data) +{ + int i; + + grub_png_init_huff_table (&data->code_table, DEFLATE_HUFF_LEN, + data->code_values, data->code_maxval, + data->code_offset); + + for (i = 0; i < 144; i++) + grub_png_insert_huff_item (&data->code_table, i, 8); + + for (; i < 256; i++) + grub_png_insert_huff_item (&data->code_table, i, 9); + + for (; i < 280; i++) + grub_png_insert_huff_item (&data->code_table, i, 7); + + for (; i < DEFLATE_HLIT_MAX; i++) + grub_png_insert_huff_item (&data->code_table, i, 8); + + grub_png_build_huff_table (&data->code_table); + + grub_png_init_huff_table (&data->dist_table, DEFLATE_HUFF_LEN, + data->dist_values, data->dist_maxval, + data->dist_offset); + + for (i = 0; i < DEFLATE_HDIST_MAX; i++) + grub_png_insert_huff_item (&data->dist_table, i, 5); + + grub_png_build_huff_table (&data->dist_table); + + return grub_errno; +} + +static grub_err_t +grub_png_init_dynamic_block (struct grub_png_data *data) +{ + int nl, nd, nb, i, prev; + struct huff_table cl; + int cl_values[sizeof (bitorder)]; + int cl_maxval[8]; + int cl_offset[8]; + grub_uint8_t lens[DEFLATE_HCLEN_MAX]; + + nl = DEFLATE_HLIT_BASE + grub_png_get_bits (data, 5); + nd = DEFLATE_HDIST_BASE + grub_png_get_bits (data, 5); + nb = DEFLATE_HCLEN_BASE + grub_png_get_bits (data, 4); + + if ((nl > DEFLATE_HLIT_MAX) || (nd > DEFLATE_HDIST_MAX) || + (nb > DEFLATE_HCLEN_MAX)) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, "png: too much data"); + + grub_png_init_huff_table (&cl, 8, cl_values, cl_maxval, cl_offset); + + for (i = 0; i < nb; i++) + lens[bitorder[i]] = grub_png_get_bits (data, 3); + + for (; i < DEFLATE_HCLEN_MAX; i++) + lens[bitorder[i]] = 0; + + for (i = 0; i < DEFLATE_HCLEN_MAX; i++) + grub_png_insert_huff_item (&cl, i, lens[i]); + + grub_png_build_huff_table (&cl); + + grub_png_init_huff_table (&data->code_table, DEFLATE_HUFF_LEN, + data->code_values, data->code_maxval, + data->code_offset); + + grub_png_init_huff_table (&data->dist_table, DEFLATE_HUFF_LEN, + data->dist_values, data->dist_maxval, + data->dist_offset); + + prev = 0; + for (i = 0; i < nl + nd; i++) + { + int n, code; + struct huff_table *ht; + + if (grub_errno) + return grub_errno; + + if (i < nl) + { + ht = &data->code_table; + code = i; + } + else + { + ht = &data->dist_table; + code = i - nl; + } + + n = grub_png_get_huff_code (data, &cl); + if (n < 16) + { + grub_png_insert_huff_item (ht, code, n); + prev = n; + } + else if (n == 16) + { + int c; + + c = 3 + grub_png_get_bits (data, 2); + while (c > 0) + { + grub_png_insert_huff_item (ht, code++, prev); + i++; + c--; + } + i--; + } + else if (n == 17) + i += 3 + grub_png_get_bits (data, 3) - 1; + else + i += 11 + grub_png_get_bits (data, 7) - 1; + } + + grub_png_build_huff_table (&data->code_table); + grub_png_build_huff_table (&data->dist_table); + + return grub_errno; +} + +static grub_err_t +grub_png_output_byte (struct grub_png_data *data, grub_uint8_t n) +{ + if (--data->raw_bytes < 0) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, "image size overflown"); + + if (data->cur_column == 0) + { + if (n >= PNG_FILTER_VALUE_LAST) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, "invalid filter value"); + + data->cur_filter = n; + } + else + *data->cur_rgb++ = n; + + data->cur_column++; + if (data->cur_column == data->row_bytes + 1) + { + grub_uint8_t *blank_line = NULL; + grub_uint8_t *cur = data->cur_rgb - data->row_bytes; + grub_uint8_t *left = cur; + grub_uint8_t *up; + + if (data->first_line) + { + blank_line = grub_zalloc (data->row_bytes); + if (blank_line == NULL) + return grub_errno; + + up = blank_line; + } + else + up = cur - data->row_bytes; + + switch (data->cur_filter) + { + case PNG_FILTER_VALUE_SUB: + { + int i; + + cur += data->bpp; + for (i = data->bpp; i < data->row_bytes; i++, cur++, left++) + *cur += *left; + + break; + } + case PNG_FILTER_VALUE_UP: + { + int i; + + for (i = 0; i < data->row_bytes; i++, cur++, up++) + *cur += *up; + + break; + } + case PNG_FILTER_VALUE_AVG: + { + int i; + + for (i = 0; i < data->bpp; i++, cur++, up++) + *cur += *up >> 1; + + for (; i < data->row_bytes; i++, cur++, up++, left++) + *cur += ((int) *up + (int) *left) >> 1; + + break; + } + case PNG_FILTER_VALUE_PAETH: + { + int i; + grub_uint8_t *upper_left = up; + + for (i = 0; i < data->bpp; i++, cur++, up++) + *cur += *up; + + for (; i < data->row_bytes; i++, cur++, up++, left++, upper_left++) + { + int a, b, c, pa, pb, pc; + + a = *left; + b = *up; + c = *upper_left; + + pa = b - c; + pb = a - c; + pc = pa + pb; + + if (pa < 0) + pa = -pa; + + if (pb < 0) + pb = -pb; + + if (pc < 0) + pc = -pc; + + *cur += ((pa <= pb) && (pa <= pc)) ? a : (pb <= pc) ? b : c; + } + } + } + + grub_free (blank_line); + + data->cur_column = 0; + data->first_line = 0; + } + + return grub_errno; +} + +static grub_err_t +grub_png_read_dynamic_block (struct grub_png_data *data) +{ + while (grub_errno == 0) + { + int n; + + n = grub_png_get_huff_code (data, &data->code_table); + if (n < 256) + { + data->slide[data->wp] = n; + grub_png_output_byte (data, n); + + data->wp++; + if (data->wp >= WSIZE) + data->wp = 0; + } + else if (n == 256) + break; + else + { + int len, dist, pos; + + n -= 257; + len = cplens[n]; + if (cplext[n]) + len += grub_png_get_bits (data, cplext[n]); + + n = grub_png_get_huff_code (data, &data->dist_table); + dist = cpdist[n]; + if (cpdext[n]) + dist += grub_png_get_bits (data, cpdext[n]); + + pos = data->wp - dist; + if (pos < 0) + pos += WSIZE; + + while (len > 0) + { + data->slide[data->wp] = data->slide[pos]; + grub_png_output_byte (data, data->slide[data->wp]); + + data->wp++; + if (data->wp >= WSIZE) + data->wp = 0; + + pos++; + if (pos >= WSIZE) + pos = 0; + + len--; + } + } + } + + return grub_errno; +} + +static grub_err_t +grub_png_decode_image_data (struct grub_png_data *data) +{ + grub_uint8_t cmf, flg; + int final; + + cmf = grub_png_get_byte (data); + flg = grub_png_get_byte (data); + + if ((cmf & 0xF) != Z_DEFLATED) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, + "png: only support deflate compression method"); + + if (flg & Z_FLAG_DICT) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, + "png: dictionary not supported"); + + do + { + int block_type; + + final = grub_png_get_bits (data, 1); + block_type = grub_png_get_bits (data, 2); + + switch (block_type) + { + case INFLATE_STORED: + { + grub_uint16_t i, len; + + data->bit_count = 0; + len = grub_png_get_byte (data); + len += ((grub_uint16_t) grub_png_get_byte (data)) << 8; + + /* Skip NLEN field. */ + grub_png_get_byte (data); + grub_png_get_byte (data); + + for (i = 0; i < len; i++) + grub_png_output_byte (data, grub_png_get_byte (data)); + + break; + } + + case INFLATE_FIXED: + grub_png_init_fixed_block (data); + grub_png_read_dynamic_block (data); + break; + + case INFLATE_DYNAMIC: + grub_png_init_dynamic_block (data); + grub_png_read_dynamic_block (data); + break; + + default: + return grub_error (GRUB_ERR_BAD_FILE_TYPE, + "png: unknown block type"); + } + } + while ((!final) && (grub_errno == 0)); + + /* Skip adler checksum. */ + grub_png_get_dword (data); + + /* Skip crc checksum. */ + grub_png_get_dword (data); + + return grub_errno; +} + +static const grub_uint8_t png_magic[8] = + { 0x89, 0x50, 0x4e, 0x47, 0xd, 0xa, 0x1a, 0x0a }; + +static void +grub_png_convert_image (struct grub_png_data *data) +{ + unsigned i; + grub_uint8_t *d1, *d2; + + d1 = (*data->bitmap)->data; + d2 = data->image_data + data->is_16bit; + +#ifndef GRUB_CPU_WORDS_BIGENDIAN +#define R4 3 +#define G4 2 +#define B4 1 +#define A4 0 +#define R3 2 +#define G3 1 +#define B3 0 +#else +#define R4 0 +#define G4 1 +#define B4 2 +#define A4 3 +#define R3 0 +#define G3 1 +#define B3 2 +#endif + + if (data->color_bits <= 4) + { + grub_uint8_t palette[16][3]; + grub_uint8_t *d1c, *d2c; + int shift; + int mask = (1 << data->color_bits) - 1; + unsigned j; + if (data->is_gray) + { + /* Generic formula is + (0xff * i) / ((1U << data->color_bits) - 1) + but for allowed bit depth of 1, 2 and for it's + equivalent to + (0xff / ((1U << data->color_bits) - 1)) * i + Precompute the multipliers to avoid division. + */ + + const grub_uint8_t multipliers[5] = { 0xff, 0xff, 0x55, 0x24, 0x11 }; + for (i = 0; i < (1U << data->color_bits); i++) + { + grub_uint8_t col = multipliers[data->color_bits] * i; + palette[i][0] = col; + palette[i][1] = col; + palette[i][2] = col; + } + } + else + grub_memcpy (palette, data->palette, 3 << data->color_bits); + d1c = d1; + d2c = d2; + for (j = 0; j < data->image_height; j++, d1c += data->image_width * 3, + d2c += data->row_bytes) + { + d1 = d1c; + d2 = d2c; + shift = 8 - data->color_bits; + for (i = 0; i < data->image_width; i++, d1 += 3) + { + grub_uint8_t col = (d2[0] >> shift) & mask; + d1[R3] = data->palette[col][2]; + d1[G3] = data->palette[col][1]; + d1[B3] = data->palette[col][0]; + shift -= data->color_bits; + if (shift < 0) + { + d2++; + shift += 8; + } + } + } + return; + } + + if (data->is_palette) + { + for (i = 0; i < (data->image_width * data->image_height); + i++, d1 += 3, d2++) + { + d1[R3] = data->palette[d2[0]][2]; + d1[G3] = data->palette[d2[0]][1]; + d1[B3] = data->palette[d2[0]][0]; + } + return; + } + + if (data->is_gray) + { + switch (data->bpp) + { + case 4: + /* 16-bit gray with alpha. */ + for (i = 0; i < (data->image_width * data->image_height); + i++, d1 += 4, d2 += 4) + { + d1[R4] = d2[3]; + d1[G4] = d2[3]; + d1[B4] = d2[3]; + d1[A4] = d2[1]; + } + break; + case 2: + if (data->is_16bit) + /* 16-bit gray without alpha. */ + { + for (i = 0; i < (data->image_width * data->image_height); + i++, d1 += 4, d2 += 2) + { + d1[R3] = d2[1]; + d1[G3] = d2[1]; + d1[B3] = d2[1]; + } + } + else + /* 8-bit gray with alpha. */ + { + for (i = 0; i < (data->image_width * data->image_height); + i++, d1 += 4, d2 += 2) + { + d1[R4] = d2[1]; + d1[G4] = d2[1]; + d1[B4] = d2[1]; + d1[A4] = d2[0]; + } + } + break; + /* 8-bit gray without alpha. */ + case 1: + for (i = 0; i < (data->image_width * data->image_height); + i++, d1 += 3, d2++) + { + d1[R3] = d2[0]; + d1[G3] = d2[0]; + d1[B3] = d2[0]; + } + break; + } + return; + } + + { + /* Only copy the upper 8 bit. */ +#ifndef GRUB_CPU_WORDS_BIGENDIAN + for (i = 0; i < (data->image_width * data->image_height * data->bpp >> 1); + i++, d1++, d2 += 2) + *d1 = *d2; +#else + switch (data->bpp) + { + /* 16-bit with alpha. */ + case 8: + for (i = 0; i < (data->image_width * data->image_height); + i++, d1 += 4, d2+=8) + { + d1[0] = d2[7]; + d1[1] = d2[5]; + d1[2] = d2[3]; + d1[3] = d2[1]; + } + break; + /* 16-bit without alpha. */ + case 6: + for (i = 0; i < (data->image_width * data->image_height); + i++, d1 += 3, d2+=6) + { + d1[0] = d2[5]; + d1[1] = d2[3]; + d1[2] = d2[1]; + } + break; + case 4: + /* 8-bit with alpha. */ + for (i = 0; i < (data->image_width * data->image_height); + i++, d1 += 4, d2 += 4) + { + d1[0] = d2[3]; + d1[1] = d2[2]; + d1[2] = d2[1]; + d1[3] = d2[0]; + } + break; + /* 8-bit without alpha. */ + case 3: + for (i = 0; i < (data->image_width * data->image_height); + i++, d1 += 3, d2 += 3) + { + d1[0] = d2[2]; + d1[1] = d2[1]; + d1[2] = d2[0]; + } + break; + } +#endif + } + +} + +static grub_err_t +grub_png_decode_png (struct grub_png_data *data) +{ + grub_uint8_t magic[8]; + + if (grub_file_read (data->file, &magic[0], 8) != 8) + return grub_errno; + + if (grub_memcmp (magic, png_magic, sizeof (png_magic))) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, "png: not a png file"); + + while (1) + { + grub_uint32_t len, type; + + len = grub_png_get_dword (data); + type = grub_png_get_dword (data); + data->next_offset = data->file->offset + len + 4; + + switch (type) + { + case PNG_CHUNK_IHDR: + grub_png_decode_image_header (data); + break; + + case PNG_CHUNK_PLTE: + grub_png_decode_image_palette (data, len); + break; + + case PNG_CHUNK_IDAT: + data->inside_idat = 1; + data->idat_remain = len; + data->bit_count = 0; + + grub_png_decode_image_data (data); + + data->inside_idat = 0; + break; + + case PNG_CHUNK_IEND: + if (data->image_data) + grub_png_convert_image (data); + + return grub_errno; + + default: + grub_file_seek (data->file, data->file->offset + len + 4); + } + + if (grub_errno) + break; + + if (data->file->offset != data->next_offset) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, + "png: chunk size error"); + } + + return grub_errno; +} + +static grub_err_t +grub_video_reader_png (struct grub_video_bitmap **bitmap, + const char *filename) +{ + grub_file_t file; + struct grub_png_data *data; + + file = grub_buffile_open (filename, GRUB_FILE_TYPE_PIXMAP, 0); + if (!file) + return grub_errno; + + data = grub_zalloc (sizeof (*data)); + if (data != NULL) + { + data->file = file; + data->bitmap = bitmap; + + grub_png_decode_png (data); + + grub_free (data->image_data); + grub_free (data); + } + + if (grub_errno != GRUB_ERR_NONE) + { + grub_video_bitmap_destroy (*bitmap); + *bitmap = 0; + } + + grub_file_close (file); + return grub_errno; +} + +#if defined(PNG_DEBUG) +static grub_err_t +grub_cmd_pngtest (grub_command_t cmd_d __attribute__ ((unused)), + int argc, char **args) +{ + struct grub_video_bitmap *bitmap = 0; + + if (argc != 1) + return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected")); + + grub_video_reader_png (&bitmap, args[0]); + if (grub_errno != GRUB_ERR_NONE) + return grub_errno; + + grub_video_bitmap_destroy (bitmap); + + return GRUB_ERR_NONE; +} +#endif + +static struct grub_video_bitmap_reader png_reader = { + .extension = ".png", + .reader = grub_video_reader_png, + .next = 0 +}; + +GRUB_MOD_INIT (png) +{ + grub_video_bitmap_reader_register (&png_reader); +#if defined(PNG_DEBUG) + cmd = grub_register_command ("pngtest", grub_cmd_pngtest, + "FILE", + "Tests loading of PNG bitmap."); +#endif +} + +GRUB_MOD_FINI (png) +{ +#if defined(PNG_DEBUG) + grub_unregister_command (cmd); +#endif + grub_video_bitmap_reader_unregister (&png_reader); +} diff --git a/grub-core/video/readers/tga.c b/grub-core/video/readers/tga.c new file mode 100644 index 0000000..7cb9d1d --- /dev/null +++ b/grub-core/video/readers/tga.c @@ -0,0 +1,511 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2006,2007 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <grub/bitmap.h> +#include <grub/types.h> +#include <grub/normal.h> +#include <grub/dl.h> +#include <grub/mm.h> +#include <grub/misc.h> +#include <grub/bufio.h> + +GRUB_MOD_LICENSE ("GPLv3+"); + +/* Uncomment following define to enable TGA debug. */ +//#define TGA_DEBUG + +#define dump_int_field(x) grub_dprintf ("tga", #x " = %d (0x%04x)\n", x, x); +#if defined(TGA_DEBUG) +static grub_command_t cmd; +#endif + +enum +{ + GRUB_TGA_IMAGE_TYPE_NONE = 0, + GRUB_TGA_IMAGE_TYPE_UNCOMPRESSED_INDEXCOLOR = 1, + GRUB_TGA_IMAGE_TYPE_UNCOMPRESSED_TRUECOLOR = 2, + GRUB_TGA_IMAGE_TYPE_UNCOMPRESSED_BLACK_AND_WHITE = 3, + GRUB_TGA_IMAGE_TYPE_RLE_INDEXCOLOR = 9, + GRUB_TGA_IMAGE_TYPE_RLE_TRUECOLOR = 10, + GRUB_TGA_IMAGE_TYPE_RLE_BLACK_AND_WHITE = 11, +}; + +enum +{ + GRUB_TGA_COLOR_MAP_TYPE_NONE = 0, + GRUB_TGA_COLOR_MAP_TYPE_INCLUDED = 1 +}; + +enum +{ + GRUB_TGA_IMAGE_ORIGIN_RIGHT = 0x10, + GRUB_TGA_IMAGE_ORIGIN_TOP = 0x20 +}; + +struct grub_tga_header +{ + grub_uint8_t id_length; + grub_uint8_t color_map_type; + grub_uint8_t image_type; + + /* Color Map Specification. */ + grub_uint16_t color_map_first_index; + grub_uint16_t color_map_length; + grub_uint8_t color_map_bpp; + + /* Image Specification. */ + grub_uint16_t image_x_origin; + grub_uint16_t image_y_origin; + grub_uint16_t image_width; + grub_uint16_t image_height; + grub_uint8_t image_bpp; + grub_uint8_t image_descriptor; +} GRUB_PACKED; + +struct tga_data +{ + struct grub_tga_header hdr; + int uses_rle; + int pktlen; + int bpp; + int is_rle; + grub_uint8_t pixel[4]; + grub_uint8_t palette[256][3]; + struct grub_video_bitmap *bitmap; + grub_file_t file; + unsigned image_width; + unsigned image_height; +}; + +static grub_err_t +fetch_pixel (struct tga_data *data) +{ + if (!data->uses_rle) + { + if (grub_file_read (data->file, &data->pixel[0], data->bpp) + != data->bpp) + return grub_errno; + return GRUB_ERR_NONE; + } + if (!data->pktlen) + { + grub_uint8_t type; + if (grub_file_read (data->file, &type, sizeof (type)) != sizeof(type)) + return grub_errno; + data->is_rle = !!(type & 0x80); + data->pktlen = (type & 0x7f) + 1; + if (data->is_rle && grub_file_read (data->file, &data->pixel[0], data->bpp) + != data->bpp) + return grub_errno; + } + if (!data->is_rle && grub_file_read (data->file, &data->pixel[0], data->bpp) + != data->bpp) + return grub_errno; + data->pktlen--; + return GRUB_ERR_NONE; +} + +static grub_err_t +tga_load_palette (struct tga_data *data) +{ + grub_size_t len = grub_le_to_cpu32 (data->hdr.color_map_length) * 3; + + if (len > sizeof (data->palette)) + len = sizeof (data->palette); + + if (grub_file_read (data->file, &data->palette, len) + != (grub_ssize_t) len) + return grub_errno; + +#ifndef GRUB_CPU_WORDS_BIGENDIAN + { + grub_size_t i; + for (i = 0; 3 * i < len; i++) + { + grub_uint8_t t; + t = data->palette[i][0]; + data->palette[i][0] = data->palette[i][2]; + data->palette[i][2] = t; + } + } +#endif + return GRUB_ERR_NONE; +} + +static grub_err_t +tga_load_index_color (struct tga_data *data) +{ + unsigned int x; + unsigned int y; + grub_uint8_t *ptr; + + for (y = 0; y < data->image_height; y++) + { + ptr = data->bitmap->data; + if ((data->hdr.image_descriptor & GRUB_TGA_IMAGE_ORIGIN_TOP) != 0) + ptr += y * data->bitmap->mode_info.pitch; + else + ptr += (data->image_height - 1 - y) * data->bitmap->mode_info.pitch; + + for (x = 0; x < data->image_width; x++) + { + grub_err_t err; + err = fetch_pixel (data); + if (err) + return err; + + ptr[0] = data->palette[data->pixel[0]][0]; + ptr[1] = data->palette[data->pixel[0]][1]; + ptr[2] = data->palette[data->pixel[0]][2]; + + ptr += 3; + } + } + return GRUB_ERR_NONE; +} + +static grub_err_t +tga_load_grayscale (struct tga_data *data) +{ + unsigned int x; + unsigned int y; + grub_uint8_t *ptr; + + for (y = 0; y < data->image_height; y++) + { + ptr = data->bitmap->data; + if ((data->hdr.image_descriptor & GRUB_TGA_IMAGE_ORIGIN_TOP) != 0) + ptr += y * data->bitmap->mode_info.pitch; + else + ptr += (data->image_height - 1 - y) * data->bitmap->mode_info.pitch; + + for (x = 0; x < data->image_width; x++) + { + grub_err_t err; + err = fetch_pixel (data); + if (err) + return err; + + ptr[0] = data->pixel[0]; + ptr[1] = data->pixel[0]; + ptr[2] = data->pixel[0]; + + ptr += 3; + } + } + return GRUB_ERR_NONE; +} + +static grub_err_t +tga_load_truecolor_R8G8B8 (struct tga_data *data) +{ + unsigned int x; + unsigned int y; + grub_uint8_t *ptr; + + for (y = 0; y < data->image_height; y++) + { + ptr = data->bitmap->data; + if ((data->hdr.image_descriptor & GRUB_TGA_IMAGE_ORIGIN_TOP) != 0) + ptr += y * data->bitmap->mode_info.pitch; + else + ptr += (data->image_height - 1 - y) * data->bitmap->mode_info.pitch; + + for (x = 0; x < data->image_width; x++) + { + grub_err_t err; + err = fetch_pixel (data); + if (err) + return err; + +#ifdef GRUB_CPU_WORDS_BIGENDIAN + ptr[0] = data->pixel[0]; + ptr[1] = data->pixel[1]; + ptr[2] = data->pixel[2]; +#else + ptr[0] = data->pixel[2]; + ptr[1] = data->pixel[1]; + ptr[2] = data->pixel[0]; +#endif + ptr += 3; + } + } + return GRUB_ERR_NONE; +} + +static grub_err_t +tga_load_truecolor_R8G8B8A8 (struct tga_data *data) +{ + unsigned int x; + unsigned int y; + grub_uint8_t *ptr; + + for (y = 0; y < data->image_height; y++) + { + ptr = data->bitmap->data; + if ((data->hdr.image_descriptor & GRUB_TGA_IMAGE_ORIGIN_TOP) != 0) + ptr += y * data->bitmap->mode_info.pitch; + else + ptr += (data->image_height - 1 - y) * data->bitmap->mode_info.pitch; + + for (x = 0; x < data->image_width; x++) + { + grub_err_t err; + err = fetch_pixel (data); + if (err) + return err; + +#ifdef GRUB_CPU_WORDS_BIGENDIAN + ptr[0] = data->pixel[0]; + ptr[1] = data->pixel[1]; + ptr[2] = data->pixel[2]; + ptr[3] = data->pixel[3]; +#else + ptr[0] = data->pixel[2]; + ptr[1] = data->pixel[1]; + ptr[2] = data->pixel[0]; + ptr[3] = data->pixel[3]; +#endif + + ptr += 4; + } + } + return GRUB_ERR_NONE; +} + +static grub_err_t +grub_video_reader_tga (struct grub_video_bitmap **bitmap, + const char *filename) +{ + grub_ssize_t pos; + struct tga_data data; + + grub_memset (&data, 0, sizeof (data)); + + data.file = grub_buffile_open (filename, GRUB_FILE_TYPE_PIXMAP, 0); + if (! data.file) + return grub_errno; + + /* TGA Specification states that we SHOULD start by reading + ID from end of file, but we really don't care about that as we are + not going to support developer area & extensions at this point. */ + + /* Read TGA header from beginning of file. */ + if (grub_file_read (data.file, &data.hdr, sizeof (data.hdr)) + != sizeof (data.hdr)) + { + grub_file_close (data.file); + return grub_errno; + } + + /* Skip ID field. */ + pos = grub_file_tell (data.file); + pos += data.hdr.id_length; + grub_file_seek (data.file, pos); + if (grub_errno != GRUB_ERR_NONE) + { + grub_file_close (data.file); + return grub_errno; + } + + grub_dprintf("tga", "tga: header\n"); + dump_int_field(data.hdr.id_length); + dump_int_field(data.hdr.color_map_type); + dump_int_field(data.hdr.image_type); + dump_int_field(data.hdr.color_map_first_index); + dump_int_field(data.hdr.color_map_length); + dump_int_field(data.hdr.color_map_bpp); + dump_int_field(data.hdr.image_x_origin); + dump_int_field(data.hdr.image_y_origin); + dump_int_field(data.hdr.image_width); + dump_int_field(data.hdr.image_height); + dump_int_field(data.hdr.image_bpp); + dump_int_field(data.hdr.image_descriptor); + + data.image_width = grub_le_to_cpu16 (data.hdr.image_width); + data.image_height = grub_le_to_cpu16 (data.hdr.image_height); + + /* Check that bitmap encoding is supported. */ + switch (data.hdr.image_type) + { + case GRUB_TGA_IMAGE_TYPE_RLE_TRUECOLOR: + case GRUB_TGA_IMAGE_TYPE_RLE_BLACK_AND_WHITE: + case GRUB_TGA_IMAGE_TYPE_RLE_INDEXCOLOR: + data.uses_rle = 1; + break; + case GRUB_TGA_IMAGE_TYPE_UNCOMPRESSED_TRUECOLOR: + case GRUB_TGA_IMAGE_TYPE_UNCOMPRESSED_BLACK_AND_WHITE: + case GRUB_TGA_IMAGE_TYPE_UNCOMPRESSED_INDEXCOLOR: + data.uses_rle = 0; + break; + + default: + grub_file_close (data.file); + return grub_error (GRUB_ERR_BAD_FILE_TYPE, + "unsupported bitmap format (unknown encoding %d)", data.hdr.image_type); + } + + data.bpp = data.hdr.image_bpp / 8; + + /* Check that bitmap depth is supported. */ + switch (data.hdr.image_type) + { + case GRUB_TGA_IMAGE_TYPE_RLE_BLACK_AND_WHITE: + case GRUB_TGA_IMAGE_TYPE_UNCOMPRESSED_BLACK_AND_WHITE: + if (data.hdr.image_bpp != 8) + { + grub_file_close (data.file); + return grub_error (GRUB_ERR_BAD_FILE_TYPE, + "unsupported bitmap format (bpp=%d)", + data.hdr.image_bpp); + } + grub_video_bitmap_create (bitmap, data.image_width, + data.image_height, + GRUB_VIDEO_BLIT_FORMAT_RGB_888); + if (grub_errno != GRUB_ERR_NONE) + { + grub_file_close (data.file); + return grub_errno; + } + + data.bitmap = *bitmap; + /* Load bitmap data. */ + tga_load_grayscale (&data); + break; + + case GRUB_TGA_IMAGE_TYPE_RLE_INDEXCOLOR: + case GRUB_TGA_IMAGE_TYPE_UNCOMPRESSED_INDEXCOLOR: + if (data.hdr.image_bpp != 8 + || data.hdr.color_map_bpp != 24 + || data.hdr.color_map_first_index != 0) + { + grub_file_close (data.file); + return grub_error (GRUB_ERR_BAD_FILE_TYPE, + "unsupported bitmap format (bpp=%d)", + data.hdr.image_bpp); + } + grub_video_bitmap_create (bitmap, data.image_width, + data.image_height, + GRUB_VIDEO_BLIT_FORMAT_RGB_888); + if (grub_errno != GRUB_ERR_NONE) + { + grub_file_close (data.file); + return grub_errno; + } + + data.bitmap = *bitmap; + /* Load bitmap data. */ + tga_load_palette (&data); + tga_load_index_color (&data); + break; + + case GRUB_TGA_IMAGE_TYPE_RLE_TRUECOLOR: + case GRUB_TGA_IMAGE_TYPE_UNCOMPRESSED_TRUECOLOR: + switch (data.hdr.image_bpp) + { + case 24: + grub_video_bitmap_create (bitmap, data.image_width, + data.image_height, + GRUB_VIDEO_BLIT_FORMAT_RGB_888); + if (grub_errno != GRUB_ERR_NONE) + { + grub_file_close (data.file); + return grub_errno; + } + + data.bitmap = *bitmap; + /* Load bitmap data. */ + tga_load_truecolor_R8G8B8 (&data); + break; + + case 32: + grub_video_bitmap_create (bitmap, data.image_width, + data.image_height, + GRUB_VIDEO_BLIT_FORMAT_RGBA_8888); + if (grub_errno != GRUB_ERR_NONE) + { + grub_file_close (data.file); + return grub_errno; + } + + data.bitmap = *bitmap; + /* Load bitmap data. */ + tga_load_truecolor_R8G8B8A8 (&data); + break; + + default: + grub_file_close (data.file); + return grub_error (GRUB_ERR_BAD_FILE_TYPE, + "unsupported bitmap format (bpp=%d)", + data.hdr.image_bpp); + } + } + + /* If there was a loading problem, destroy bitmap. */ + if (grub_errno != GRUB_ERR_NONE) + { + grub_video_bitmap_destroy (*bitmap); + *bitmap = 0; + } + + grub_file_close (data.file); + return grub_errno; +} + +#if defined(TGA_DEBUG) +static grub_err_t +grub_cmd_tgatest (grub_command_t cmd_d __attribute__ ((unused)), + int argc, char **args) +{ + struct grub_video_bitmap *bitmap = 0; + + if (argc != 1) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name required"); + + grub_video_reader_tga (&bitmap, args[0]); + if (grub_errno != GRUB_ERR_NONE) + return grub_errno; + + grub_video_bitmap_destroy (bitmap); + + return GRUB_ERR_NONE; +} +#endif + +static struct grub_video_bitmap_reader tga_reader = { + .extension = ".tga", + .reader = grub_video_reader_tga, + .next = 0 +}; + +GRUB_MOD_INIT(tga) +{ + grub_video_bitmap_reader_register (&tga_reader); +#if defined(TGA_DEBUG) + cmd = grub_register_command ("tgatest", grub_cmd_tgatest, + "FILE", "Tests loading of TGA bitmap."); +#endif +} + +GRUB_MOD_FINI(tga) +{ +#if defined(TGA_DEBUG) + grub_unregister_command (cmd); +#endif + grub_video_bitmap_reader_unregister (&tga_reader); +} |