The R Project SVN R

Rev

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

Rev 10960 Rev 15641
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  Guido Masarotto and the R Development Core Team
3
 *  Copyright (C) 1999, 2001  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 73... Line 73...
73
  warning("libpng: %s",(char *) msg);
73
  warning("libpng: %s",(char *) msg);
74
}
74
}
75
 
75
 
76
int R_SaveAsPng(void  *d, int width, int height, 
76
int R_SaveAsPng(void  *d, int width, int height, 
77
		unsigned long (*gp)(void *, int, int),
77
		unsigned long (*gp)(void *, int, int),
78
		int bgr, FILE *fp) 
78
		int bgr, FILE *fp, unsigned int transparent) 
79
{
79
{
80
  png_structp png_ptr;
80
  png_structp png_ptr;
81
  png_infop info_ptr;
81
  png_infop info_ptr;
82
  unsigned long  col, palette[256];
82
  unsigned long  col, palette[256];
83
  png_color pngpalette[256];
83
  png_color pngpalette[256];
84
  png_bytep pscanline, scanline = calloc(3*width,sizeof(png_byte));
84
  png_bytep pscanline, scanline = calloc(3*width,sizeof(png_byte));
-
 
85
  png_byte trans[256];
-
 
86
  png_color_16 trans_values[1];
85
  int i, j, r, ncols, mid, high, low, withpalette;
87
  int i, j, r, ncols, mid, high, low, withpalette;
86
  DECLARESHIFTS;
88
  DECLARESHIFTS;
87
 
89
 
88
  /* Have we enough memory?*/
90
  /* Have we enough memory?*/
89
  if (scanline == NULL) 
91
  if (scanline == NULL) 
Line 121... Line 123...
121
  png_set_error_fn(png_ptr, NULL, my_png_error, my_png_warning);
123
  png_set_error_fn(png_ptr, NULL, my_png_error, my_png_warning);
122
  
124
  
123
  /* I/O initialization functions is REQUIRED */
125
  /* I/O initialization functions is REQUIRED */
124
  png_init_io(png_ptr, fp);
126
  png_init_io(png_ptr, fp);
125
  /* Have we less than 256 different colors? */
127
  /* Have we less than 256 different colors? */
126
  ncols = mid = 0;
128
  ncols = 0;
-
 
129
  if(transparent) palette[ncols++] = transparent & 0xFFFFFFUL;
-
 
130
  mid = ncols;
127
  withpalette = 1;
131
  withpalette = 1;
128
  for (i = 0; (i < height) && withpalette ; i++) {
132
  for (i = 0; (i < height) && withpalette ; i++) {
129
    for (j = 0; (j < width) && withpalette ; j++) {
133
    for (j = 0; (j < width) && withpalette ; j++) {
130
      col = gp(d,i,j) & 0xFFFFFFUL ;
134
      col = gp(d,i,j) & 0xFFFFFFUL ;
131
      /* binary search the palette: */
135
      /* binary search the palette: */
Line 161... Line 165...
161
   */
165
   */
162
  png_set_IHDR(png_ptr, info_ptr, width, height, 8, 
166
  png_set_IHDR(png_ptr, info_ptr, width, height, 8, 
163
	       withpalette ? PNG_COLOR_TYPE_PALETTE : PNG_COLOR_TYPE_RGB,
167
	       withpalette ? PNG_COLOR_TYPE_PALETTE : PNG_COLOR_TYPE_RGB,
164
	       PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, 
168
	       PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, 
165
	       PNG_FILTER_TYPE_BASE);
169
	       PNG_FILTER_TYPE_BASE);
-
 
170
 
166
  if (withpalette) {
171
  if (withpalette) {
167
    for (i = 0; i < ncols ; i++) {
172
    for (i = 0; i < ncols ; i++) {
168
      col = palette[i];
173
      col = palette[i];
169
      pngpalette[i].red = GETRED(col);
174
      pngpalette[i].red = GETRED(col);
170
      pngpalette[i].green = GETGREEN(col);
175
      pngpalette[i].green = GETGREEN(col);
171
      pngpalette[i].blue = GETBLUE(col);
176
      pngpalette[i].blue = GETBLUE(col);
172
    } 
177
    } 
173
    png_set_PLTE(png_ptr, info_ptr, pngpalette, ncols);
178
    png_set_PLTE(png_ptr, info_ptr, pngpalette, ncols);
174
  } 
179
  } 
-
 
180
  /* Deal with transparency */
-
 
181
  if(transparent) {
-
 
182
      if(withpalette) {
-
 
183
	  for (i = 0; i < ncols ; i++)
-
 
184
	      trans[i] = (palette[i] == (transparent & 0xFFFFFFUL)) ? 0:255;
-
 
185
      } else {
-
 
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
 
175
  /* Write the file header information.  REQUIRED */
193
  /* Write the file header information.  REQUIRED */
176
  png_write_info(png_ptr, info_ptr);
194
  png_write_info(png_ptr, info_ptr);
177
 
195
 
178
  /* 
196
  /* 
179
   * Now, write the pixels
197
   * Now, write the pixels