The R Project SVN R

Rev

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

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