-
Notifications
You must be signed in to change notification settings - Fork 10
/
conv_shared.cpp
345 lines (280 loc) · 11.9 KB
/
conv_shared.cpp
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
/* -*- C++ -*-
*
* conv_shared.cpp - Shared code of sarconv and nsaconv
*
* Copyright (c) 2001-2006 Ogapee. All rights reserved.
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern "C"{
#include <jpeglib.h>
};
#include <bzlib.h>
#include "resize_image.h"
int scale_ratio_upper;
int scale_ratio_lower;
unsigned char *rescaled_tmp2_buffer = NULL;
size_t rescaled_tmp2_length = 0;
unsigned char *rescaled_tmp_buffer = NULL;
size_t rescaled_tmp_length = 0;
static unsigned char *restored_buffer = NULL;
static size_t restored_length = 0;
#define INPUT_BUFFER_SIZE 4096
typedef struct {
struct jpeg_source_mgr pub;
unsigned char *buf;
size_t left;
} my_source_mgr;
typedef struct {
struct jpeg_destination_mgr pub;
unsigned char *buf;
size_t left;
} my_destination_mgr;
void rescaleImage( unsigned char *original_buffer, int width, int height, int byte_per_pixel,
bool src_pad_flag, bool dst_pad_flag, bool palette_flag )
{
size_t width_pad = 0;
if ( src_pad_flag ) width_pad = (4 - width * byte_per_pixel % 4) % 4;
size_t w = (int)(width * scale_ratio_upper / scale_ratio_lower);
size_t h = (int)(height * scale_ratio_upper / scale_ratio_lower);
if ( w==0 ) w=1;
if ( h==0 ) h=1;
size_t w_pad = 0;
if ( dst_pad_flag ) w_pad = (4 - w * byte_per_pixel % 4) % 4;
if ( (w * byte_per_pixel + w_pad) * h > rescaled_tmp_length ){
int len = (w * byte_per_pixel + w_pad) * h;
if ( rescaled_tmp_buffer ) delete[] rescaled_tmp_buffer;
rescaled_tmp_buffer = new unsigned char[ len ];
rescaled_tmp_length = len;
}
size_t len = (width * byte_per_pixel + width_pad) * (height+1) + byte_per_pixel;
if ( len<16 ) len = 16;
if ( len > rescaled_tmp2_length ){
if ( rescaled_tmp2_buffer ) delete[] rescaled_tmp2_buffer;
rescaled_tmp2_buffer = new unsigned char[ len ];
rescaled_tmp2_length = len;
}
resizeImage( rescaled_tmp_buffer, w, h, w*byte_per_pixel+w_pad,
original_buffer, width, height, width*byte_per_pixel+width_pad,
byte_per_pixel, rescaled_tmp2_buffer, width*byte_per_pixel+width_pad, palette_flag );
}
void init_source (j_decompress_ptr cinfo)
{
}
boolean fill_input_buffer (j_decompress_ptr cinfo)
{
my_source_mgr *src = (my_source_mgr *)cinfo->src;
src->pub.next_input_byte = src->buf;
src->pub.bytes_in_buffer = src->left;
return TRUE;
}
void skip_input_data (j_decompress_ptr cinfo, long num_bytes)
{
my_source_mgr *src = (my_source_mgr *)cinfo->src;
src->pub.next_input_byte += (size_t) num_bytes;
src->pub.bytes_in_buffer -= (size_t) num_bytes;
}
void term_source (j_decompress_ptr cinfo)
{
}
void init_destination (j_compress_ptr cinfo)
{
my_destination_mgr * dest = (my_destination_mgr *) cinfo->dest;
dest->pub.next_output_byte = dest->buf;
dest->pub.free_in_buffer = dest->left;
}
boolean empty_output_buffer (j_compress_ptr cinfo)
{
my_destination_mgr * dest = (my_destination_mgr *) cinfo->dest;
dest->pub.next_output_byte = dest->buf;
dest->pub.free_in_buffer = dest->left;
return TRUE;
}
void term_destination (j_compress_ptr cinfo)
{
}
size_t rescaleJPEGWrite( unsigned int width, unsigned int height, int byte_per_pixel, unsigned char **rescaled_buffer,
int quality, bool bmp2jpeg_flag )
{
jpeg_error_mgr jerr;
struct jpeg_compress_struct cinfo2;
JSAMPROW row_pointer[1];
cinfo2.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo2);
cinfo2.dest = (struct jpeg_destination_mgr *)
(*cinfo2.mem->alloc_small) ((j_common_ptr) &cinfo2, JPOOL_PERMANENT,
sizeof(my_destination_mgr));
my_destination_mgr * dest = (my_destination_mgr *) cinfo2.dest;
dest->buf = *rescaled_buffer;
dest->left = restored_length;
dest->pub.init_destination = init_destination;
dest->pub.empty_output_buffer = empty_output_buffer;
dest->pub.term_destination = term_destination;
cinfo2.image_width = (int)(width * scale_ratio_upper / scale_ratio_lower);
if ( cinfo2.image_width == 0 ) cinfo2.image_width = 1;
cinfo2.image_height = (int)(height * scale_ratio_upper / scale_ratio_lower);
if ( cinfo2.image_height == 0 ) cinfo2.image_height = 1;
cinfo2.input_components = byte_per_pixel;
if ( cinfo2.input_components == 1 )
cinfo2.in_color_space = JCS_GRAYSCALE;
else
cinfo2.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo2);
jpeg_set_quality(&cinfo2, quality, TRUE );
cinfo2.optimize_coding = TRUE;
//jpeg_simple_progression (&cinfo2);
jpeg_start_compress(&cinfo2, TRUE);
int row_stride = cinfo2.image_width * byte_per_pixel;
while (cinfo2.next_scanline < cinfo2.image_height) {
if (bmp2jpeg_flag){
unsigned char *src = row_pointer[0] = &rescaled_tmp_buffer[(cinfo2.image_height - 1 - cinfo2.next_scanline) * row_stride];
for(unsigned int i=0 ; i<cinfo2.image_width ; i++, src+=3){
unsigned char tmp = src[2];
src[2] = src[0];
src[0] = tmp;
}
}
else{
row_pointer[0] = &rescaled_tmp_buffer[cinfo2.next_scanline * row_stride];
}
jpeg_write_scanlines(&cinfo2, row_pointer, 1);
}
jpeg_finish_compress(&cinfo2);
size_t datacount = dest->left - dest->pub.free_in_buffer;
jpeg_destroy_compress(&cinfo2);
return datacount;
}
size_t rescaleJPEG( unsigned char *original_buffer, size_t length, unsigned char **rescaled_buffer, int quality )
{
struct jpeg_decompress_struct cinfo;
jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
cinfo.src = (struct jpeg_source_mgr *)
(*cinfo.mem->alloc_small) ((j_common_ptr) &cinfo, JPOOL_PERMANENT,
sizeof(my_source_mgr));
my_source_mgr * src = (my_source_mgr *) cinfo.src;
src->buf = original_buffer;
src->left = length;
src->pub.init_source = init_source;
src->pub.fill_input_buffer = fill_input_buffer;
src->pub.skip_input_data = skip_input_data;
src->pub.resync_to_restart = jpeg_resync_to_restart;
src->pub.term_source = term_source;
src->pub.bytes_in_buffer = 0;
src->pub.next_input_byte = NULL;
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
if ( cinfo.output_width * cinfo.output_height * cinfo.output_components + 0x400 > restored_length ){
restored_length = cinfo.output_width * cinfo.output_height * cinfo.output_components + 0x400;
if ( restored_buffer ) delete[] restored_buffer;
restored_buffer = new unsigned char[ restored_length ];
if ( *rescaled_buffer ) delete[] *rescaled_buffer;
*rescaled_buffer = new unsigned char[ restored_length ];
}
int row_stride = cinfo.output_width * cinfo.output_components;
JSAMPARRAY buf = (*cinfo.mem->alloc_sarray)
((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
unsigned char *buf_p = restored_buffer;
while (cinfo.output_scanline < cinfo.output_height) {
jpeg_read_scanlines(&cinfo, buf, 1);
memcpy( buf_p, buf[0], row_stride );
buf_p += cinfo.output_width * cinfo.output_components;
}
rescaleImage( restored_buffer, cinfo.output_width, cinfo.output_height, cinfo.output_components, false, false, false );
size_t datacount = rescaleJPEGWrite(cinfo.output_width, cinfo.output_height, cinfo.output_components, rescaled_buffer, quality, false);
jpeg_destroy_decompress(&cinfo);
return datacount;
}
void rescaleBMPWrite( unsigned char *original_buffer, size_t total_size, int width, int height, unsigned char **rescaled_buffer )
{
int buffer_offset = original_buffer[10] + (original_buffer[11] << 8);
memcpy( *rescaled_buffer, original_buffer, buffer_offset );
memcpy( *rescaled_buffer + buffer_offset, rescaled_tmp_buffer, total_size - buffer_offset );
*(*rescaled_buffer + 2) = total_size & 0xff;
*(*rescaled_buffer + 3) = (total_size >> 8) & 0xff;
*(*rescaled_buffer + 4) = (total_size >> 16) & 0xff;
*(*rescaled_buffer + 5) = (total_size >> 24) & 0xff;
*(*rescaled_buffer + 18) = width & 0xff;
*(*rescaled_buffer + 19) = (width >> 8) & 0xff;
*(*rescaled_buffer + 20) = (width >> 16) & 0xff;
*(*rescaled_buffer + 21) = (width >> 24) & 0xff;
*(*rescaled_buffer + 22) = height & 0xff;
*(*rescaled_buffer + 23) = (height >> 8) & 0xff;
*(*rescaled_buffer + 24) = (height >> 16) & 0xff;
*(*rescaled_buffer + 25) = (height >> 24) & 0xff;
*(*rescaled_buffer + 34) = 0;
*(*rescaled_buffer + 35) = 0;
*(*rescaled_buffer + 36) = 0;
*(*rescaled_buffer + 37) = 0;
#if 0
FILE *fp = fopen( "test.bmp", "wb" );
fwrite( *rescaled_buffer, 1, width2 * height2 * byte_per_pixel + 54 + color_num*4, fp );
fclose(fp);
getchar();
#endif
}
size_t rescaleBMP( unsigned char *original_buffer, unsigned char **rescaled_buffer,
bool output_jpeg_flag, int quality )
{
if (original_buffer[14] != 40){
if (original_buffer[14] == 12)
fprintf( stderr, " OS/2 format is not supported.\n");
else
fprintf( stderr, " this bitmap can't be handled.\n");
exit(-1);
}
int buffer_offset = original_buffer[10] + (original_buffer[11] << 8);
int width = original_buffer[18] + (original_buffer[19] << 8);
int height = original_buffer[22] + (original_buffer[23] << 8);
int bit_per_pixel = original_buffer[28];
if (bit_per_pixel == 1 || bit_per_pixel == 4){
fprintf( stderr, " bit_per_pixel %d is not supported.\n", bit_per_pixel);
exit(-1);
}
int byte_per_pixel = bit_per_pixel / 8;
int color_num = original_buffer[46] + ((int)original_buffer[47] << 8) + (original_buffer[48] << 16) + (original_buffer[49] << 24);
if (bit_per_pixel == 8 && color_num == 0) color_num = 256;
bool palette_flag = false;
if (bit_per_pixel == 8) palette_flag = true;
if (palette_flag) output_jpeg_flag = false;
size_t width2 = (int)(width * scale_ratio_upper / scale_ratio_lower);
if ( width2 == 0 ) width2 = 1;
size_t width2_pad = (4 - width2 * byte_per_pixel % 4) % 4;
size_t height2 = (int)(height * scale_ratio_upper / scale_ratio_lower);
if ( height2 == 0 ) height2 = 1;
size_t total_size = (width2 * byte_per_pixel + width2_pad) * height2 + buffer_offset;
if ( total_size+0x400 > restored_length ){
restored_length = total_size+0x400;
if ( restored_buffer ) delete[] restored_buffer;
restored_buffer = new unsigned char[ restored_length ];
if ( *rescaled_buffer ) delete[] *rescaled_buffer;
*rescaled_buffer = new unsigned char[ restored_length ];
}
if (output_jpeg_flag){
rescaleImage( original_buffer+buffer_offset, width, height, byte_per_pixel, true, false, palette_flag );
total_size = rescaleJPEGWrite(width, height, byte_per_pixel, rescaled_buffer, quality, true);
}
else {
rescaleImage( original_buffer+buffer_offset, width, height, byte_per_pixel, true, true, palette_flag );
rescaleBMPWrite(original_buffer, total_size, width2, height2, rescaled_buffer);
}
return total_size;
}