The R Project SVN R

Rev

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

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