-
Notifications
You must be signed in to change notification settings - Fork 71
/
sqJPEGReadWriter2Plugin.c
314 lines (277 loc) · 9.99 KB
/
sqJPEGReadWriter2Plugin.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
/* sqJPEGReadWriter2Plugin.c
* Cross-platform interface to JPEG processing.
*
* Author: Laura Perez Carrato
*
* Copyright (c) 2013 3D Immersive Collaboration Consulting, LLC.
*
* All rights reserved.
*
* This file is part of Squeak.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "JPEGReadWriter2Plugin.h"
#include <stdlib.h> /* abs */
/*
* For more info regarding what's being done here, download libjpeg 6b from
* http://www.ijg.org/files/ and take a look at example.c
*/
void
primJPEGWriteImageonByteArrayformqualityprogressiveJPEGerrorMgrWriteScanlines(
unsigned int width,
unsigned int height,
int nativeDepth,
unsigned int* bitmap,
char* jpegCompressStruct,
char* jpegErrorMgr2Struct,
int quality,
int progressiveFlag,
unsigned int pixelsPerWord,
unsigned int wordsPerRow,
char* destination,
unsigned int* destinationSizePtr)
{
j_compress_ptr pcinfo = (j_compress_ptr)jpegCompressStruct;
error_ptr2 pjerr = (error_ptr2)jpegErrorMgr2Struct;
pcinfo->err = jpeg_std_error(&pjerr->pub);
pjerr->setjmp_buffer = (jmp_buf *) malloc(sizeof(jmp_buf));
pjerr->pub.error_exit = error_exit;
if (setjmp(*pjerr->setjmp_buffer)) {
jpeg_destroy_compress(pcinfo);
*destinationSizePtr = 0;
}
if (*destinationSizePtr) {
jpeg_create_compress(pcinfo);
jpeg_mem_dest(pcinfo, destination, destinationSizePtr);
pcinfo->image_width = width;
pcinfo->image_height = height;
pcinfo->input_components = (abs(nativeDepth) != 8 ? 3 : 1);
pcinfo->in_color_space = (abs(nativeDepth) != 8 ? JCS_RGB : JCS_GRAYSCALE);
jpeg_set_defaults(pcinfo);
if (quality > 0)
jpeg_set_quality (pcinfo, quality, 1);
if (progressiveFlag)
jpeg_simple_progression(pcinfo);
jpeg_start_compress(pcinfo, TRUE);
unsigned int rowStride = wordsPerRow * pixelsPerWord * pcinfo->input_components;
JSAMPARRAY buffer = (*(pcinfo->mem)->alloc_sarray)
((j_common_ptr) pcinfo, JPOOL_IMAGE, rowStride, 1);
while (pcinfo->next_scanline < pcinfo->image_height) {
unsigned int i;
unsigned int j;
for(i = 0, j = 0; i < rowStride; i +=(pcinfo->input_components * pixelsPerWord), j++) {
unsigned int bitmapWord = bitmap[pcinfo->next_scanline * wordsPerRow + j];
switch (nativeDepth) {
case 32:
case -32:
buffer[0][i] = (bitmapWord >> 16) & 255;
buffer[0][i+1] = (bitmapWord >> 8) & 255;
buffer[0][i+2] = bitmapWord & 255;
break;
case 16:
buffer[0][i] = (bitmapWord >> 23) & 248;
buffer[0][i+1] = (bitmapWord >> 18) & 248;
buffer[0][i+2] = (bitmapWord >> 13) & 248;
buffer[0][i+3] = (bitmapWord >> 7) & 248;
buffer[0][i+4] = (bitmapWord >> 2) & 248;
buffer[0][i+5] = (bitmapWord << 3) & 248;
break;
case -16:
buffer[0][i] = (bitmapWord >> 7) & 248;
buffer[0][i+1] = (bitmapWord >> 2) & 248;
buffer[0][i+2] = (bitmapWord << 3) & 248;
buffer[0][i+3] = (bitmapWord >> 23) & 248;
buffer[0][i+4] = (bitmapWord >> 18) & 248;
buffer[0][i+5] = (bitmapWord >> 13) & 248;
break;
case 8:
buffer[0][i] = (bitmapWord >> 24) & 255;
buffer[0][i+1] = (bitmapWord >> 16) & 255;
buffer[0][i+2] = (bitmapWord >> 8) & 255;
buffer[0][i+3] = bitmapWord & 255;
break;
case -8:
buffer[0][i] = bitmapWord & 255;
buffer[0][i+1] = (bitmapWord >> 8) & 255;
buffer[0][i+2] = (bitmapWord >> 16) & 255;
buffer[0][i+3] = (bitmapWord >> 24) & 255;
break;
}
}
(void) jpeg_write_scanlines(pcinfo, buffer, 1);
}
jpeg_finish_compress(pcinfo);
jpeg_destroy_compress(pcinfo);
}
free(pjerr->setjmp_buffer);
}
void
primJPEGReadImagefromByteArrayonFormdoDitheringerrorMgrReadScanlines(
char* jpegDecompressStruct,
char* jpegErrorMgr2Struct,
char* source,
unsigned int sourceSize,
int ditherFlag,
unsigned int* bitmap,
unsigned int pixelsPerWord,
unsigned int wordsPerRow,
int nativeDepth)
{
j_decompress_ptr pcinfo = (j_decompress_ptr)jpegDecompressStruct;
error_ptr2 pjerr = (error_ptr2)jpegErrorMgr2Struct;
int ok = 1;
pcinfo->err = jpeg_std_error(&pjerr->pub);
pjerr->setjmp_buffer = (jmp_buf *) malloc(sizeof(jmp_buf));
pjerr->pub.error_exit = error_exit;
if (setjmp(*pjerr->setjmp_buffer)) {
jpeg_destroy_decompress(pcinfo);
ok = 0;
}
if (ok)
ok = jpeg_mem_src_newLocationOfData(pcinfo, source, sourceSize);
if (ok) {
jpeg_start_decompress(pcinfo);
int depth = abs(nativeDepth);
unsigned int rowStride = pcinfo->output_width * pcinfo->output_components;
JSAMPARRAY buffer = (*(pcinfo->mem)->alloc_sarray)
((j_common_ptr) pcinfo, JPOOL_IMAGE, rowStride, 1);
int redOffset1, redOffset2;
int greenOffset1, greenOffset2;
int blueOffset1, blueOffset2;
if (pcinfo->out_color_components == 3) {
redOffset1 = 0; redOffset2 = 3;
greenOffset1 = 1; greenOffset2 = 4;
blueOffset1 = 2; blueOffset2 = 5;
}
else {
redOffset1 = 0; redOffset2 = 1;
greenOffset1 = 0; greenOffset2 = 1;
blueOffset1 = 0; blueOffset2 = 1;
}
int grayOffset1 = 0;
int grayOffset2 = 1;
int grayOffset3 = 2;
int grayOffset4 = 3;
// Dither Matrix taken from Form>>orderedDither32To16, but rewritten for this method
int ditherMatrix1[] = { 2, 0, 14, 12, 1, 3, 13, 15 };
int ditherMatrix2[] = { 10, 8, 6, 4, 9, 11, 5, 7 };
while (pcinfo->output_scanline < pcinfo->output_height) {
(void) jpeg_read_scanlines(pcinfo, buffer, 1);
unsigned int i;
unsigned int j;
for(i = 0, j = 0; i < rowStride; i +=(pcinfo->out_color_components * pixelsPerWord), j++) {
unsigned int bitmapWord;
switch (depth) {
case 32: ;
unsigned char red = buffer[0][i+redOffset1];
unsigned char green = buffer[0][i+greenOffset1];
unsigned char blue = buffer[0][i+blueOffset1];
bitmapWord = (255 << 24) | (red << 16) | (green << 8) | blue;
break;
case 16: ;
unsigned char red1 = buffer[0][i+redOffset1];
unsigned char red2 = buffer[0][i+redOffset2];
unsigned char green1 = buffer[0][i+greenOffset1];
unsigned char green2 = buffer[0][i+greenOffset2];
unsigned char blue1 = buffer[0][i+blueOffset1];
unsigned char blue2 = buffer[0][i+blueOffset2];
if (ditherFlag) {
// Do 4x4 ordered dithering. Taken from Form>>orderedDither32To16
int dmv1, dmv2, di, dmi, dmo;
dmv1 = ditherMatrix1[((pcinfo->output_scanline & 3) << 1) | (j&1)];
dmv2 = ditherMatrix2[((pcinfo->output_scanline & 3) << 1) | (j&1)];
di = (red1 * 496) >> 8; dmi = di & 15; dmo = di >> 4;
red1 = dmv1 < dmi ? dmo+1 : dmo;
di = (green1 * 496) >> 8; dmi = di & 15; dmo = di >> 4;
green1 = dmv1 < dmi ? dmo+1 : dmo;
di = (blue1 * 496) >> 8; dmi = di & 15; dmo = di >> 4;
blue1 = dmv1 < dmi ? dmo+1 : dmo;
di = (red2 * 496) >> 8; dmi = di & 15; dmo = di >> 4;
red2 = dmv2 < dmi ? dmo+1 : dmo;
di = (green2 * 496) >> 8; dmi = di & 15; dmo = di >> 4;
green2 = dmv2 < dmi ? dmo+1 : dmo;
di = (blue2 * 496) >> 8; dmi = di & 15; dmo = di >> 4;
blue2 = dmv2 < dmi ? dmo+1 : dmo;
}
else {
red1 = red1 >> 3;
red2 = red2 >> 3;
green1 = green1 >> 3;
green2 = green2 >> 3;
blue1 = blue1 >> 3;
blue2 = blue2 >> 3;
}
switch (nativeDepth) {
case 16:
bitmapWord = 32768 | (red1 << 10) | (green1 << 5) | blue1;
bitmapWord = (bitmapWord << 16) | 32768 | (red2 << 10) | (green2 << 5) | blue2;
break;
case -16:
bitmapWord = 32768 | (red2 << 10) | (green2 << 5) | blue2;
bitmapWord = (bitmapWord << 16) | 32768 | (red1 << 10) | (green1 << 5) | blue1;
break;
}
break;
case 8: ;
unsigned char gray1 = buffer[0][i+grayOffset1];
unsigned char gray2 = buffer[0][i+grayOffset2];
unsigned char gray3 = buffer[0][i+grayOffset3];
unsigned char gray4 = buffer[0][i+grayOffset4];
switch (nativeDepth) {
case 8:
bitmapWord = gray1 << 24 | gray2 << 16 | gray3 << 8 | gray4;
break;
case -8:
bitmapWord = gray4 << 24 | gray3 << 16 | gray2 << 8 | gray1;
break;
}
break;
}
bitmap[((pcinfo->output_scanline - 1) * wordsPerRow) + j] = bitmapWord;
}
}
jpeg_finish_decompress(pcinfo);
jpeg_destroy_decompress(pcinfo);
}
free(pjerr->setjmp_buffer);
}
void
primJPEGReadHeaderfromByteArraysizeerrorMgrReadHeader(
char* jpegDecompressStruct,
char* source,
unsigned int sourceSize,
char* jpegErrorMgr2Struct)
{
j_decompress_ptr pcinfo = (j_decompress_ptr)jpegDecompressStruct;
error_ptr2 pjerr = (error_ptr2)jpegErrorMgr2Struct;
pcinfo->err = jpeg_std_error(&pjerr->pub);
pjerr->setjmp_buffer = (jmp_buf *) malloc(sizeof(jmp_buf));
pjerr->pub.error_exit = error_exit;
if (setjmp(*pjerr->setjmp_buffer)) {
jpeg_destroy_decompress(pcinfo);
sourceSize = 0;
}
if (sourceSize) {
jpeg_create_decompress(pcinfo);
jpeg_mem_src(pcinfo, source, sourceSize);
jpeg_read_header(pcinfo, TRUE);
}
free(pjerr->setjmp_buffer);
}