Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1JCuP8-0004Jc-FL for mharc-grub-devel@gnu.org; Thu, 10 Jan 2008 05:12:55 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1JCuP3-0004Fo-Ok for grub-devel@gnu.org; Thu, 10 Jan 2008 05:12:49 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1JCuP2-0004E0-Ex for grub-devel@gnu.org; Thu, 10 Jan 2008 05:12:49 -0500 Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1JCuP2-0004Dd-2r for grub-devel@gnu.org; Thu, 10 Jan 2008 05:12:48 -0500 Received: from py-out-1112.google.com ([64.233.166.182]) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1JCuP1-0000P7-R6 for grub-devel@gnu.org; Thu, 10 Jan 2008 05:12:48 -0500 Received: by py-out-1112.google.com with SMTP id u52so721777pyb.1 for ; Thu, 10 Jan 2008 02:12:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; bh=ku3fu4lcvYZXP83gPJuMVCIsgtb3nid7z3MM7j+xTnE=; b=c4bJsEGtJUJ4aV+scLjYdHwcMZOMaxf5dNAxoHImwyDFtJjJaI8TDhuscZPc4c+5AyTLx6yyJ5bFi9d6JI+02zJN7sR1VU7KIo8tVsCk0PjRktapDarO4ix8Oc3GXhtW6sE1xCKrDLUYIBuc0BcK+3e76FDl7Ticvq98+j1TeCs= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=Nme/4zayjv+cTunfdbqLIqfOAE8GMuL6XyXZCGNekfozpU6rnPNU+/ywFlyTYTe8uRHsLvKu6R+rKLCNe700LhqlWyUQlERcKUH619c6cWlB5jQKAKN30XzCVhG6sati7GKhw/DZ4yXnXYNPb1kRSwA1uW3MGxradv3A9FdBoYc= Received: by 10.35.11.1 with SMTP id o1mr2034033pyi.31.1199959967173; Thu, 10 Jan 2008 02:12:47 -0800 (PST) Received: by 10.35.76.20 with HTTP; Thu, 10 Jan 2008 02:12:47 -0800 (PST) Message-ID: Date: Thu, 10 Jan 2008 18:12:47 +0800 From: Bean To: "The development of GRUB 2" In-Reply-To: <47852F7C.6030407@nic.fi> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline References: <47852F7C.6030407@nic.fi> X-detected-kernel: by monty-python.gnu.org: Linux 2.6 (newer, 2) Subject: Re: [PATCH] jpeg image reader X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GRUB 2 List-Id: The development of GRUB 2 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 10 Jan 2008 10:12:50 -0000 On Jan 10, 2008 4:33 AM, Vesa J=E4=E4skel=E4inen wrote: > Nice job for implementing JPEG. This is the major reason I made the > bitmap loader interface, to allow other people to write loaders... now > if we would just have PNG support ;) This is my next project. > I know lots of JPEG loaders seems to use longjmp for error handling, > personally I don't like it, but I suppose it is ok. In grub2 you could > just check if grub_errno !=3D GRUB_ERR_NONE and then just return and at > that level do the same check. This mechanism works a bit like c++'s > exceptions. error can occur deep within function call. i can check for return value, but longjmp seems easier. > > + get_word (data); > > not important word? The first word of a segment is the length, but we don't need it. > try to keep malloc on own line and then check for pointer or for > grub_errno (or both). ok. > +static int > +get_huff_leng (struct grub_jpeg_data *data, int num) _length? change to get_huff_code. > > + get_word (data); > > + id =3D get_byte (data); > > + if (id >=3D 0x10) > > 0x10? the upper 4-bit is precision, 0 is 8-bit, 1 is 16-bit, etc. > > +static void > > +decode_du (struct grub_jpeg_data *data, int id, int *du) > > +{ > > + int pos, h1, h2, qt; > > + > > + grub_memset (du, 0, sizeof (int) * 64); > > du is always that size ? perhaps add warning about it to prototype, or > provide length? now i use type jpeg_data_unit. > > + if (get_marker (data) !=3D 0xD8) > > 0xD8? use symbols now. diff --git a/conf/i386-pc.rmk b/conf/i386-pc.rmk index a46bc74..4beeeff 100644 --- a/conf/i386-pc.rmk +++ b/conf/i386-pc.rmk @@ -136,7 +136,7 @@ pkglib_MODULES =3D biosdisk.mod _chain.mod _linux.mod linux.mod normal.mod \ =09_multiboot.mod chain.mod multiboot.mod reboot.mod halt.mod=09\ =09vbe.mod vbetest.mod vbeinfo.mod video.mod gfxterm.mod \ =09videotest.mod play.mod bitmap.mod tga.mod cpuid.mod serial.mod=09\ -=09ata.mod vga.mod +=09ata.mod vga.mod jpeg.mod # For biosdisk.mod. biosdisk_mod_SOURCES =3D disk/i386/pc/biosdisk.c @@ -249,6 +249,11 @@ tga_mod_SOURCES =3D video/readers/tga.c tga_mod_CFLAGS =3D $(COMMON_CFLAGS) tga_mod_LDFLAGS =3D $(COMMON_LDFLAGS) +# For jpeg.mod +jpeg_mod_SOURCES =3D video/readers/jpeg.c +jpeg_mod_CFLAGS =3D $(COMMON_CFLAGS) +jpeg_mod_LDFLAGS =3D $(COMMON_LDFLAGS) + # For cpuid.mod. cpuid_mod_SOURCES =3D commands/i386/cpuid.c cpuid_mod_CFLAGS =3D $(COMMON_CFLAGS) diff --git a/video/readers/jpeg.c b/video/readers/jpeg.c new file mode 100755 index 0000000..bb1eda5 --- /dev/null +++ b/video/readers/jpeg.c @@ -0,0 +1,839 @@ +/* + * 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 . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Uncomment following define to enable JPEG debug. */ +//#define JPEG_DEBUG + +typedef enum +{=09=09=09=09/* JPEG marker codes */ + M_SOF0 =3D 0xc0, + M_SOF1 =3D 0xc1, + M_SOF2 =3D 0xc2, + M_SOF3 =3D 0xc3, + + M_SOF5 =3D 0xc5, + M_SOF6 =3D 0xc6, + M_SOF7 =3D 0xc7, + + M_JPG =3D 0xc8, + M_SOF9 =3D 0xc9, + M_SOF10 =3D 0xca, + M_SOF11 =3D 0xcb, + + M_SOF13 =3D 0xcd, + M_SOF14 =3D 0xce, + M_SOF15 =3D 0xcf, + + M_DHT =3D 0xc4, + + M_DAC =3D 0xcc, + + M_RST0 =3D 0xd0, + M_RST1 =3D 0xd1, + M_RST2 =3D 0xd2, + M_RST3 =3D 0xd3, + M_RST4 =3D 0xd4, + M_RST5 =3D 0xd5, + M_RST6 =3D 0xd6, + M_RST7 =3D 0xd7, + + M_SOI =3D 0xd8, + M_EOI =3D 0xd9, + M_SOS =3D 0xda, + M_DQT =3D 0xdb, + M_DNL =3D 0xdc, + M_DRI =3D 0xdd, + M_DHP =3D 0xde, + M_EXP =3D 0xdf, + + M_APP0 =3D 0xe0, + M_APP1 =3D 0xe1, + M_APP2 =3D 0xe2, + M_APP3 =3D 0xe3, + M_APP4 =3D 0xe4, + M_APP5 =3D 0xe5, + M_APP6 =3D 0xe6, + M_APP7 =3D 0xe7, + M_APP8 =3D 0xe8, + M_APP9 =3D 0xe9, + M_APP10 =3D 0xea, + M_APP11 =3D 0xeb, + M_APP12 =3D 0xec, + M_APP13 =3D 0xed, + M_APP14 =3D 0xee, + M_APP15 =3D 0xef, + + M_JPG0 =3D 0xf0, + M_JPG13 =3D 0xfd, + M_COM =3D 0xfe, + + M_TEM =3D 0x01, + + M_ERROR =3D 0x100 +} JPEG_MARKER; + +typedef int jpeg_data_unit[64]; + +struct grub_jpeg_data +{ + grub_file_t file; + grub_jmp_buf jumper; + + int image_width; + int 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 ydu[4]; + jpeg_data_unit crdu; + jpeg_data_unit cbdu; + + int Cr_r_tab[256], Cb_b_tab[256], Cr_g_tab[256], Cb_g_tab[256]; + + int dc_value[3]; + + int bit_mask, bit_save; + + struct grub_video_bitmap **bitmap; +}; + + +static grub_uint8_t +get_byte (struct grub_jpeg_data *data) +{ + grub_uint8_t r; + + if (grub_file_read (data->file, (char *) &r, 1) !=3D 1) + grub_longjmp (data->jumper, 1); + + return r; +} + +static grub_uint16_t +get_word (struct grub_jpeg_data *data) +{ + grub_uint16_t aa, bb; + + aa =3D get_byte (data); + bb =3D get_byte (data); + return (aa << 8) + bb; +} + +static int +get_bit (struct grub_jpeg_data *data) +{ + int ret; + + if (data->bit_mask =3D=3D 0) + { + data->bit_save =3D get_byte (data); + if (data->bit_save =3D=3D 0xFF) +=09{ +=09 if (get_byte (data) !=3D 0) +=09 { +=09 grub_error (GRUB_ERR_BAD_FILE_TYPE, +=09=09=09 "jpeg: invalid 0xFF in data stream"); +=09 grub_longjmp (data->jumper, 1); +=09 } +=09} + data->bit_mask =3D 0x80; + } + + ret =3D (data->bit_save & data->bit_mask); + data->bit_mask >>=3D 1; + return ret; +} + +static int +get_huff_code (struct grub_jpeg_data *data, int num) +{ + int value, i, bit; + + if (num =3D=3D 0) + return 0; + + bit =3D get_bit (data); + value =3D (bit !=3D 0); + for (i =3D 1; i < num; i++) + value =3D value * 2 + (get_bit (data) !=3D 0); + if (!bit) + value +=3D 1 - (1 << num); + + return value; +} + +static int +get_huff_base (struct grub_jpeg_data *data, int id) +{ + int code, i; + + code =3D 0; + for (i =3D 0; i < 16; i++) + { + code <<=3D 1; + if (get_bit (data)) +=09code++; + if (code < data->huff_maxval[id][i]) +=09return data->huff_value[id][code + data->huff_offset[id][i]]; + } + grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: huffman decode fails"); + grub_longjmp (data->jumper, 1); + return 0; +} + +static void +decode_huff_table (struct grub_jpeg_data *data) +{ + int id, ac, i, n, base, ofs; + grub_uint8_t count[16]; + + get_word (data); + id =3D get_byte (data); + ac =3D (id >> 4); + id &=3D 0xF; + if (id > 1) + { + grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: too many huffman table"); + grub_longjmp (data->jumper, 1); + } + + if (grub_file_read (data->file, (char *) &count, sizeof (count)) !=3D + sizeof (count)) + grub_longjmp (data->jumper, 1); + + n =3D 0; + for (i =3D 0; i < 16; i++) + n +=3D count[i]; + + id +=3D ac * 2; + data->huff_value[id] =3D grub_malloc (n); + if (data->huff_value[id] =3D=3D NULL) + grub_longjmp (data->jumper, 1); + + if (grub_file_read (data->file, (char *) data->huff_value[id], n) !=3D n= ) + grub_longjmp (data->jumper, 1); + + base =3D 0; + ofs =3D 0; + for (i =3D 0; i < 16; i++) + { + base +=3D count[i]; + ofs +=3D count[i]; + + data->huff_maxval[id][i] =3D base; + data->huff_offset[id][i] =3D ofs - base; + + base <<=3D 1; + } +} + +static void +decode_quan_table (struct grub_jpeg_data *data) +{ + int id; + + get_word (data); + id =3D get_byte (data); + if (id >=3D 0x10) + { + grub_error (GRUB_ERR_BAD_FILE_TYPE, +=09=09 "jpeg: only 8-bit precision is supported"); + grub_longjmp (data->jumper, 1); + } + + if (id > 1) + { + grub_error (GRUB_ERR_BAD_FILE_TYPE, +=09=09 "jpeg: too many quantization table"); + grub_longjmp (data->jumper, 1); + } + + if (grub_file_read (data->file, (char *) &data->quan_table[id], 64) !=3D= 64) + grub_longjmp (data->jumper, 1); +} + +static void +decode_sof (struct grub_jpeg_data *data) +{ + int nc, i; + + get_word (data); + if (get_byte (data) !=3D 8) + { + grub_error (GRUB_ERR_BAD_FILE_TYPE, +=09=09 "jpeg: only 8-bit precision is supported"); + grub_longjmp (data->jumper, 1); + } + + data->image_height =3D get_word (data); + data->image_width =3D get_word (data); + + if ((!data->image_height) || (!data->image_width)) + { + grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: invalid image size"); + grub_longjmp (data->jumper, 1); + } + + nc =3D get_byte (data); + if (nc !=3D 3) + { + grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: component count must be 3= "); + grub_longjmp (data->jumper, 1); + } + + for (i =3D 0; i < nc; i++) + { + int id, ss; + + id =3D get_byte (data) - 1; + if ((id < 0) || (id >=3D 3)) +=09{ +=09 grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: invalid index"); +=09 grub_longjmp (data->jumper, 1); +=09} + ss =3D get_byte (data); + if (((!id) && (ss !=3D 0x22)) || ((id) && (ss !=3D 0x11))) +=09{ +=09 grub_error (GRUB_ERR_BAD_FILE_TYPE, +=09=09 "jpeg: only support 2:1:1 sampling"); +=09 grub_longjmp (data->jumper, 1); +=09} + data->comp_index[id][0] =3D get_byte (data); + } +} + +#define DCTSIZE 8 + +#define CONST_BITS 8 +#define PASS1_BITS 2 + +#define FIX_1_082392200 ((int) 277)=09/* FIX(1.082392200) */ +#define FIX_1_414213562 ((int) 362)=09/* FIX(1.414213562) */ +#define FIX_1_847759065 ((int) 473)=09/* FIX(1.847759065) */ +#define FIX_2_613125930 ((int) 669)=09/* FIX(2.613125930) */ + +#define ONE=09((long) 1) +#define DESCALE(x,n) (((x) + (ONE << ((n)-1))) >> (n)) + +#define MULTIPLY(var,const) DESCALE((var) * (const), CONST_BITS) + +static const grub_uint8_t natural_order[64] =3D { + 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 +}; + +static const int aanscales[64] =3D { + /* precomputed values scaled up by 14 bits */ + 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, + 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270, + 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906, + 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315, + 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, + 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552, + 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446, + 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247 +}; + +static void +idct_transform (jpeg_data_unit du) +{ + int *pd; + int ctr; + int tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; + int tmp10, tmp11, tmp12, tmp13; + int z5, z10, z11, z12, z13; + + /* Pass 1: process columns from input, store into work array. */ + + pd =3D du; + for (ctr =3D DCTSIZE; ctr > 0; ctr--) + { + if ((pd[DCTSIZE * 1] | pd[DCTSIZE * 2] | pd[DCTSIZE * 3] | +=09 pd[DCTSIZE * 4] | pd[DCTSIZE * 5] | pd[DCTSIZE * 6] | +=09 pd[DCTSIZE * 7]) =3D=3D 0) +=09{ +=09 pd[DCTSIZE * 1] =3D pd[DCTSIZE * 2] +=09 =3D pd[DCTSIZE * 3] =3D pd[DCTSIZE * 4] +=09 =3D pd[DCTSIZE * 5] =3D pd[DCTSIZE * 6] +=09 =3D pd[DCTSIZE * 7] =3D pd[DCTSIZE * 0]; + +=09 pd++;=09=09=09/* advance pointers to next column */ +=09 continue; +=09} + + /* Even part */ + + tmp0 =3D pd[DCTSIZE * 0]; + tmp1 =3D pd[DCTSIZE * 2]; + tmp2 =3D pd[DCTSIZE * 4]; + tmp3 =3D pd[DCTSIZE * 6]; + + tmp10 =3D tmp0 + tmp2;=09/* phase 3 */ + tmp11 =3D tmp0 - tmp2; + + tmp13 =3D tmp1 + tmp3;=09/* phases 5-3 */ + tmp12 =3D MULTIPLY (tmp1 - tmp3, FIX_1_414213562) - tmp13;=09/* 2*c4= */ + + tmp0 =3D tmp10 + tmp13;=09/* phase 2 */ + tmp3 =3D tmp10 - tmp13; + tmp1 =3D tmp11 + tmp12; + tmp2 =3D tmp11 - tmp12; + + /* Odd part */ + + tmp4 =3D pd[DCTSIZE * 1]; + tmp5 =3D pd[DCTSIZE * 3]; + tmp6 =3D pd[DCTSIZE * 5]; + tmp7 =3D pd[DCTSIZE * 7]; + + z13 =3D tmp6 + tmp5;=09/* phase 6 */ + z10 =3D tmp6 - tmp5; + z11 =3D tmp4 + tmp7; + z12 =3D tmp4 - tmp7; + + tmp7 =3D z11 + z13;=09=09/* phase 5 */ + tmp11 =3D MULTIPLY (z11 - z13, FIX_1_414213562);=09/* 2*c4 */ + + z5 =3D MULTIPLY (z10 + z12, FIX_1_847759065);=09/* 2*c2 */ + tmp10 =3D MULTIPLY (z12, FIX_1_082392200) - z5;=09/* 2*(c2-c6) */ + tmp12 =3D MULTIPLY (z10, -FIX_2_613125930) + z5;=09/* -2*(c2+c6) */ + + + tmp6 =3D tmp12 - tmp7;=09/* phase 2 */ + tmp5 =3D tmp11 - tmp6; + tmp4 =3D tmp10 + tmp5; + + pd[DCTSIZE * 0] =3D (int) (tmp0 + tmp7); + pd[DCTSIZE * 7] =3D (int) (tmp0 - tmp7); + pd[DCTSIZE * 1] =3D (int) (tmp1 + tmp6); + pd[DCTSIZE * 6] =3D (int) (tmp1 - tmp6); + pd[DCTSIZE * 2] =3D (int) (tmp2 + tmp5); + pd[DCTSIZE * 5] =3D (int) (tmp2 - tmp5); + pd[DCTSIZE * 4] =3D (int) (tmp3 + tmp4); + pd[DCTSIZE * 3] =3D (int) (tmp3 - tmp4); + + pd++;=09=09=09/* advance pointers to next column */ + } + + /* Pass 2: process rows from work array, store into output array. */ + /* Note that we must descale the results by a factor of 8 =3D=3D 2**3, = */ + /* and also undo the PASS1_BITS scaling. */ + + pd =3D du; + for (ctr =3D 0; ctr < DCTSIZE; ctr++) + { + /* Even part */ + + tmp10 =3D pd[0] + pd[4]; + tmp11 =3D pd[0] - pd[4]; + + tmp13 =3D pd[2] + pd[6]; + tmp12 =3D MULTIPLY (pd[2] - pd[6], FIX_1_414213562) - tmp13; + + tmp0 =3D tmp10 + tmp13; + tmp3 =3D tmp10 - tmp13; + tmp1 =3D tmp11 + tmp12; + tmp2 =3D tmp11 - tmp12; + + /* Odd part */ + + z13 =3D pd[5] + pd[3]; + z10 =3D pd[5] - pd[3]; + z11 =3D pd[1] + pd[7]; + z12 =3D pd[1] - pd[7]; + + tmp7 =3D z11 + z13;=09=09/* phase 5 */ + tmp11 =3D MULTIPLY (z11 - z13, FIX_1_414213562);=09/* 2*c4 */ + + z5 =3D MULTIPLY (z10 + z12, FIX_1_847759065);=09/* 2*c2 */ + tmp10 =3D MULTIPLY (z12, FIX_1_082392200) - z5;=09/* 2*(c2-c6) */ + tmp12 =3D MULTIPLY (z10, -FIX_2_613125930) + z5;=09/* -2*(c2+c6) */ + + tmp6 =3D tmp12 - tmp7;=09/* phase 2 */ + tmp5 =3D tmp11 - tmp6; + tmp4 =3D tmp10 + tmp5; + + /* Final output stage: scale down by a factor of 8 and range-limit = */ + + pd[0] =3D DESCALE (tmp0 + tmp7, PASS1_BITS + 3) + 128; + pd[7] =3D DESCALE (tmp0 - tmp7, PASS1_BITS + 3) + 128; + pd[1] =3D DESCALE (tmp1 + tmp6, PASS1_BITS + 3) + 128; + pd[6] =3D DESCALE (tmp1 - tmp6, PASS1_BITS + 3) + 128; + pd[2] =3D DESCALE (tmp2 + tmp5, PASS1_BITS + 3) + 128; + pd[5] =3D DESCALE (tmp2 - tmp5, PASS1_BITS + 3) + 128; + pd[4] =3D DESCALE (tmp3 + tmp4, PASS1_BITS + 3) + 128; + pd[3] =3D DESCALE (tmp3 - tmp4, PASS1_BITS + 3) + 128; + + pd +=3D DCTSIZE;=09=09/* advance pointer to next row */ + } + + for (ctr =3D 0; ctr < 64; ctr++) + { + if (du[ctr] < 0) +=09du[ctr] =3D 0; + if (du[ctr] > 255) +=09du[ctr] =3D 255; + } +} + +static void +decode_du (struct grub_jpeg_data *data, int id, jpeg_data_unit du) +{ + int pos, h1, h2, qt; + + grub_memset (du, 0, sizeof (jpeg_data_unit)); + + qt =3D data->comp_index[id][0]; + h1 =3D data->comp_index[id][1]; + h2 =3D data->comp_index[id][2]; + + data->dc_value[id] +=3D get_huff_code (data, get_huff_base (data, h1)); + + du[0] =3D + data->dc_value[id] * DESCALE ((int) data->quan_table[qt][0] * +=09=09=09=09 (int) aanscales[0], 14 - 2); + pos =3D 1; + while (pos < 64) + { + int num, val; + + num =3D get_huff_base (data, h2); + if (!num) +=09break; + val =3D get_huff_code (data, num & 0xF); + num >>=3D 4; + pos +=3D num; + du[natural_order[pos]] =3D +=09val * DESCALE ((int) data->quan_table[qt][pos] * +=09=09 (int) aanscales[natural_order[pos]], 14 - 2); + pos++; + } + + idct_transform (du); +} + +#define SCALEBITS=0916=09/* speediest right-shift on some machines */ +#define ONE_HALF=09((int) 1 << (SCALEBITS-1)) +#define FIX(x)=09=09((int) ((x) * (1L<Cr_r_tab[i] =3D (int) (FIX (1.40200) * x + ONE_HALF) >> SCALEB= ITS; + data->Cb_b_tab[i] =3D (int) (FIX (1.77200) * x + ONE_HALF) >> SCALEB= ITS; + data->Cr_g_tab[i] =3D (-FIX (0.71414)) * x; + data->Cb_g_tab[i] =3D (-FIX (0.34414)) * x + ONE_HALF; + } +} + +static void +decode_sos (struct grub_jpeg_data *data) +{ + int nc, i, r1, c1; + grub_uint8_t *ptr; + + get_word (data); + nc =3D get_byte (data); + + if (nc !=3D 3) + { + grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: component count must be 3= "); + grub_longjmp (data->jumper, 1); + } + + for (i =3D 0; i < nc; i++) + { + int id, ht; + + id =3D get_byte (data) - 1; + if ((id < 0) || (id >=3D 3)) +=09{ +=09 grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: invalid index"); +=09 grub_longjmp (data->jumper, 1); +=09} + + ht =3D get_byte (data); + data->comp_index[id][1] =3D (ht >> 4); + data->comp_index[id][2] =3D (ht & 0xF) + 2; + } + + get_byte (data); + get_word (data); + + if (grub_video_bitmap_create (data->bitmap, data->image_width, +=09=09=09=09data->image_height, +=09=09=09=09GRUB_VIDEO_BLIT_FORMAT_R8G8B8)) + grub_longjmp (data->jumper, 1); + + ptr =3D (*data->bitmap)->data; + data->bit_mask =3D 0x0; + + for (r1 =3D 0; r1 < (data->image_height >> 4); r1++) + for (c1 =3D 0; c1 < (data->image_width >> 4); c1++) + { +=09int r2, c2, r3, c3; + +=09decode_du (data, 0, data->ydu[0]); +=09decode_du (data, 0, data->ydu[1]); + +=09decode_du (data, 0, data->ydu[2]); +=09decode_du (data, 0, data->ydu[3]); + +=09decode_du (data, 1, data->cbdu); +=09decode_du (data, 2, data->crdu); + +=09for (r2 =3D 0; r2 < 2; r2++) +=09 for (c2 =3D 0; c2 < 2; c2++) +=09 for (r3 =3D 0; r3 < 8; r3++) +=09 for (c3 =3D 0; c3 < 8; c3++) +=09=09{ +=09=09 int i0, i1, i2, cr, cb, dd; + +=09=09 i0 =3D +=09=09 ((r1 * 16 + r2 * 8 + r3) * (data->image_width) + c1 * 16 + +=09=09 c2 * 8 + c3) * 3; +=09=09 i1 =3D r3 * 8 + c3; +=09=09 i2 =3D (r2 * 32 + c2 * 4) + (r3 >> 1) * 8 + (c3 >> 1); + +=09=09 cr =3D data->crdu[i2]; +=09=09 cb =3D data->cbdu[i2]; + +=09=09 if (((c3 & 1) !=3D 0) && ((i2 & 7) !=3D 7)) +=09=09 { +=09=09 cr =3D (cr + data->crdu[i2 + 1]) >> 1; +=09=09 cb =3D (cb + data->cbdu[i2 + 1]) >> 1; +=09=09 } + +=09=09 if (((r3 & 1) !=3D 0) && (i2 < 64 - 8)) +=09=09 { +=09=09 cr =3D (cr + data->crdu[i2 + 8]) >> 1; +=09=09 cb =3D (cb + data->cbdu[i2 + 8]) >> 1; +=09=09 } + +=09=09 /* Red */ +=09=09 dd =3D data->ydu[r2 * 2 + c2][i1] + data->Cr_r_tab[cr]; +=09=09 if (dd < 0) +=09=09 dd =3D 0; +=09=09 if (dd > 255) +=09=09 dd =3D 255; +=09=09 ptr[i0] =3D dd; + +=09=09 /* Green */ +=09=09 dd =3D +=09=09 data->ydu[r2 * 2 + c2][i1] + +=09=09 ((data->Cb_g_tab[cb] + data->Cr_g_tab[cr]) >> SCALEBITS); +=09=09 if (dd < 0) +=09=09 dd =3D 0; +=09=09 if (dd > 255) +=09=09 dd =3D 255; +=09=09 ptr[i0 + 1] =3D dd; + +=09=09 /* Blue */ +=09=09 dd =3D data->ydu[r2 * 2 + c2][i1] + data->Cb_b_tab[cb]; +=09=09 if (dd < 0) +=09=09 dd =3D 0; +=09=09 if (dd > 255) +=09=09 dd =3D 255; +=09=09 ptr[i0 + 2] =3D dd; +=09=09} + } +} + + +static grub_uint8_t +get_marker (struct grub_jpeg_data *data) +{ + grub_uint8_t r; + + r =3D get_byte (data); + + if (r !=3D 0xFF)=09=09/* escape character */ + { + grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: invalid maker"); + grub_longjmp (data->jumper, 1); + } + + return get_byte (data); +} + +static void +decode_jpeg (struct grub_jpeg_data *data) +{ + build_color_table (data); + + if (get_marker (data) !=3D M_SOI)=09/* Start Of Image */ + { + grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: invalid jpeg file"); + grub_longjmp (data->jumper, 1); + } + + while (1) + { + grub_uint8_t marker; + + marker =3D get_marker (data); +#ifdef JPEG_DEBUG + grub_printf ("jpeg marker: %x\n", marker); +#endif + switch (marker) +=09{ +=09case M_DHT:=09=09/* Define Huffman Table */ +=09 decode_huff_table (data); +=09 break; +=09case M_DQT:=09=09/* Define Quantization Table */ +=09 decode_quan_table (data); +=09 break; +=09case M_SOF0:=09=09/* Start Of Frame 0 */ +=09 decode_sof (data); +=09 break; +=09case M_SOS:=09=09/* Start Of Scan */ +=09 decode_sos (data); +=09 break; +=09case M_EOI:=09=09/* End Of Image */ +=09 return; +=09case M_APP0:=09=09/* JFIF APP0 segment */ +=09case M_COM:=09=09/* Comment */ +=09 { +=09 grub_uint16_t sz; + +=09 sz =3D get_word (data); +=09 grub_file_seek (data->file, data->file->offset + sz - 2); +=09 break; +=09 } +=09default: +=09 grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: unrecognized marker %x", +=09=09 marker); +=09 grub_longjmp (data->jumper, 1); +=09} + } +} + +static grub_err_t +grub_video_reader_jpeg (struct grub_video_bitmap **bitmap, +=09=09=09const char *filename) +{ + grub_file_t file; + struct grub_jpeg_data *data; + + file =3D grub_file_open (filename); + if (!file) + return grub_errno; + + data =3D grub_malloc (sizeof (*data)); + if (data !=3D NULL) + { + int i; + + grub_memset (data, 0, sizeof (*data)); + data->file =3D file; + data->bitmap =3D bitmap; + if (!grub_setjmp (data->jumper)) +=09decode_jpeg (data); + + for (i =3D 0; i < 4; i++) +=09if (data->huff_value[i]) +=09 grub_free (data->huff_value[i]); + grub_free (data); + } + + if (grub_errno !=3D GRUB_ERR_NONE) + { + grub_video_bitmap_destroy (*bitmap); + *bitmap =3D 0; + } + + grub_file_close (file); + return grub_errno; +} + +#if defined(JPEG_DEBUG) +static grub_err_t +grub_cmd_jpegtest (struct grub_arg_list *state __attribute__ ((unused)), +=09=09 int argc, char **args) +{ + struct grub_video_bitmap *bitmap =3D 0; + + if (argc !=3D 1) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name required"); + + grub_video_reader_jpeg (&bitmap, args[0]); + if (grub_errno !=3D GRUB_ERR_NONE) + return grub_errno; + + grub_video_bitmap_destroy (bitmap); + + return GRUB_ERR_NONE; +} +#endif + +static struct grub_video_bitmap_reader jpg_reader =3D { + .extension =3D ".jpg", + .reader =3D grub_video_reader_jpeg, + .next =3D 0 +}; + +static struct grub_video_bitmap_reader jpeg_reader =3D { + .extension =3D ".jpeg", + .reader =3D grub_video_reader_jpeg, + .next =3D 0 +}; + +GRUB_MOD_INIT (video_reader_jpeg) +{ + grub_video_bitmap_reader_register (&jpg_reader); + grub_video_bitmap_reader_register (&jpeg_reader); +#if defined(JPEG_DEBUG) + grub_register_command ("jpegtest", grub_cmd_jpegtest, +=09=09=09 GRUB_COMMAND_FLAG_BOTH, "jpegtest FILE", +=09=09=09 "Tests loading of JPEG bitmap.", 0); +#endif +} + +GRUB_MOD_FINI (video_reader_jpeg) +{ +#if defined(JPEG_DEBUG) + grub_unregister_command ("jpegtest"); +#endif + grub_video_bitmap_reader_unregister (&jpeg_reader); + grub_video_bitmap_reader_unregister (&jpg_reader); +} --=20 Bean