IMG_jpg.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 26 Oct 2018 14:58:01 -0700
changeset 617 d64228a395fc
parent 575 36e9e2255178
child 638 e3e9d7430674
permissions -rw-r--r--
Fixed webp library detection when cross-compiling for Windows
slouken@0
     1
/*
slouken@280
     2
  SDL_image:  An example image loading library for use with SDL
slouken@575
     3
  Copyright (C) 1997-2018 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@0
    22
/* This is a JPEG image file loading framework */
slouken@0
    23
slouken@0
    24
#include <stdio.h>
slouken@61
    25
#include <setjmp.h>
slouken@0
    26
slouken@0
    27
#include "SDL_image.h"
slouken@0
    28
slouken@511
    29
#if !(defined(__APPLE__) || defined(SDL_IMAGE_USE_WIC_BACKEND)) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
slouken@511
    30
slouken@0
    31
#ifdef LOAD_JPG
slouken@0
    32
slouken@504
    33
#define USE_JPEGLIB
slouken@504
    34
slouken@0
    35
#include <jpeglib.h>
slouken@0
    36
slouken@282
    37
#ifdef JPEG_TRUE  /* MinGW version of jpeg-8.x renamed TRUE to JPEG_TRUE etc. */
slouken@368
    38
    typedef JPEG_boolean boolean;
slouken@368
    39
    #define TRUE JPEG_TRUE
slouken@368
    40
    #define FALSE JPEG_FALSE
slouken@273
    41
#endif
slouken@273
    42
slouken@0
    43
/* Define this for fast loading and not as good image quality */
slouken@0
    44
/*#define FAST_JPEG*/
slouken@0
    45
slouken@123
    46
/* Define this for quicker (but less perfect) JPEG identification */
slouken@123
    47
#define FAST_IS_JPEG
slouken@123
    48
slouken@143
    49
static struct {
slouken@368
    50
    int loaded;
slouken@368
    51
    void *handle;
slouken@368
    52
    void (*jpeg_calc_output_dimensions) (j_decompress_ptr cinfo);
slouken@368
    53
    void (*jpeg_CreateDecompress) (j_decompress_ptr cinfo, int version, size_t structsize);
slouken@368
    54
    void (*jpeg_destroy_decompress) (j_decompress_ptr cinfo);
slouken@368
    55
    boolean (*jpeg_finish_decompress) (j_decompress_ptr cinfo);
slouken@368
    56
    int (*jpeg_read_header) (j_decompress_ptr cinfo, boolean require_image);
slouken@368
    57
    JDIMENSION (*jpeg_read_scanlines) (j_decompress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION max_lines);
slouken@368
    58
    boolean (*jpeg_resync_to_restart) (j_decompress_ptr cinfo, int desired);
slouken@368
    59
    boolean (*jpeg_start_decompress) (j_decompress_ptr cinfo);
slouken@502
    60
    void (*jpeg_CreateCompress) (j_compress_ptr cinfo, int version, size_t structsize);
slouken@502
    61
    void (*jpeg_start_compress) (j_compress_ptr cinfo, boolean write_all_tables);
slouken@502
    62
    void (*jpeg_set_quality) (j_compress_ptr cinfo, int quality, boolean force_baseline);
slouken@502
    63
    void (*jpeg_set_defaults) (j_compress_ptr cinfo);
slouken@502
    64
    JDIMENSION (*jpeg_write_scanlines) (j_compress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION num_lines);
slouken@502
    65
    void (*jpeg_finish_compress) (j_compress_ptr cinfo);
slouken@502
    66
    void (*jpeg_destroy_compress) (j_compress_ptr cinfo);
slouken@368
    67
    struct jpeg_error_mgr * (*jpeg_std_error) (struct jpeg_error_mgr * err);
slouken@143
    68
} lib;
slouken@143
    69
slouken@143
    70
#ifdef LOAD_JPG_DYNAMIC
slouken@537
    71
