hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 1 | /* |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 2 | * jdatadst-tj.c |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 3 | * |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 4 | * This file was part of the Independent JPEG Group's software: |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 5 | * Copyright (C) 1994-1996, Thomas G. Lane. |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 6 | * Modified 2009-2012 by Guido Vollbeding. |
| 7 | * libjpeg-turbo Modifications: |
Jonathan Wright | db870df | 2020-08-05 11:42:22 +0100 | [diff] [blame] | 8 | * Copyright (C) 2011, 2014, 2016, 2019, D. R. Commander. |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 9 | * For conditions of distribution and use, see the accompanying README.ijg |
| 10 | * file. |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 11 | * |
| 12 | * This file contains compression data destination routines for the case of |
| 13 | * emitting JPEG data to memory or to a file (or any stdio stream). |
| 14 | * While these routines are sufficient for most applications, |
| 15 | * some will want to use a different destination manager. |
| 16 | * IMPORTANT: we assume that fwrite() will correctly transcribe an array of |
| 17 | * JOCTETs into 8-bit-wide elements on external storage. If char is wider |
| 18 | * than 8 bits on your machine, you may need to do some tweaking. |
| 19 | */ |
| 20 | |
| 21 | /* this is not a core library module, so it doesn't define JPEG_INTERNALS */ |
| 22 | #include "jinclude.h" |
| 23 | #include "jpeglib.h" |
| 24 | #include "jerror.h" |
| 25 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 26 | #ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 27 | extern void *malloc(size_t size); |
| 28 | extern void free(void *ptr); |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 29 | #endif |
Jonathan Wright | db870df | 2020-08-05 11:42:22 +0100 | [diff] [blame] | 30 | void jpeg_mem_dest_tj(j_compress_ptr cinfo, unsigned char **outbuffer, |
| 31 | unsigned long *outsize, boolean alloc); |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 32 | |
| 33 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 34 | #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */ |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 35 | |
| 36 | |
| 37 | /* Expanded data destination object for memory output */ |
| 38 | |
| 39 | typedef struct { |
| 40 | struct jpeg_destination_mgr pub; /* public fields */ |
| 41 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 42 | unsigned char **outbuffer; /* target buffer */ |
| 43 | unsigned long *outsize; |
| 44 | unsigned char *newbuffer; /* newly allocated buffer */ |
| 45 | JOCTET *buffer; /* start of buffer */ |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 46 | size_t bufsize; |
| 47 | boolean alloc; |
| 48 | } my_mem_destination_mgr; |
| 49 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 50 | typedef my_mem_destination_mgr *my_mem_dest_ptr; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 51 | |
| 52 | |
| 53 | /* |
| 54 | * Initialize destination --- called by jpeg_start_compress |
| 55 | * before any data is actually written. |
| 56 | */ |
| 57 | |
| 58 | METHODDEF(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 59 | init_mem_destination(j_compress_ptr cinfo) |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 60 | { |
| 61 | /* no work necessary here */ |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /* |
| 66 | * Empty the output buffer --- called whenever buffer fills up. |
| 67 | * |
| 68 | * In typical applications, this should write the entire output buffer |
| 69 | * (ignoring the current state of next_output_byte & free_in_buffer), |
| 70 | * reset the pointer & count to the start of the buffer, and return TRUE |
| 71 | * indicating that the buffer has been dumped. |
| 72 | * |
| 73 | * In applications that need to be able to suspend compression due to output |
| 74 | * overrun, a FALSE return indicates that the buffer cannot be emptied now. |
| 75 | * In this situation, the compressor will return to its caller (possibly with |
| 76 | * an indication that it has not accepted all the supplied scanlines). The |
| 77 | * application should resume compression after it has made more room in the |
| 78 | * output buffer. Note that there are substantial restrictions on the use of |
| 79 | * suspension --- see the documentation. |
| 80 | * |
| 81 | * When suspending, the compressor will back up to a convenient restart point |
| 82 | * (typically the start of the current MCU). next_output_byte & free_in_buffer |
| 83 | * indicate where the restart point will be if the current call returns FALSE. |
| 84 | * Data beyond this point will be regenerated after resumption, so do not |
| 85 | * write it out when emptying the buffer externally. |
| 86 | */ |
| 87 | |
| 88 | METHODDEF(boolean) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 89 | empty_mem_output_buffer(j_compress_ptr cinfo) |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 90 | { |
| 91 | size_t nextsize; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 92 | JOCTET *nextbuffer; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 93 | my_mem_dest_ptr dest = (my_mem_dest_ptr)cinfo->dest; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 94 | |
| 95 | if (!dest->alloc) ERREXIT(cinfo, JERR_BUFFER_SIZE); |
| 96 | |
| 97 | /* Try to allocate new buffer with double size */ |
| 98 | nextsize = dest->bufsize * 2; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 99 | nextbuffer = (JOCTET *)malloc(nextsize); |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 100 | |
| 101 | if (nextbuffer == NULL) |
| 102 | ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10); |
| 103 | |
| 104 | MEMCOPY(nextbuffer, dest->buffer, dest->bufsize); |
| 105 | |
Jonathan Wright | db870df | 2020-08-05 11:42:22 +0100 | [diff] [blame] | 106 | free(dest->newbuffer); |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 107 | |
| 108 | dest->newbuffer = nextbuffer; |
| 109 | |
| 110 | dest->pub.next_output_byte = nextbuffer + dest->bufsize; |
| 111 | dest->pub.free_in_buffer = dest->bufsize; |
| 112 | |
| 113 | dest->buffer = nextbuffer; |
| 114 | dest->bufsize = nextsize; |
| 115 | |
| 116 | return TRUE; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /* |
| 121 | * Terminate destination --- called by jpeg_finish_compress |
| 122 | * after all data has been written. Usually needs to flush buffer. |
| 123 | * |
| 124 | * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding |
| 125 | * application must deal with any cleanup that should happen even |
| 126 | * for error exit. |
| 127 | */ |
| 128 | |
| 129 | METHODDEF(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 130 | term_mem_destination(j_compress_ptr cinfo) |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 131 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 132 | my_mem_dest_ptr dest = (my_mem_dest_ptr)cinfo->dest; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 133 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 134 | if (dest->alloc) *dest->outbuffer = dest->buffer; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 135 | *dest->outsize = (unsigned long)(dest->bufsize - dest->pub.free_in_buffer); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | /* |
| 140 | * Prepare for output to a memory buffer. |
| 141 | * The caller may supply an own initial buffer with appropriate size. |
| 142 | * Otherwise, or when the actual data output exceeds the given size, |
| 143 | * the library adapts the buffer size as necessary. |
| 144 | * The standard library functions malloc/free are used for allocating |
| 145 | * larger memory, so the buffer is available to the application after |
| 146 | * finishing compression, and then the application is responsible for |
| 147 | * freeing the requested memory. |
| 148 | */ |
| 149 | |
| 150 | GLOBAL(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 151 | jpeg_mem_dest_tj(j_compress_ptr cinfo, unsigned char **outbuffer, |
| 152 | unsigned long *outsize, boolean alloc) |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 153 | { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 154 | boolean reused = FALSE; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 155 | my_mem_dest_ptr dest; |
| 156 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 157 | if (outbuffer == NULL || outsize == NULL) /* sanity check */ |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 158 | ERREXIT(cinfo, JERR_BUFFER_SIZE); |
| 159 | |
| 160 | /* The destination object is made permanent so that multiple JPEG images |
| 161 | * can be written to the same buffer without re-executing jpeg_mem_dest. |
| 162 | */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 163 | if (cinfo->dest == NULL) { /* first time for this JPEG object? */ |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 164 | cinfo->dest = (struct jpeg_destination_mgr *) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 165 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT, |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 166 | sizeof(my_mem_destination_mgr)); |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 167 | dest = (my_mem_dest_ptr)cinfo->dest; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 168 | dest->newbuffer = NULL; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 169 | dest->buffer = NULL; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 170 | } else if (cinfo->dest->init_destination != init_mem_destination) { |
| 171 | /* It is unsafe to reuse the existing destination manager unless it was |
| 172 | * created by this function. |
| 173 | */ |
| 174 | ERREXIT(cinfo, JERR_BUFFER_SIZE); |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 177 | dest = (my_mem_dest_ptr)cinfo->dest; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 178 | dest->pub.init_destination = init_mem_destination; |
| 179 | dest->pub.empty_output_buffer = empty_mem_output_buffer; |
| 180 | dest->pub.term_destination = term_mem_destination; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 181 | if (dest->buffer == *outbuffer && *outbuffer != NULL && alloc) |
| 182 | reused = TRUE; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 183 | dest->outbuffer = outbuffer; |
| 184 | dest->outsize = outsize; |
| 185 | dest->alloc = alloc; |
| 186 | |
| 187 | if (*outbuffer == NULL || *outsize == 0) { |
| 188 | if (alloc) { |
| 189 | /* Allocate initial buffer */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 190 | dest->newbuffer = *outbuffer = (unsigned char *)malloc(OUTPUT_BUF_SIZE); |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 191 | if (dest->newbuffer == NULL) |
| 192 | ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10); |
| 193 | *outsize = OUTPUT_BUF_SIZE; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 194 | } else |
| 195 | ERREXIT(cinfo, JERR_BUFFER_SIZE); |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | dest->pub.next_output_byte = dest->buffer = *outbuffer; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 199 | if (!reused) |
| 200 | dest->bufsize = *outsize; |
| 201 | dest->pub.free_in_buffer = dest->bufsize; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 202 | } |