The R Project SVN R

Rev

Rev 8958 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 8958 Rev 10960
1
/*
1
/*
2
 *  R : A Computer Language for Statistical Data Analysis
2
 *  R : A Computer Language for Statistical Data Analysis
3
 *  Copyright (C) 1999  Guido Masarotto and the R Development Core Team
3
 *  Copyright (C) 1999  Guido Masarotto and the R Development Core Team
4
 *
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation; either version 2 of the License, or
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
8
 *  (at your option) any later version.
9
 *
9
 *
10
 *  This program is distributed in the hope that it will be useful,
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
13
 *  GNU General Public License for more details.
14
 *
14
 *
15
 *  You should have received a copy of the GNU General Public License
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
18
 */
19
 
19
 
20
 
20
 
21
/* 
21
/* 
22
 * This file aims to be system independent so it sees the underlying
22
 * This file aims to be system independent so it sees the underlying
23
 * structures only using:
23
 * structures only using:
24
 * void *d : an 'opaque' view of the source of the pixels;
24
 * void *d : an 'opaque' view of the source of the pixels;
25
 * int width, height: dimensions in pixels;
25
 * int width, height: dimensions in pixels;
26
 * unsigned long (*gp)(void *d, int x, int y): a function which
26
 * unsigned long (*gp)(void *d, int x, int y): a function which
27
 *     returns the colour of the (x,y) pixels stored either as
27
 *     returns the colour of the (x,y) pixels stored either as
28
 *     BGR (R model, see include/Graphics.h) or as RGB in the
28
 *     BGR (R model, see include/Graphics.h) or as RGB in the
29
 *     24 least sig. bits (8 bit for channel).
29
 *     24 least sig. bits (8 bit for channel).
30
 *     (0,0) is the left-top corner. (3,2) is the second pixel 
30
 *     (0,0) is the left-top corner. (3,2) is the second pixel 
31
 *     in the fourth scanline.
31
 *     in the fourth scanline.
32
 * int bgr: if != 0, order is BGR else is RGB.
32
 * int bgr: if != 0, order is BGR else is RGB.
33
 * int quality: only for jpeg (0-100 measure of how much to compress).
33
 * int quality: only for jpeg (0-100 measure of how much to compress).
34
 * FILE * fp is the destination. 
34
 * FILE * fp is the destination. 
35
 * 
35
 * 
36
 */
36
 */
37
 
37
 
38
#include <stdio.h>
38
#include <stdio.h>
39
#include <stdlib.h>
39
#include <stdlib.h>
40
#include <setjmp.h>
40
#include <setjmp.h>
41
 
41
 
42
/* 8 bits red, green and blue channel */
42
/* 8 bits red, green and blue channel */
43
#define DECLARESHIFTS int RSHIFT=(bgr)?0:16, GSHIFT=8, BSHIFT=(bgr)?16:0
43
#define DECLARESHIFTS int RSHIFT=(bgr)?0:16, GSHIFT=8, BSHIFT=(bgr)?16:0
44
#define GETRED(col)    (((col) >> RSHIFT) & 0xFFUL)
44
#define GETRED(col)    (((col) >> RSHIFT) & 0xFFUL)
45
#define GETGREEN(col)  (((col) >> GSHIFT) & 0xFFUL)
45
#define GETGREEN(col)  (((col) >> GSHIFT) & 0xFFUL)
46
#define GETBLUE(col)   (((col) >> BSHIFT) & 0xFFUL)
46
#define GETBLUE(col)   (((col) >> BSHIFT) & 0xFFUL)
47
 
47
 
48
void R_ShowMessage(char *msg);
48
void R_ShowMessage(char *msg);
49
#include "Error.h"
49
#include "R_ext/Error.h"
50
 
50
 
51
#ifdef HAVE_PNG
51
#ifdef HAVE_PNG
52
 
52
 
53
#include "png.h"
53
#include "png.h"
54
/* 
54
/* 
55
 * Try to save the content of the device 'd' in 'filename' as png.
55
 * Try to save the content of the device 'd' in 'filename' as png.
56
 * If numbers of colors is less than 256 we use a 'palette' png.
56
 * If numbers of colors is less than 256 we use a 'palette' png.
57
 * Return 1 on success, 0 on failure 
57
 * Return 1 on success, 0 on failure 
58
*/
58
*/
59
 
59
 
