IMG_jpg.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 01 Jan 2017 18:50:51 -0800
changeset 496 6332f9425dcc
parent 490 fd7214657669
child 502 7f374d902c16
permissions -rw-r--r--
Updated copyright for 2017
slouken@0
     1
/*
slouken@280
     2
  SDL_image:  An example image loading library for use with SDL
slouken@496
     3
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@280
     5
  This software is provided 'as-is', without any express or implied
slouken@280
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@280
     7
  arising from the use of this software.
slouken@0
     8
slouken@280
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@280
    10
  including commercial applications, and to alter it and redistribute it
slouken@280
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@280
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@280
    14
     claim that you wrote the original software. If you use this software
slouken@280
    15
     in a product, an acknowledgment in the product documentation would be
slouken@280
    16
     appreciated but is not required.
slouken@280
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@280
    18
     misrepresented as being the original software.
slouken@280
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@0
    21
slouken@237
    22
#if !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
slouken@237
    23
slouken@0
    24
/* This is a JPEG image file loading framework */
slouken@0
    25
slouken@0
    26
#include <stdio.h>
slouken@7
    27
#include <string.h>
slouken@61
    28
#include <setjmp.h>
slouken@0
    29
slouken@0
    30
#include "SDL_image.h"
slouken@0
    31
slouken@0
    32
#ifdef LOAD_JPG
slouken@0
    33
slouken@0
    34
#include <jpeglib.h>
slouken@0
    35
slouken@282
    36
#ifdef JPEG_TRUE  /* MinGW version of jpeg-8.x renamed TRUE to JPEG_TRUE etc. */
slouken@368
    37
    typedef JPEG_boolean boolean;
slouken@368
    38
    #define TRUE JPEG_TRUE
slouken@368
    39
    #define FALSE JPEG_FALSE
slouken@273
    40
#endif
slouken@273
    41
slouken@0
    42
/* Define this for fast loading and not as good image quality */
slouken@0
    43
/*#define FAST_JPEG*/
slouken@0
    44
slouken@123
    45
/* Define this for quicker (but less perfect) JPEG identification */
slouken@123
    46
#define FAST_IS_JPEG
slouken@123
    47
slouken@143
    48
static struct {
slouken@368
    49
    int loaded;
slouken@368
    50
    void *handle;
slouken@368
    51
    void (*jpeg_calc_output_dimensions) (j_decompress_ptr cinfo);
slouken@368
    52
    void (*jpeg_CreateDecompress) (j_decompress_ptr cinfo, int version, size_t structsize);
slouken@368
    53
    void (*jpeg_destroy_decompress) (j_decompress_ptr cinfo);
slouken@368
    54
    boolean (*jpeg_finish_decompress) (j_decompress_ptr cinfo);
slouken@368
    55
    int (*jpeg_read_header) (j_decompress_ptr cinfo, boolean require_image);
slouken@368
    56
    JDIMENSION (*jpeg_read_scanlines) (j_decompress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION max_lines);
slouken@368
    57
    boolean (*jpeg_resync_to_restart) (j_decompress_ptr cinfo, int desired);
slouken@368
    58
    boolean (*jpeg_start_decompress) (j_decompress_ptr cinfo);
slouken@368
    59
    struct jpeg_error_mgr * (*jpeg_std_error) (struct jpeg_error_mgr * err);
slouken@143
    60
} lib;
slouken@143
    61
slouken@143
    62
#ifdef LOAD_JPG_DYNAMIC
slouken@205
    63
