/* GridFlow Copyright (c) 2001-2011 by Mathieu Bouchard This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. See file ../COPYING for further informations on licensing terms. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /* !@#$ not handling abort on compress */ /* !@#$ not handling abort on decompress */ #define PNG_INCLUDE_HERE #include "gridflow.hxx.fcs" \class FormatPNG : Format { P bit_packing; png_structp png; png_infop info; \constructor (t_symbol *mode, string filename) { Format::_0_open(mode,filename); uint32 mask[3] = {0x0000ff,0x00ff00,0xff0000}; bit_packing = new BitPacking(is_le(),3,3,mask); } \decl 0 bang (); \grin 0 int }; GRID_INLET(0) { if (in.dim.n!=3) RAISE("expecting 3 dimensions: rows,columns,channels"); int sc = in.dim[2]; if (sc<1 || sc>4) RAISE("expecting 1 to 4 channels (got %d)",sc); in.set_chunk(0); } GRID_FLOW { png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png) RAISE("!png"); info = png_create_info_struct(png); if (!info) {if (png) png_destroy_write_struct(&png, NULL); RAISE("!info");} if (setjmp(png_jmpbuf(png))) {png_destroy_write_struct(&png, &info); RAISE("png write error");} png_init_io(png, f); static int color_type[] = { PNG_COLOR_TYPE_GRAY, PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_RGB, PNG_COLOR_TYPE_RGBA, }; int sc = in.dim[2]; png_set_IHDR(png,info,in.dim[1],in.dim[0],8, color_type[sc-1], PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); png_write_info(png,info); png_set_packing(png); // this would have been the GRID_FLOW section int rowsize = in.dim[1]*sc; uint8 row[rowsize]; while (n) { for (int i=0; i