-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjpeg_write.c
365 lines (313 loc) · 12.4 KB
/
jpeg_write.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* jpeg_write.c
*
* JPEG_WRITE(JPEGOBJ,FILENAME)
*
* Reads JPEGOBJ, a Matlab struct containing the JPEG header,
* quantization tables and the DCT coefficients (as returned by JPEG_READ),
* and writes the information into a JPEG file with the name FILENAME.
*
* This software is based in part on the work of the Independent JPEG Group.
* In order to compile, you must first build IJG's JPEG Tools code library,
* available at ftp://ftp.uu.net/graphics/jpeg/jpegsrc.v6b.tar.gz.
*
* Phil Sallee, Surya De 6/2003
*
* Copyright (c) 2003 The Regents of the University of California.
* All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for educational, research and non-profit purposes,
* without fee, and without a written agreement is hereby granted,
* provided that the above copyright notice, this paragraph and the
* following three paragraphs appear in all copies.
*
* Permission to incorporate this software into commercial products may
* be obtained by contacting the University of California. Contact Jo Clare
* Peterman, University of California, 428 Mrak Hall, Davis, CA, 95616.
*
* This software program and documentation are copyrighted by The Regents
* of the University of California. The software program and
* documentation are supplied "as is", without any accompanying services
* from The Regents. The Regents does not warrant that the operation of
* the program will be uninterrupted or error-free. The end-user
* understands that the program was developed for research purposes and
* is advised not to rely exclusively on the program for any reason.
*
* IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
* FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
* INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND
* ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF
* CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
* BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE
* MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <jerror.h>
#include <jpeglib.h>
#include <setjmp.h>
#include <jpegint.h>
#include "mex.h"
/* We need to create our own error handler so that we can override the
* default handler in case a fatal error occurs. The standard error_exit
* method calls exit() which doesn't clean things up properly and also
* exits Matlab. This is described in the example.c routine provided in
* the IJG's code library.
*/
struct my_error_mgr {
struct jpeg_error_mgr pub; /* "public" fields */
jmp_buf setjmp_buffer; /* for return to caller */
};
/* The default output_message routine causes a seg fault in Matlab,
* at least on Windows. Its generally used to emit warnings, since
* fatal errors call the error_exit routine, so we emit a Matlab
* warning instead. If desired, warnings can be turned off by the
* user with "warnings off". -- PAS 10/03
*/
METHODDEF(void)
my_output_message (j_common_ptr cinfo)
{
char buffer[JMSG_LENGTH_MAX];
/* Create the message */
(*cinfo->err->format_message) (cinfo, buffer);
mexWarnMsgTxt(buffer);
}
typedef struct my_error_mgr * my_error_ptr;
METHODDEF(void)
my_error_exit (j_common_ptr cinfo)
{
char buffer[JMSG_LENGTH_MAX];
/* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
my_error_ptr myerr = (my_error_ptr) cinfo->err;
/* create the message */
(*cinfo->err->format_message) (cinfo, buffer);
printf("Error: %s\n",buffer);
/* return control to the setjmp point */
longjmp(myerr->setjmp_buffer, 1);
}
/* mex function */
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
struct jpeg_compress_struct cinfo;
struct my_error_mgr jerr;
const mxArray *mxjpeg_obj;
mxArray *mxcoef_arrays,*mxhuff_tables,*mxcomp_info,*mxtemp,
*mxquant_tables,*mxcomments;
char *filename,*comment;
int strlen,c_height,c_width,ci,i,j,n,t;
FILE *outfile;
jvirt_barray_ptr *coef_arrays = NULL;
jpeg_component_info *compptr;
JDIMENSION blk_x,blk_y;
JBLOCKARRAY buffer;
JCOEFPTR bufptr;
double *mp, *mptop;
/* check the input values */
if (nrhs != 2) mexErrMsgTxt("Two input arguments required.");
/* check the output values */
if (nlhs != 0) mexErrMsgTxt("Too many output arguments.");
if (mxIsChar(prhs[1]) != 1) mexErrMsgTxt("Filename must be a string.");
/* get filename */
strlen = mxGetM(prhs[1])*mxGetN(prhs[1]) + 1;
filename = mxCalloc(strlen, sizeof(char));
mxGetString(prhs[1],filename,strlen);
/* open the output file*/
if ((outfile = fopen(filename, "wb")) == NULL)
mexErrMsgTxt("Can't open file.");
/* set up the normal JPEG error routines, then override error_exit. */
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = my_error_exit;
jerr.pub.output_message = my_output_message;
/* establish the setjmp return context for my_error_exit to use. */
if (setjmp(jerr.setjmp_buffer))
{
jpeg_destroy_compress(&cinfo);
fclose(outfile);
mexErrMsgTxt("Error writing to file.");
}
/* set the input */
mxjpeg_obj = prhs[0];
/* initialize JPEG decompression object */
jpeg_create_compress(&cinfo);
/* write the output file */
jpeg_stdio_dest(&cinfo, outfile);
/* Set the compression object with our parameters */
cinfo.image_width =
(unsigned int) mxGetScalar(mxGetField(mxjpeg_obj,0,"image_width"));
cinfo.image_height =
(unsigned int) mxGetScalar(mxGetField(mxjpeg_obj,0,"image_height"));
cinfo.input_components =
(int) mxGetScalar(mxGetField(mxjpeg_obj,0,"image_components"));
cinfo.in_color_space =
(int) mxGetScalar(mxGetField(mxjpeg_obj,0,"image_color_space"));
/* set the compression object with default parameters */
jpeg_set_defaults(&cinfo);
cinfo.optimize_coding =
(unsigned char) mxGetScalar(mxGetField(mxjpeg_obj,0,"optimize_coding"));
cinfo.num_components =
(int) mxGetScalar(mxGetField(mxjpeg_obj,0,"jpeg_components"));
cinfo.jpeg_color_space =
(int) mxGetScalar(mxGetField(mxjpeg_obj,0,"jpeg_color_space"));
/* basic support for writing progressive mode JPEG */
if (mxGetField(mxjpeg_obj,0,"progressive_mode")) {
if ((int) mxGetScalar(mxGetField(mxjpeg_obj,0,"progressive_mode")))
jpeg_simple_progression(&cinfo);
}
/* obtain the component array from the jpeg object */
mxcomp_info = mxGetField(mxjpeg_obj,0,"comp_info");
/* copy component information into cinfo from jpeg_obj*/
for (ci = 0; ci < cinfo.num_components; ci++)
{
cinfo.comp_info[ci].component_id =
(int) mxGetScalar(mxGetField(mxcomp_info,ci,"component_id"));
cinfo.comp_info[ci].h_samp_factor =
(int) mxGetScalar(mxGetField(mxcomp_info,ci,"h_samp_factor"));
cinfo.comp_info[ci].v_samp_factor =
(int) mxGetScalar(mxGetField(mxcomp_info,ci,"v_samp_factor"));
cinfo.comp_info[ci].quant_tbl_no =
(int) mxGetScalar(mxGetField(mxcomp_info,ci,"quant_tbl_no"))-1;
cinfo.comp_info[ci].ac_tbl_no =
(int) mxGetScalar(mxGetField(mxcomp_info,ci,"ac_tbl_no"))-1;
cinfo.comp_info[ci].dc_tbl_no =
(int) mxGetScalar(mxGetField(mxcomp_info,ci,"dc_tbl_no"))-1;
}
/* request virtual block arrays */
mxcoef_arrays = mxGetField(mxjpeg_obj, 0, "coef_arrays");
coef_arrays = (jvirt_barray_ptr *)
(cinfo.mem->alloc_small) ((j_common_ptr) &cinfo, JPOOL_IMAGE,
sizeof(jvirt_barray_ptr) * cinfo.num_components);
for (ci = 0; ci < cinfo.num_components; ci++)
{
compptr = cinfo.comp_info + ci;
c_height = mxGetM(mxGetCell(mxcoef_arrays,ci));
c_width = mxGetN(mxGetCell(mxcoef_arrays,ci));
compptr->height_in_blocks = c_height / DCTSIZE;
compptr->width_in_blocks = c_width / DCTSIZE;
coef_arrays[ci] = (cinfo.mem->request_virt_barray)
((j_common_ptr) &cinfo, JPOOL_IMAGE, TRUE,
(JDIMENSION) jround_up((long) compptr->width_in_blocks,
(long) compptr->h_samp_factor),
(JDIMENSION) jround_up((long) compptr->height_in_blocks,
(long) compptr->v_samp_factor),
(JDIMENSION) compptr->v_samp_factor);
}
/* realize virtual block arrays */
jpeg_write_coefficients(&cinfo,coef_arrays);
/* populate the array with the DCT coefficients */
for (ci = 0; ci < cinfo.num_components; ci++)
{
compptr = cinfo.comp_info + ci;
/* Get a pointer to the mx coefficient array */
mxtemp = mxGetCell(mxcoef_arrays,ci);
mp = mxGetPr(mxtemp);
mptop = mp;
c_height = mxGetM(mxtemp);
c_width = mxGetN(mxtemp);
/* Copy coefficients to virtual block arrays */
for (blk_y = 0; blk_y < compptr->height_in_blocks; blk_y++)
{
buffer = (cinfo.mem->access_virt_barray)
((j_common_ptr) &cinfo, coef_arrays[ci], blk_y, 1, TRUE);
for (blk_x = 0; blk_x < compptr->width_in_blocks; blk_x++)
{
bufptr = buffer[0][blk_x];
for (i = 0; i < DCTSIZE; i++) /* for each row in block */
for (j = 0; j < DCTSIZE; j++) /* for each column in block */
bufptr[i*DCTSIZE+j] = (JCOEF) mp[j*c_height+i];
mp+=DCTSIZE*c_height;
}
mp=(mptop+=DCTSIZE);
}
}
/* get the quantization tables */
mxquant_tables = mxGetField(mxjpeg_obj,0,"quant_tables");
for (n = 0; n < mxGetN(mxquant_tables); n++)
{
if (cinfo.quant_tbl_ptrs[n] == NULL)
cinfo.quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) &cinfo);
/* Fill the table */
mxtemp = mxGetCell(mxquant_tables,n);
mp = mxGetPr(mxtemp);
for (i = 0; i < DCTSIZE; i++)
for (j = 0; j < DCTSIZE; j++) {
t = mp[j*DCTSIZE+i];
if (t<1 || t>65535)
mexErrMsgTxt("Quantization table entries not in range 1..65535");
cinfo.quant_tbl_ptrs[n]->quantval[i*DCTSIZE+j] =
(UINT16) t;
}
}
/* set remaining quantization table slots to null */
for (; n < NUM_QUANT_TBLS; n++)
cinfo.quant_tbl_ptrs[n] = NULL;
/* Get the AC and DC huffman tables but check for optimized coding first*/
if (cinfo.optimize_coding == FALSE)
{
if (mxGetField(mxjpeg_obj,0,"ac_huff_tables") != NULL)
{
mxhuff_tables = mxGetField(mxjpeg_obj,0,"ac_huff_tables");
if( mxGetN(mxhuff_tables) > 0)
{
for (n = 0; n < mxGetN(mxhuff_tables); n++)
{
if (cinfo.ac_huff_tbl_ptrs[n] == NULL)
cinfo.ac_huff_tbl_ptrs[n] =
jpeg_alloc_huff_table((j_common_ptr) &cinfo);
mxtemp = mxGetField(mxhuff_tables,n,"counts");
mp = mxGetPr(mxtemp);
for (i = 1; i <= 16; i++)
cinfo.ac_huff_tbl_ptrs[n]->bits[i] = (UINT8) *mp++;
mxtemp = mxGetField(mxhuff_tables,n,"symbols");
mp = mxGetPr(mxtemp);
for (i = 0; i < 256; i++)
cinfo.ac_huff_tbl_ptrs[n]->huffval[i] = (UINT8) *mp++;
}
for (; n < NUM_HUFF_TBLS; n++) cinfo.ac_huff_tbl_ptrs[n] = NULL;
}
}
if (mxGetField(mxjpeg_obj,0, "dc_huff_tables") != NULL)
{
mxhuff_tables = mxGetField(mxjpeg_obj,0,"dc_huff_tables");
if( mxGetN(mxhuff_tables) > 0)
{
for (n = 0; n < mxGetN(mxhuff_tables); n++)
{
if (cinfo.dc_huff_tbl_ptrs[n] == NULL)
cinfo.dc_huff_tbl_ptrs[n] =
jpeg_alloc_huff_table((j_common_ptr) &cinfo);
mxtemp = mxGetField(mxhuff_tables,n,"counts");
mp = mxGetPr(mxtemp);
for (i = 1; i <= 16; i++)
cinfo.dc_huff_tbl_ptrs[n]->bits[i] = (unsigned char) *mp++;
mxtemp = mxGetField(mxhuff_tables,n,"symbols");
mp = mxGetPr(mxtemp);
for (i = 0; i < 256; i++)
cinfo.dc_huff_tbl_ptrs[n]->huffval[i] = (unsigned char) *mp++;
}
for (; n < NUM_HUFF_TBLS; n++) cinfo.dc_huff_tbl_ptrs[n] = NULL;
}
}
}
/* copy markers */
mxcomments = mxGetField(mxjpeg_obj,0,"comments");
n = mxGetN(mxcomments);
for (i = 0; i < n; i++)
{
mxtemp = mxGetCell(mxcomments,i);
strlen = mxGetN(mxtemp) + 1;
comment = mxCalloc(strlen, sizeof(char));
mxGetString(mxtemp,comment,strlen);
jpeg_write_marker(&cinfo, JPEG_COM, comment, strlen-1);
mxFree(comment);
}
/* done with cinfo */
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
/* close the file */
fclose(outfile);
}