#define FUNCTION_LOADER(FUNC, SIG) \
slouken@537
    72
    lib.FUNC = (SIG) SDL_LoadFunction(lib.handle, #FUNC); \
slouken@537
    73
    if (lib.FUNC == NULL) { SDL_UnloadObject(lib.handle); return -1; }
slouken@537
    74
#else
slouken@537
    75
#define FUNCTION_LOADER(FUNC, SIG) \
slouken@537
    76
    lib.FUNC = FUNC;
slouken@537
    77
#endif
slouken@537
    78
slouken@205
    79
int IMG_InitJPG()
slouken@143
    80
{
slouken@368
    81
    if ( lib.loaded == 0 ) {
slouken@537
    82
#ifdef LOAD_JPG_DYNAMIC
slouken@368
    83
        lib.handle = SDL_LoadObject(LOAD_JPG_DYNAMIC);
slouken@368
    84
        if ( lib.handle == NULL ) {
slouken@368
    85
            return -1;
slouken@368
    86
        }
slouken@537
    87
#endif
slouken@537
    88
        FUNCTION_LOADER(jpeg_calc_output_dimensions, void (*) (j_decompress_ptr cinfo))
slouken@537
    89
        FUNCTION_LOADER(jpeg_CreateDecompress, void (*) (j_decompress_ptr cinfo, int version, size_t structsize))
slouken@537
    90
        FUNCTION_LOADER(jpeg_destroy_decompress, void (*) (j_decompress_ptr cinfo))
slouken@537
    91
        FUNCTION_LOADER(jpeg_finish_decompress, boolean (*) (j_decompress_ptr cinfo))
slouken@537
    92
        FUNCTION_LOADER(jpeg_read_header, int (*) (j_decompress_ptr cinfo, boolean require_image))
slouken@537
    93
        FUNCTION_LOADER(jpeg_read_scanlines, JDIMENSION (*) (j_decompress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION max_lines))
slouken@537
    94
        FUNCTION_LOADER(jpeg_resync_to_restart, boolean (*) (j_decompress_ptr cinfo, int desired))
slouken@537
    95
        FUNCTION_LOADER(jpeg_start_decompress, boolean (*) (j_decompress_ptr cinfo))
slouken@537
    96
        FUNCTION_LOADER(jpeg_CreateCompress, void (*) (j_compress_ptr cinfo, int version, size_t structsize))
slouken@537
    97
        FUNCTION_LOADER(jpeg_start_compress, void (*) (j_compress_ptr cinfo, boolean write_all_tables))
slouken@537
    98
        FUNCTION_LOADER(jpeg_set_quality, void (*) (j_compress_ptr cinfo, int quality, boolean force_baseline))
slouken@537
    99
        FUNCTION_LOADER(jpeg_set_defaults, void (*) (j_compress_ptr cinfo))
slouken@537
   100
        FUNCTION_LOADER(jpeg_write_scanlines, JDIMENSION (*) (j_compress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION num_lines))
slouken@537
   101
        FUNCTION_LOADER(jpeg_finish_compress, void (*) (j_compress_ptr cinfo))
slouken@537
   102
        FUNCTION_LOADER(jpeg_destroy_compress, void (*) (j_compress_ptr cinfo))
slouken@537
   103
        FUNCTION_LOADER(jpeg_std_error, struct jpeg_error_mgr * (*) (struct jpeg_error_mgr * err))
slouken@368
   104
    }
slouken@368
   105
    ++lib.loaded;
slouken@143
   106
slouken@368
   107
    return 0;
slouken@143
   108
}
slouken@205
   109
void IMG_QuitJPG()
slouken@143
   110
{
slouken@368
   111
    if ( lib.loaded == 0 ) {
slouken@368
   112
        return;
slouken@368
   113
    }
slouken@368
   114
    if ( lib.loaded == 1 ) {
slouken@537
   115
#ifdef LOAD_JPG_DYNAMIC
slouken@368
   116
        SDL_UnloadObject(lib.handle);
slouken@537
   117
#endif
slouken@368
   118
    }
slouken@368
   119
    --lib.loaded;
slouken@143
   120
}
slouken@143
   121
slouken@0
   122
/* See if an image is contained in a data source */
slouken@0
   123
int IMG_isJPG(SDL_RWops *src)
slouken@0
   124
{
slouken@368
   125
    Sint64 start;
slouken@368
   126
    int is_JPG;
slouken@368
   127
    int in_scan;
slouken@368
   128
    Uint8 magic[4];
slouken@0
   129
slouken@368
   130
    /* This detection code is by Steaphan Greene <stea@cs.binghamton.edu> */
slouken@368
   131
    /* Blame me, not Sam, if this doesn't work right. */
slouken@368
   132
    /* And don't forget to report the problem to the the sdl list too! */
slouken@123
   133
slouken@368
   134
    if ( !src )
slouken@368
   135
        return 0;
slouken@368
   136
    start = SDL_RWtell(src);
slouken@368
   137
    is_JPG = 0;
slouken@368
   138
    in_scan = 0;
slouken@368
   139
    if ( SDL_RWread(src, magic, 2, 1) ) {
slouken@368
   140
        if ( (magic[0] == 0xFF) && (magic[1] == 0xD8) ) {
slouken@368
   141
            is_JPG = 1;
slouken@368
   142
            while (is_JPG == 1) {
slouken@368
   143
                if(SDL_RWread(src, magic, 1, 2) != 2) {
slouken@368
   144
                    is_JPG = 0;
slouken@368
   145
                } else if( (magic[0] != 0xFF) && (in_scan == 0) ) {
slouken@368
   146
                    is_JPG = 0;
slouken@368
   147
                } else if( (magic[0] != 0xFF) || (magic[1] == 0xFF) ) {
slouken@368
   148
                    /* Extra padding in JPEG (legal) */
slouken@368
   149
                    /* or this is data and we are scanning */
slouken@368
   150
                    SDL_RWseek(src, -1, RW_SEEK_CUR);
slouken@368
   151
                } else if(magic[1] == 0xD9) {
slouken@368
   152
                    /* Got to end of good JPEG */
slouken@368
   153
                    break;
slouken@368
   154
                } else if( (in_scan == 1) && (magic[1] == 0x00) ) {
slouken@368
   155
                    /* This is an encoded 0xFF within the data */
slouken@368
   156
                } else if( (magic[1] >= 0xD0) && (magic[1] < 0xD9) ) {
slouken@368
   157
                    /* These have nothing else */
slouken@368
   158
                } else if(SDL_RWread(src, magic+2, 1, 2) != 2) {
slouken@368
   159
                    is_JPG = 0;
slouken@368
   160
                } else {
slouken@368
   161
                    /* Yes, it's big-endian */
slouken@368
   162
                    Sint64 innerStart;
slouken@368
   163
                    Uint32 size;
slouken@368
   164
                    Sint64 end;
slouken@368
   165
                    innerStart = SDL_RWtell(src);
slouken@368
   166
                    size = (magic[2] << 8) + magic[3];
slouken@368
   167
                    end = SDL_RWseek(src, size-2, RW_SEEK_CUR);
slouken@368
   168
                    if ( end != innerStart + size - 2 ) is_JPG = 0;
slouken@368
   169
                    if ( magic[1] == 0xDA ) {
slouken@368
   170
                        /* Now comes the actual JPEG meat */
slouken@368
   171
#ifdef  FAST_IS_JPEG
slouken@368
   172
                        /* Ok, I'm convinced.  It is a JPEG. */
slouken@368
   173
                        break;
slouken@123
   174
#else
slouken@368
   175
                        /* I'm not convinced.  Prove it! */
slouken@368
   176
                        in_scan = 1;
slouken@123
   177
#endif
slouken@368
   178
                    }
slouken@368
   179
                }
slouken@368
   180
            }
slouken@368
   181
        }
slouken@368
   182
    }
slouken@368
   183
    SDL_RWseek(src, start, RW_SEEK_SET);
slouken@368
   184
    return(is_JPG);
slouken@0
   185
}
slouken@0
   186
slouken@368
   187
#define INPUT_BUFFER_SIZE   4096
slouken@0
   188
typedef struct {
slouken@368
   189
    struct jpeg_source_mgr pub;
slouken@0
   190
slouken@368
   191
    SDL_RWops *ctx;
slouken@368
   192
    Uint8 buffer[INPUT_BUFFER_SIZE];
slouken@0
   193
} my_source_mgr;
slouken@0
   194
slouken@0
   195
/*
slouken@0
   196
 * Initialize source --- called by jpeg_read_header
slouken@0
   197
 * before any data is actually read.
slouken@0
   198
 */
slouken@27
   199
static void init_source (j_decompress_ptr cinfo)
slouken@0
   200
{
slouken@368
   201
    /* We don't actually need to do anything */
slouken@368
   202
    return;
slouken@0
   203
}
slouken@0
   204
slouken@0
   205
/*
slouken@0
   206
 * Fill the input buffer --- called whenever buffer is emptied.
slouken@0
   207
 */
slouken@288
   208
static boolean fill_input_buffer (j_decompress_ptr cinfo)
slouken@0
   209
{
slouken@368
   210
    my_source_mgr * src = (my_source_mgr *) cinfo->src;
slouken@368
   211
    int nbytes;
slouken@0
   212
slouken@537
   213
    nbytes = (int)SDL_RWread(src->ctx, src->buffer, 1, INPUT_BUFFER_SIZE);
slouken@368
   214
    if (nbytes <= 0) {
slouken@368
   215
        /* Insert a fake EOI marker */
slouken@368
   216
        src->buffer[0] = (Uint8) 0xFF;
slouken@368
   217
        src->buffer[1] = (Uint8) JPEG_EOI;
slouken@368
   218
        nbytes = 2;
slouken@368
   219
    }
slouken@368
   220
    src->pub.next_input_byte = src->buffer;
slouken@368
   221
    src->pub.bytes_in_buffer = nbytes;
slouken@0
   222
slouken@368
   223
    return TRUE;
slouken@0
   224
}
slouken@0
   225
slouken@0
   226
slouken@0
   227
/*
slouken@0
   228
 * Skip data --- used to skip over a potentially large amount of
slouken@0
   229
 * uninteresting data (such as an APPn marker).
slouken@0
   230
 *
slouken@0
   231
 * Writers of suspendable-input applications must note that skip_input_data
slouken@0
   232
 * is not granted the right to give a suspension return.  If the skip extends
slouken@0
   233
 * beyond the data currently in the buffer, the buffer can be marked empty so
slouken@0
   234
 * that the next read will cause a fill_input_buffer call that can suspend.
slouken@0
   235
 * Arranging for additional bytes to be discarded before reloading the input
slouken@0
   236
 * buffer is the application writer's problem.
slouken@0
   237
 */
slouken@27
   238
static void skip_input_data (j_decompress_ptr cinfo, long num_bytes)
slouken@0
   239
{
slouken@368
   240
    my_source_mgr * src = (my_source_mgr *) cinfo->src;
slouken@0
   241
slouken@368
   242
    /* Just a dumb implementation for now.  Could use fseek() except
slouken@368
   243
     * it doesn't work on pipes.  Not clear that being smart is worth
slouken@368
   244
     * any trouble anyway --- large skips are infrequent.
slouken@368
   245
     */
slouken@368
   246
    if (num_bytes > 0) {
slouken@368
   247
        while (num_bytes > (long) src->pub.bytes_in_buffer) {
slouken@368
   248
            num_bytes -= (long) src->pub.bytes_in_buffer;
slouken@368
   249
            (void) src->pub.fill_input_buffer(cinfo);
slouken@368
   250
            /* note we assume that fill_input_buffer will never
slouken@368
   251
             * return FALSE, so suspension need not be handled.
slouken@368
   252
             */
slouken@368
   253
        }
slouken@368
   254
        src->pub.next_input_byte += (size_t) num_bytes;
slouken@368
   255
        src->pub.bytes_in_buffer -= (size_t) num_bytes;
slouken@368
   256
    }
slouken@0
   257
}
slouken@0
   258
slouken@0
   259
/*
slouken@0
   260
 * Terminate source --- called by jpeg_finish_decompress
slouken@0
   261
 * after all data has been read.
slouken@0
   262
 */
slouken@27
   263
static void term_source (j_decompress_ptr cinfo)
slouken@0
   264
{
slouken@368
   265
    /* We don't actually need to do anything */
slouken@368
   266
    return;
slouken@0
   267
}
slouken@0
   268
slouken@0
   269
/*
slouken@0
   270
 * Prepare for input from a stdio stream.
slouken@0
   271
 * The caller must have already opened the stream, and is responsible
slouken@0
   272
 * for closing it after finishing decompression.
slouken@0
   273
 */
slouken@27
   274
static void jpeg_SDL_RW_src (j_decompress_ptr cinfo, SDL_RWops *ctx)
slouken@0
   275
{
slouken@0
   276
  my_source_mgr *src;
slouken@0
   277
slouken@0
   278
  /* The source object and input buffer are made permanent so that a series
slouken@0
   279
   * of JPEG images can be read from the same file by calling jpeg_stdio_src
slouken@0
   280
   * only before the first one.  (If we discarded the buffer at the end of
slouken@0
   281
   * one image, we'd likely lose the start of the next one.)
slouken@0
   282
   * This makes it unsafe to use this manager and a different source
slouken@0
   283
   * manager serially with the same JPEG object.  Caveat programmer.
slouken@0
   284
   */
slouken@368
   285
  if (cinfo->src == NULL) { /* first time for this JPEG object? */
slouken@0
   286
    cinfo->src = (struct jpeg_source_mgr *)
slouken@0
   287
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
slouken@368
   288
                  sizeof(my_source_mgr));
slouken@0
   289
    src = (my_source_mgr *) cinfo->src;
slouken@0
   290
  }
slouken@0
   291
slouken@0
   292
  src = (my_source_mgr *) cinfo->src;
slouken@27
   293
  src->pub.init_source = init_source;
slouken@27
   294
  src->pub.fill_input_buffer = fill_input_buffer;
slouken@27
   295
  src->pub.skip_input_data = skip_input_data;
slouken@143
   296
  src->pub.resync_to_restart = lib.jpeg_resync_to_restart; /* use default method */
slouken@27
   297
  src->pub.term_source = term_source;
slouken@0
   298
  src->ctx = ctx;
slouken@0
   299
  src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
slouken@0
   300
  src->pub.next_input_byte = NULL; /* until buffer loaded */
slouken@0
   301
}
slouken@0
   302