60
/*  
60
/*  
61
    I don't use 'error' since (1) we must free 'scanline' and 
61
    I don't use 'error' since (1) we must free 'scanline' and 
62
   (2) we can be arrived here from a button or menuitem callback maybe
62
   (2) we can be arrived here from a button or menuitem callback maybe
63
   in a different thread from the one where R runs.
63
   in a different thread from the one where R runs.
64
*/ 
64
*/ 
65
static void my_png_error(png_structp png_ptr, png_const_charp msg) 
65
static void my_png_error(png_structp png_ptr, png_const_charp msg) 
66
{
66
{
67
  R_ShowMessage((char *) msg);
67
  R_ShowMessage((char *) msg);
68
  longjmp(png_ptr->jmpbuf,1);
68
  longjmp(png_ptr->jmpbuf,1);
69
}
69
}
70
 
70
 
71
static void my_png_warning(png_structp png_ptr, png_const_charp msg) 
71
static void my_png_warning(png_structp png_ptr, png_const_charp msg) 
72
{
72
{
73
  warning("libpng: %s",(char *) msg);
73
  warning("libpng: %s",(char *) msg);
74
}
74
}
75
 
75
 
76
int R_SaveAsPng(void  *d, int width, int height, 
76
int R_SaveAsPng(void  *d, int width, int height, 
77
		unsigned long (*gp)(void *, int, int),
77
		unsigned long (*gp)(void *, int, int),
78
		int bgr, FILE *fp) 
78
		int bgr, FILE *fp) 
