The R Project SVN R

Rev

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

Rev 44498 Rev 44542
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, 2004  Guido Masarotto and the R Development Core Team
3
 *  Copyright (C) 1999-2008  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 39... Line 39...
39
#include <stdlib.h>
39
#include <stdlib.h>
40
#include <setjmp.h>
40
#include <setjmp.h>
41
 
41
 
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) & 0xFFU)
44
#define GETRED(col)    (((col) >> RSHIFT) & 0xFF)
45
#define GETGREEN(col)  (((col) >> GSHIFT) & 0xFFU)
45
#define GETGREEN(col)  (((col) >> GSHIFT) & 0xFF)
46
#define GETBLUE(col)   (((col) >> BSHIFT) & 0xFFU)
46
#define GETBLUE(col)   (((col) >> BSHIFT) & 0xFF)
47
 
47
 
48
#include <R_ext/Error.h>
48
#include <R_ext/Error.h>
49
 
49
 
50
#ifdef HAVE_PNG
50
#ifdef HAVE_PNG
51
 
51
 
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
 
-
 
77
__declspec(dllexport)
75
__declspec(dllexport)
78
int R_SaveAsPng(void  *d, int width, int height, 
76
int R_SaveAsPng(void  *d, int width, int height, 
79
		unsigned int (*gp)(void *, int, int),
77
		unsigned int (*gp)(void *, int, int),
80
		int bgr, FILE *fp, unsigned int transparent, int res) 
78
		int bgr, FILE *fp, unsigned int transparent, int res) 
81
{
79
{
82
  png_structp png_ptr;
80
  png_structp png_ptr;
83
  png_infop info_ptr;
81
  png_infop info_ptr;
84
  unsigned int  col, palette[256];
82
  unsigned int  col, palette[256];
85
  png_color pngpalette[256];
83
  png_color pngpalette[256];
86
  png_bytep pscanline, scanline = calloc(3*width,sizeof(png_byte));
84
  png_bytep pscanline, scanline = (png_bytep) calloc(3*width,sizeof(png_byte));
87
  png_byte trans[256];
85
  png_byte trans[256];
88
  png_color_16 trans_values[1];
86
  png_color_16 trans_values[1];
89
  int i, j, r, ncols, mid, high, low, withpalette;
87
  int i, j, r, ncols, mid, high, low, withpalette;
90
  DECLARESHIFTS;
88
  DECLARESHIFTS;
91
 
89
 
Line 131... Line 129...
131
  if(transparent) palette[ncols++] = transparent & 0xFFFFFFUL;
129
  if(transparent) palette[ncols++] = transparent & 0xFFFFFFUL;
132
  mid = ncols;
130
  mid = ncols;
133
  withpalette = 1;
131
  withpalette = 1;
134
  for (i = 0; (i < height) && withpalette ; i++) {
132
  for (i = 0; (i < height) && withpalette ; i++) {
135
    for (j = 0; (j < width) && withpalette ; j++) {
133
    for (j = 0; (j < width) && withpalette ; j++) {
136
      col = gp(d,i,j) & 0xFFFFFFU ;
134
      col = gp(d,i,j) & 0xFFFFFF ;
137
      /* binary search the palette: */
135
      /* binary search the palette: */
138
      low = 0;  
136
      low = 0;  
139
      high = ncols - 1;
137
      high = ncols - 1;
140
      while (low <= high) {
138
      while (low <= high) {
141
	mid = (low + high)/2;
139
	mid = (low + high)/2;
Line 181... Line 179...
181
  } 
179
  } 
182
  /* Deal with transparency */
180
  /* Deal with transparency */
183
  if(transparent) {
181
  if(transparent) {
184
      if(withpalette) {
182
      if(withpalette) {
185
	  for (i = 0; i < ncols ; i++)
183
	  for (i = 0; i < ncols ; i++)
186
	      trans[i] = (palette[i] == (transparent & 0xFFFFFFU)) ? 0:255;
184
	      trans[i] = (palette[i] == (transparent & 0xFFFFFF)) ? 0:255;
187
      } else {
185
      } else {
188
	  trans_values[0].red = GETRED(transparent);
186
	  trans_values[0].red = GETRED(transparent);
189
	  trans_values[0].blue = GETBLUE(transparent);
187
	  trans_values[0].blue = GETBLUE(transparent);
190
	  trans_values[0].green = GETGREEN(transparent);
188
	  trans_values[0].green = GETGREEN(transparent);
191
      }
189
      }
Line 204... Line 202...
204
   */
202
   */
205
  for (i=0 ; i<height ; i++) { 
203
  for (i=0 ; i<height ; i++) { 
206
    /* Build the scanline */
204
    /* Build the scanline */
207
    pscanline = scanline;
205
    pscanline = scanline;
208
    for ( j=0 ; j<width ; j++) {
206
    for ( j=0 ; j<width ; j++) {
209
      col = gp(d, i, j) & 0xFFFFFFU;
207
      col = gp(d, i, j) & 0xFFFFFF;
210
      if (withpalette) { 
208
      if (withpalette) { 
211
	    /* binary search the palette (the colour must be there): */
209
	    /* binary search the palette (the colour must be there): */
212
	    low = 0;  high = ncols - 1;
210
	    low = 0;  high = ncols - 1;
213
	    while (low <= high) {
211
	    while (low <= high) {
214
		mid = (low + high)/2;
212
		mid = (low + high)/2;
Line 289... Line 287...
289
		int bgr, int quality, FILE *outfile, int res) 
287
		int bgr, int quality, FILE *outfile, int res) 
290
{
288
{
291
  struct jpeg_compress_struct cinfo;
289
  struct jpeg_compress_struct cinfo;
292
  struct my_error_mgr jerr;
290
  struct my_error_mgr jerr;
293
  /* More stuff */
291
  /* More stuff */
294
  JSAMPLE *pscanline, *scanline = calloc(3*width,sizeof(JSAMPLE));
292
  JSAMPLE *pscanline, *scanline = (JSAMPLE *) calloc(3*width,sizeof(JSAMPLE));
295
  int i, j;
293
  int i, j;
296
  unsigned int col;
294
  unsigned int col;
297
  DECLARESHIFTS;
295
  DECLARESHIFTS;
298
 
296
 
299
  /* Have we enough memory?*/
297
  /* Have we enough memory?*/
Line 347... Line 345...
347
  /*           jpeg_write_scanlines(...); */
345
  /*           jpeg_write_scanlines(...); */
348
  for (i=0 ; i<height ; i++) { 
346
  for (i=0 ; i<height ; i++) { 
349
  /* Build the scanline */
347
  /* Build the scanline */
350
    pscanline = scanline;
348
    pscanline = scanline;
351
    for ( j=0 ; j<width ; j++) {
349
    for ( j=0 ; j<width ; j++) {
352
      col = gp(d, i, j) & 0xFFFFFFU;
350
      col = gp(d, i, j) & 0xFFFFFF;
353
      *pscanline++ = GETRED(col) ;
351
      *pscanline++ = GETRED(col) ;
354
      *pscanline++ = GETGREEN(col) ;
352
      *pscanline++ = GETGREEN(col) ;
355
      *pscanline++ = GETBLUE(col) ;
353
      *pscanline++ = GETBLUE(col) ;
356
    }
354
    }
357
    jpeg_write_scanlines(&cinfo, (JSAMPARRAY) &scanline, 1);
355
    jpeg_write_scanlines(&cinfo, (JSAMPARRAY) &scanline, 1);