blob: a2219dff7cf96433c523aeb9d409bd98e38da00e [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:
Alex Naidis6eb7d372016-10-16 23:10:08 +02008 * Copyright (C) 2011, 2014, 2016, D. R. Commander.
9 * 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() */
Alex Naidis6eb7d372016-10-16 23:10:08 +020027extern void *malloc (size_t size);
DRCbc56b752014-05-16 10:43:44 +000028extern void free (void *ptr);
DRC9b28def2011-05-21 14:37:15 +000029#endif
30
31
DRCe5eaf372014-05-09 18:00:32 +000032#define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
DRC9b28def2011-05-21 14:37:15 +000033
34
35/* Expanded data destination object for memory output */
36
37typedef struct {
38 struct jpeg_destination_mgr pub; /* public fields */
39
Alex Naidis6eb7d372016-10-16 23:10:08 +020040 unsigned char **outbuffer; /* target buffer */
41 unsigned long *outsize;
42 unsigned char *newbuffer; /* newly allocated buffer */
43 JOCTET *buffer; /* start of buffer */
DRC9b28def2011-05-21 14:37:15 +000044 size_t bufsize;
45 boolean alloc;
46} my_mem_destination_mgr;
47
Alex Naidis6eb7d372016-10-16 23:10:08 +020048typedef my_mem_destination_mgr *my_mem_dest_ptr;
DRC9b28def2011-05-21 14:37:15 +000049
50
51/*
52 * Initialize destination --- called by jpeg_start_compress
53 * before any data is actually written.
54 */
55
56METHODDEF(void)
57init_mem_destination (j_compress_ptr cinfo)
58{
59 /* no work necessary here */
60}
61
62
63/*
64 * Empty the output buffer --- called whenever buffer fills up.
65 *
66 * In typical applications, this should write the entire output buffer
67 * (ignoring the current state of next_output_byte & free_in_buffer),
68 * reset the pointer & count to the start of the buffer, and return TRUE
69 * indicating that the buffer has been dumped.
70 *
71 * In applications that need to be able to suspend compression due to output
72 * overrun, a FALSE return indicates that the buffer cannot be emptied now.
73 * In this situation, the compressor will return to its caller (possibly with
74 * an indication that it has not accepted all the supplied scanlines). The
75 * application should resume compression after it has made more room in the
76 * output buffer. Note that there are substantial restrictions on the use of
77 * suspension --- see the documentation.
78 *
79 * When suspending, the compressor will back up to a convenient restart point
80 * (typically the start of the current MCU). next_output_byte & free_in_buffer
81 * indicate where the restart point will be if the current call returns FALSE.
82 * Data beyond this point will be regenerated after resumption, so do not
83 * write it out when emptying the buffer externally.
84 */
85
86METHODDEF(boolean)
87empty_mem_output_buffer (j_compress_ptr cinfo)
88{
89 size_t nextsize;
Alex Naidis6eb7d372016-10-16 23:10:08 +020090 JOCTET *nextbuffer;
DRC9b28def2011-05-21 14:37:15 +000091 my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest;
92
93 if (!dest->alloc) ERREXIT(cinfo, JERR_BUFFER_SIZE);
94
95 /* Try to allocate new buffer with double size */
96 nextsize = dest->bufsize * 2;
DRCb5f2e452013-01-01 10:17:03 +000097 nextbuffer = (JOCTET *) malloc(nextsize);
DRC9b28def2011-05-21 14:37:15 +000098
99 if (nextbuffer == NULL)
100 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
101
102 MEMCOPY(nextbuffer, dest->buffer, dest->bufsize);
103
104 if (dest->newbuffer != NULL)
105 free(dest->newbuffer);
106
107 dest->newbuffer = nextbuffer;
108
109 dest->pub.next_output_byte = nextbuffer + dest->bufsize;
110 dest->pub.free_in_buffer = dest->bufsize;
111
112 dest->buffer = nextbuffer;
113 dest->bufsize = nextsize;
114
115 return TRUE;
116}
117
118
119/*
120 * Terminate destination --- called by jpeg_finish_compress
121 * after all data has been written. Usually needs to flush buffer.
122 *
123 * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
124 * application must deal with any cleanup that should happen even
125 * for error exit.
126 */
127
128METHODDEF(void)
129term_mem_destination (j_compress_ptr cinfo)
130{
131 my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest;
132
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -0500133 if (dest->alloc) *dest->outbuffer = dest->buffer;
DRC9b28def2011-05-21 14:37:15 +0000134 *dest->outsize = (unsigned long)(dest->bufsize - dest->pub.free_in_buffer);
135}
136
137
138/*
139 * Prepare for output to a memory buffer.
140 * The caller may supply an own initial buffer with appropriate size.
141 * Otherwise, or when the actual data output exceeds the given size,
142 * the library adapts the buffer size as necessary.
143 * The standard library functions malloc/free are used for allocating
144 * larger memory, so the buffer is available to the application after
145 * finishing compression, and then the application is responsible for
146 * freeing the requested memory.
147 */
148
149GLOBAL(void)
150jpeg_mem_dest_tj (j_compress_ptr cinfo,
Alex Naidis6eb7d372016-10-16 23:10:08 +0200151 unsigned char **outbuffer, unsigned long *outsize,
DRCe5eaf372014-05-09 18:00:32 +0000152 boolean alloc)
DRC9b28def2011-05-21 14:37:15 +0000153{
DRCfe80ec22014-08-21 15:51:47 +0000154 boolean reused = FALSE;
DRC9b28def2011-05-21 14:37:15 +0000155 my_mem_dest_ptr dest;
156
DRCe5eaf372014-05-09 18:00:32 +0000157 if (outbuffer == NULL || outsize == NULL) /* sanity check */
DRC9b28def2011-05-21 14:37:15 +0000158 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 */
DRCe5eaf372014-05-09 18:00:32 +0000163 if (cinfo->dest == NULL) { /* first time for this JPEG object? */
DRC9b28def2011-05-21 14:37:15 +0000164 cinfo->dest = (struct jpeg_destination_mgr *)
165 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
DRC5de454b2014-05-18 19:04:03 +0000166 sizeof(my_mem_destination_mgr));
DRC90215cc2011-05-24 15:15:15 +0000167 dest = (my_mem_dest_ptr) cinfo->dest;
168 dest->newbuffer = NULL;
DRCcce13c72014-08-21 22:15:19 +0000169 dest->buffer = NULL;
Alex Naidis6eb7d372016-10-16 23:10:08 +0200170 } 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);
DRC9b28def2011-05-21 14:37:15 +0000175 }
176
177 dest = (my_mem_dest_ptr) cinfo->dest;
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;
DRC88b90bd2014-08-21 22:16:25 +0000181 if (dest->buffer == *outbuffer && *outbuffer != NULL && alloc)
DRCfe80ec22014-08-21 15:51:47 +0000182 reused = TRUE;
DRC9b28def2011-05-21 14:37:15 +0000183 dest->outbuffer = outbuffer;
184 dest->outsize = outsize;
DRC9b28def2011-05-21 14:37:15 +0000185 dest->alloc = alloc;
186
187 if (*outbuffer == NULL || *outsize == 0) {
188 if (alloc) {
189 /* Allocate initial buffer */
DRCb5f2e452013-01-01 10:17:03 +0000190 dest->newbuffer = *outbuffer = (unsigned char *) malloc(OUTPUT_BUF_SIZE);
DRC9b28def2011-05-21 14:37:15 +0000191 if (dest->newbuffer == NULL)
192 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
193 *outsize = OUTPUT_BUF_SIZE;
194 }
195 else ERREXIT(cinfo, JERR_BUFFER_SIZE);
196 }
197
198 dest->pub.next_output_byte = dest->buffer = *outbuffer;
DRCfe80ec22014-08-21 15:51:47 +0000199 if (!reused)
200 dest->bufsize = *outsize;
201 dest->pub.free_in_buffer = dest->bufsize;
DRC9b28def2011-05-21 14:37:15 +0000202}