79
{
79
{
80
  png_structp png_ptr;
80
  png_structp png_ptr;
81
  png_infop info_ptr;
81
  png_infop info_ptr;
82
  unsigned long  col, palette[256];
82
  unsigned long  col, palette[256];
83
  png_color pngpalette[256];
83
  png_color pngpalette[256];
84
  png_bytep pscanline, scanline = calloc(3*width,sizeof(png_byte));
84
  png_bytep pscanline, scanline = calloc(3*width,sizeof(png_byte));
85
  int i, j, r, ncols, mid, high, low, withpalette;
85
  int i, j, r, ncols, mid, high, low, withpalette;
86
  DECLARESHIFTS;
86
  DECLARESHIFTS;
87
 
87
 
88
  /* Have we enough memory?*/
88
  /* Have we enough memory?*/
89
  if (scanline == NULL) 
89
  if (scanline == NULL) 
90
    return 0;
90
    return 0;
91
 
91
 
92
  /* Create and initialize the png_struct with the desired error handler
92
  /* Create and initialize the png_struct with the desired error handler
93
   * functions.  If you want to use the default stderr and longjump method,
93
   * functions.  If you want to use the default stderr and longjump method,
94
   * you can supply NULL for the last three parameters.  We also check that
94
   * you can supply NULL for the last three parameters.  We also check that
95
   * the library version is compatible with the one used at compile time,
95
   * the library version is compatible with the one used at compile time,
96
   * in case we are using dynamically linked libraries.  REQUIRED.
96
   * in case we are using dynamically linked libraries.  REQUIRED.
97
   */
97
   */
98
  png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
98
  png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
99
  if (png_ptr == NULL) {
99
  if (png_ptr == NULL) {
100
    free(scanline);
100
    free(scanline);
101
    return 0;
101
    return 0;
102
  }
102
  }
103
 
103
 
104
  /* Allocate/initialize the image information data.  REQUIRED */
104
  /* Allocate/initialize the image information data.  REQUIRED */
105
  info_ptr = png_create_info_struct(png_ptr);
105
  info_ptr = png_create_info_struct(png_ptr);
106
  if (info_ptr == NULL) {
106
  if (info_ptr == NULL) {
107
    free(scanline);
107
    free(scanline);
108
    png_destroy_write_struct(&png_ptr,  (png_infopp)NULL);
108
    png_destroy_write_struct(&png_ptr,  (png_infopp)NULL);
109
    return 0;
109
    return 0;
110
  }
110
  }
111
  
111
  
112
  /* Set error handling.  REQUIRED if you aren't supplying your own
112
  /* Set error handling.  REQUIRED if you aren't supplying your own
113
   * error handling functions in the png_create_write_struct() call.
113
   * error handling functions in the png_create_write_struct() call.
114
   */
114
   */
115
  if (setjmp(png_ptr->jmpbuf)) {
115
  if (setjmp(png_ptr->jmpbuf)) {
116
    /* If we get here, we had a problem writing the file */
116
    /* If we get here, we had a problem writing the file */
117
    free(scanline);
117
    free(scanline);
118
    png_destroy_write_struct(&png_ptr,  (png_infopp)NULL);
118
    png_destroy_write_struct(&png_ptr,  (png_infopp)NULL);
119
    return 0;
119
    return 0;
120
  }
120
  }
121
  png_set_error_fn(png_ptr, NULL, my_png_error, my_png_warning);
121
  png_set_error_fn(png_ptr, NULL, my_png_error, my_png_warning);
122
  
122
  
123
  /* I/O initialization functions is REQUIRED */
123
  /* I/O initialization functions is REQUIRED */
124
  png_init_io(png_ptr, fp);
124
  png_init_io(png_ptr, fp);
125
  /* Have we less than 256 different colors? */
125
  /* Have we less than 256 different colors? */
126
  ncols = mid = 0;
126
  ncols = mid = 0;
127
  withpalette = 1;
127
  withpalette = 1;
128
  for (i = 0; (i < height) && withpalette ; i++) {
128
  for (i = 0; (i < height) && withpalette ; i++) {
129
    for (j = 0; (j < width) && withpalette ; j++) {
129
    for (j = 0; (j < width) && withpalette ; j++) {
130
      col = gp(d,i,j) & 0xFFFFFFUL ;
130
      col = gp(d,i,j) & 0xFFFFFFUL ;
131
      /* binary search the palette: */
131
      /* binary search the palette: */
132
      low = 0;  
132
      low = 0;  
133
      high = ncols - 1;
133
      high = ncols - 1;
134
      while (low <= high) {
134
      while (low <= high) {
135
	mid = (low + high)/2;
135
	mid = (low + high)/2;
136
	if ( col < palette[mid] ) high = mid - 1;
136
	if ( col < palette[mid] ) high = mid - 1;
137
	else if ( col > palette[mid] ) low  = mid + 1;
137
	else if ( col > palette[mid] ) low  = mid + 1;
138
	else break;
138
	else break;
139
      }
139
      }
140
      if (high < low) {
140
      if (high < low) {
141
	/* didn't find colour in palette, insert it: */
141
	/* didn't find colour in palette, insert it: */
142
	if (ncols >= 256) {
142
	if (ncols >= 256) {
143
	  withpalette = 0;
143
	  withpalette = 0;
144
	} else {
144
	} else {
145
	  for (r = ncols; r > low; r--)
145
	  for (r = ncols; r > low; r--)
146
	    palette[r] = palette[r-1] ;
146
	    palette[r] = palette[r-1] ;
147
	  palette[low] = col;
147
	  palette[low] = col;
148
	  ncols ++;
148
	  ncols ++;
149
	}
149
	}
150
      }
150
      }
151
    }
151
    }
152
  }
152
  }
153
 
153
 
154
  /* Set the image information here.  Width and height are up to 2^31,
154
  /* Set the image information here.  Width and height are up to 2^31,
155
   * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
155
   * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
156
   * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
156
   * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
157
   * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
157
   * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
158
   * or PNG_COLOR_TYPE_RGB_ALPHA.  interlace is either PNG_INTERLACE_NONE or
158
   * or PNG_COLOR_TYPE_RGB_ALPHA.  interlace is either PNG_INTERLACE_NONE or
159
   * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
159
   * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
160
   * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED
160
   * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED
161
   */
161
   */
162
  png_set_IHDR(png_ptr, info_ptr, width, height, 8, 
162
  png_set_IHDR(png_ptr, info_ptr, width, height, 8, 
163
	       withpalette ? PNG_COLOR_TYPE_PALETTE : PNG_COLOR_TYPE_RGB,
163
	       withpalette ? PNG_COLOR_TYPE_PALETTE : PNG_COLOR_TYPE_RGB,
164
	       PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, 
164
	       PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, 
165
	       PNG_FILTER_TYPE_BASE);
165
	       PNG_FILTER_TYPE_BASE);
166
  if (withpalette) {
166
  if (withpalette) {
167
    for (i = 0; i < ncols ; i++) {
167
    for (i = 0; i < ncols ; i++) {
168
      col = palette[i];
168
      col = palette[i];
169
      pngpalette[i].red = GETRED(col);
169
      pngpalette[i].red = GETRED(col);
170
      pngpalette[i].green = GETGREEN(col);
170
      pngpalette[i].green = GETGREEN(col);
171
      pngpalette[i].blue = GETBLUE(col);
171
      pngpalette[i].blue = GETBLUE(col);
172
    } 
172
    } 
173
    png_set_PLTE(png_ptr, info_ptr, pngpalette, ncols);
173
    png_set_PLTE(png_ptr, info_ptr, pngpalette, ncols);
174
  } 
174
  } 
175
  /* Write the file header information.  REQUIRED */
175
  /* Write the file header information.  REQUIRED */
176
  png_write_info(png_ptr, info_ptr);
176
  png_write_info(png_ptr, info_ptr);
177
 
177
 
178
  /* 
178
  /* 
179
   * Now, write the pixels
179
   * Now, write the pixels
180
   */
180
   */
181
  for (i=0 ; i<height ; i++) { 
181
  for (i=0 ; i<height ; i++) { 
182
    /* Build the scanline */
182
    /* Build the scanline */
183
    pscanline = scanline;
183
    pscanline = scanline;
184
    for ( j=0 ; j<width ; j++) {
184
    for ( j=0 ; j<width ; j++) {
185
      col = gp(d, i, j);
185
      col = gp(d, i, j);
186
      if (withpalette) { 
186
      if (withpalette) { 
187
	    /* binary search the palette (the colour must be there): */
187
	    /* binary search the palette (the colour must be there): */
188
	    low = 0;  high = ncols - 1;
188
	    low = 0;  high = ncols - 1;
189
	    while (low <= high) {
189
	    while (low <= high) {
190
		mid = (low + high)/2;
190
		mid = (low + high)/2;
191
		if      (col < palette[mid]) high = mid - 1;
191
		if      (col < palette[mid]) high = mid - 1;
192
		else if (col > palette[mid]) low  = mid + 1;
192
		else if (col > palette[mid]) low  = mid + 1;
193
		else break;
193
		else break;
194
	    }
194
	    }
195
	    *pscanline++ = mid;
195
	    *pscanline++ = mid;
196
      } else { 
196
      } else { 
197
	*pscanline++ = GETRED(col) ;
197
	*pscanline++ = GETRED(col) ;
198
        *pscanline++ = GETGREEN(col) ;
198
        *pscanline++ = GETGREEN(col) ;
199
        *pscanline++ = GETBLUE(col) ;
199
        *pscanline++ = GETBLUE(col) ;
200
      }
200
      }
201
    }
201
    }
202
    png_write_row(png_ptr, scanline);
202
    png_write_row(png_ptr, scanline);
203
  } 
203
  } 
204
 
204
 
205
  /* It is REQUIRED to call this to finish writing the rest of the file */
205
  /* It is REQUIRED to call this to finish writing the rest of the file */
206
  png_write_end(png_ptr, info_ptr);
206
  png_write_end(png_ptr, info_ptr);
207
  
207
  
208
  /* clean up after the write, and free any memory allocated */
208
  /* clean up after the write, and free any memory allocated */
209
  free(scanline);
209
  free(scanline);
210
  png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
210
  png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
211
 
211
 
212
  /* that's it */
212
  /* that's it */
213
  return 1;
213
  return 1;
214
}
214
}
215
 
