The R Project SVN R

Rev

Rev 29864 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 29864 Rev 30691
Line 1... Line 1...
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  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.
Line 70... Line 70...
70
static void my_png_warning(png_structp png_ptr, png_const_charp msg) 
70
static void my_png_warning(png_structp png_ptr, png_const_charp msg) 
71
{
71
{
72
  warning("libpng: %s",(char *) msg);
72
  warning("libpng: %s",(char *) msg);
73
}
73
}
74
 
74
 
-
 
75
#define CN (100.0/2.54)
-
 
76
 
75
int R_SaveAsPng(void  *d, int width, int height, 
77
int R_SaveAsPng(void  *d, int width, int height, 
76
		unsigned long (*gp)(void *, int, int),
78
		unsigned long (*gp)(void *, int, int),
77
		int bgr, FILE *fp, unsigned int transparent) 
79
		int bgr, FILE *fp, unsigned int transparent, int res) 
78
{
80
{
79
  png_structp png_ptr;
81
  png_structp png_ptr;
80
  png_infop info_ptr;
82
  png_infop info_ptr;
81
  unsigned long  col, palette[256];
83
  unsigned long  col, palette[256];
82
  png_color pngpalette[256];
84
  png_color pngpalette[256];
Line 187... Line 189...
187
	  trans_values[0].green = GETGREEN(transparent);
189
	  trans_values[0].green = GETGREEN(transparent);
188
      }
190
      }
189
      png_set_tRNS(png_ptr, info_ptr, trans, ncols, trans_values);
191
      png_set_tRNS(png_ptr, info_ptr, trans, ncols, trans_values);
190
  }
192
  }
191
 
193
 
-
 
194
  if(res > 0) 
-
 
195
      png_set_pHYs(png_ptr, info_ptr, res/0.0254, res/0.0254,
-
 
196
		   PNG_RESOLUTION_METER);
-
 
197
 
192
  /* Write the file header information.  REQUIRED */
198
  /* Write the file header information.  REQUIRED */
193
  png_write_info(png_ptr, info_ptr);
199
  png_write_info(png_ptr, info_ptr);
194
 
200
 
195
  /* 
201
  /* 
196
   * Now, write the pixels
202
   * Now, write the pixels
Line 276... Line 282...
276
 
282
 
277
 
283
 
278
 
284
 
279
int R_SaveAsJpeg(void  *d, int width, int height, 
285
int R_SaveAsJpeg(void  *d, int width, int height, 
280
		unsigned long (*gp)(void *, int, int),
286
		unsigned long (*gp)(void *, int, int),
281
		int bgr, int quality, FILE *outfile) 
287
		int bgr, int quality, FILE *outfile, int res) 
282
{
288
{
283
  struct jpeg_compress_struct cinfo;
289
  struct jpeg_compress_struct cinfo;
284
  struct my_error_mgr jerr;
290
  struct my_error_mgr jerr;
285
  /* More stuff */
291
  /* More stuff */
286
  JSAMPLE *pscanline, *scanline = calloc(3*width,sizeof(JSAMPLE));
292
  JSAMPLE *pscanline, *scanline = calloc(3*width,sizeof(JSAMPLE));
Line 324... Line 330...
324
  cinfo.image_width = width; 	/* image width and height, in pixels */
330
  cinfo.image_width = width; 	/* image width and height, in pixels */
325
  cinfo.image_height = height;
331
  cinfo.image_height = height;
326
  cinfo.input_components = 3;		/* # of color components per pixel */
332
  cinfo.input_components = 3;		/* # of color components per pixel */
327
  cinfo.in_color_space = JCS_RGB; 	/* colorspace of input image */
333
  cinfo.in_color_space = JCS_RGB; 	/* colorspace of input image */
328
  jpeg_set_defaults(&cinfo);
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
  }
329
  jpeg_set_quality(&cinfo, quality, TRUE);
340
  jpeg_set_quality(&cinfo, quality, TRUE);
330
  /* Step 4: Start compressor */
341
  /* Step 4: Start compressor */
331
  jpeg_start_compress(&cinfo, TRUE);
342
  jpeg_start_compress(&cinfo, TRUE);
332
 
343
 
333
  /* Step 5: while (scan lines remain to be written) */
344
  /* Step 5: while (scan lines remain to be written) */
Line 373... Line 384...
373
#define BMPLONG(a) {lng=a;if(fwrite(&lng,sizeof(long),1,fp)!=1) BMPERROR}
384
#define BMPLONG(a) {lng=a;if(fwrite(&lng,sizeof(long),1,fp)!=1) BMPERROR}
374
#define BMPPUTC(a) if(fputc(a,fp)==EOF) BMPERROR;
385
#define BMPPUTC(a) if(fputc(a,fp)==EOF) BMPERROR;
375
#define HEADERSIZE 54
386
#define HEADERSIZE 54
376
 
387
 
377
int R_SaveAsBmp(void  *d, int width, int height, 
388
int R_SaveAsBmp(void  *d, int width, int height, 
378
		unsigned long (*gp)(void *, int, int), int bgr, FILE *fp) 
389
		unsigned long (*gp)(void *, int, int), int bgr, FILE *fp,
-
 
390
		int res) 
379
{
391
{
380
  unsigned long  col, palette[256];
392
  unsigned long  col, palette[256];
381
  int i, j, r, ncols, mid, high, low, withpalette;
393
  int i, j, r, ncols, mid, high, low, withpalette;
382
  int bfOffBits, bfSize, biBitCount, biClrUsed , pad;
394
  int bfOffBits, bfSize, biBitCount, biClrUsed , pad;
383
  unsigned short wrd;
395
  unsigned short wrd;
384
  unsigned long dwrd;
396
  unsigned long dwrd;
385
  long lng;
397
  long lng, lres;
386
  DECLARESHIFTS;
398
  DECLARESHIFTS;
387
 
399
 
388
  /* Have we less than 256 different colors? */
400
  /* Have we less than 256 different colors? */
389
  ncols = mid = 0;
401
  ncols = mid = 0;
390
  withpalette = 1;
402
  withpalette = 1;
Line 437... Line 449...
437
  BMPLONG(height); /* biHeight */
449
  BMPLONG(height); /* biHeight */
438
  BMPW(1);	/* biPlanes - must be 1 */
450
  BMPW(1);	/* biPlanes - must be 1 */
439
  BMPW(biBitCount); /* biBitCount */
451
  BMPW(biBitCount); /* biBitCount */
440
  BMPDW(0); /* biCompression=BI_RGB */
452
  BMPDW(0); /* biCompression=BI_RGB */
441
  BMPDW(0); /* biSizeImage (with BI_RGB not needed)*/
453
  BMPDW(0); /* biSizeImage (with BI_RGB not needed)*/
-
 
454
  lres = (long)(0.5 + res/0.0254);
442
  BMPLONG(0); /* XPels/M <- used only by Windows?*/
455
  BMPLONG(lres); /* XPels/M <- used only by Windows?*/
443
  BMPLONG(0); /* XPels/M */
456
  BMPLONG(lres); /* XPels/M */
444
  BMPDW(biClrUsed); /* biClrUsed */
457
  BMPDW(biClrUsed); /* biClrUsed */
445
  BMPDW(0) ; /* biClrImportant All colours are important */
458
  BMPDW(0) ; /* biClrImportant All colours are important */
446
 
459
 
447
  /* and now the image */
460
  /* and now the image */
448
  if (withpalette) {
461
  if (withpalette) {