int IMG_InitJPG()
slouken@143
    64
{
slouken@368
    65
    if ( lib.loaded == 0 ) {
slouken@368
    66
        lib.handle = SDL_LoadObject(LOAD_JPG_DYNAMIC);
slouken@368
    67
        if ( lib.handle == NULL ) {
slouken@368
    68
            return -1;
slouken@368
    69
        }
slouken@368
    70
        lib.jpeg_calc_output_dimensions =
slouken@368
    71
            (void (*) (j_decompress_ptr))
slouken@368
    72
            SDL_LoadFunction(lib.handle, "jpeg_calc_output_dimensions");
slouken@368
    73
        if ( lib.jpeg_calc_output_dimensions == NULL ) {
slouken@368
    74
            SDL_UnloadObject(lib.handle);
slouken@368
    75
            return -1;
slouken@368
    76
        }
slouken@368
    77
        lib.jpeg_CreateDecompress =
slouken@368
    78
            (void (*) (j_decompress_ptr, int, size_t))
slouken@368
    79
            SDL_LoadFunction(lib.handle, "jpeg_CreateDecompress");
slouken@368
    80
        if ( lib.jpeg_CreateDecompress == NULL ) {
slouken@368
    81
            SDL_UnloadObject(lib.handle);
slouken@368
    82
            return -1;
slouken@368
    83
        }
slouken@368
    84
        lib.jpeg_destroy_decompress =
slouken@368
    85
            (void (*) (j_decompress_ptr))
slouken@368
    86
            SDL_LoadFunction(lib.handle, "jpeg_destroy_decompress");
slouken@368
    87
        if ( lib.jpeg_destroy_decompress == NULL ) {
slouken@368
    88
            SDL_UnloadObject(lib.handle);
slouken@368
    89
            return -1;
slouken@368
    90
        }
slouken@368
    91
        lib.jpeg_finish_decompress =
slouken@368
    92
            (boolean (*) (j_decompress_ptr))
slouken@368
    93
            SDL_LoadFunction(lib.handle, "jpeg_finish_decompress");
slouken@368
    94
        if ( lib.jpeg_finish_decompress == NULL ) {
slouken@368
    95
            SDL_UnloadObject(lib.handle);
slouken@368
    96
            return -1;
slouken@368
    97
        }
slouken@368
    98
        lib.jpeg_read_header =
slouken@368
    99
            (int (*) (j_decompress_ptr, boolean))
slouken@368
   100
            SDL_LoadFunction(lib.handle, "jpeg_read_header");
slouken@368
   101
        if ( lib.jpeg_read_header == NULL ) {
slouken@368
   102
            SDL_UnloadObject(lib.handle);
slouken@368
   103
            return -1;
slouken@368
   104
        }
slouken@368
   105
        lib.jpeg_read_scanlines =
slouken@368
   106
            (JDIMENSION (*) (j_decompress_ptr, JSAMPARRAY, JDIMENSION))
slouken@368
   107
            SDL_LoadFunction(lib.handle, "jpeg_read_scanlines");
slouken@368
   108
        if ( lib.jpeg_read_scanlines == NULL ) {
slouken@368
   109
            SDL_UnloadObject(lib.handle);
slouken@368
   110
            return -1;
slouken@368
   111
        }
slouken@368
   112
        lib.jpeg_resync_to_restart =
slouken@368
   113
            (boolean (*) (j_decompress_ptr, int))
slouken@368
   114
            SDL_LoadFunction(lib.handle, "jpeg_resync_to_restart");
slouken@368
   115
        if ( lib.jpeg_resync_to_restart == NULL ) {
slouken@368
   116
            SDL_UnloadObject(lib.handle);
slouken@368
   117
            return -1;
slouken@368
   118
        }
slouken@368
   119
        lib.jpeg_start_decompress =
slouken@368
   120
            (boolean (*) (j_decompress_ptr))
slouken@368
   121
            SDL_LoadFunction(lib.handle, "jpeg_start_decompress");
slouken@368
   122
        if ( lib.jpeg_start_decompress == NULL ) {
slouken@368
   123
            SDL_UnloadObject(lib.handle);
slouken@368
   124
            return -1;
slouken@368
   125
        }
slouken@368
   126
        lib.jpeg_std_error =
slouken@368
   127
            (struct jpeg_error_mgr * (*) (struct jpeg_error_mgr *))
slouken@368
   128
            SDL_LoadFunction(lib.handle, "jpeg_std_error");
slouken@368
   129
        if ( lib.jpeg_std_error == NULL ) {
slouken@368
   130
            SDL_UnloadObject(lib.handle);
slouken@368
   131
            return -1;
slouken@368
   132
        }
slouken@368
   133
    }
slouken@368
   134
    ++lib.loaded;
slouken@143
   135
slouken@368
   136
    return 0;
slouken@143
   137
}
slouken@205
   138