slouken@61
   303
struct my_error_mgr {
slouken@368
   304
    struct jpeg_error_mgr errmgr;
slouken@368
   305
    jmp_buf escape;
slouken@61
   306
};
slouken@61
   307
slouken@61
   308
static void my_error_exit(j_common_ptr cinfo)
slouken@61
   309
{
slouken@368
   310
    struct my_error_mgr *err = (struct my_error_mgr *)cinfo->err;
slouken@368
   311
    longjmp(err->escape, 1);
slouken@61
   312
}
slouken@61
   313
slouken@61
   314
static void output_no_message(j_common_ptr cinfo)
slouken@61
   315
{
slouken@368
   316
    /* do nothing */
slouken@61
   317
}
slouken@61
   318
slouken@0
   319
/* Load a JPEG type image from an SDL datasource */
slouken@0
   320
SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
slouken@0
   321
{
slouken@368
   322
    Sint64 start;
slouken@368
   323
    struct jpeg_decompress_struct cinfo;
slouken@368
   324
    JSAMPROW rowptr[1];
slouken@368
   325
    SDL_Surface *volatile surface = NULL;
slouken@368
   326
    struct my_error_mgr jerr;
slouken@0
   327
slouken@368
   328
    if ( !src ) {
slouken@368
   329
        /* The error message has been set in SDL_RWFromFile */
slouken@368
   330
        return NULL;
slouken@368
   331
    }
slouken@368
   332
    start = SDL_RWtell(src);
slouken@98
   333
slouken@490
   334
    if ( (IMG_Init(IMG_INIT_JPG) & IMG_INIT_JPG) == 0 ) {
slouken@368
   335
        return NULL;
slouken@368
   336
    }
slouken@143
   337
slouken@368
   338
    /* Create a decompression structure and load the JPEG header */
slouken@368
   339
    cinfo.err = lib.jpeg_std_error(&jerr.errmgr);
slouken@368
   340
    jerr.errmgr.error_exit = my_error_exit;
slouken@368
   341
    jerr.errmgr.output_message = output_no_message;
slouken@368
   342
    if(setjmp(jerr.escape)) {
slouken@368
   343
        /* If we get here, libjpeg found an error */
slouken@368
   344
        lib.jpeg_destroy_decompress(&cinfo);
slouken@368
   345
        if ( surface != NULL ) {
slouken@368
   346
            SDL_FreeSurface(surface);
slouken@368
   347
        }
slouken@368
   348
        SDL_RWseek(src, start, RW_SEEK_SET);
slouken@368
   349
        IMG_SetError("JPEG loading error");
slouken@368
   350
        return NULL;
slouken@368
   351
    }
slouken@61
   352
slouken@368
   353
    lib.jpeg_create_decompress(&cinfo);
slouken@368
   354
    jpeg_SDL_RW_src(&cinfo, src);
slouken@368
   355
    lib.jpeg_read_header(&cinfo, TRUE);
slouken@0
   356
slouken@368
   357
    if(cinfo.num_components == 4) {
slouken@368
   358
        /* Set 32-bit Raw output */
slouken@368
   359
        cinfo.out_color_space = JCS_CMYK;
slouken@368
   360
        cinfo.quantize_colors = FALSE;
slouken@368
   361
        lib.jpeg_calc_output_dimensions(&cinfo);
slouken@123
   362
slouken@368
   363
        /* Allocate an output surface to hold the image */
slouken@368
   364
        surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
slouken@368
   365
                cinfo.output_width, cinfo.output_height, 32,
slouken@123
   366
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@368
   367
                           0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
slouken@123
   368
#else
slouken@368
   369
                           0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF);
slouken@123
   370
#endif
slouken@368
   371
    } else {
slouken@368
   372
        /* Set 24-bit RGB output */
slouken@368
   373
        cinfo.out_color_space = JCS_RGB;
slouken@368
   374
        cinfo.quantize_colors = FALSE;
slouken@0
   375
#ifdef FAST_JPEG
slouken@368
   376
        cinfo.scale_num   = 1;
slouken@368
   377
        cinfo.scale_denom = 1;
slouken@368
   378
        cinfo.dct_method = JDCT_FASTEST;
slouken@368
   379
        cinfo.do_fancy_upsampling = FALSE;
slouken@0
   380
#endif
slouken@368
   381
        lib.jpeg_calc_output_dimensions(&cinfo);
slouken@0
   382
slouken@368
   383
        /* Allocate an output surface to hold the image */
slouken@368
   384
        surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
slouken@368
   385
                cinfo.output_width, cinfo.output_height, 24,
slouken@0
   386
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@368
   387
                           0x0000FF, 0x00FF00, 0xFF0000,
slouken@0
   388
#else
slouken@368
   389
                           0xFF0000, 0x00FF00, 0x0000FF,
slouken@0
   390
#endif
slouken@368
   391
                           0);
