The R Project SVN R

Rev

Rev 44540 | Rev 44580 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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