1 /* Panorama_Tools - Generate, Edit and Convert Panoramic Images
2 Copyright (C) 1998,1999 - Helmut Dersch der@fh-furtwangen.de
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, or (at your option)
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /*------------------------------------------------------------*/
27 int writeJPEG( Image *im, fullPath *sfile, int quality, int progressive )
29 struct jpeg_compress_struct cinfo;
30 struct jpeg_error_mgr jerr;
33 int scanlines_written;
34 unsigned char *data,*buf;
36 cinfo.err = jpeg_std_error(&jerr);
37 jpeg_create_compress(&cinfo);
39 // if( GetFullPath (sfile, filename))
41 strcpy(filename, sfile->name);
42 if ((outfile = fopen(filename, "wb")) == NULL)
44 PrintError("can't open %s", filename);
47 // TwoToOneByte( im );
49 jpeg_stdio_dest(&cinfo, outfile);
53 cinfo.image_width = im->width; /* image width and height, in pixels */
54 cinfo.image_height = im->height;
55 cinfo.input_components = 3; /* # of color components per pixel */
56 cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
58 jpeg_set_defaults(&cinfo);
60 jpeg_set_quality (&cinfo, quality, TRUE);
63 jpeg_simple_progression (&cinfo);
66 jpeg_start_compress(&cinfo, TRUE);
68 scanlines_written = 0;
70 buf = (unsigned char*)malloc( im->bytesPerLine );
74 PrintError("Not enough memory");
80 while ( scanlines_written < im->height )
82 memcpy(buf, data, im->bytesPerLine );
83 if( im->bitsPerPixel == 32 ) // Convert 4->3 samples
86 unsigned char *c1=buf, *c2=buf;
87 for(x=0; x < im->width; x++)
97 if( jpeg_write_scanlines(&cinfo, (JSAMPARRAY) &buf, 1) )
100 data += im->bytesPerLine;
103 jpeg_finish_compress(&cinfo);
104 jpeg_destroy_compress(&cinfo);
114 int readJPEG ( Image *im, fullPath *sfile )
116 struct jpeg_decompress_struct cinfo;
117 struct jpeg_error_mgr jerr;
120 int scan_lines_to_be_read, i, scanheight, scan_lines_read;
124 //PrintError("%s", sfile->name);
126 cinfo.err = jpeg_std_error(&jerr);
127 jpeg_create_decompress(&cinfo);
130 // if( GetFullPath (sfile, filename))
133 strcpy(filename, sfile->name);
134 if ((infile = fopen(filename, "rb")) == NULL)
136 PrintError("can't open %s", filename);
140 jpeg_stdio_src(&cinfo, infile);
142 jpeg_read_header(&cinfo, TRUE);
144 jpeg_start_decompress(&cinfo);
146 SetImageDefaults( im );
147 im->width = cinfo.output_width;
148 im->height = cinfo.output_height;
149 if( cinfo.output_components != 3 )
151 PrintError("Image must be rgb");
157 im->bitsPerPixel = 24;
158 im->bytesPerLine = im->width*3 ;
159 //im->bytesPerLine *= 12;
160 im->dataSize = im->width * 4 * im->height;
161 im->data = (unsigned char**)mymalloc( im->dataSize );
162 if( im->data == NULL )
164 PrintError("Not enough memory");
169 scanheight = cinfo.rec_outbuf_height;
170 sarray = (JSAMPARRAY) malloc( scanheight * sizeof( JSAMPROW ) );
172 scan_lines_to_be_read = im->height;
175 while (scan_lines_to_be_read)
177 for(i=0; i<scanheight; i++)
179 sarray[i] = (JSAMPROW) (data + i*im->bytesPerLine);
182 scan_lines_read = jpeg_read_scanlines(&cinfo, sarray, scanheight);
184 scan_lines_to_be_read -= scan_lines_read;
185 data += scan_lines_read * im->bytesPerLine;
187 jpeg_finish_decompress(&cinfo);
188 jpeg_destroy_decompress(&cinfo);
190 ThreeToFourBPP( im );