| 6430 |
guido |
1 |
/*
|
|
|
2 |
* R : A Computer Language for Statistical Data Analysis
|
| 59039 |
ripley |
3 |
* Copyright (C) 1999-2011 Guido Masarotto and the R Core Team
|
| 6430 |
guido |
4 |
*
|
|
|
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
|
|
|
7 |
* the Free Software Foundation; either version 2 of the License, or
|
|
|
8 |
* (at your option) any later version.
|
|
|
9 |
*
|
|
|
10 |
* This program is distributed in the hope that it will be useful,
|
|
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
13 |
* GNU General Public License for more details.
|
|
|
14 |
*
|
|
|
15 |
* You should have received a copy of the GNU General Public License
|
| 42300 |
ripley |
16 |
* along with this program; if not, a copy is available at
|
| 42301 |
ripley |
17 |
* http://www.r-project.org/Licenses/
|
| 6430 |
guido |
18 |
*/
|
|
|
19 |
|
|
|
20 |
|
| 44570 |
ripley |
21 |
/*
|
| 6644 |
ripley |
22 |
* This file aims to be system independent so it sees the underlying
|
| 6430 |
guido |
23 |
* structures only using:
|
|
|
24 |
* void *d : an 'opaque' view of the source of the pixels;
|
|
|
25 |
* int width, height: dimensions in pixels;
|
| 44570 |
ripley |
26 |
* unsigned int (*gp)(void *d, int x, int y): a function which
|
| 6430 |
guido |
27 |
* returns the colour of the (x,y) pixels stored either as
|
| 44570 |
ripley |
28 |
* BGR (R model, see GraphicsDevice.h) or as RGB in the
|
| 6430 |
guido |
29 |
* 24 least sig. bits (8 bit for channel).
|
| 44570 |
ripley |
30 |
* (0,0) is the left-top corner. (3,2) is the third pixel
|
| 6430 |
guido |
31 |
* in the fourth scanline.
|
|
|
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).
|
| 44570 |
ripley |
34 |
* FILE * fp is the destination.
|
|
|
35 |
*
|
| 6430 |
guido |
36 |
*/
|
|
|
37 |
|
|
|
38 |
#include <stdio.h>
|
|
|
39 |
#include <stdlib.h>
|
|
|
40 |
#include <setjmp.h>
|
|
|
41 |
|
|
|
42 |
/* 8 bits red, green and blue channel */
|
|
|
43 |
#define DECLARESHIFTS int RSHIFT=(bgr)?0:16, GSHIFT=8, BSHIFT=(bgr)?16:0
|
| 44540 |
ripley |
44 |
#define GETRED(col) (((col) >> RSHIFT) & 0xFF)
|
|
|
45 |
#define GETGREEN(col) (((col) >> GSHIFT) & 0xFF)
|
|
|
46 |
#define GETBLUE(col) (((col) >> BSHIFT) & 0xFF)
|
| 44570 |
ripley |
47 |
#define GETALPHA(col) (((col) >> 24) & 0xFF)
|
| 6430 |
guido |
48 |
|
| 19504 |
ripley |
49 |
#include <R_ext/Error.h>
|
| 6430 |
guido |
50 |
|
|
|
51 |
#ifdef HAVE_PNG
|
|
|
52 |
|
|
|
53 |
#include "png.h"
|
| 44570 |
ripley |
54 |
/*
|
| 6430 |
guido |
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.
|
| 44570 |
ripley |
57 |
* Return 1 on success, 0 on failure
|
| 6430 |
guido |
58 |
*/
|
|
|
59 |
|
| 44570 |
ripley |
60 |
/*
|
|
|
61 |
I don't use 'error' since (1) we must free 'scanline' and
|
| 6430 |
guido |
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.
|
| 44570 |
ripley |
64 |
*/
|
|
|
65 |
static void my_png_error(png_structp png_ptr, png_const_charp msg)
|
| 6430 |
guido |
66 |
{
|
| 44570 |
ripley |
67 |
R_ShowMessage((char *) msg);
|
| 51085 |
ripley |
68 |
#if PNG_LIBPNG_VER < 10400
|
| 44570 |
ripley |
69 |
longjmp(png_ptr->jmpbuf,1);
|
| 51085 |
ripley |
70 |
#else
|
|
|
71 |
longjmp(png_jmpbuf(png_ptr),1);
|
|
|
72 |
#endif
|
| 6430 |
guido |
73 |
}
|
|
|
74 |
|
| 44570 |
ripley |
75 |
static void my_png_warning(png_structp png_ptr, png_const_charp msg)
|
| 6430 |
guido |
76 |
{
|
| 44570 |
ripley |
77 |
warning("libpng: %s",(char *) msg);
|
| 6430 |
guido |
78 |
}
|
|
|
79 |
|
| 37364 |
ripley |
80 |
__declspec(dllexport)
|
| 44570 |
ripley |
81 |
int R_SaveAsPng(void *d, int width, int height,
|
| 44496 |
ripley |
82 |
unsigned int (*gp)(void *, int, int),
|
| 44570 |
ripley |
83 |
int bgr, FILE *fp, unsigned int transparent, int res)
|
| 6430 |
guido |
84 |
{
|
| 44570 |
ripley |
85 |
png_structp png_ptr;
|
|
|
86 |
png_infop info_ptr;
|
|
|
87 |
unsigned int col, palette[256];
|
|
|
88 |
png_color pngpalette[256];
|
| 55676 |
ripley |
89 |
png_bytep pscanline;
|
|
|
90 |
png_bytep scanline = (png_bytep) calloc((size_t)(4*width),sizeof(png_byte));
|
| 44570 |
ripley |
91 |
png_byte trans[256];
|
|
|
92 |
png_color_16 trans_values[1];
|
| 55676 |
ripley |
93 |
int i, j, r, ncols, mid, high, low, withpalette, have_alpha;
|
|
|
94 |
volatile DECLARESHIFTS;
|
| 6430 |
guido |
95 |
|
| 44570 |
ripley |
96 |
/* Have we enough memory?*/
|
|
|
97 |
if (scanline == NULL)
|
|
|
98 |
return 0;
|
| 6430 |
guido |
99 |
|
| 55676 |
ripley |
100 |
if (fp == NULL) {
|
|
|
101 |
free(scanline);
|
|
|
102 |
return 0;
|
|
|
103 |
}
|
|
|
104 |
|
| 44570 |
ripley |
105 |
/* Create and initialize the png_struct with the desired error handler
|
|
|
106 |
* functions. If you want to use the default stderr and longjump method,
|
|
|
107 |
* you can supply NULL for the last three parameters. We also check that
|
|
|
108 |
* the library version is compatible with the one used at compile time,
|
|
|
109 |
* in case we are using dynamically linked libraries. REQUIRED.
|
|
|
110 |
*/
|
|
|
111 |
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
|
|
112 |
if (png_ptr == NULL) {
|
|
|
113 |
free(scanline);
|
|
|
114 |
return 0;
|
|
|
115 |
}
|
| 6430 |
guido |
116 |
|
| 44570 |
ripley |
117 |
/* Allocate/initialize the image information data. REQUIRED */
|
|
|
118 |
info_ptr = png_create_info_struct(png_ptr);
|
|
|
119 |
if (info_ptr == NULL) {
|
|
|
120 |
free(scanline);
|
|
|
121 |
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
|
|
|
122 |
return 0;
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
/* Set error handling. REQUIRED if you aren't supplying your own
|
|
|
126 |
* error handling functions in the png_create_write_struct() call.
|
|
|
127 |
*/
|
| 51085 |
ripley |
128 |
#if PNG_LIBPNG_VER < 10400
|
|
|
129 |
if (setjmp(png_ptr->jmpbuf))
|
|
|
130 |
#else
|
|
|
131 |
if (setjmp(png_jmpbuf(png_ptr)))
|
|
|
132 |
#endif
|
| 51087 |
ripley |
133 |
{
|
| 44570 |
ripley |
134 |
/* If we get here, we had a problem writing the file */
|
|
|
135 |
free(scanline);
|
| 44580 |
ripley |
136 |
png_destroy_write_struct(&png_ptr, &info_ptr);
|
| 44570 |
ripley |
137 |
return 0;
|
|
|
138 |
}
|
|
|
139 |
png_set_error_fn(png_ptr, NULL, my_png_error, my_png_warning);
|
|
|
140 |
|
|
|
141 |
/* I/O initialization functions is REQUIRED */
|
|
|
142 |
png_init_io(png_ptr, fp);
|
|
|
143 |
/* Have we less than 256 different colors? */
|
|
|
144 |
ncols = 0;
|
|
|
145 |
if(transparent) palette[ncols++] = transparent & 0xFFFFFF;
|
|
|
146 |
mid = ncols;
|
|
|
147 |
withpalette = 1;
|
| 55676 |
ripley |
148 |
have_alpha = 0;
|
| 44570 |
ripley |
149 |
for (i = 0; (i < height) && withpalette ; i++) {
|
|
|
150 |
for (j = 0; (j < width) && withpalette ; j++) {
|
| 55676 |
ripley |
151 |
col = gp(d,i,j);
|
|
|
152 |
if (GETALPHA(col) < 255) have_alpha = 1;
|
| 44570 |
ripley |
153 |
/* binary search the palette: */
|
|
|
154 |
low = 0;
|
|
|
155 |
high = ncols - 1;
|
|
|
156 |
while (low <= high) {
|
|
|
157 |
mid = (low + high)/2;
|
|
|
158 |
if ( col < palette[mid] ) high = mid - 1;
|
|
|
159 |
else if ( col > palette[mid] ) low = mid + 1;
|
|
|
160 |
else break;
|
|
|
161 |
}
|
|
|
162 |
if (high < low) {
|
|
|
163 |
/* didn't find colour in palette, insert it: */
|
|
|
164 |
if (ncols >= 256) {
|
|
|
165 |
withpalette = 0;
|
|
|
166 |
} else {
|
|
|
167 |
for (r = ncols; r > low; r--)
|
|
|
168 |
palette[r] = palette[r-1] ;
|
|
|
169 |
palette[low] = col;
|
|
|
170 |
ncols ++;
|
|
|
171 |
}
|
|
|
172 |
}
|
| 6430 |
guido |
173 |
}
|
|
|
174 |
}
|
| 55709 |
ripley |
175 |
col = gp(d,0,0);
|
|
|
176 |
//have_alpha &= (transparent == 0);
|
| 6430 |
guido |
177 |
|
| 44570 |
ripley |
178 |
/* Set the image information here. Width and height are up to 2^31,
|
|
|
179 |
* bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
|
|
|
180 |
* the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
|
|
|
181 |
* PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
|
|
|
182 |
* or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
|
|
|
183 |
* PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
|
|
|
184 |
* currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED
|
|
|
185 |
*/
|
|
|
186 |
png_set_IHDR(png_ptr, info_ptr, width, height, 8,
|
| 55676 |
ripley |
187 |
withpalette ? PNG_COLOR_TYPE_PALETTE :
|
|
|
188 |
(have_alpha ? PNG_COLOR_TYPE_RGB_ALPHA : PNG_COLOR_TYPE_RGB),
|
| 44570 |
ripley |
189 |
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
|
|
|
190 |
PNG_FILTER_TYPE_BASE);
|
| 15641 |
ripley |
191 |
|
| 44570 |
ripley |
192 |
if (withpalette) {
|
|
|
193 |
for (i = 0; i < ncols ; i++) {
|
|
|
194 |
col = palette[i];
|
| 55676 |
ripley |
195 |
if(transparent) {
|
|
|
196 |
trans[i] = (col == transparent) ? 0:255;
|
|
|
197 |
pngpalette[i].red = GETRED(col);
|
|
|
198 |
pngpalette[i].green = GETGREEN(col);
|
|
|
199 |
pngpalette[i].blue = GETBLUE(col);
|
|
|
200 |
} else {
|
|
|
201 |
/* PNG needs NON-premultiplied alpha */
|
|
|
202 |
int a = GETALPHA(col);
|
|
|
203 |
trans[i] = a;
|
|
|
204 |
if(a == 255 || a == 0) {
|
|
|
205 |
pngpalette[i].red = GETRED(col);
|
|
|
206 |
pngpalette[i].green = GETGREEN(col);
|
|
|
207 |
pngpalette[i].blue = GETBLUE(col);
|
|
|
208 |
} else {
|
|
|
209 |
pngpalette[i].red = 0.49 + 255.0*GETRED(col)/a;
|
|
|
210 |
pngpalette[i].green = 0.49 + 255.0*GETGREEN(col)/a;
|
|
|
211 |
pngpalette[i].blue = 0.49 + 255.0*GETBLUE(col)/a;
|
|
|
212 |
|
|
|
213 |
}
|
|
|
214 |
}
|
| 44570 |
ripley |
215 |
}
|
|
|
216 |
png_set_PLTE(png_ptr, info_ptr, pngpalette, ncols);
|
| 55676 |
ripley |
217 |
if (transparent || have_alpha)
|
|
|
218 |
png_set_tRNS(png_ptr, info_ptr, trans, ncols, trans_values);
|
| 44570 |
ripley |
219 |
}
|
|
|
220 |
/* Deal with transparency */
|
| 55676 |
ripley |
221 |
if(transparent && !withpalette) {
|
|
|
222 |
trans_values[0].red = GETRED(transparent);
|
|
|
223 |
trans_values[0].blue = GETBLUE(transparent);
|
|
|
224 |
trans_values[0].green = GETGREEN(transparent);
|
| 44570 |
ripley |
225 |
png_set_tRNS(png_ptr, info_ptr, trans, ncols, trans_values);
|
|
|
226 |
}
|
| 15641 |
ripley |
227 |
|
| 44570 |
ripley |
228 |
if(res > 0)
|
|
|
229 |
png_set_pHYs(png_ptr, info_ptr, res/0.0254, res/0.0254,
|
|
|
230 |
PNG_RESOLUTION_METER);
|
| 29909 |
ripley |
231 |
|
| 44570 |
ripley |
232 |
/* Write the file header information. REQUIRED */
|
|
|
233 |
png_write_info(png_ptr, info_ptr);
|
| 6430 |
guido |
234 |
|
| 44570 |
ripley |
235 |
/*
|
|
|
236 |
* Now, write the pixels
|
|
|
237 |
*/
|
|
|
238 |
for (i = 0 ; i < height ; i++) {
|
|
|
239 |
/* Build the scanline */
|
|
|
240 |
pscanline = scanline;
|
|
|
241 |
for (j = 0 ; j < width ; j++) {
|
| 55676 |
ripley |
242 |
col = gp(d, i, j);
|
| 44570 |
ripley |
243 |
if (withpalette) {
|
|
|
244 |
/* binary search the palette (the colour must be there): */
|
|
|
245 |
low = 0; high = ncols - 1;
|
|
|
246 |
while (low <= high) {
|
|
|
247 |
mid = (low + high)/2;
|
|
|
248 |
if (col < palette[mid]) high = mid - 1;
|
|
|
249 |
else if (col > palette[mid]) low = mid + 1;
|
|
|
250 |
else break;
|
|
|
251 |
}
|
|
|
252 |
*pscanline++ = mid;
|
|
|
253 |
} else {
|
| 55676 |
ripley |
254 |
if(have_alpha) {
|
|
|
255 |
/* PNG needs NON-premultiplied alpha */
|
|
|
256 |
int a = GETALPHA(col);
|
|
|
257 |
if(a == 255 || a == 0) {
|
|
|
258 |
*pscanline++ = GETRED(col) ;
|
|
|
259 |
*pscanline++ = GETGREEN(col) ;
|
|
|
260 |
*pscanline++ = GETBLUE(col) ;
|
|
|
261 |
*pscanline++ = a;
|
|
|
262 |
} else {
|
|
|
263 |
*pscanline++ = 0.49 + 255.0*GETRED(col)/a ;
|
|
|
264 |
*pscanline++ = 0.49 + 255.0*GETGREEN(col)/a ;
|
|
|
265 |
*pscanline++ = 0.49 + 255.0*GETBLUE(col)/a ;
|
|
|
266 |
*pscanline++ = a;
|
|
|
267 |
}
|
|
|
268 |
} else {
|
|
|
269 |
*pscanline++ = GETRED(col) ;
|
|
|
270 |
*pscanline++ = GETGREEN(col) ;
|
|
|
271 |
*pscanline++ = GETBLUE(col) ;
|
|
|
272 |
}
|
| 6430 |
guido |
273 |
}
|
| 44570 |
ripley |
274 |
}
|
|
|
275 |
png_write_row(png_ptr, scanline);
|
| 6430 |
guido |
276 |
}
|
|
|
277 |
|
| 44570 |
ripley |
278 |
/* It is REQUIRED to call this to finish writing the rest of the file */
|
|
|
279 |
png_write_end(png_ptr, info_ptr);
|
| 6430 |
guido |
280 |
|
| 44570 |
ripley |
281 |
/* clean up after the write, and free any memory allocated */
|
|
|
282 |
free(scanline);
|
| 44580 |
ripley |
283 |
png_destroy_write_struct(&png_ptr, &info_ptr);
|
| 44570 |
ripley |
284 |
|
|
|
285 |
/* that's it */
|
|
|
286 |
return 1;
|
| 6430 |
guido |
287 |
}
|
|
|
288 |
|
|
|
289 |
#endif /* HAVE_PNG */
|
|
|
290 |
|
|
|
291 |
|
| 44570 |
ripley |
292 |
#ifdef HAVE_JPEG
|
| 6430 |
guido |
293 |
|
| 51087 |
ripley |
294 |
/* jconfig.h included by jpeglib.h may define these unconditionally */
|
|
|
295 |
#undef HAVE_STDDEF_H
|
|
|
296 |
#undef HAVE_STDLIB_H
|
| 6430 |
guido |
297 |
#include <jpeglib.h>
|
|
|
298 |
|
|
|
299 |
/* Here's the extended error handler struct */
|
|
|
300 |
|
|
|
301 |
struct my_error_mgr {
|
| 44570 |
ripley |
302 |
struct jpeg_error_mgr pub; /* "public" fields */
|
|
|
303 |
jmp_buf setjmp_buffer; /* for return to caller */
|
| 6430 |
guido |
304 |
};
|
|
|
305 |
|
|
|
306 |
typedef struct my_error_mgr * my_error_ptr;
|
|
|
307 |
|
|
|
308 |
/*
|
|
|
309 |
* Here's the routine that will replace the standard error_exit method:
|
|
|
310 |
*/
|
|
|
311 |
|
|
|
312 |
static void my_error_exit (j_common_ptr cinfo)
|
|
|
313 |
{
|
| 44570 |
ripley |
314 |
/* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
|
|
|
315 |
my_error_ptr myerr = (my_error_ptr) cinfo->err;
|
| 6430 |
guido |
316 |
|
| 44570 |
ripley |
317 |
/* Always display the message. */
|
|
|
318 |
(*cinfo->err->output_message) (cinfo);
|
| 6430 |
guido |
319 |
|
| 44570 |
ripley |
320 |
/* Return control to the setjmp point */
|
|
|
321 |
longjmp(myerr->setjmp_buffer, 1);
|
| 6430 |
guido |
322 |
}
|
|
|
323 |
|
|
|
324 |
/* We also replace the output method */
|
|
|
325 |
static void my_output_message (j_common_ptr cinfo)
|
|
|
326 |
{
|
| 44570 |
ripley |
327 |
char buffer[JMSG_LENGTH_MAX];
|
| 6430 |
guido |
328 |
|
| 44570 |
ripley |
329 |
/* Create the message */
|
|
|
330 |
(*cinfo->err->format_message) (cinfo, buffer);
|
| 6430 |
guido |
331 |
|
| 44570 |
ripley |
332 |
/* and show it */
|
|
|
333 |
R_ShowMessage(buffer);
|
| 6430 |
guido |
334 |
}
|
|
|
335 |
|
|
|
336 |
|
|
|
337 |
|
| 37364 |
ripley |
338 |
__declspec(dllexport)
|
| 44570 |
ripley |
339 |
int R_SaveAsJpeg(void *d, int width, int height,
|
| 44496 |
ripley |
340 |
unsigned int (*gp)(void *, int, int),
|
| 44570 |
ripley |
341 |
int bgr, int quality, FILE *outfile, int res)
|
| 6430 |
guido |
342 |
{
|
| 44570 |
ripley |
343 |
struct jpeg_compress_struct cinfo;
|
|
|
344 |
struct my_error_mgr jerr;
|
|
|
345 |
/* More stuff */
|
|
|
346 |
JSAMPLE *pscanline, *scanline = (JSAMPLE *) calloc(3*width,sizeof(JSAMPLE));
|
|
|
347 |
int i, j;
|
|
|
348 |
unsigned int col;
|
|
|
349 |
DECLARESHIFTS;
|
| 6430 |
guido |
350 |
|
| 44570 |
ripley |
351 |
/* Have we enough memory?*/
|
|
|
352 |
if (scanline == NULL)
|
|
|
353 |
return 0;
|
| 6430 |
guido |
354 |
|
| 55676 |
ripley |
355 |
if (outfile == NULL) {
|
|
|
356 |
free(scanline);
|
|
|
357 |
return 0;
|
|
|
358 |
}
|
|
|
359 |
|
| 44570 |
ripley |
360 |
/* Step 1: allocate and initialize JPEG compression object */
|
| 6430 |
guido |
361 |
|
| 44570 |
ripley |
362 |
/*
|
|
|
363 |
* We set up the normal JPEG error routines, then override error_exit
|
|
|
364 |
* and output_message
|
| 6430 |
guido |
365 |
*/
|
| 44570 |
ripley |
366 |
cinfo.err = jpeg_std_error(&jerr.pub);
|
|
|
367 |
jerr.pub.error_exit = my_error_exit ;
|
|
|
368 |
jerr.pub.output_message = my_output_message ;
|
|
|
369 |
/* Establish the setjmp return context for my_error_exit to use. */
|
|
|
370 |
if (setjmp(jerr.setjmp_buffer)) {
|
|
|
371 |
/* If we get here, the JPEG code has signaled an error.
|
|
|
372 |
* We need to clean up the JPEG object, close the input file, and return.
|
|
|
373 |
*/
|
|
|
374 |
jpeg_destroy_compress(&cinfo);
|
|
|
375 |
free(scanline);
|
|
|
376 |
if (outfile) fclose(outfile);
|
|
|
377 |
return 0;
|
|
|
378 |
}
|
|
|
379 |
/* Now we can initialize the JPEG compression object. */
|
|
|
380 |
jpeg_create_compress(&cinfo);
|
| 6430 |
guido |
381 |
|
| 44570 |
ripley |
382 |
/* Step 2: specify data destination (eg, a file) */
|
|
|
383 |
jpeg_stdio_dest(&cinfo, outfile);
|
| 6430 |
guido |
384 |
|
| 44570 |
ripley |
385 |
/* Step 3: set parameters for compression */
|
|
|
386 |
/* First we supply a description of the input image.
|
|
|
387 |
* Four fields of the cinfo struct must be filled in:
|
|
|
388 |
*/
|
| 51087 |
ripley |
389 |
cinfo.image_width = width; /* image width and height, in pixels */
|
| 44570 |
ripley |
390 |
cinfo.image_height = height;
|
|
|
391 |
cinfo.input_components = 3; /* # of color components per pixel */
|
| 51087 |
ripley |
392 |
cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
|
| 44570 |
ripley |
393 |
jpeg_set_defaults(&cinfo);
|
|
|
394 |
if(res > 0) {
|
|
|
395 |
cinfo.density_unit = 1; /* pixels per inch */
|
|
|
396 |
cinfo.X_density = res;
|
|
|
397 |
cinfo.Y_density = res;
|
|
|
398 |
}
|
|
|
399 |
jpeg_set_quality(&cinfo, quality, TRUE);
|
|
|
400 |
/* Step 4: Start compressor */
|
|
|
401 |
jpeg_start_compress(&cinfo, TRUE);
|
| 6430 |
guido |
402 |
|
| 44570 |
ripley |
403 |
/* Step 5: while (scan lines remain to be written) */
|
|
|
404 |
/* jpeg_write_scanlines(...); */
|
|
|
405 |
for (i=0 ; i<height ; i++) {
|
|
|
406 |
/* Build the scanline */
|
|
|
407 |
pscanline = scanline;
|
|
|
408 |
for ( j=0 ; j<width ; j++) {
|
|
|
409 |
col = gp(d, i, j) & 0xFFFFFF;
|
|
|
410 |
*pscanline++ = GETRED(col) ;
|
|
|
411 |
*pscanline++ = GETGREEN(col) ;
|
|
|
412 |
*pscanline++ = GETBLUE(col) ;
|
|
|
413 |
}
|
|
|
414 |
jpeg_write_scanlines(&cinfo, (JSAMPARRAY) &scanline, 1);
|
| 6430 |
guido |
415 |
}
|
|
|
416 |
|
| 44570 |
ripley |
417 |
/* Step 6: Finish compression */
|
| 6430 |
guido |
418 |
|
| 44570 |
ripley |
419 |
jpeg_finish_compress(&cinfo);
|
| 6430 |
guido |
420 |
|
| 44570 |
ripley |
421 |
/* Step 7: release JPEG compression object */
|
| 6430 |
guido |
422 |
|
| 44570 |
ripley |
423 |
/* This is an important step since it will release a good deal of memory. */
|
|
|
424 |
free(scanline);
|
|
|
425 |
jpeg_destroy_compress(&cinfo);
|
| 6430 |
guido |
426 |
|
| 6458 |
guido |
427 |
|
| 44570 |
ripley |
428 |
/* And we're done! */
|
|
|
429 |
return 1;
|
| 6430 |
guido |
430 |
}
|
|
|
431 |
|
|
|
432 |
#endif /* HAVE_JPEG */
|
|
|
433 |
|
| 44594 |
ripley |
434 |
#ifdef HAVE_TIFF
|
|
|
435 |
#include <tiffio.h>
|
|
|
436 |
|
|
|
437 |
__declspec(dllexport)
|
|
|
438 |
int R_SaveAsTIFF(void *d, int width, int height,
|
|
|
439 |
unsigned int (*gp)(void *, int, int),
|
|
|
440 |
int bgr, const char *outfile, int res, int compression)
|
|
|
441 |
{
|
|
|
442 |
TIFF *out;
|
|
|
443 |
int sampleperpixel;
|
|
|
444 |
tsize_t linebytes;
|
|
|
445 |
unsigned char *buf, *pscanline;
|
|
|
446 |
unsigned int col, i, j;
|
|
|
447 |
int have_alpha = 0;
|
|
|
448 |
|
|
|
449 |
DECLARESHIFTS;
|
|
|
450 |
|
|
|
451 |
for (i = 0; i < height; i++)
|
|
|
452 |
for (j = 0; j < width; j++) {
|
|
|
453 |
col = gp(d,i,j);
|
|
|
454 |
if (GETALPHA(col) < 255) {
|
|
|
455 |
have_alpha = 1;
|
|
|
456 |
break;
|
|
|
457 |
}
|
|
|
458 |
}
|
|
|
459 |
sampleperpixel = 3 + have_alpha;
|
| 51087 |
ripley |
460 |
|
| 44594 |
ripley |
461 |
out = TIFFOpen(outfile, "w");
|
|
|
462 |
if (!out) {
|
|
|
463 |
warning("unable to open TIFF file '%s'", outfile);
|
|
|
464 |
return 0;
|
|
|
465 |
}
|
|
|
466 |
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);
|
|
|
467 |
TIFFSetField(out, TIFFTAG_IMAGELENGTH, height);
|
|
|
468 |
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, sampleperpixel);
|
|
|
469 |
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
|
|
|
470 |
TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
|
|
|
471 |
TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
|
|
|
472 |
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
|
|
|
473 |
#if 0
|
|
|
474 |
/* Possible compression values
|
|
|
475 |
COMPRESSION_NONE = 1;
|
|
|
476 |
COMPRESSION_CCITTRLE = 2;
|
|
|
477 |
COMPRESSION_CCITTFAX3 = COMPRESSION_CCITT_T4 = 3;
|
|
|
478 |
COMPRESSION_CCITTFAX4 = COMPRESSION_CCITT_T6 = 4;
|
|
|
479 |
COMPRESSION_LZW = 5;
|
|
|
480 |
COMPRESSION_JPEG = 7;
|
|
|
481 |
COMPRESSION_DEFLATE = 32946;
|
| 51087 |
ripley |
482 |
COMPRESSION_ADOBE_DEFLATE = 8;
|
| 44594 |
ripley |
483 |
*/
|
|
|
484 |
TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
|
|
|
485 |
#endif
|
|
|
486 |
if(compression > 1)
|
|
|
487 |
TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
|
|
|
488 |
|
|
|
489 |
if (res > 0) {
|
|
|
490 |
TIFFSetField(out, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
|
|
|
491 |
TIFFSetField(out, TIFFTAG_XRESOLUTION, (float) res);
|
|
|
492 |
TIFFSetField(out, TIFFTAG_YRESOLUTION, (float) res);
|
|
|
493 |
}
|
|
|
494 |
|
|
|
495 |
linebytes = sampleperpixel * width;
|
|
|
496 |
if (TIFFScanlineSize(out))
|
|
|
497 |
buf =(unsigned char *)_TIFFmalloc(linebytes);
|
|
|
498 |
else
|
|
|
499 |
buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(out));
|
| 51087 |
ripley |
500 |
|
| 44594 |
ripley |
501 |
for (i = 0; i < height; i++) {
|
|
|
502 |
pscanline = buf;
|
|
|
503 |
for(j = 0; j < width; j++) {
|
|
|
504 |
col = gp(d, i, j);
|
|
|
505 |
*pscanline++ = GETRED(col) ;
|
|
|
506 |
*pscanline++ = GETGREEN(col) ;
|
|
|
507 |
*pscanline++ = GETBLUE(col) ;
|
|
|
508 |
if(have_alpha) *pscanline++ = GETALPHA(col) ;
|
|
|
509 |
}
|
|
|
510 |
TIFFWriteScanline(out, buf, i, 0);
|
|
|
511 |
}
|
|
|
512 |
TIFFClose(out);
|
|
|
513 |
_TIFFfree(buf);
|
|
|
514 |
return 1;
|
|
|
515 |
}
|
|
|
516 |
#endif /* HAVE_TIFF */
|
|
|
517 |
|
|
|
518 |
|
| 44570 |
ripley |
519 |
/*
|
| 6458 |
guido |
520 |
* Try to save the content of the device 'd' in 'filename' as Windows BMP.
|
|
|
521 |
* If numbers of colors is less than 256 we use a 'palette' BMP.
|
| 44570 |
ripley |
522 |
* Return 1 on success, 0 on failure
|
| 6458 |
guido |
523 |
*/
|
| 6430 |
guido |
524 |
|
| 44570 |
ripley |
525 |
#define BMPERROR {R_ShowMessage("Problems writing to 'bmp' file");return 0;}
|
| 6458 |
guido |
526 |
#define BMPW(a) {wrd=a;if(fwrite(&wrd,sizeof(unsigned short),1,fp)!=1) BMPERROR}
|
| 44496 |
ripley |
527 |
#define BMPDW(a) {dwrd=a;if(fwrite(&dwrd,sizeof(unsigned int),1,fp)!=1) BMPERROR}
|
| 6458 |
guido |
528 |
#define BMPPUTC(a) if(fputc(a,fp)==EOF) BMPERROR;
|
|
|
529 |
#define HEADERSIZE 54
|
| 6430 |
guido |
530 |
|
| 37364 |
ripley |
531 |
__declspec(dllexport)
|
| 44570 |
ripley |
532 |
int R_SaveAsBmp(void *d, int width, int height,
|
| 44496 |
ripley |
533 |
unsigned int (*gp)(void *, int, int), int bgr, FILE *fp,
|
| 44570 |
ripley |
534 |
int res)
|
| 6458 |
guido |
535 |
{
|
| 44570 |
ripley |
536 |
unsigned int col, palette[256];
|
|
|
537 |
int i, j, r, ncols, mid, high, low, withpalette;
|
|
|
538 |
int bfOffBits, bfSize, biBitCount, biClrUsed , pad;
|
|
|
539 |
unsigned short wrd;
|
|
|
540 |
unsigned int dwrd;
|
| 44641 |
ripley |
541 |
int lres;
|
| 44570 |
ripley |
542 |
DECLARESHIFTS;
|
| 6430 |
guido |
543 |
|
| 55676 |
ripley |
544 |
if (fp == NULL)
|
|
|
545 |
return 0;
|
|
|
546 |
|
| 44570 |
ripley |
547 |
/* Have we less than 256 different colors? */
|
|
|
548 |
ncols = mid = 0;
|
|
|
549 |
withpalette = 1;
|
|
|
550 |
for (i = 0; i < 256 ; i++) palette[i] = 0;
|
|
|
551 |
for (i = 0; (i < height) && withpalette ; i++) {
|
|
|
552 |
for (j = 0; (j < width) && withpalette ; j++) {
|
|
|
553 |
col = gp(d,i,j) & 0xFFFFFF ;
|
|
|
554 |
/* binary search the palette: */
|
|
|
555 |
low = 0;
|
|
|
556 |
high = ncols - 1;
|
|
|
557 |
while (low <= high) {
|
|
|
558 |
mid = (low + high)/2;
|
|
|
559 |
if ( col < palette[mid] ) high = mid - 1;
|
|
|
560 |
else if ( col > palette[mid] ) low = mid + 1;
|
|
|
561 |
else break;
|
|
|
562 |
}
|
|
|
563 |
if (high < low) {
|
|
|
564 |
/* didn't find colour in palette, insert it: */
|
|
|
565 |
if (ncols >= 256) {
|
|
|
566 |
withpalette = 0;
|
|
|
567 |
} else {
|
|
|
568 |
for (r = ncols; r > low; r--)
|
|
|
569 |
palette[r] = palette[r-1] ;
|
|
|
570 |
palette[low] = col;
|
|
|
571 |
ncols ++;
|
|
|
572 |
}
|
|
|
573 |
}
|
| 6458 |
guido |
574 |
}
|
|
|
575 |
}
|
| 44570 |
ripley |
576 |
/* Compute some part of the header */
|
|
|
577 |
if (withpalette) {
|
|
|
578 |
bfOffBits = HEADERSIZE + 4 * 256;
|
|
|
579 |
bfSize = bfOffBits + width * height ;
|
|
|
580 |
biBitCount = 8;
|
|
|
581 |
biClrUsed = 256;
|
|
|
582 |
} else {
|
|
|
583 |
bfOffBits = HEADERSIZE + 4;
|
|
|
584 |
bfSize = bfOffBits + 3 * width * height ;
|
|
|
585 |
biBitCount = 24;
|
|
|
586 |
biClrUsed = 0;
|
|
|
587 |
}
|
| 6430 |
guido |
588 |
|
| 44570 |
ripley |
589 |
/* write the header */
|
| 51087 |
ripley |
590 |
|
| 44641 |
ripley |
591 |
BMPPUTC('B');BMPPUTC('M');
|
| 44570 |
ripley |
592 |
BMPDW(bfSize); /*bfSize*/
|
|
|
593 |
BMPW(0);BMPW(0); /* bfReserved1 and bfReserved2 must be 0*/
|
|
|
594 |
BMPDW(bfOffBits); /* bfOffBits */
|
| 44641 |
ripley |
595 |
BMPDW(40); /* Windows V3. size 40 bytes */
|
|
|
596 |
BMPDW(width); /* biWidth */
|
|
|
597 |
BMPDW(height); /* biHeight */
|
| 44570 |
ripley |
598 |
BMPW(1); /* biPlanes - must be 1 */
|
|
|
599 |
BMPW(biBitCount); /* biBitCount */
|
|
|
600 |
BMPDW(0); /* biCompression=BI_RGB */
|
|
|
601 |
BMPDW(0); /* biSizeImage (with BI_RGB not needed)*/
|
| 44641 |
ripley |
602 |
lres = (int)(0.5 + res/0.0254);
|
|
|
603 |
BMPDW(lres); /* XPels/M */
|
|
|
604 |
BMPDW(lres); /* XPels/M */
|
| 44570 |
ripley |
605 |
BMPDW(biClrUsed); /* biClrUsed */
|
|
|
606 |
BMPDW(0) ; /* biClrImportant All colours are important */
|
| 6430 |
guido |
607 |
|
| 44570 |
ripley |
608 |
/* and now the image */
|
|
|
609 |
if (withpalette) {
|
|
|
610 |
/* 8 bit image; write the palette */
|
|
|
611 |
for (i = 0; i < 256; i++) {
|
|
|
612 |
col = palette[i];
|
|
|
613 |
BMPPUTC(GETBLUE(col));
|
|
|
614 |
BMPPUTC(GETGREEN(col));
|
|
|
615 |
BMPPUTC(GETRED(col));
|
|
|
616 |
BMPPUTC(0);
|
| 6458 |
guido |
617 |
}
|
| 44570 |
ripley |
618 |
/* Rows must be padded to 4-byte boundary */
|
|
|
619 |
for (pad = 0; ((width+pad) & 3) != 0; pad++);
|
|
|
620 |
/* and then the pixels */
|
|
|
621 |
for (i = height-1 ; i >= 0 ; i--) {
|
|
|
622 |
for (j = 0 ; j < width ; j++) {
|
|
|
623 |
col = gp(d, i, j) & 0xFFFFFF;
|
|
|
624 |
/* binary search the palette (the colour must be there): */
|
|
|
625 |
low = 0; high = ncols - 1;
|
|
|
626 |
while (low <= high) {
|
|
|
627 |
mid = (low + high)/2;
|
|
|
628 |
if (col < palette[mid]) high = mid - 1;
|
|
|
629 |
else if (col > palette[mid]) low = mid + 1;
|
|
|
630 |
else break;
|
|
|
631 |
}
|
|
|
632 |
BMPPUTC(mid);
|
|
|
633 |
}
|
|
|
634 |
for (j = 0; j < pad; j++) BMPPUTC(0);
|
|
|
635 |
}
|
|
|
636 |
} else {
|
|
|
637 |
/* 24 bits image */
|
|
|
638 |
BMPDW(0); /* null bmiColors */
|
|
|
639 |
for (pad = 0; ((3*width+pad) & 3) != 0; pad++); /*padding*/
|
|
|
640 |
for (i = height-1 ; i>=0 ; i--) {
|
|
|
641 |
for (j = 0 ; j < width ; j++) {
|
|
|
642 |
col = gp(d, i, j) & 0xFFFFFF;
|
|
|
643 |
BMPPUTC(GETBLUE(col));
|
|
|
644 |
BMPPUTC(GETGREEN(col));
|
|
|
645 |
BMPPUTC(GETRED(col));
|
|
|
646 |
}
|
|
|
647 |
for (j = 0; j < pad; j++) BMPPUTC(0);
|
|
|
648 |
}
|
| 6458 |
guido |
649 |
}
|
| 44570 |
ripley |
650 |
return 1;
|
| 6458 |
guido |
651 |
}
|