IMG_jpg.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 21 Aug 2005 07:13:02 +0000
changeset 108 a02f321125f1
parent 98 9f94c4674cc9
child 109 0d1bd5675598
permissions -rw-r--r--
Date: Fri, 05 Aug 2005 16:52:24 +0900
From: Toshio KATO
Subject: Jpeg file load error

I can not load Jpeg file format except JFIF or Exif.
My Jpeg file is as following;

FF D8 FF E2 00 0E 56 56 4C 00 30 30 0A 00 00 00 00 00 FF DB 00 84 00 0C
08 09 0A 09 07 0C 0A 0A 0A 0D 0D 0C 0E 12 1E 13 12 10 10 12 25 1A 1C 16
1E 2B 26 2E 2D 2B 26 2A 29 30 36 45 3A 30 33 41 34 29 2A 3C 52 3D 41 47
49 4D 4E 4D 2E 3A 55 5B 54 4B 5A 45 4C 4D 4A 01 0D 0D 0D 12 10 12 23 13
13 23 4A 31 2A 31 4A 4A 4A 4A 4A 4A 4A .......................

56 56 4c="VVL"

For the present I patched the IMGjpeg.c and compile.
load and disp ok!
     1 /*
     2     SDL_image:  An example image loading library for use with SDL
     3     Copyright (C) 1999-2004 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /* $Id$ */
    24 
    25 /* This is a JPEG image file loading framework */
    26 
    27 #include <stdio.h>
    28 #include <string.h>
    29 #include <setjmp.h>
    30 
    31 #include "SDL_image.h"
    32 
    33 #ifdef LOAD_JPG
    34 
    35 #include <jpeglib.h>
    36 
    37 /* Define this for fast loading and not as good image quality */
    38 /*#define FAST_JPEG*/
    39 
    40 /* See if an image is contained in a data source */
    41 int IMG_isJPG(SDL_RWops *src)
    42 {
    43 	int is_JPG;
    44 	Uint8 magic[4];
    45 
    46 	is_JPG = 0;
    47 	if ( SDL_RWread(src, magic, 2, 1) ) {
    48 		if ( (magic[0] == 0xFF) && (magic[1] == 0xD8) ) {
    49 			SDL_RWread(src, magic, 4, 1);
    50 			SDL_RWread(src, magic, 4, 1);
    51 			if ( memcmp((char *)magic, "JFIF", 4) == 0 ||
    52 			     memcmp((char *)magic, "Exif", 4) == 0 ||
    53 			     memcmp((char *)magic, "VVL", 3) == 0 ) {
    54 				is_JPG = 1;
    55 			}
    56 		}
    57 	}
    58 	return(is_JPG);
    59 }
    60 
    61 #define INPUT_BUFFER_SIZE	4096
    62 typedef struct {
    63 	struct jpeg_source_mgr pub;
    64 
    65 	SDL_RWops *ctx;
    66 	Uint8 buffer[INPUT_BUFFER_SIZE];
    67 } my_source_mgr;
    68 
    69 /*
    70  * Initialize source --- called by jpeg_read_header
    71  * before any data is actually read.
    72  */
    73 static void init_source (j_decompress_ptr cinfo)
    74 {
    75 	/* We don't actually need to do anything */
    76 	return;
    77 }
    78 
    79 /*
    80  * Fill the input buffer --- called whenever buffer is emptied.
    81  */
    82 static int fill_input_buffer (j_decompress_ptr cinfo)
    83 {
    84 	my_source_mgr * src = (my_source_mgr *) cinfo->src;
    85 	int nbytes;
    86 
    87 	nbytes = SDL_RWread(src->ctx, src->buffer, 1, INPUT_BUFFER_SIZE);
    88 	if (nbytes <= 0) {
    89 		/* Insert a fake EOI marker */
    90 		src->buffer[0] = (Uint8) 0xFF;
    91 		src->buffer[1] = (Uint8) JPEG_EOI;
    92 		nbytes = 2;
    93 	}
    94 	src->pub.next_input_byte = src->buffer;
    95 	src->pub.bytes_in_buffer = nbytes;
    96 
    97 	return TRUE;
    98 }
    99 
   100 
   101 /*
   102  * Skip data --- used to skip over a potentially large amount of
   103  * uninteresting data (such as an APPn marker).
   104  *
   105  * Writers of suspendable-input applications must note that skip_input_data
   106  * is not granted the right to give a suspension return.  If the skip extends
   107  * beyond the data currently in the buffer, the buffer can be marked empty so
   108  * that the next read will cause a fill_input_buffer call that can suspend.
   109  * Arranging for additional bytes to be discarded before reloading the input
   110  * buffer is the application writer's problem.
   111  */
   112 static void skip_input_data (j_decompress_ptr cinfo, long num_bytes)
   113 {
   114 	my_source_mgr * src = (my_source_mgr *) cinfo->src;
   115 
   116 	/* Just a dumb implementation for now.	Could use fseek() except
   117 	 * it doesn't work on pipes.  Not clear that being smart is worth
   118 	 * any trouble anyway --- large skips are infrequent.
   119 	 */
   120 	if (num_bytes > 0) {
   121 		while (num_bytes > (long) src->pub.bytes_in_buffer) {
   122 			num_bytes -= (long) src->pub.bytes_in_buffer;
   123 			(void) src->pub.fill_input_buffer(cinfo);
   124 			/* note we assume that fill_input_buffer will never
   125 			 * return FALSE, so suspension need not be handled.
   126 			 */
   127 		}
   128 		src->pub.next_input_byte += (size_t) num_bytes;
   129 		src->pub.bytes_in_buffer -= (size_t) num_bytes;
   130 	}
   131 }
   132 
   133 /*
   134  * Terminate source --- called by jpeg_finish_decompress
   135  * after all data has been read.
   136  */
   137 static void term_source (j_decompress_ptr cinfo)
   138 {
   139 	/* We don't actually need to do anything */
   140 	return;
   141 }
   142 
   143 /*
   144  * Prepare for input from a stdio stream.
   145  * The caller must have already opened the stream, and is responsible
   146  * for closing it after finishing decompression.
   147  */
   148 static void jpeg_SDL_RW_src (j_decompress_ptr cinfo, SDL_RWops *ctx)
   149 {
   150   my_source_mgr *src;
   151 
   152   /* The source object and input buffer are made permanent so that a series
   153    * of JPEG images can be read from the same file by calling jpeg_stdio_src
   154    * only before the first one.  (If we discarded the buffer at the end of
   155    * one image, we'd likely lose the start of the next one.)
   156    * This makes it unsafe to use this manager and a different source
   157    * manager serially with the same JPEG object.  Caveat programmer.
   158    */
   159   if (cinfo->src == NULL) {	/* first time for this JPEG object? */
   160     cinfo->src = (struct jpeg_source_mgr *)
   161       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
   162 				  sizeof(my_source_mgr));
   163     src = (my_source_mgr *) cinfo->src;
   164   }
   165 
   166   src = (my_source_mgr *) cinfo->src;
   167   src->pub.init_source = init_source;
   168   src->pub.fill_input_buffer = fill_input_buffer;
   169   src->pub.skip_input_data = skip_input_data;
   170   src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
   171   src->pub.term_source = term_source;
   172   src->ctx = ctx;
   173   src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
   174   src->pub.next_input_byte = NULL; /* until buffer loaded */
   175 }
   176 
   177 struct my_error_mgr {
   178 	struct jpeg_error_mgr errmgr;
   179 	jmp_buf escape;
   180 };
   181 
   182 static void my_error_exit(j_common_ptr cinfo)
   183 {
   184 	struct my_error_mgr *err = (struct my_error_mgr *)cinfo->err;
   185 	longjmp(err->escape, 1);
   186 }
   187 
   188 static void output_no_message(j_common_ptr cinfo)
   189 {
   190 	/* do nothing */
   191 }
   192 
   193 /* Load a JPEG type image from an SDL datasource */
   194 SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
   195 {
   196 	struct jpeg_decompress_struct cinfo;
   197 	JSAMPROW rowptr[1];
   198 	SDL_Surface *volatile surface = NULL;
   199 	struct my_error_mgr jerr;
   200 
   201 	if ( !src ) {
   202 		/* The error message has been set in SDL_RWFromFile */
   203 		return NULL;
   204 	}
   205 
   206 	/* Create a decompression structure and load the JPEG header */
   207 	cinfo.err = jpeg_std_error(&jerr.errmgr);
   208 	jerr.errmgr.error_exit = my_error_exit;
   209 	jerr.errmgr.output_message = output_no_message;
   210 	if(setjmp(jerr.escape)) {
   211 		/* If we get here, libjpeg found an error */
   212 		jpeg_destroy_decompress(&cinfo);
   213 		IMG_SetError("JPEG loading error");
   214 		SDL_FreeSurface(surface);
   215 		return NULL;
   216 	}
   217 
   218 	jpeg_create_decompress(&cinfo);
   219 	jpeg_SDL_RW_src(&cinfo, src);
   220 	jpeg_read_header(&cinfo, TRUE);
   221 
   222 	/* Set 24-bit RGB output */
   223 	cinfo.out_color_space = JCS_RGB;
   224 	cinfo.quantize_colors = FALSE;
   225 #ifdef FAST_JPEG
   226 	cinfo.scale_num   = 1;
   227 	cinfo.scale_denom = 1;
   228 	cinfo.dct_method = JDCT_FASTEST;
   229 	cinfo.do_fancy_upsampling = FALSE;
   230 #endif
   231 	jpeg_calc_output_dimensions(&cinfo);
   232 
   233 	/* Allocate an output surface to hold the image */
   234 	surface = SDL_AllocSurface(SDL_SWSURFACE,
   235 	               cinfo.output_width, cinfo.output_height, 24,
   236 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   237 	                           0x0000FF, 0x00FF00, 0xFF0000,
   238 #else
   239 	                           0xFF0000, 0x00FF00, 0x0000FF,
   240 #endif
   241 				   0);
   242 	if ( surface == NULL ) {
   243 		IMG_SetError("Out of memory");
   244 		goto done;
   245 	}
   246 
   247 	/* Decompress the image */
   248 	jpeg_start_decompress(&cinfo);
   249 	while ( cinfo.output_scanline < cinfo.output_height ) {
   250 		rowptr[0] = (JSAMPROW)(Uint8 *)surface->pixels +
   251 		                    cinfo.output_scanline * surface->pitch;
   252 		jpeg_read_scanlines(&cinfo, rowptr, (JDIMENSION) 1);
   253 	}
   254 	jpeg_finish_decompress(&cinfo);
   255 
   256 	/* Clean up and return */
   257 done:
   258 	jpeg_destroy_decompress(&cinfo);
   259 	return(surface);
   260 }
   261 
   262 #else
   263 
   264 /* See if an image is contained in a data source */
   265 int IMG_isJPG(SDL_RWops *src)
   266 {
   267 	return(0);
   268 }
   269 
   270 /* Load a JPEG type image from an SDL datasource */
   271 SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
   272 {
   273 	return(NULL);
   274 }
   275 
   276 #endif /* LOAD_JPG */