/* PNGWRAPPER! This file is included in QuteMol project as temporary hack waiting for WxWidgets to support alpha channel in PNG writing. */ #include #include #include #include #include #include using namespace std; #include #include "progress.h" typedef Byte byte; void downsample2x2(byte * data, int sx, int sy){ int j=0; for (int y=0; y PNG_COLOR_TYPE_GRAY (bit depths 1, 2, 4, 8, 16) // 2 -> PNG_COLOR_TYPE_GRAY_ALPHA (bit depths 8, 16) // 3 -> PNG_COLOR_TYPE_RGB (bit_depths 8, 16) // 4 -> PNG_COLOR_TYPE_RGB_ALPHA (bit_depths 8, 16) int pixel_type = 1; switch(type) { case 1: pixel_type = PNG_COLOR_TYPE_GRAY; break; case 2: pixel_type = PNG_COLOR_TYPE_GRAY_ALPHA; break; case 3: pixel_type = PNG_COLOR_TYPE_RGB; break; case 4: pixel_type = PNG_COLOR_TYPE_RGB_ALPHA; break; } //TRACE("sx %d, sy %d, depth %d, pixel_type %d\n", sx, sy, depth, pixel_type); png_set_IHDR(png_ptr, png_info_ptr, sx, sy, depth, pixel_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); //TRACE("png_set_IHDR\n"); png_write_info(png_ptr, png_info_ptr); //TRACE("png_write_info\n"); //TRACE("uffa");*/ char **rows = new char *[sy]; for(int i = 0; i < sy; i++) rows[i] = (char *)data + i * type * (depth/8) * sx; if(reverse) { if (sy>200) StartProgress("Saving PNG", sy); for(int r = sy -1; r >= 0; r--) { if (r%100) { if (!UpdateProgress(sy-r)) { fclose(fp); return false; } } png_write_rows(png_ptr, (png_byte **)(&(rows[r])), 1); } EndProgress(); } else { png_write_rows(png_ptr, (png_byte **)(rows), sy); } delete []rows; TRACE("png_write_rows\n"); png_write_end(png_ptr, png_info_ptr); TRACE("png_write_end\n"); png_destroy_write_struct(&png_ptr, &png_info_ptr); TRACE("png_destroy_write\n"); fclose(fp); return true; }