1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
/******************************************************************************
* $Id: mapimageio.c 9105 2009-06-15 19:09:44Z warmerdam $
*
* Project: MapServer
* Purpose: Low level PNG/JPEG image io functions independent of GD.
* Author: Thomas Bonfort (tbonfort)
*
******************************************************************************
* Copyright (c) 2009 Thomas Bonfort
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies of this Software or works derived from this Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "png.h"
#include "setjmp.h"
#include "mapserver.h"
#include "jpeglib.h"
MS_CVSID("$Id: mapimageio.c 9105 2009-06-15 19:09:44Z warmerdam $")
typedef struct _streamInfo {
FILE *fp;
bufferObj *buffer;
} streamInfo;
void png_write_data_to_stream(png_structp png_ptr, png_bytep data, png_size_t length) {
FILE *fp = ((streamInfo*)png_get_io_ptr(png_ptr))->fp;
fwrite(data,length,1,fp);
}
void png_write_data_to_buffer(png_structp png_ptr, png_bytep data, png_size_t length) {
bufferObj *buffer = ((streamInfo*)png_get_io_ptr(png_ptr))->buffer;
msBufferAppend(buffer,data,length);
}
void png_flush_data(png_structp png_ptr) {
// do nothing
}
typedef struct {
struct jpeg_destination_mgr pub;
unsigned char *data;
} ms_destination_mgr;
typedef struct {
ms_destination_mgr mgr;
FILE *stream;
} ms_stream_destination_mgr;
typedef struct {
ms_destination_mgr mgr;
bufferObj *buffer;
} ms_buffer_destination_mgr;
#define OUTPUT_BUF_SIZE 4096
void
jpeg_init_destination (j_compress_ptr cinfo) {
ms_destination_mgr *dest = (ms_destination_mgr*) cinfo->dest;
/* Allocate the output buffer --- it will be released when done with image */
dest->data = (unsigned char *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
OUTPUT_BUF_SIZE * sizeof (unsigned char));
dest->pub.next_output_byte = dest->data;
dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
}
void jpeg_stream_term_destination (j_compress_ptr cinfo) {
ms_stream_destination_mgr *dest = (ms_stream_destination_mgr*) cinfo->dest;
fwrite(dest->mgr.data, OUTPUT_BUF_SIZE-dest->mgr.pub.free_in_buffer, 1, dest->stream);
dest->mgr.pub.next_output_byte = dest->mgr.data;
dest->mgr.pub.free_in_buffer = OUTPUT_BUF_SIZE;
}
void jpeg_buffer_term_destination (j_compress_ptr cinfo) {
ms_buffer_destination_mgr *dest = (ms_buffer_destination_mgr*) cinfo->dest;
msBufferAppend(dest->buffer, dest->mgr.data, OUTPUT_BUF_SIZE-dest->mgr.pub.free_in_buffer);
dest->mgr.pub.next_output_byte = dest->mgr.data;
dest->mgr.pub.free_in_buffer = OUTPUT_BUF_SIZE;
}
int jpeg_stream_empty_output_buffer (j_compress_ptr cinfo) {
ms_stream_destination_mgr *dest = (ms_stream_destination_mgr*) cinfo->dest;
fwrite(dest->mgr.data, OUTPUT_BUF_SIZE, 1, dest->stream);
dest->mgr.pub.next_output_byte = dest->mgr.data;
dest->mgr.pub.free_in_buffer = OUTPUT_BUF_SIZE;
return TRUE;
}
int jpeg_buffer_empty_output_buffer (j_compress_ptr cinfo) {
ms_buffer_destination_mgr *dest = (ms_buffer_destination_mgr*) cinfo->dest;
msBufferAppend(dest->buffer, dest->mgr.data, OUTPUT_BUF_SIZE);
dest->mgr.pub.next_output_byte = dest->mgr.data;
dest->mgr.pub.free_in_buffer = OUTPUT_BUF_SIZE;
return TRUE;
}
int saveAsJPEG(rasterBufferObj *data, streamInfo *info,int quality) {
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
ms_destination_mgr *dest;
JSAMPLE *rowdata;
unsigned int row;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
if (cinfo.dest == NULL)
{
if(info->fp) {
cinfo.dest = (struct jpeg_destination_mgr *)
(*cinfo.mem->alloc_small) ((j_common_ptr) &cinfo, JPOOL_PERMANENT,
sizeof (ms_stream_destination_mgr));
((ms_stream_destination_mgr*)cinfo.dest)->mgr.pub.empty_output_buffer = jpeg_stream_empty_output_buffer;
((ms_stream_destination_mgr*)cinfo.dest)->mgr.pub.term_destination = jpeg_stream_term_destination;
((ms_stream_destination_mgr*)cinfo.dest)->stream = info->fp;
} else {
cinfo.dest = (struct jpeg_destination_mgr *)
(*cinfo.mem->alloc_small) ((j_common_ptr) &cinfo, JPOOL_PERMANENT,
sizeof (ms_buffer_destination_mgr));
((ms_buffer_destination_mgr*)cinfo.dest)->mgr.pub.empty_output_buffer = jpeg_buffer_empty_output_buffer;
((ms_buffer_destination_mgr*)cinfo.dest)->mgr.pub.term_destination = jpeg_buffer_term_destination;
((ms_buffer_destination_mgr*)cinfo.dest)->buffer = info->buffer;
}
}
dest = (ms_destination_mgr*) cinfo.dest;
dest->pub.init_destination = jpeg_init_destination;
cinfo.image_width = data->width;
cinfo.image_height = data->height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE);
jpeg_start_compress(&cinfo, TRUE);
rowdata = (JSAMPLE*)malloc(data->width*cinfo.input_components*sizeof(JSAMPLE));
for(row=0;row<data->height;row++) {
JSAMPLE *pixptr = rowdata;
int col;
unsigned char *r,*g,*b;
r=data->r+row*data->row_step;
g=data->g+row*data->row_step;
b=data->b+row*data->row_step;
for(col=0;col<data->width;col++) {
*(pixptr++) = *r;
*(pixptr++) = *g;
*(pixptr++) = *b;
r+=data->pixel_step;
g+=data->pixel_step;
b+=data->pixel_step;
}
(void) jpeg_write_scanlines(&cinfo, &rowdata, 1);
}
/* Step 6: Finish compression */
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
free(rowdata);
return MS_SUCCESS;
}
int saveAsPNG(rasterBufferObj *data, streamInfo *info) {
png_infop info_ptr;
int color_type;
int row;
unsigned int *rowdata;
png_structp png_ptr = png_create_write_struct(
PNG_LIBPNG_VER_STRING, NULL,NULL,NULL);
if (!png_ptr)
return (MS_FAILURE);
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
png_destroy_write_struct(&png_ptr,
(png_infopp)NULL);
return (MS_FAILURE);
}
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_write_struct(&png_ptr, &info_ptr);
return (MS_FAILURE);
}
if(info->fp)
png_set_write_fn(png_ptr,info, png_write_data_to_stream, png_flush_data);
else
png_set_write_fn(png_ptr,info, png_write_data_to_buffer, png_flush_data);
if(data->a)
color_type = PNG_COLOR_TYPE_RGB_ALPHA;
else
color_type = PNG_COLOR_TYPE_RGB;
png_set_IHDR(png_ptr, info_ptr, data->width, data->height,
8, color_type, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_write_info(png_ptr, info_ptr);
if(!data->a && data->pixel_step==4)
png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
rowdata = (unsigned int*)malloc(data->width*sizeof(unsigned int));
for(row=0;row<data->height;row++) {
unsigned int *pixptr = rowdata;
int col;
unsigned char *a,*r,*g,*b;
r=data->r+row*data->row_step;
g=data->g+row*data->row_step;
b=data->b+row*data->row_step;
if(data->a) {
a=data->a+row*data->row_step;
for(col=0;col<data->width;col++) {
if(*a) {
unsigned char *pix = (unsigned char*)pixptr;
pix[0] = ((int)(*r) * 255) / *a;
pix[1] = ((int)(*g) * 255) / *a;
pix[2] = ((int)(*b) * 255) / *a;
pix[3] = *a;
} else {
*pixptr=0;
}
pixptr++;
a+=data->pixel_step;
r+=data->pixel_step;
g+=data->pixel_step;
b+=data->pixel_step;
}
} else {
for(col=0;col<data->width;col++) {
unsigned char *pix = (unsigned char*)pixptr;
pix[0] = *r;
pix[1] = *g;
pix[2] = *b;
pixptr++;
r+=data->pixel_step;
g+=data->pixel_step;
b+=data->pixel_step;
}
}
png_write_row(png_ptr,(png_bytep)rowdata);
}
png_write_end(png_ptr, info_ptr);
free(rowdata);
png_destroy_write_struct(&png_ptr, &info_ptr);
return MS_SUCCESS;
}
int msSaveRasterBuffer(rasterBufferObj *data, FILE *stream,
outputFormatObj *format) {
if(msCaseFindSubstring(format->driver,"/png")) {
streamInfo info;
info.fp = stream;
info.buffer = NULL;
return saveAsPNG(data,&info);
} else if(msCaseFindSubstring(format->driver,"/jpeg")) {
streamInfo info;
info.fp = stream;
info.buffer=NULL;
return saveAsJPEG(data,&info,atoi(msGetOutputFormatOption( format, "QUALITY", "75")));
} else {
msSetError(MS_MISCERR,"unsupported image format\n", "msSaveRasterBuffer()");
return MS_FAILURE;
}
}
int msSaveRasterBufferToBuffer(rasterBufferObj *data, bufferObj *buffer,
outputFormatObj *format) {
if(msCaseFindSubstring(format->driver,"/png")) {
streamInfo info;
info.fp = NULL;
info.buffer = buffer;
return saveAsPNG(data,&info);
} else if(msCaseFindSubstring(format->driver,"/jpeg")) {
streamInfo info;
info.fp = NULL;
info.buffer=buffer;
return saveAsJPEG(data,&info,atoi(msGetOutputFormatOption( format, "QUALITY", "75")));
} else {
msSetError(MS_MISCERR,"unsupported image format\n", "msSaveRasterBuffer()");
return MS_FAILURE;
}
}
|