slouken@368
   392
    }
slouken@123
   393
slouken@368
   394
    if ( surface == NULL ) {
slouken@368
   395
        lib.jpeg_destroy_decompress(&cinfo);
slouken@368
   396
        SDL_RWseek(src, start, RW_SEEK_SET);
slouken@368
   397
        IMG_SetError("Out of memory");
slouken@368
   398
        return NULL;
slouken@368
   399
    }
slouken@0
   400
slouken@368
   401
    /* Decompress the image */
slouken@368
   402
    lib.jpeg_start_decompress(&cinfo);
slouken@368
   403
    while ( cinfo.output_scanline < cinfo.output_height ) {
slouken@368
   404
        rowptr[0] = (JSAMPROW)(Uint8 *)surface->pixels +
slouken@368
   405
                            cinfo.output_scanline * surface->pitch;
slouken@368
   406
        lib.jpeg_read_scanlines(&cinfo, rowptr, (JDIMENSION) 1);
slouken@368
   407
    }
slouken@368
   408
    lib.jpeg_finish_decompress(&cinfo);
slouken@368
   409
    lib.jpeg_destroy_decompress(&cinfo);
slouken@143
   410
slouken@368
   411
    return(surface);
slouken@0
   412
}
slouken@0
   413
slouken@502
   414