215
 
216
#endif /* HAVE_PNG */
216
#endif /* HAVE_PNG */
217
 
217
 
218
 
218
 
219
#ifdef HAVE_JPEG 
219
#ifdef HAVE_JPEG 
220
 
220
 
221
#include <jpeglib.h>
221
#include <jpeglib.h>
222
 
222
 
223
/* Here's the extended error handler struct */
223
/* Here's the extended error handler struct */
224
 
224
 
225
struct my_error_mgr {
225
struct my_error_mgr {
226
  struct jpeg_error_mgr pub;	/* "public" fields */
226
  struct jpeg_error_mgr pub;	/* "public" fields */
227
  jmp_buf setjmp_buffer;	/* for return to caller */
227
  jmp_buf setjmp_buffer;	/* for return to caller */
228
};
228
};
229
 
229
 
230
typedef struct my_error_mgr * my_error_ptr;
230
typedef struct my_error_mgr * my_error_ptr;
231
 
231
 
232
/*
232
/*
233
 * Here's the routine that will replace the standard error_exit method:
233
 * Here's the routine that will replace the standard error_exit method:
234
*/
234
*/
235
 
235
 
236
static void my_error_exit (j_common_ptr cinfo)
236
static void my_error_exit (j_common_ptr cinfo)
237
{
237
{
238
  /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
238
  /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
239
  my_error_ptr myerr = (my_error_ptr) cinfo->err;
239
  my_error_ptr myerr = (my_error_ptr) cinfo->err;
240
 
240
 
241
  /* Always display the message. */
241
  /* Always display the message. */
242
  (*cinfo->err->output_message) (cinfo);
242
  (*cinfo->err->output_message) (cinfo);
243
 
243
 
244
  /* Return control to the setjmp point */
244
  /* Return control to the setjmp point */
245
  longjmp(myerr->setjmp_buffer, 1);
245
  longjmp(myerr->setjmp_buffer, 1);
246
}
246
}
247
 
247
 
248
/* We also replace the output method */
248
/* We also replace the output method */
249
static void my_output_message (j_common_ptr cinfo)
249
static void my_output_message (j_common_ptr cinfo)
250
{
250
{
251
  char buffer[JMSG_LENGTH_MAX];
251
  char buffer[JMSG_LENGTH_MAX];
252
 
252
 
253
  /* Create the message */
253
  /* Create the message */
254
  (*cinfo->err->format_message) (cinfo, buffer);
254
  (*cinfo->err->format_message) (cinfo, buffer);
255
 
255
 
256
  /* and show it */  
256
  /* and show it */  
257
  R_ShowMessage(buffer);
257
  R_ShowMessage(buffer);
258
}
258
}
259
 