void IMG_QuitJPG()
slouken@143
   139
{
slouken@368
   140
    if ( lib.loaded == 0 ) {
slouken@368
   141
        return;
slouken@368
   142
    }
slouken@368
   143
    if ( lib.loaded == 1 ) {
slouken@368
   144
        SDL_UnloadObject(lib.handle);
slouken@368
   145
    }
slouken@368
   146
    --lib.loaded;
slouken@143
   147
}
slouken@143
   148
#else
slouken@205
   149
int IMG_InitJPG()
slouken@143
   150
{
slouken@368
   151
    if ( lib.loaded == 0 ) {
slouken@368
   152
        lib.jpeg_calc_output_dimensions = jpeg_calc_output_dimensions;
slouken@368
   153
        lib.jpeg_CreateDecompress = jpeg_CreateDecompress;
slouken@368
   154
        lib.jpeg_destroy_decompress = jpeg_destroy_decompress;
slouken@368
   155
        lib.jpeg_finish_decompress = jpeg_finish_decompress;
slouken@368
   156
        lib.jpeg_read_header = jpeg_read_header;
slouken@368
   157
        lib.jpeg_read_scanlines = jpeg_read_scanlines;
slouken@368
   158
        lib.jpeg_resync_to_restart = jpeg_resync_to_restart;
slouken@368
   159
        lib.jpeg_start_decompress = jpeg_start_decompress;
slouken@368
   160
        lib.jpeg_std_error = jpeg_std_error;
slouken@368
   161
    }
slouken@368
   162
    ++lib.loaded;
slouken@146
   163
slouken@368
   164
    return 0;
slouken@143
   165
}
slouken@205
   166
void IMG_QuitJPG()
slouken@143
   167
{
slouken@368
   168
    if ( lib.loaded == 0 ) {
slouken@368
   169
        return;
slouken@368
   170
    }
slouken@368
   171
    if ( lib.loaded == 1 ) {
slouken@368
   172
    }
slouken@368
   173
    --lib.loaded;
slouken@143
   174
}
slouken@143
   175
#endif /* LOAD_JPG_DYNAMIC */
slouken@143
   176
slouken@0
   177
/* See if an image is contained in a data source */
slouken@0
   178