#define OUTPUT_BUFFER_SIZE   4096
slouken@502
   415
typedef struct {
slouken@502
   416
    struct jpeg_destination_mgr pub;
slouken@502
   417
slouken@502
   418
    SDL_RWops *ctx;
slouken@502
   419
    Uint8 buffer[OUTPUT_BUFFER_SIZE];
slouken@502
   420
} my_destination_mgr;
slouken@502
   421
slouken@502
   422
static void init_destination(j_compress_ptr cinfo)
slouken@502
   423
{
slouken@502
   424
    /* We don't actually need to do anything */
slouken@502
   425
    return;
slouken@502
   426
}
slouken@502
   427
slouken@502
   428
static boolean empty_output_buffer(j_compress_ptr cinfo)
slouken@502
   429
{
slouken@502
   430
    my_destination_mgr * dest = (my_destination_mgr *)cinfo->dest;
slouken@502
   431
slouken@502
   432
    /* In typical applications, it should write out the *entire* buffer */
slouken@502
   433
    SDL_RWwrite(dest->ctx, dest->buffer, 1, OUTPUT_BUFFER_SIZE);
slouken@502
   434
    dest->pub.next_output_byte = dest->buffer;
slouken@502
   435
    dest->pub.free_in_buffer = OUTPUT_BUFFER_SIZE;
slouken@502
   436
slouken@522
   437
    return TRUE;
slouken@502
   438
}
slouken@502
   439