259
 
260
 
260
 
261
 
261
 
262
int R_SaveAsJpeg(void  *d, int width, int height, 
262
int R_SaveAsJpeg(void  *d, int width, int height, 
263
		unsigned long (*gp)(void *, int, int),
263
		unsigned long (*gp)(void *, int, int),
264
		int bgr, int quality, FILE *outfile) 
264
		int bgr, int quality, FILE *outfile) 
265
{
265
{
266
  struct jpeg_compress_struct cinfo;
266
  struct jpeg_compress_struct cinfo;
267
  struct my_error_mgr jerr;
267
  struct my_error_mgr jerr;
268
  /* More stuff */
268
  /* More stuff */
269
  JSAMPLE *pscanline, *scanline = calloc(3*width,sizeof(JSAMPLE));
269
  JSAMPLE *pscanline, *scanline = calloc(3*width,sizeof(JSAMPLE));
270
  int i, j;
270
  int i, j;
271
  unsigned long col;
271
  unsigned long col;
272
  DECLARESHIFTS;
272
  DECLARESHIFTS;
273
 
273
 
274
  /* Have we enough memory?*/
274
  /* Have we enough memory?*/
275
  if (scanline == NULL) 
275
  if (scanline == NULL) 
276
    return 0;
276
    return 0;
277
 
277
 
278
  /* Step 1: allocate and initialize JPEG compression object */
278
  /* Step 1: allocate and initialize JPEG compression object */
279
 
279
 
280
  /* 
280
  /* 
281
   * We set up the normal JPEG error routines, then override error_exit
281
   * We set up the normal JPEG error routines, then override error_exit
282
   * and output_message
282
   * and output_message
283
  */
283
  */
284
  cinfo.err = jpeg_std_error(&jerr.pub);
284
  cinfo.err = jpeg_std_error(&jerr.pub);
285
  jerr.pub.error_exit = my_error_exit ;
285
  jerr.pub.error_exit = my_error_exit ;
286
  jerr.pub.output_message = my_output_message ;
286
  jerr.pub.output_message = my_output_message ;
287
  /* Establish the setjmp return context for my_error_exit to use. */
287
  /* Establish the setjmp return context for my_error_exit to use. */
288
  if (setjmp(jerr.setjmp_buffer)) {
288
  if (setjmp(jerr.setjmp_buffer)) {
289
    /* If we get here, the JPEG code has signaled an error.
289
    /* If we get here, the JPEG code has signaled an error.
290
     * We need to clean up the JPEG object, close the input file, and return.
290
     * We need to clean up the JPEG object, close the input file, and return.
291
     */
291
     */
292
    jpeg_destroy_compress(&cinfo);
292
    jpeg_destroy_compress(&cinfo);
293
    free(scanline);
293
    free(scanline);
294
    if (outfile) fclose(outfile);
294
    if (outfile) fclose(outfile);
295
    return 0;
295
    return 0;
296
  }
296
  }
297
  /* Now we can initialize the JPEG compression object. */
297
  /* Now we can initialize the JPEG compression object. */
298
  jpeg_create_compress(&cinfo);
298
  jpeg_create_compress(&cinfo);
299
 
299
 
300
  /* Step 2: specify data destination (eg, a file) */
300
  /* Step 2: specify data destination (eg, a file) */
301
  jpeg_stdio_dest(&cinfo, outfile);
301
  jpeg_stdio_dest(&cinfo, outfile);
302
 
302
 
303
  /* Step 3: set parameters for compression */
303
  /* Step 3: set parameters for compression */
304
  /* First we supply a description of the input image.
304
  /* First we supply a description of the input image.
305
   * Four fields of the cinfo struct must be filled in:
305
   * Four fields of the cinfo struct must be filled in:
306
   */
306
   */
307
  cinfo.image_width = width; 	/* image width and height, in pixels */
307
  cinfo.image_width = width; 	/* image width and height, in pixels */
308
  cinfo.image_height = height;
308
  cinfo.image_height = height;
309
  cinfo.input_components = 3;		/* # of color components per pixel */
309
  cinfo.input_components = 3;		/* # of color components per pixel */
310
  cinfo.in_color_space = JCS_RGB; 	/* colorspace of input image */
310
  cinfo.in_color_space = JCS_RGB; 	/* colorspace of input image */
311
  jpeg_set_defaults(&cinfo);
311
  jpeg_set_defaults(&cinfo);
312
  jpeg_set_quality(&cinfo, quality, TRUE);
312
  jpeg_set_quality(&cinfo, quality, TRUE);
313
  /* Step 4: Start compressor */
313
  /* Step 4: Start compressor */
314
  jpeg_start_compress(&cinfo, TRUE);
314
  jpeg_start_compress(&cinfo, TRUE);
315
 
315
 
316
  /* Step 5: while (scan lines remain to be written) */
316
  /* Step 5: while (scan lines remain to be written) */
317
  /*           jpeg_write_scanlines(...); */
317
  /*           jpeg_write_scanlines(...); */
318
  for (i=0 ; i<height ; i++) { 
318
  for (i=0 ; i<height ; i++) { 
319
  /* Build the scanline */
319
  /* Build the scanline */
320
    pscanline = scanline;
320
    pscanline = scanline;
321
    for ( j=0 ; j<width ; j++) {
321
    for ( j=0 ; j<width ; j++) {
322
      col = gp(d, i, j);
322
      col = gp(d, i, j);
323
      *pscanline++ = GETRED(col) ;
323
      *pscanline++ = GETRED(col) ;
324
      *pscanline++ = GETGREEN(col) ;
324
      *pscanline++ = GETGREEN(col) ;
325
      *pscanline++ = GETBLUE(col) ;
325
      *pscanline++ = GETBLUE(col) ;
326
    }
326
    }
327
    jpeg_write_scanlines(&cinfo, (JSAMPARRAY) &scanline, 1);
327
    jpeg_write_scanlines(&cinfo, (JSAMPARRAY) &scanline, 1);
328
  } 
328
  } 
329
 
329
 
330
  /* Step 6: Finish compression */
330
  /* Step 6: Finish compression */
331
 
331
 
332
  jpeg_finish_compress(&cinfo);
332
  jpeg_finish_compress(&cinfo);
333
 
333
 
334
  /* Step 7: release JPEG compression object */
334
  /* Step 7: release JPEG compression object */
335
 
335
 
336
  /* This is an important step since it will release a good deal of memory. */
336
  /* This is an important step since it will release a good deal of memory. */
337
  free(scanline);
337
  free(scanline);
338
  jpeg_destroy_compress(&cinfo);
338
  jpeg_destroy_compress(&cinfo);
339
 
339
 
340
 
340
 
341
  /* And we're done! */
341
  /* And we're done! */
342
  return 1;
342
  return 1;
343
}
343
}
344
 
