~ubuntu-branches/ubuntu/karmic/tilp/karmic

« back to all changes in this revision

Viewing changes to src/img/jpg.c

  • Committer: Bazaar Package Importer
  • Author(s): Julien BLACHE
  • Date: 2002-04-22 14:08:55 UTC
  • Revision ID: james.westby@ubuntu.com-20020422140855-f2uqleap86io68xy
Tags: upstream-5.03
ImportĀ upstreamĀ versionĀ 5.03

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  tilp - a linking program for TI graphing calculators
 
2
 *  Copyright (C) 1999-2002  Romain Lievin
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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.
 
17
 */
 
18
/*
 
19
#ifdef __WIN32__
 
20
# define HAVE_LIBJPEG
 
21
# include <stddef.h>
 
22
# include <jmorecfg.h>
 
23
# include <jpeglib.h>
 
24
#endif
 
25
*/
 
26
 
 
27
#ifdef HAVE_CONFIG_H
 
28
#include <config.h>
 
29
#endif
 
30
#include <stdio.h>
 
31
#include <stdlib.h>
 
32
#include <string.h>
 
33
#include <glib.h>
 
34
#ifdef HAVE_LIBJPEG
 
35
# include <jpeglib.h>
 
36
#endif
 
37
 
 
38
#include "img_fmt.h"
 
39
#include "tilibs.h"
 
40
 
 
41
 
 
42
/***********/
 
43
/* Writing */
 
44
/***********/
 
45
 
 
46
int write_jpg_2_colors(FILE *file, Image *img) // tested: OK (14/05)
 
47
{
 
48
#ifdef HAVE_LIBJPEG
 
49
  struct jpeg_compress_struct cinfo;
 
50
  struct jpeg_error_mgr jerr;
 
51
  unsigned char *c;
 
52
 
 
53
  DISPLAY("write_jpg_2_colors\n");
 
54
  convert_bitmap_to_bytemap(img);
 
55
  if(!(img->inverted)) invert_bytemap(img);
 
56
  // Initialize JPEG library
 
57
  cinfo.err = jpeg_std_error(&jerr);
 
58
  jpeg_create_compress(&cinfo);
 
59
  jpeg_stdio_dest(&cinfo, file);
 
60
  
 
61
  cinfo.image_width = img->width;
 
62
  cinfo.image_height = img->height;
 
63
  cinfo.input_components = 1; // # of color components per pixel
 
64
  cinfo.in_color_space = JCS_GRAYSCALE; // gray scales
 
65
 
 
66
  jpeg_set_defaults(&cinfo);
 
67
  jpeg_set_quality(&cinfo, 100, TRUE); // quality = 100% (better)
 
68
  jpeg_start_compress(&cinfo, TRUE);
 
69
  
 
70
  //fprintf(stdout, "output_scanline: %i\n", cinfo.output_scanline);
 
71
  c = img->bytemap;
 
72
  while(cinfo.next_scanline < cinfo.image_height) // save jpeg image
 
73
    {
 
74
      jpeg_write_scanlines(&cinfo, &c, 1);
 
75
      c += (img->width);
 
76
    }
 
77
 
 
78
  jpeg_finish_compress(&cinfo);
 
79
  jpeg_destroy_compress(&cinfo);
 
80
#endif
 
81
    
 
82
  return 0;
 
83
}
 
84
 
 
85
int write_jpg_256_colors(FILE *file, Image *img)
 
86
{
 
87
  //fprintf(stderr, "write_jpg_256_colors\n");
 
88
 
 
89
  convert_bytemap_to_rgbmap(img);
 
90
  write_jpg_true_colors(file, img);
 
91
 
 
92
  return 0;
 
93
}
 
94
 
 
95
int write_jpg_true_colors(FILE *file, Image *img)
 
96
{
 
97
#ifdef HAVE_LIBJPEG
 
98
  struct jpeg_compress_struct cinfo;
 
99
  struct jpeg_error_mgr jerr;
 
100
  unsigned char *c;
 
101
 
 
102
  //fprintf(stderr, "write_jpg_true_colors\n");
 
103
  // Initialize JPEG library
 
104
  cinfo.err = jpeg_std_error(&jerr);
 
105
  jpeg_create_compress(&cinfo);
 
106
  jpeg_stdio_dest(&cinfo, file);
 
107
  
 
108
  cinfo.image_width = img->width;
 
109
  cinfo.image_height = img->height;
 
110
  cinfo.input_components = 3; // # of color components per pixel
 
111
  cinfo.in_color_space = JCS_RGB; // rrggbb
 
112
 
 
113
  jpeg_set_defaults(&cinfo);
 
114
  //jpeg_set_quality(&cinfo, quality, TRUE); // base-line
 
115
  jpeg_start_compress(&cinfo, TRUE);
 
116
  
 
117
  //fprintf(stdout, "output_scanline: %i\n", cinfo.output_scanline);
 
118
  c = img->rgbmap;
 
119
  while(cinfo.next_scanline < cinfo.image_height) // save jpeg image
 
120
    {
 
121
      jpeg_write_scanlines(&cinfo, &c, 1);
 
122
      c += 3*(img->width);
 
123
    }
 
124
 
 
125
  jpeg_finish_compress(&cinfo);
 
126
  jpeg_destroy_compress(&cinfo);
 
127
#endif
 
128
  return 0;
 
129
}
 