slouken@502
   440
static void term_destination(j_compress_ptr cinfo)
slouken@502
   441
{
slouken@502
   442
    my_destination_mgr * dest = (my_destination_mgr *)cinfo->dest;
slouken@502
   443
slouken@502
   444
    /*  In most applications, this must flush any data remaining in the buffer */
slouken@502
   445
    SDL_RWwrite(dest->ctx, dest->buffer, 1, OUTPUT_BUFFER_SIZE - dest->pub.free_in_buffer);
slouken@502
   446
}
slouken@502
   447
slouken@502
   448
static void jpeg_SDL_RW_dest(j_compress_ptr cinfo, SDL_RWops *ctx)
slouken@502
   449
{
slouken@502
   450
    my_destination_mgr *dest;
slouken@502
   451
slouken@502
   452
    if (cinfo->dest == NULL) {
slouken@502
   453
        cinfo->dest = (struct jpeg_destination_mgr *)
slouken@502
   454
            (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
slouken@502
   455
            sizeof(my_destination_mgr));
slouken@502
   456
        dest = (my_destination_mgr *)cinfo->dest;
slouken@502
   457
    }
slouken@502
   458
slouken@502
   459
    dest = (my_destination_mgr *)cinfo->dest;
slouken@502
   460
    dest->pub.init_destination = init_destination;
slouken@502
   461
    dest->pub.empty_output_buffer = empty_output_buffer;
slouken@502
   462
    dest->pub.term_destination = term_destination;
slouken@502
   463
    dest->ctx = ctx;
slouken@502
   464
    dest->pub.next_output_byte = dest->buffer;
slouken@502
   465
    dest->pub.free_in_buffer = OUTPUT_BUFFER_SIZE;
slouken@502
   466
}
slouken@502
   467
