blob: f6ded64abb95d04e9b88d01f969f143a0d807f65 [file] [log] [blame]
DRC9b28def2011-05-21 14:37:15 +00001/*
DRCb24a7222013-01-01 10:13:34 +00002 * jdatadst-tj.c
DRC9b28def2011-05-21 14:37:15 +00003 *
DRCb24a7222013-01-01 10:13:34 +00004 * This file was part of the Independent JPEG Group's software:
DRC9b28def2011-05-21 14:37:15 +00005 * Copyright (C) 1994-1996, Thomas G. Lane.
DRCb5f2e452013-01-01 10:17:03 +00006 * Modified 2009-2012 by Guido Vollbeding.
DRCa6ef2822013-09-28 03:23:49 +00007 * libjpeg-turbo Modifications:
DRC6399d0a2019-04-23 14:10:04 -05008 * Copyright (C) 2011, 2014, 2016, 2019, D. R. Commander.
DRC7e3acc02015-10-10 10:25:46 -05009 * For conditions of distribution and use, see the accompanying README.ijg
10 * file.
DRC9b28def2011-05-21 14:37:15 +000011 *
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
DRCe5eaf372014-05-09 18:00:32 +000026#ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */
DRC19c791c2018-03-08 10:55:20 -060027extern void *malloc(size_t size);
28extern void free(void *ptr);
DRC9b28def2011-05-21 14:37:15 +000029#endif
DRC6399d0a2019-04-23 14:10:04 -050030void jpeg_mem_dest_tj(j_compress_ptr cinfo, unsigned char **outbuffer,
31 unsigned long *outsize, boolean alloc);
DRC9b28def2011-05-21 14:37:15 +000032
33
DRCe5eaf372014-05-09 18:00:32 +000034#define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
DRC9b28def2011-05-21 14:37:15 +000035
36
37/* Expanded data destination object for memory output */
38
39typedef struct {
40 struct jpeg_destination_mgr pub; /* public fields */
41
DRCbd498032016-02-19 08:53:33 -060042 unsigned char **outbuffer; /* target buffer */
43 unsigned long *outsize;
44 unsigned char *newbuffer; /* newly allocated buffer */
45 JOCTET *buffer; /* start of buffer */
DRC9b28def2011-05-21 14:37:15 +000046 size_t bufsize;
47 boolean alloc;
48} my_mem_destination_mgr;
49
DRCbd498032016-02-19 08:53:33 -060050typedef my_mem_destination_mgr *my_mem_dest_ptr;
DRC9b28def2011-05-21 14:37:15 +000051
52
53/*
54 * Initialize destination --- called by jpeg_start_compress
55 * before any data is actually written.
56 */
57
58METHODDEF(void)
DRC19c791c2018-03-08 10:55:20 -060059init_mem_destination(j_compress_ptr cinfo)
DRC9b28def2011-05-21 14:37:15 +000060{
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
88METHODDEF(boolean)
DRC19c791c2018-03-08 10:55:20 -060089empty_mem_output_buffer(j_compress_ptr cinfo)
DRC9b28def2011-05-21 14:37:15 +000090{
91 size_t nextsize;
DRCbd498032016-02-19 08:53:33 -060092 JOCTET *nextbuffer;
DRC19c791c2018-03-08 10:55:20 -060093 my_mem_dest_ptr dest = (my_mem_dest_ptr)cinfo->dest;
DRC9b28def2011-05-21 14:37:15 +000094
95 if (!dest->alloc) ERREXIT(cinfo, JERR_BUFFER_SIZE);
96
97 /* Try to allocate new buffer with double size */
98 nextsize = dest->bufsize * 2;
DRC19c791c2018-03-08 10:55:20 -060099 nextbuffer = (JOCTET *)malloc(nextsize);
DRC9b28def2011-05-21 14:37:15 +0000100
101 if (nextbuffer == NULL)
102 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
103
104 MEMCOPY(nextbuffer, dest->buffer, dest->bufsize);
105
106 if (dest->newbuffer != NULL)
107 free(dest->newbuffer);
108
109 dest->newbuffer = nextbuffer;
110
111 dest->pub.next_output_byte = nextbuffer + dest->bufsize;
112 dest->pub.free_in_buffer = dest->bufsize;
113
114 dest->buffer = nextbuffer;
115 dest->bufsize = nextsize;
116
117 return TRUE;
118}
119
120
121/*
122 * Terminate destination --- called by jpeg_finish_compress
123 * after all data has been written. Usually needs to flush buffer.
124 *
125 * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
126 * application must deal with any cleanup that should happen even
127 * for error exit.
128 */
129
130METHODDEF(void)
DRC19c791c2018-03-08 10:55:20 -0600131term_mem_destination(j_compress_ptr cinfo)
DRC9b28def2011-05-21 14:37:15 +0000132{
DRC19c791c2018-03-08 10:55:20 -0600133 my_mem_dest_ptr dest = (my_mem_dest_ptr)cinfo->dest;
DRC9b28def2011-05-21 14:37:15 +0000134
DRC9d9d8fe2017-11-17 18:15:42 -0600135 if (dest->alloc) *dest->outbuffer = dest->buffer;
DRC9b28def2011-05-21 14:37:15 +0000136 *dest->outsize = (unsigned long)(dest->bufsize - dest->pub.free_in_buffer);
137}
138
139
140/*
141 * Prepare for output to a memory buffer.
142 * The caller may supply an own initial buffer with appropriate size.
143 * Otherwise, or when the actual data output exceeds the given size,
144 * the library adapts the buffer size as necessary.
145 * The standard library functions malloc/free are used for allocating
146 * larger memory, so the buffer is available to the application after
147 * finishing compression, and then the application is responsible for
148 * freeing the requested memory.
149 */
150
151GLOBAL(void)
DRC19c791c2018-03-08 10:55:20 -0600152jpeg_mem_dest_tj(j_compress_ptr cinfo, unsigned char **outbuffer,
153 unsigned long *outsize, boolean alloc)
DRC9b28def2011-05-21 14:37:15 +0000154{
DRCfe80ec22014-08-21 15:51:47 +0000155 boolean reused = FALSE;
DRC9b28def2011-05-21 14:37:15 +0000156 my_mem_dest_ptr dest;
157
DRCe5eaf372014-05-09 18:00:32 +0000158 if (outbuffer == NULL || outsize == NULL) /* sanity check */
DRC9b28def2011-05-21 14:37:15 +0000159 ERREXIT(cinfo, JERR_BUFFER_SIZE);
160
161 /* The destination object is made permanent so that multiple JPEG images
162 * can be written to the same buffer without re-executing jpeg_mem_dest.
163 */
DRCe5eaf372014-05-09 18:00:32 +0000164 if (cinfo->dest == NULL) { /* first time for this JPEG object? */
DRC9b28def2011-05-21 14:37:15 +0000165 cinfo->dest = (struct jpeg_destination_mgr *)
DRC19c791c2018-03-08 10:55:20 -0600166 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
DRC5de454b2014-05-18 19:04:03 +0000167 sizeof(my_mem_destination_mgr));
DRC19c791c2018-03-08 10:55:20 -0600168 dest = (my_mem_dest_ptr)cinfo->dest;
DRC90215cc2011-05-24 15:15:15 +0000169 dest->newbuffer = NULL;
DRCcce13c72014-08-21 22:15:19 +0000170 dest->buffer = NULL;
DRC68cf83d2016-05-10 21:04:02 -0500171 } else if (cinfo->dest->init_destination != init_mem_destination) {
172 /* It is unsafe to reuse the existing destination manager unless it was
173 * created by this function.
174 */
175 ERREXIT(cinfo, JERR_BUFFER_SIZE);
DRC9b28def2011-05-21 14:37:15 +0000176 }
177
DRC19c791c2018-03-08 10:55:20 -0600178 dest = (my_mem_dest_ptr)cinfo->dest;
DRC9b28def2011-05-21 14:37:15 +0000179 dest->pub.init_destination = init_mem_destination;
180 dest->pub.empty_output_buffer = empty_mem_output_buffer;
181 dest->pub.term_destination = term_mem_destination;
DRC88b90bd2014-08-21 22:16:25 +0000182 if (dest->buffer == *outbuffer && *outbuffer != NULL && alloc)
DRCfe80ec22014-08-21 15:51:47 +0000183 reused = TRUE;
DRC9b28def2011-05-21 14:37:15 +0000184 dest->outbuffer = outbuffer;
185 dest->outsize = outsize;
DRC9b28def2011-05-21 14:37:15 +0000186 dest->alloc = alloc;
187
188 if (*outbuffer == NULL || *outsize == 0) {
189 if (alloc) {
190 /* Allocate initial buffer */
DRC19c791c2018-03-08 10:55:20 -0600191 dest->newbuffer = *outbuffer = (unsigned char *)malloc(OUTPUT_BUF_SIZE);
DRC9b28def2011-05-21 14:37:15 +0000192 if (dest->newbuffer == NULL)
193 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
194 *outsize = OUTPUT_BUF_SIZE;
DRC19c791c2018-03-08 10:55:20 -0600195 } else
196 ERREXIT(cinfo, JERR_BUFFER_SIZE);
DRC9b28def2011-05-21 14:37:15 +0000197 }
198
199 dest->pub.next_output_byte = dest->buffer = *outbuffer;
DRCfe80ec22014-08-21 15:51:47 +0000200 if (!reused)
201 dest->bufsize = *outsize;
202 dest->pub.free_in_buffer = dest->bufsize;
DRC9b28def2011-05-21 14:37:15 +0000203}