int IMG_isJPG(SDL_RWops *src)
slouken@0
   179
{
slouken@368
   180
    Sint64 start;
slouken@368
   181
    int is_JPG;
slouken@368
   182
    int in_scan;
slouken@368
   183
    Uint8 magic[4];
slouken@0
   184
slouken@368
   185
    /* This detection code is by Steaphan Greene <stea@cs.binghamton.edu> */
slouken@368
   186
    /* Blame me, not Sam, if this doesn't work right. */
slouken@368
   187
    /* And don't forget to report the problem to the the sdl list too! */
slouken@123
   188
slouken@368
   189
    if ( !src )
slouken@368
   190
        return 0;
slouken@368
   191
    start = SDL_RWtell(src);
slouken@368
   192
    is_JPG = 0;
slouken@368
   193
    in_scan = 0;
slouken@368
   194
    if ( SDL_RWread(src, magic, 2, 1) ) {
slouken@368
   195
        if ( (magic[0] == 0xFF) && (magic[1] == 0xD8) ) {
slouken@368
   196
            is_JPG = 1;
slouken@368
   197
            while (is_JPG == 1) {
slouken@368
   198
                if(SDL_RWread(src, magic, 1, 2) != 2) {
slouken@368
   199
                    is_JPG = 0;
slouken@368
   200
                } else if( (magic[0] != 0xFF) && (in_scan == 0) ) {
slouken@368
   201
                    is_JPG = 0;
slouken@368
   202
                } else if( (magic[0] != 0xFF) || (magic[1] == 0xFF) ) {
slouken@368
   203
                    /* Extra padding in JPEG (legal) */
slouken@368
   204
                    /* or this is data and we are scanning */
slouken@368
   205
                    SDL_RWseek(src, -1, RW_SEEK_CUR);
slouken@368
   206
                } else if(magic[1] == 0xD9) {
slouken@368
   207
                    /* Got to end of good JPEG */
slouken@368
   208
                    break;
slouken@368
   209
                } else if( (in_scan == 1) && (magic[1] == 0x00) ) {
slouken@368
   210
                    /* This is an encoded 0xFF within the data */
slouken@368
   211
                } else if( (magic[1] >= 0xD0) && (magic[1] < 0xD9) ) {
slouken@368
   212
                    /* These have nothing else */
slouken@368
   213
                } else if(SDL_RWread(src, magic+2, 1, 2) != 2) {
slouken@368
   214
                    is_JPG = 0;
slouken@368
   215
                } else {
slouken@368
   216
                    /* Yes, it's big-endian */
slouken@368
   217
                    Sint64 innerStart;
slouken@368
   218
                    Uint32 size;
slouken@368
   219
                    Sint64 end;
slouken@368
   220
                    innerStart = SDL_RWtell(src);
slouken@368
   221
                    size = (magic[2] << 8) + magic[3];
slouken@368
   222
                    end = SDL_RWseek(src, size-2, RW_SEEK_CUR);
slouken@368
   223
                    if ( end != innerStart + size - 2 ) is_JPG = 0;
slouken@368
   224
                    if ( magic[1] == 0xDA ) {
slouken@368
   225
                        /* Now comes the actual JPEG meat */
slouken@368
   226
#ifdef  FAST_IS_JPEG
slouken@368
   227
                        /* Ok, I'm convinced.  It is a JPEG. */
slouken@368
   228
                        break;
slouken@123
   229
#else
slouken@368
   230
                        /* I'm not convinced.  Prove it! */
slouken@368
   231
                        in_scan = 1;
slouken@123
   232
#endif
slouken@368
   233
                    }
slouken@368
   234
                }
slouken@368
   235
            }
slouken@368
   236
        }
slouken@368
   237
    }
slouken@368
   238
    SDL_RWseek(src, start, RW_SEEK_SET);
slouken@368
   239
    return(is_JPG);
slouken@0
   240
}
slouken@0
   241
slouken@368
   242
#define INPUT_BUFFER_SIZE   4096
slouken@0
   243
typedef struct {
slouken@368
   244
    struct jpeg_source_mgr pub;
slouken@0
   245
slouken@368
   246
    SDL_RWops *ctx;
slouken@368
   247
    Uint8 buffer[INPUT_BUFFER_SIZE];
slouken@0
   248
} my_source_mgr;
slouken@0
   249
slouken@0
   250
/*
slouken@0
   251
 * Initialize source --- called by jpeg_read_header
slouken@0
   252
 * before any data is actually read.
slouken@0
   253
 */
slouken@27
   254
static void init_source (j_decompress_ptr cinfo)
slouken@0
   255
{
slouken@368
   256
    /* We don't actually need to do anything */
slouken@368
   257
    return;
slouken@0
   258
}
slouken@0
   259
slouken@0
   260
/*
slouken@0
   261
 * Fill the input buffer --- called whenever buffer is emptied.
slouken@0
   262
 */
slouken@288
   263
static boolean fill_input_buffer (j_decompress_ptr cinfo)
slouken@0
   264
{
slouken@368
   265
    my_source_mgr * src = (my_source_mgr *) cinfo->src;
slouken@368
   266
    int nbytes;
slouken@0
   267
slouken@368
   268
    nbytes = SDL_RWread(src->ctx, src->buffer, 1, INPUT_BUFFER_SIZE);
slouken@368
   269
    if (nbytes <= 0) {
slouken@368
   270
        /* Insert a fake EOI marker */
slouken@368
   271
        src->buffer[0] = (Uint8) 0xFF;
slouken@368
   272
        src->buffer[1] = (Uint8) JPEG_EOI;
slouken@368
   273
        nbytes = 2;
slouken@368
   274
    }
slouken@368
   275
    src->pub.next_input_byte = src->buffer;
slouken@368
   276
    src->pub.bytes_in_buffer = nbytes;
slouken@0
   277
slouken@368
   278
    return TRUE;
slouken@0
   279
}
slouken@0
   280
