44 void png_read_vfile(png_structp png_ptr, png_bytep data, png_size_t byte_count)
47 vfile->
read(data, byte_count);
49 void png_write_vfile(png_structp png_ptr, png_bytep data, png_size_t byte_count)
52 vfile->
write(data,byte_count);
54 void png_flush_vfile(png_structp )
58 void vl_error_fn(png_structp , png_const_charp error_msg)
62 void vl_warning_fn(png_structp , png_const_charp warning_msg)
93 png_uint_32 width, height;
94 int bit_depth, color_type;
102 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL,
NULL,
NULL);
103 png_set_error_fn(png_ptr, png_get_error_ptr(png_ptr), vl_error_fn, vl_warning_fn);
112 info_ptr = png_create_info_struct(png_ptr);
113 endinfo = png_create_info_struct(png_ptr);
120 if (setjmp(png_jmpbuf(png_ptr)))
123 png_destroy_read_struct(&png_ptr, &info_ptr, &endinfo);
130 unsigned char header[8];
131 int count = (int)file->
read(header,8);
132 if (count == 8 && png_check_sig(header, 8))
134 png_set_read_fn(png_ptr,file,png_read_vfile);
135 png_set_sig_bytes(png_ptr, 8);
139 png_destroy_read_struct(&png_ptr, &info_ptr, &endinfo);
147 png_read_info(png_ptr, info_ptr);
149 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
NULL,
NULL,
NULL);
200 png_set_packing(png_ptr);
207 if (color_type == PNG_COLOR_TYPE_PALETTE)
208 png_set_palette_to_rgb(png_ptr);
211 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
212 png_set_expand_gray_1_2_4_to_8(png_ptr);
218 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
219 png_set_tRNS_to_alpha(png_ptr);
229 png_color_16 my_background, *image_background;
230 if (png_get_bKGD(png_ptr, info_ptr, &image_background))
231 png_set_background(png_ptr, image_background, PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
233 png_set_background(png_ptr, &my_background, PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
237 double screen_gamma = 2.2;
250 if (png_get_gAMA(png_ptr, info_ptr, &image_gamma))
251 png_set_gamma(png_ptr, screen_gamma, image_gamma);
253 png_set_gamma(png_ptr, screen_gamma, 1.0/screen_gamma);
259 if (color_type & PNG_COLOR_MASK_COLOR)
264 * This reduces the image to the application supplied palette */
268 png_color std_color_cube[MAX_SCREEN_COLORS];
270 png_set_dither(png_ptr, std_color_cube, MAX_SCREEN_COLORS,
271 MAX_SCREEN_COLORS, png_uint_16p_NULL, 0);
273 * This reduces the image to the palette supplied in the file */
275 if (png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette))
277 png_uint_16p histogram =
NULL;
279 png_get_hIST(png_ptr, info_ptr, &histogram);
281 png_set_dither(png_ptr, palette, num_palette,
282 max_screen_colors, histogram, 0);
289 png_set_invert_mono(png_ptr);
297 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
299 png_color_8p sig_bit;
301 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
302 png_set_shift(png_ptr, sig_bit);
308 if (color_type & PNG_COLOR_MASK_COLOR)
309 png_set_bgr(png_ptr);
314 png_set_swap_alpha(png_ptr);
319 unsigned short bet = 0x00FF;
320 bool little_endian_cpu = ((
unsigned char*)&bet)[0] == 0xFF;
321 if (little_endian_cpu && bit_depth > 8)
322 png_set_swap(png_ptr);
327 png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
364 std::vector<png_bytep> row_p;
365 row_p.resize(height);
366 for(
unsigned i=0; i<height; ++i)
367 row_p[height - 1 - i] = (png_bytep)img->
pixels()+img->
pitch()*i;
369 png_read_image(png_ptr, &row_p[0]);
370 png_read_end(png_ptr, endinfo);
373 png_destroy_read_struct(&png_ptr, &info_ptr, &endinfo);
387 unsigned char header[8];
388 int count = (int)file->
read(header,8);
390 if (count == 8 && png_check_sig(header, 8))
410 int w = src->
width();
412 int d = src->
depth();
426 Log::error(
Say(
"savePNG('%s'): could not convert image to IT_UNSIGNED_SHORT.\n") << fout->
path() );
438 Log::error(
Say(
"savePNG('%s'): could not convert image to IF_RGBA.\n") << fout->
path() );
452 png_structp png =
NULL;
453 png_infop info =
NULL;
455 png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
NULL,
NULL,
NULL);
459 info = png_create_info_struct(png);
463 png_set_write_fn(png,fout,png_write_vfile,png_flush_vfile);
465 png_set_compression_level(png, compression);
467 std::vector< png_bytep > rows;
469 for(
int i=0, y = h-1; i<h; ++i,--y)
470 rows[i] = (png_bytep)(src->
pixels()+src->
pitch()*y);
475 case IF_RGB: color = PNG_COLOR_TYPE_RGB;
break;
476 case IF_RGBA: color = PNG_COLOR_TYPE_RGB_ALPHA;
break;
486 png_set_IHDR( png, info, w, h, bit_depth, color,
487 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
489 unsigned short bet = 0x00FF;
490 bool little_endian_cpu = ((
unsigned char*)&bet)[0] == 0xFF;
491 if (little_endian_cpu && bit_depth == 16)
493 png_set_rows(png, info, &rows[0]);
494 png_write_png(png, info, PNG_TRANSFORM_SWAP_ENDIAN,
NULL);
498 png_write_info(png, info);
499 png_write_image(png, &rows[0]);
500 png_write_end(png,
NULL);
503 png_destroy_write_struct(&png,&info);
long long read(void *buffer, long long byte_count)
Reads byte_count bytes from a file. Returns the number of bytes actually read.
VLCORE_EXPORT FileSystem * defFileSystem()
Returns the default FileSystem used by VisualizationLibrary.
An abstract class representing a file.
A simple String formatting class.
const unsigned char * pixels() const
Raw pointer to pixels.
void setObjectName(const char *name)
The name of the object, by default set to the object's class name in debug builds.
static void warning(const String &message)
Use this function to provide information about situations that might lead to errors or loss of data...
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
ref< Image > convertType(EImageType new_type) const
Converts the type() of an image.
static void error(const String &message)
Use this function to provide information about run-time errors: file not found, out of memory...
virtual ref< VirtualFile > locateFile(const String &full_path, const String &alternate_path=String()) const
Looks for a VirtualFile on the disk and in the currently active FileSystem.
virtual void close()=0
Closes the file.
const String & path() const
Returns the path of the file.
Visualization Library main namespace.
VLCORE_EXPORT bool savePNG(const Image *src, const String &path, int compression=6)
long long write(const void *buffer, long long byte_count)
Writes byte_count bytes to a file. Returns the number of bytes actually written.
virtual bool open(EOpenMode mode)=0
Opens the file in the specified mode.
VLCORE_EXPORT bool isPNG(VirtualFile *file)
ref< Image > convertFormat(EImageFormat new_format) const
Converts the format() of an image.
A VirtualFile that operates on regular disk files.
Implements a generic 1d, 2d, 3d and cubemap image that can have mipmaps.
The ref<> class is used to reference-count an Object.
void allocate2D(int x, int y, int bytealign, EImageFormat format, EImageType type)
std::string toStdString() const
Returns a UTF8 encoded std::string.
VLCORE_EXPORT ref< Image > loadPNG(VirtualFile *file)
EImageFormat format() const