slouken@504
   468
static int IMG_SaveJPG_RW_jpeglib(SDL_Surface *surface, SDL_RWops *dst, int freedst, int quality)
slouken@502
   469
{
slouken@514
   470
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@514
   471
    static const Uint32 jpg_format = SDL_PIXELFORMAT_RGB24;
slouken@514
   472
#else
slouken@514
   473
    static const Uint32 jpg_format = SDL_PIXELFORMAT_BGR24;
slouken@514
   474
#endif
slouken@502
   475
    struct jpeg_compress_struct cinfo;
slouken@502
   476
    struct my_error_mgr jerr;
slouken@502
   477
    JSAMPROW row_pointer[1];
slouken@502
   478
    SDL_Surface* jpeg_surface = surface;
slouken@504
   479
    int result = -1;
slouken@502
   480
slouken@502
   481
    if (!dst) {
slouken@502
   482
        SDL_SetError("Passed NULL dst");
slouken@504
   483
        goto done;
slouken@502
   484
    }
slouken@502
   485
slouken@502
   486
    if (!IMG_Init(IMG_INIT_JPG)) {
slouken@504
   487
        goto done;
slouken@502
   488
    }
slouken@502
   489
slouken@502
   490
    /* Convert surface to format we can save */
slouken@502
   491
    if (surface->format->format != jpg_format) {
slouken@502
   492
        jpeg_surface = SDL_ConvertSurfaceFormat(surface, jpg_format, 0);
slouken@502
   493
        if (!jpeg_surface) {
slouken@504
   494
            goto done;
slouken@502
   495
        }
slouken@502
   496
    }
slouken@502
   497
slouken@502
   498
    /* Create a decompression structure and load the JPEG header */
slouken@502
   499
    cinfo.err = lib.jpeg_std_error(&jerr.errmgr);
slouken@502
   500
    jerr.errmgr.error_exit = my_error_exit;
slouken@502
   501
    jerr.errmgr.output_message = output_no_message;
slouken@502
   502
slouken@502
   503
    lib.jpeg_create_compress(&cinfo);
slouken@502
   504
    jpeg_SDL_RW_dest(&cinfo, dst);
slouken@502
   505
flibitijibibo@547
   506
    cinfo.image_width = jpeg_surface->w;
flibitijibibo@547
   507
    cinfo.image_height = jpeg_surface->h;
slouken@502
   508
    cinfo.in_color_space = JCS_RGB;
slouken@502
   509
    cinfo.input_components = 3;
slouken@502
   510
slouken@502
   511
    lib.jpeg_set_defaults(&cinfo);
slouken@522
   512
    lib.jpeg_set_quality(&cinfo, quality, TRUE);
slouken@522
   513
    lib.jpeg_start_compress(&cinfo, TRUE);
slouken@502
   514
slouken@502
   515
    while (cinfo.next_scanline < cinfo.image_height) {
flibitijibibo@547
   516
        int offset = cinfo.next_scanline * jpeg_surface->pitch;
slouken@502
   517
        row_pointer[0] = ((Uint8*)jpeg_surface->pixels) + offset;
slouken@502
   518
        lib.jpeg_write_scanlines(&cinfo, row_pointer, 1);
slouken@502
   519
    }
slouken@502
   520
slouken@502
   521
    lib.jpeg_finish_compress(&cinfo);
slouken@502
   522
    lib.jpeg_destroy_compress(&cinfo);
slouken@502
   523
slouken@502
   524
    if (jpeg_surface != surface) {
slouken@502
   525
        SDL_FreeSurface(jpeg_surface);
slouken@502
   526
    }
slouken@502
   527
slouken@504
   528
    result = 0;
slouken@502
   529
slouken@502
   530
done:
slouken@502
   531
    if (freedst) {
slouken@502
   532
        SDL_RWclose(dst);
slouken@502
   533
    }
slouken@502
   534
    return result;
slouken@502
   535
}
slouken@502
   536