slouken@0
   281
slouken@0
   282
/*
slouken@0
   283
 * Skip data --- used to skip over a potentially large amount of
slouken@0
   284
 * uninteresting data (such as an APPn marker).
slouken@0
   285
 *
slouken@0
   286
 * Writers of suspendable-input applications must note that skip_input_data
slouken@0
   287
 * is not granted the right to give a suspension return.  If the skip extends
slouken@0
   288
 * beyond the data currently in the buffer, the buffer can be marked empty so
slouken@0
   289
 * that the next read will cause a fill_input_buffer call that can suspend.
slouken@0
   290
 * Arranging for additional bytes to be discarded before reloading the input
slouken@0
   291
 * buffer is the application writer's problem.
slouken@0
   292
 */
slouken@27
   293
static void skip_input_data (j_decompress_ptr cinfo, long num_bytes)
slouken@0
   294
{
slouken@368
   295
    my_source_mgr * src = (my_source_mgr *) cinfo->src;
slouken@0
   296
slouken@368
   297
    /* Just a dumb implementation for now.  Could use fseek() except
slouken@368
   298
     * it doesn't work on pipes.  Not clear that being smart is worth
slouken@368
   299
     * any trouble anyway --- large skips are infrequent.
slouken@368
   300
     */
slouken@368
   301
    if (num_bytes > 0) {
slouken@368
   302
        while (num_bytes > (long) src->pub.bytes_in_buffer) {
slouken@368
   303
            num_bytes -= (long) src->pub.bytes_in_buffer;
slouken@368
   304
            (void) src->pub.fill_input_buffer(cinfo);
slouken@368
   305
            /* note we assume that fill_input_buffer will never
slouken@368
   306
             * return FALSE, so suspension need not be handled.
slouken@368
   307
             */
slouken@368
   308
        }
slouken@368
   309
        src->pub.next_input_byte += (size_t) num_bytes;
slouken@368
   310
        src->pub.bytes_in_buffer -= (size_t) num_bytes;
slouken@368
   311
    }
slouken@0
   312
}
slouken@0
   313
slouken@0
   314
/*
slouken@0
   315
 * Terminate source --- called by jpeg_finish_decompress
slouken@0
   316
 * after all data has been read.
slouken@0
   317
 */
slouken@27
   318
static void term_source (j_decompress_ptr cinfo)
slouken@0
   319
{
slouken@368
   320
    /* We don't actually need to do anything */
slouken@368
   321
    return;
slouken@0
   322
}
slouken@0
   323
slouken@0
   324
/*
slouken@0
   325
 * Prepare for input from a stdio stream.
slouken@0
   326
 * The caller must have already opened the stream, and is responsible
slouken@0
   327
 * for closing it after finishing decompression.
slouken@0
   328
 */
slouken@27
   329
static void jpeg_SDL_RW_src (j_decompress_ptr cinfo, SDL_RWops *ctx)
slouken@0
   330
{
slouken@0
   331
  my_source_mgr *src;
slouken@0
   332
slouken@0
   333
  /* The source object and input buffer are made permanent so that a series
slouken@0
   334
   * of JPEG images can be read from the same file by calling jpeg_stdio_src
slouken@0
   335
   * only before the first one.  (If we discarded the buffer at the end of
slouken@0
   336
   * one image, we'd likely lose the start of the next one.)
slouken@0
   337
   * This makes it unsafe to use this manager and a different source
slouken@0
   338
   * manager serially with the same JPEG object.  Caveat programmer.
slouken@0
   339
   */
slouken@368
   340
  if (cinfo->src == NULL) { /* first time for this JPEG object? */
slouken@0
   341
    cinfo->src = (struct jpeg_source_mgr *)
slouken@0
   342
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
slouken@368
   343
                  sizeof(my_source_mgr));
slouken@0
   344
    src = (my_source_mgr *) cinfo->src;
slouken@0
   345
  }