130
 
 
131
int write_jpg_format(FILE *file, Image *img)
 
132
{
 
133
  DISPLAY("write_jpg_format\n");
 
134
  if(img->encoding == IMG_COL_TYPE)
 
135
    {
 
136
      if(img->depth < 257)
 
137
        return write_jpg_256_colors(file, img);
 
138
      else
 
139
        return write_jpg_true_colors(file, img);
 
140
    }
 
141
  else
 
142
    return write_jpg_2_colors(file, img);
 
143
}
 
144
 
 
145
 
 
146
/***********/
 
147
/* Reading */
 
148
/***********/
 
149
 
 
150
int read_jpg_2_colors(FILE *file, Image *img)
 
151
{
 
152
  //fprintf(stderr, "read_jpg_2_colors\n");
 
153
 
 
154
  return 0;
 
155
}
 
156
 
 
157
int read_jpg_256_colors(FILE *file, Image *img) //tested: OK
 
158
{
 
159
#ifdef HAVE_LIBJPEG
 
160
  struct jpeg_decompress_struct cinfo;
 
161
  struct jpeg_error_mgr jerr;
 
162
  unsigned char *c;
 
163
  //int x, y;
 
164
  int j; 
 
165
 
 
166
  //fprintf(stderr, "read_jpg_256_colors\n");
 
167
  // Initialize JPEG library
 
168
  cinfo.err = jpeg_std_error(&jerr);
 
169
  jpeg_create_decompress(&cinfo);
 
170
  jpeg_stdio_src(&cinfo, file);
 
171
  jpeg_read_header(&cinfo, TRUE);
 
172
 
 
173
  // Get image size
 
174
  //fprintf(stdout, "JPEG image_width: %i\n", cinfo.image_width);
 
175
  //fprintf(stdout, "JPEG image_height: %i\n", cinfo.image_height);
 
176
  img->width = cinfo.image_width;
 
177
  img->height = cinfo.image_height;
 
178
 
 
179
  // Initialize color mapping
 
180
  cinfo.quantize_colors = TRUE; // colormapped output wanted
 
181
  cinfo.desired_number_of_colors = 255; // decrease color depth
 
182
 
 
183
  // Read and decompress image
 
184
  jpeg_start_decompress(&cinfo);
 
185
 
 
186
  fprintf(stdout, "JPEG output_width: %i\n", cinfo.output_width);
 
187
  fprintf(stdout, "JPEG output_height: %i\n", cinfo.output_height);
 
188
  fprintf(stdout, "JPEG output_components: %i\n", cinfo.output_components);
 
189
  fprintf(stdout, "JPEG out_color_components: %i\n", cinfo.out_color_components);
 
190
 
 
191
  fprintf(stdout, "JPEG actual_number_of_colors: %i\n", cinfo.actual_number_of_colors);
 
192
  img->depth = cinfo.actual_number_of_colors;
 
193
 
 
194
  // Copy colormap into palette
 
195
  (img->colormap) = (byte *)malloc(3 * 256 * sizeof(byte));
 
196
  for(j=0; j<cinfo.actual_number_of_colors; j++) // copy the colormap
 
197
    {
 
198
      (img->colormap)[3*j+0] = cinfo.colormap[0][j];
 
199
      (img->colormap)[3*j+1] = cinfo.colormap[1][j];
 
200
      (img->colormap)[3*j+2] = cinfo.colormap[2][j];
 
201
    }
 
202
 
 
203
  if (cinfo.output_components != 1) // 1: palettized, 3: rrggbb
 
204
    return -1;
 
205
 
 
206
  // Allocate image
 
207
  img->bytemap = c = (byte *)malloc(img->width * img->height);
 
208
  if(c == NULL) g_error("Malloc error.\n");
 
209
 
 
210
  //fprintf(stdout, "output_scanline: %i\n", cinfo.output_scanline);
 
211
  while(cinfo.output_scanline < cinfo.output_height) // load jpeg image
 
212
    {
 
213
      c += img->width;
 
214
      jpeg_read_scanlines(&cinfo, &c, 1);
 
215
    }
 
216
  //fprintf(stdout, "jpeg_start, img->bytemap = %p\n", img->bytemap);
 
217
  //fprintf(stdout, "jpeg_end, img->bytemap = %p\n", c);
 
218
 
 
219
  jpeg_finish_decompress(&cinfo);
 
220
  jpeg_destroy_decompress(&cinfo);
 
221
 
 
222
 
 
223
#endif
 
224
  return 0;
 
225
}
 
226
 
 
227
int read_jpg_format (FILE *file, Image *img)
 
228
{
 
229
  //fprintf(stderr, "read_jpg_format\n");
 
230
  if(img->encoding == IMG_COL_TYPE)
 
231
    return read_jpg_256_colors(file, img);
 
232
  else
 
233
    return read_jpg_2_colors(file, img);
 
234
}