344
 
345
#endif /* HAVE_JPEG */
345
#endif /* HAVE_JPEG */
346
 
346
 
347
/* 
347
/* 
348
 * Try to save the content of the device 'd' in 'filename' as Windows BMP.
348
 * Try to save the content of the device 'd' in 'filename' as Windows BMP.
349
 * If numbers of colors is less than 256 we use a 'palette' BMP.
349
 * If numbers of colors is less than 256 we use a 'palette' BMP.
350
 * Return 1 on success, 0 on failure 
350
 * Return 1 on success, 0 on failure 
351
*/
351
*/
352
 
352
 
353
#define BMPERROR {R_ShowMessage("Problems writing to 'bmp' file");return 0;} 
353
#define BMPERROR {R_ShowMessage("Problems writing to 'bmp' file");return 0;} 
354
#define BMPW(a) {wrd=a;if(fwrite(&wrd,sizeof(unsigned short),1,fp)!=1) BMPERROR}
354
#define BMPW(a) {wrd=a;if(fwrite(&wrd,sizeof(unsigned short),1,fp)!=1) BMPERROR}
355
#define BMPDW(a) {dwrd=a;if(fwrite(&dwrd,sizeof(unsigned long),1,fp)!=1) BMPERROR}
355
#define BMPDW(a) {dwrd=a;if(fwrite(&dwrd,sizeof(unsigned long),1,fp)!=1) BMPERROR}
356
#define BMPLONG(a) {lng=a;if(fwrite(&lng,sizeof(long),1,fp)!=1) BMPERROR}
356
#define BMPLONG(a) {lng=a;if(fwrite(&lng,sizeof(long),1,fp)!=1) BMPERROR}
357
#define BMPPUTC(a) if(fputc(a,fp)==EOF) BMPERROR;
357
#define BMPPUTC(a) if(fputc(a,fp)==EOF) BMPERROR;
358
#define HEADERSIZE 54
358
#define HEADERSIZE 54
359
 
359
 
360
int R_SaveAsBmp(void  *d, int width, int height, 
360
int R_SaveAsBmp(void  *d, int width, int height, 
361
		unsigned long (*gp)(void *, int, int), int bgr, FILE *fp) 
361
		unsigned long (*gp)(void *, int, int), int bgr, FILE *fp) 