slouken@0
   346
slouken@0
   347
  src = (my_source_mgr *) cinfo->src;
slouken@27
   348
  src->pub.init_source = init_source;
slouken@27
   349
  src->pub.fill_input_buffer = fill_input_buffer;
slouken@27
   350
  src->pub.skip_input_data = skip_input_data;
slouken@143
   351
  src->pub.resync_to_restart = lib.jpeg_resync_to_restart; /* use default method */
slouken@27
   352
  src->pub.term_source = term_source;
slouken@0
   353
  src->ctx = ctx;
slouken@0
   354
  src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
slouken@0
   355
  src->pub.next_input_byte = NULL; /* until buffer loaded */
slouken@0
   356
}
slouken@0
   357
slouken@61
   358
struct my_error_mgr {
slouken@368
   359
    struct jpeg_error_mgr errmgr;
slouken@368
   360
    jmp_buf escape;
slouken@61
   361
};
slouken@61
   362
slouken@61
   363
static void my_error_exit(j_common_ptr cinfo)
slouken@61
   364
{
slouken@368
   365
    struct my_error_mgr *err = (struct my_error_mgr *)cinfo->err;
slouken@368
   366
    longjmp(err->escape, 1);
slouken@61
   367
}
slouken@61
   368
slouken@61
   369
static void output_no_message(j_common_ptr cinfo)
slouken@61
   370
{
slouken@368
   371
    /* do nothing */
slouken@61
   372
}
slouken@61
   373
slouken@0
   374
/* Load a JPEG type image from an SDL datasource */
slouken@0
   375
SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
slouken@0
   376
{
slouken@368
   377
    Sint64 start;
slouken@368
   378
    struct jpeg_decompress_struct cinfo;
slouken@368
   379
    JSAMPROW rowptr[1];
slouken@368
   380
    SDL_Surface *volatile surface = NULL;
slouken@368
   381
    struct my_error_mgr jerr;
slouken@0
   382
slouken@368
   383
    if ( !src ) {
slouken@368
   384
        /* The error message has been set in SDL_RWFromFile */
slouken@368
   385
        return NULL;
slouken@368
   386
    }
slouken@368
   387
    start = SDL_RWtell(src);
slouken@98
   388
slouken@490
   389
    if ( (IMG_Init(IMG_INIT_JPG) & IMG_INIT_JPG) == 0 ) {
slouken@368
   390
        return NULL;
slouken@368
   391
    }
slouken@143
   392
slouken@368
   393
    /* Create a decompression structure and load the JPEG header */
slouken@368
   394
    cinfo.err = lib.jpeg_std_error(&jerr.errmgr);
slouken@368
   395
    jerr.errmgr.error_exit = my_error_exit;
slouken@368
   396
    jerr.errmgr.output_message = output_no_message;
slouken@368
   397
    if(setjmp(jerr.escape)) {
slouken@368
   398
        /* If we get here, libjpeg found an error */
slouken@368
   399
        lib.jpeg_destroy_decompress(&cinfo);
slouken@368
   400
        if ( surface != NULL ) {
slouken@368
   401
            SDL_FreeSurface(surface);
slouken@368
   402
        }
slouken@368
   403
        SDL_RWseek(src, start, RW_SEEK_SET);
slouken@368
   404
        IMG_SetError("JPEG loading error");
slouken@368
   405
        return NULL;
slouken@368
   406
    }
slouken@61
   407
slouken@368
   408
    lib.jpeg_create_decompress(&cinfo);
slouken@368
   409
    jpeg_SDL_RW_src(&cinfo, src);
slouken@368
   410
    lib.jpeg_read_header(&cinfo, TRUE);
slouken@0
   411
slouken@368
   412
    if(cinfo.num_components == 4) {
slouken@368
   413
        /* Set 32-bit Raw output */
slouken@368
   414
        cinfo.out_color_space = JCS_CMYK;
slouken@368
   415
        cinfo.quantize_colors = FALSE;
slouken@368
   416
        lib.jpeg_calc_output_dimensions(&cinfo);
slouken@123
   417
slouken@368
   418
        /* Allocate an output surface to hold the image */
slouken@368
   419
        surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
slouken@368
   420
                cinfo.output_width, cinfo.output_height, 32,
slouken@123
   421
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@368
   422
                           0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
slouken@123
   423
#else
slouken@368
   424
                           0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF);
slouken@123
   425
#endif
slouken@368
   426
    } else {
slouken@368
   427
        /* Set 24-bit RGB output */
slouken@368
   428
        cinfo.out_color_space = JCS_RGB;
slouken@368
   429
        cinfo.quantize_colors = FALSE;
slouken@0
   430
#ifdef FAST_JPEG
slouken@368
   431
        cinfo.scale_num   = 1;
slouken@368
   432
        cinfo.scale_denom = 1;
slouken@368
   433
        cinfo.dct_method = JDCT_FASTEST;
slouken@368
   434
        cinfo.do_fancy_upsampling = FALSE;
slouken@0
   435
#endif
slouken@368
   436
        lib.jpeg_calc_output_dimensions(&cinfo);
slouken@0
   437
slouken@368
   438
        /* Allocate an output surface to hold the image */
slouken@368
   439
        surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
slouken@368
   440
                cinfo.output_width, cinfo.output_height, 24,
slouken@0
   441
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@368
   442
                           0x0000FF, 0x00FF00, 0xFF0000,
slouken@0
   443
#else
slouken@368
   444
                           0xFF0000, 0x00FF00, 0x0000FF,
slouken@0
   445
#endif
slouken@368
   446
                           0);
