1
/* tilp - a linking program for TI graphing calculators
2
* Copyright (C) 1999-2002 Romain Lievin
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
# include <jmorecfg.h>
46
int write_jpg_2_colors(FILE *file, Image *img) // tested: OK (14/05)
49
struct jpeg_compress_struct cinfo;
50
struct jpeg_error_mgr jerr;
53
DISPLAY("write_jpg_2_colors\n");
54
convert_bitmap_to_bytemap(img);
55
if(!(img->inverted)) invert_bytemap(img);
57
// Initialize JPEG library
58
cinfo.err = jpeg_std_error(&jerr);
59
jpeg_create_compress(&cinfo);
60
jpeg_stdio_dest(&cinfo, file);
62
cinfo.image_width = img->width;
63
cinfo.image_height = img->height;
64
cinfo.input_components = 1; // # of color components per pixel
65
cinfo.in_color_space = JCS_GRAYSCALE; // gray scales
67
jpeg_set_defaults(&cinfo);
68
jpeg_set_quality(&cinfo, 100, TRUE); // quality = 100% (better)
69
jpeg_start_compress(&cinfo, TRUE);
71
//fprintf(stdout, "output_scanline: %i\n", cinfo.output_scanline);
73
while(cinfo.next_scanline < cinfo.image_height) // save jpeg image
75
jpeg_write_scanlines(&cinfo, &c, 1);
79
jpeg_finish_compress(&cinfo);
80
jpeg_destroy_compress(&cinfo);
86
int write_jpg_256_colors(FILE *file, Image *img)
88
//fprintf(stderr, "write_jpg_256_colors\n");
90
convert_bytemap_to_rgbmap(img);
91
write_jpg_true_colors(file, img);
96
int write_jpg_true_colors(FILE *file, Image *img)
99
struct jpeg_compress_struct cinfo;
100
struct jpeg_error_mgr jerr;
103
//fprintf(stderr, "write_jpg_true_colors\n");
104
// Initialize JPEG library
105
cinfo.err = jpeg_std_error(&jerr);
106
jpeg_create_compress(&cinfo);
107
jpeg_stdio_dest(&cinfo, file);
109
cinfo.image_width = img->width;
110
cinfo.image_height = img->height;
111
cinfo.input_components = 3; // # of color components per pixel
112
cinfo.in_color_space = JCS_RGB; // rrggbb
114
jpeg_set_defaults(&cinfo);
115
//jpeg_set_quality(&cinfo, quality, TRUE); // base-line
116
jpeg_start_compress(&cinfo, TRUE);
118
//fprintf(stdout, "output_scanline: %i\n", cinfo.output_scanline);
120
while(cinfo.next_scanline < cinfo.image_height) // save jpeg image
122
jpeg_write_scanlines(&cinfo, &c, 1);
126
jpeg_finish_compress(&cinfo);
127
jpeg_destroy_compress(&cinfo);
132
int write_jpg_format(FILE *file, Image *img)
134
DISPLAY("write_jpg_format\n");
135
if(img->encoding == IMG_COL_TYPE)
138
return write_jpg_256_colors(file, img);
140
return write_jpg_true_colors(file, img);
145
return write_jpg_2_colors(file, img);
154
int read_jpg_2_colors(FILE *file, Image *img)
156
//fprintf(stderr, "read_jpg_2_colors\n");
161
int read_jpg_256_colors(FILE *file, Image *img) //tested: OK
164
struct jpeg_decompress_struct cinfo;
165
struct jpeg_error_mgr jerr;
170
//fprintf(stderr, "read_jpg_256_colors\n");
171
// Initialize JPEG library
172
cinfo.err = jpeg_std_error(&jerr);
173
jpeg_create_decompress(&cinfo);
174
jpeg_stdio_src(&cinfo, file);
175
jpeg_read_header(&cinfo, TRUE);
178
//fprintf(stdout, "JPEG image_width: %i\n", cinfo.image_width);
179
//fprintf(stdout, "JPEG image_height: %i\n", cinfo.image_height);
180
img->width = cinfo.image_width;
181
img->height = cinfo.image_height;
183
// Initialize color mapping
184
cinfo.quantize_colors = TRUE; // colormapped output wanted
185
cinfo.desired_number_of_colors = 255; // decrease color depth
187
// Read and decompress image
188
jpeg_start_decompress(&cinfo);
190
fprintf(stdout, "JPEG output_width: %i\n", cinfo.output_width);
191
fprintf(stdout, "JPEG output_height: %i\n", cinfo.output_height);
192
fprintf(stdout, "JPEG output_components: %i\n", cinfo.output_components);
193
fprintf(stdout, "JPEG out_color_components: %i\n", cinfo.out_color_components);
195
fprintf(stdout, "JPEG actual_number_of_colors: %i\n", cinfo.actual_number_of_colors);
196
img->depth = cinfo.actual_number_of_colors;
198
// Copy colormap into palette
199
(img->colormap) = (uint8_t *)malloc(3 * 256 * sizeof(uint8_t));
200
for(j=0; j<cinfo.actual_number_of_colors; j++) // copy the colormap
202
(img->colormap)[3*j+0] = cinfo.colormap[0][j];
203
(img->colormap)[3*j+1] = cinfo.colormap[1][j];
204
(img->colormap)[3*j+2] = cinfo.colormap[2][j];
207
if (cinfo.output_components != 1) // 1: palettized, 3: rrggbb
211
img->bytemap = c = (uint8_t *)malloc(img->width * img->height);
212
if(c == NULL) g_error("Malloc error.\n");
214
//fprintf(stdout, "output_scanline: %i\n", cinfo.output_scanline);
215
while(cinfo.output_scanline < cinfo.output_height) // load jpeg image
218
jpeg_read_scanlines(&cinfo, &c, 1);
220
//fprintf(stdout, "jpeg_start, img->bytemap = %p\n", img->bytemap);
221
//fprintf(stdout, "jpeg_end, img->bytemap = %p\n", c);
223
jpeg_finish_decompress(&cinfo);
224
jpeg_destroy_decompress(&cinfo);
231
int read_jpg_format (FILE *file, Image *img)
235
//fprintf(stderr, "read_jpg_format\n");
236
if(img->encoding == IMG_COL_TYPE)
237
return read_jpg_256_colors(file, img);
239
return read_jpg_2_colors(file, img);