362
{
362
{
363
  unsigned long  col, palette[256];
363
  unsigned long  col, palette[256];
364
  int i, j, r, ncols, mid, high, low, withpalette;
364
  int i, j, r, ncols, mid, high, low, withpalette;
365
  int bfOffBits, bfSize, biBitCount, biClrUsed , pad;
365
  int bfOffBits, bfSize, biBitCount, biClrUsed , pad;
366
  unsigned short wrd;
366
  unsigned short wrd;
367
  unsigned long dwrd;
367
  unsigned long dwrd;
368
  long lng;
368
  long lng;
369
  DECLARESHIFTS;
369
  DECLARESHIFTS;
370
 
370
 
371
  /* Have we less than 256 different colors? */
371
  /* Have we less than 256 different colors? */
372
  ncols = mid = 0;
372
  ncols = mid = 0;
373
  withpalette = 1;
373
  withpalette = 1;
374
  for (i=0; i<256 ; i++) palette[i] = 0;
374
  for (i=0; i<256 ; i++) palette[i] = 0;
375
  for (i = 0; (i < height) && withpalette ; i++) {
375
  for (i = 0; (i < height) && withpalette ; i++) {
376
    for (j = 0; (j < width) && withpalette ; j++) {
376
    for (j = 0; (j < width) && withpalette ; j++) {
377
      col = gp(d,i,j) & 0xFFFFFFUL ;
377
      col = gp(d,i,j) & 0xFFFFFFUL ;
378
      /* binary search the palette: */
378
      /* binary search the palette: */
379
      low = 0;  
379
      low = 0;  
380
      high = ncols - 1;
380
      high = ncols - 1;
381
      while (low <= high) {
381
      while (low <= high) {
382
	mid = (low + high)/2;
382
	mid = (low + high)/2;
383
	if ( col < palette[mid] ) high = mid - 1;
383
	if ( col < palette[mid] ) high = mid - 1;
384
	else if ( col > palette[mid] ) low  = mid + 1;
384
	else if ( col > palette[mid] ) low  = mid + 1;
385
	else break;
385
	else break;
386
      }
386
      }
387
      if (high < low) {
387
      if (high < low) {
388
	/* didn't find colour in palette, insert it: */
388
	/* didn't find colour in palette, insert it: */
389
	if (ncols >= 256) {
389
	if (ncols >= 256) {
390
	  withpalette = 0;
390
	  withpalette = 0;
391
	} else {
391
	} else {
392
	  for (r = ncols; r > low; r--)
392
	  for (r = ncols; r > low; r--)
393
	    palette[r] = palette[r-1] ;
393
	    palette[r] = palette[r-1] ;
394
	  palette[low] = col;
394
	  palette[low] = col;
395
	  ncols ++;
395
	  ncols ++;
396
	}
396
	}
397
      }
397
      }
398
    }
398
    }
399
  }
399
  }
400
  /* Compute some part of the header */
400
  /* Compute some part of the header */
401
  if (withpalette) {
401
  if (withpalette) {
402
    bfOffBits = HEADERSIZE + 4 * 256; 
402
    bfOffBits = HEADERSIZE + 4 * 256; 
403
    bfSize = bfOffBits + width * height ;
403
    bfSize = bfOffBits + width * height ;
404
    biBitCount = 8;
404
    biBitCount = 8;
405
    biClrUsed = 256;
405
    biClrUsed = 256;
406
  } else {
406
  } else {
407
    bfOffBits = HEADERSIZE + 4;
407
    bfOffBits = HEADERSIZE + 4;
408
    bfSize = bfOffBits + 3 * width * height ;
408
    bfSize = bfOffBits + 3 * width * height ;
409
    biBitCount = 24;
409
    biBitCount = 24;
410
    biClrUsed = 0;
410
    biClrUsed = 0;
411
  }
411
  }
412
 
412
 
413
  /* write the header */
413
  /* write the header */
414
  BMPW(0x4D42); /* bfType must be "BM" */
414
  BMPW(0x4D42); /* bfType must be "BM" */
415
  BMPDW(bfSize); /*bfSize*/ 
415
  BMPDW(bfSize); /*bfSize*/ 
416
  BMPW(0);BMPW(0); /* bfReserved1 and bfReserved2 must be 0*/
416
  BMPW(0);BMPW(0); /* bfReserved1 and bfReserved2 must be 0*/
417
  BMPDW(bfOffBits); /* bfOffBits */
417
  BMPDW(bfOffBits); /* bfOffBits */
418
  BMPDW(40);	/* biSize */
418
  BMPDW(40);	/* biSize */
419
  BMPLONG(width); /* biWidth */
419
  BMPLONG(width); /* biWidth */
420
  BMPLONG(height); /* biHeight */
420
  BMPLONG(height); /* biHeight */
421
  BMPW(1);	/* biPlanes - must be 1 */
421
  BMPW(1);	/* biPlanes - must be 1 */
422
  BMPW(biBitCount); /* biBitCount */
422
  BMPW(biBitCount); /* biBitCount */
423
  BMPDW(0); /* biCompression=BI_RGB */
423
  BMPDW(0); /* biCompression=BI_RGB */
424
  BMPDW(0); /* biSizeImage (with BI_RGB not needed)*/
424
  BMPDW(0); /* biSizeImage (with BI_RGB not needed)*/
425
  BMPLONG(0); /* XPels/M <- used only by Windows?*/
425
  BMPLONG(0); /* XPels/M <- used only by Windows?*/
426
  BMPLONG(0); /* XPels/M */
426
  BMPLONG(0); /* XPels/M */
427
  BMPDW(biClrUsed); /* biClrUsed */
427
  BMPDW(biClrUsed); /* biClrUsed */
428
  BMPDW(0) ; /* biClrImportant All colours are important */
428
  BMPDW(0) ; /* biClrImportant All colours are important */
429
 
429
 
430
  /* and now the image */
430
  /* and now the image */
431
  if (withpalette) {
431
  if (withpalette) {
432
    /* 8 bit image; write the palette */
432
    /* 8 bit image; write the palette */
433
    for ( i=0; i<256; i++) {
433
    for ( i=0; i<256; i++) {
434
      col = palette[i];
434
      col = palette[i];
435
      BMPPUTC(GETBLUE(col));
435
      BMPPUTC(GETBLUE(col));
436
      BMPPUTC(GETGREEN(col));
436
      BMPPUTC(GETGREEN(col));
437
      BMPPUTC(GETRED(col));
437
      BMPPUTC(GETRED(col));
438
      BMPPUTC(0);
438
      BMPPUTC(0);
439
    }
439
    }
440
    /* Rows must be padded to 4-byte boundary */
440
    /* Rows must be padded to 4-byte boundary */
441
    for ( pad=0 ; ((width+pad) & 3) != 0; pad++);
441
    for ( pad=0 ; ((width+pad) & 3) != 0; pad++);
442
    /* and then the pixels */
442
    /* and then the pixels */
443
    for (i=height-1 ; i>=0 ; i--) { 
443
    for (i=height-1 ; i>=0 ; i--) { 
444
      for ( j=0 ; j<width ; j++) {
444
      for ( j=0 ; j<width ; j++) {
445
	col = gp(d, i, j)&0xFFFFFFUL;
445
	col = gp(d, i, j)&0xFFFFFFUL;
446
	/* binary search the palette (the colour must be there): */
446
	/* binary search the palette (the colour must be there): */
447
	low = 0;  high = ncols - 1;
447
	low = 0;  high = ncols - 1;
448
	while (low <= high) {
448
	while (low <= high) {
449
	  mid = (low + high)/2;
449
	  mid = (low + high)/2;
450
	  if      (col < palette[mid]) high = mid - 1;
450
	  if      (col < palette[mid]) high = mid - 1;
451
	  else if (col > palette[mid]) low  = mid + 1;
451
	  else if (col > palette[mid]) low  = mid + 1;
452
	  else break;
452
	  else break;
453
	}
453
	}
454
	BMPPUTC(mid);
454
	BMPPUTC(mid);
455
      }
455
      }
456
      for (j=0; j<pad; j++) BMPPUTC(0);
456
      for (j=0; j<pad; j++) BMPPUTC(0);
457
    }
457
    }
458
  } else {
458
  } else {
459
    /* 24 bits image */
459
    /* 24 bits image */
460
    BMPDW(0); /* null bmiColors */
460
    BMPDW(0); /* null bmiColors */
461
    for ( pad=0 ; ((3*width+pad) & 3) != 0; pad++); /*padding*/    
461
    for ( pad=0 ; ((3*width+pad) & 3) != 0; pad++); /*padding*/    
462
    for (i=height-1 ; i>=0 ; i--) { 
462
    for (i=height-1 ; i>=0 ; i--) { 
463
      for ( j=0 ; j<width ; j++) {
463
      for ( j=0 ; j<width ; j++) {
464
	col = gp(d, i, j)&0xFFFFFFUL;
464
	col = gp(d, i, j)&0xFFFFFFUL;
465
	BMPPUTC(GETBLUE(col));
465
	BMPPUTC(GETBLUE(col));
466
	BMPPUTC(GETGREEN(col));
466
	BMPPUTC(GETGREEN(col));
467
	BMPPUTC(GETRED(col));
467
	BMPPUTC(GETRED(col));
468
      }
468
      }
469
      for (j=0; j<pad; j++) BMPPUTC(0);
469
      for (j=0; j<pad; j++) BMPPUTC(0);
470
    }
470
    }
471
  }
471
  }
472
  return 1;
472
  return 1;
473
}
473
}
474
 
474
 
475
 
475
 
476
 
476
 
477
 
477
 
478
 
478
 
479
 
479
 
480
 
480
 
481
 
481