slouken@368
   447
    }
slouken@123
   448
slouken@368
   449
    if ( surface == NULL ) {
slouken@368
   450
        lib.jpeg_destroy_decompress(&cinfo);
slouken@368
   451
        SDL_RWseek(src, start, RW_SEEK_SET);
slouken@368
   452
        IMG_SetError("Out of memory");
slouken@368
   453
        return NULL;
slouken@368
   454
    }
slouken@0
   455
slouken@368
   456
    /* Decompress the image */
slouken@368
   457
    lib.jpeg_start_decompress(&cinfo);
slouken@368
   458
    while ( cinfo.output_scanline < cinfo.output_height ) {
slouken@368
   459
        rowptr[0] = (JSAMPROW)(Uint8 *)surface->pixels +
slouken@368
   460
                            cinfo.output_scanline * surface->pitch;
slouken@368
   461
        lib.jpeg_read_scanlines(&cinfo, rowptr, (JDIMENSION) 1);
slouken@368
   462
    }
slouken@368
   463
    lib.jpeg_finish_decompress(&cinfo);
slouken@368
   464
    lib.jpeg_destroy_decompress(&cinfo);
slouken@143
   465
slouken@368
   466
    return(surface);
slouken@0
   467
}
slouken@0
   468
slouken@0
   469
#else
slouken@0
   470
slouken@205
   471
int IMG_InitJPG()
slouken@205
   472
{
slouken@368
   473
    IMG_SetError("JPEG images are not supported");
slouken@368
   474
    return(-1);
slouken@205
   475
}
slouken@205
   476
slouken@205
   477
void IMG_QuitJPG()
slouken@205
   478
{
slouken@205
   479
}
slouken@205
   480
slouken@0
   481
/* See if an image is contained in a data source */
slouken@0
   482
int IMG_isJPG(SDL_RWops *src)
slouken@0
   483
{
slouken@368
   484
    return(0);
slouken@0
   485
}
slouken@0
   486
slouken@0
   487
/* Load a JPEG type image from an SDL datasource */
slouken@0
   488
SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
slouken@0
   489
{
slouken@368
   490
    return(NULL);
slouken@0
   491
}
slouken@0
   492
slouken@0
   493
#endif /* LOAD_JPG */
slouken@237
   494
slouken@237
   495
#endif /* !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND) */