slouken@0
   537
#else
slouken@0
   538
slouken@205
   539
int IMG_InitJPG()
slouken@205
   540
{
slouken@368
   541
    IMG_SetError("JPEG images are not supported");
slouken@368
   542
    return(-1);
slouken@205
   543
}
slouken@205
   544
slouken@205
   545
void IMG_QuitJPG()
slouken@205
   546
{
slouken@205
   547
}
slouken@205
   548
slouken@0
   549
/* See if an image is contained in a data source */
slouken@0
   550
int IMG_isJPG(SDL_RWops *src)
slouken@0
   551
{
slouken@368
   552
    return(0);
slouken@0
   553
}
slouken@0
   554
slouken@0
   555
/* Load a JPEG type image from an SDL datasource */
slouken@0
   556
SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
slouken@0
   557
{
slouken@368
   558
    return(NULL);
slouken@0
   559
}
slouken@0
   560
slouken@504
   561
#endif /* LOAD_JPG */
slouken@504
   562
slouken@504
   563
#endif /* !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND) */
slouken@504
   564
slouken@504
   565
/* We'll always have JPG save support */
slouken@504
   566
#define SAVE_JPG
slouken@504
   567
slouken@504
   568
#ifdef SAVE_JPG
slouken@504
   569
slouken@502
   570
int IMG_SaveJPG(SDL_Surface *surface, const char *file, int quality)
slouken@502
   571
{
slouken@504
   572
    SDL_RWops *dst = SDL_RWFromFile(file, "wb");
slouken@504
   573
    if (dst) {
slouken@504
   574
        return IMG_SaveJPG_RW(surface, dst, 1, quality);
slouken@504
   575
    } else {
slouken@504
   576
        return -1;
slouken@504
   577
    }
slouken@502
   578
}
slouken@502
   579
slouken@502
   580
int IMG_SaveJPG_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst, int quality)
slouken@502
   581
{
slouken@504
   582
#ifdef USE_JPEGLIB
slouken@504
   583
    return IMG_SaveJPG_RW_jpeglib(surface, dst, freedst, quality);
slouken@504
   584
#else
slouken@504
   585
    return IMG_SetError("SDL_image not built with jpeglib, saving not supported");
slouken@504
   586
#endif
slouken@502
   587
}
slouken@502
   588
slouken@504
   589
#endif /* SAVE_JPG */