+++ /dev/null
-/*---------------------------------------------------------------------------\r
-\r
- wpng - simple PNG-writing program wpng.c\r
-\r
- This program converts certain NetPBM binary files (grayscale and RGB,\r
- maxval = 255) to PNG. Non-interlaced PNGs are written progressively;\r
- interlaced PNGs are read and written in one memory-intensive blast.\r
-\r
- Thanks to Jean-loup Gailly for providing the necessary trick to read\r
- interactive text from the keyboard while stdin is redirected. Thanks\r
- to Cosmin Truta for Cygwin fixes.\r
-\r
- NOTE: includes provisional support for PNM type "8" (portable alphamap)\r
- images, presumed to be a 32-bit interleaved RGBA format; no pro-\r
- vision for possible interleaved grayscale+alpha (16-bit) format.\r
- THIS IS UNLIKELY TO BECOME AN OFFICIAL NETPBM ALPHA FORMAT!\r
-\r
- to do:\r
- - delete output file if quit before calling any writepng routines\r
- - process backspace with -text option under DOS/Win? (currently get ^H)\r
-\r
- ---------------------------------------------------------------------------\r
-\r
- Changelog:\r
- - 1.01: initial public release\r
- - 1.02: modified to allow abbreviated options\r
- - 1.03: removed extraneous character from usage screen; fixed bug in\r
- command-line parsing\r
- - 1.04: fixed DOS/OS2/Win32 detection, including partial Cygwin fix\r
- (see http://home.att.net/~perlspinr/diffs/GregBook_cygwin.diff)\r
- - 2.00: dual-licensed (added GNU GPL)\r
-\r
- [REPORTED BUG (win32 only): "contrib/gregbook/wpng.c - cmd line\r
- dose not work! In order to do something useful I needed to redirect\r
- both input and output, with cygwin and with bcc32 as well. Under\r
- Linux, the same wpng appears to work fine. I don't know what is\r
- the problem."]\r
-\r
- ---------------------------------------------------------------------------\r
-\r
- Copyright (c) 1998-2007 Greg Roelofs. All rights reserved.\r
-\r
- This software is provided "as is," without warranty of any kind,\r
- express or implied. In no event shall the author or contributors\r
- be held liable for any damages arising in any way from the use of\r
- this software.\r
-\r
- The contents of this file are DUAL-LICENSED. You may modify and/or\r
- redistribute this software according to the terms of one of the\r
- following two licenses (at your option):\r
-\r
-\r
- LICENSE 1 ("BSD-like with advertising clause"):\r
-\r
- Permission is granted to anyone to use this software for any purpose,\r
- including commercial applications, and to alter it and redistribute\r
- it freely, subject to the following restrictions:\r
-\r
- 1. Redistributions of source code must retain the above copyright\r
- notice, disclaimer, and this list of conditions.\r
- 2. Redistributions in binary form must reproduce the above copyright\r
- notice, disclaimer, and this list of conditions in the documenta-\r
- tion and/or other materials provided with the distribution.\r
- 3. All advertising materials mentioning features or use of this\r
- software must display the following acknowledgment:\r
-\r
- This product includes software developed by Greg Roelofs\r
- and contributors for the book, "PNG: The Definitive Guide,"\r
- published by O'Reilly and Associates.\r
-\r
-\r
- LICENSE 2 (GNU GPL v2 or later):\r
-\r
- This program is free software; you can redistribute it and/or modify\r
- it under the terms of the GNU General Public License as published by\r
- the Free Software Foundation; either version 2 of the License, or\r
- (at your option) any later version.\r
-\r
- This program is distributed in the hope that it will be useful,\r
- but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
- GNU General Public License for more details.\r
-\r
- You should have received a copy of the GNU General Public License\r
- along with this program; if not, write to the Free Software Foundation,\r
- Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
-\r
- ---------------------------------------------------------------------------*/\r
-\r
-#define PROGNAME "wpng"\r
-#define VERSION "2.00 of 2 June 2007"\r
-#define APPNAME "Simple PGM/PPM/PAM to PNG Converter"\r
-\r
-#if defined(__MSDOS__) || defined(__OS2__)\r
-# define DOS_OS2_W32\r
-#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__)\r
-# ifndef __GNUC__ /* treat Win32 native ports of gcc as Unix environments */\r
-# define DOS_OS2_W32\r
-# endif\r
-#endif\r
-\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <string.h>\r
-#include <setjmp.h> /* for jmpbuf declaration in writepng.h */\r
-#include <time.h>\r
-\r
-#ifdef DOS_OS2_W32\r
-# include <io.h> /* for isatty(), setmode() prototypes */\r
-# include <fcntl.h> /* O_BINARY for fdopen() without text translation */\r
-# ifdef __EMX__\r
-# ifndef getch\r
-# define getch() _read_kbd(0, 1, 0) /* need getche() */\r
-# endif\r
-# else /* !__EMX__ */\r
-# ifdef __GO32__\r
-# include <pc.h>\r
-# define getch() getkey() /* GRR: need getche() */\r
-# else\r
-# include <conio.h> /* for getche() console input */\r
-# endif\r
-# endif /* ?__EMX__ */\r
-# define FGETS(buf,len,stream) dos_kbd_gets(buf,len)\r
-#else\r
-# include <unistd.h> /* for isatty() prototype */\r
-# define FGETS fgets\r
-#endif\r
-\r
-/* #define DEBUG : this enables the Trace() macros */\r
-\r
-/* #define FORBID_LATIN1_CTRL : this requires the user to re-enter any\r
- text that includes control characters discouraged by the PNG spec; text\r
- that includes an escape character (27) must be re-entered regardless */\r
-\r
-#include "writepng.h" /* typedefs, common macros, writepng prototypes */\r
-\r
-\r
-\r
-/* local prototypes */\r
-\r
-static int wpng_isvalid_latin1(uch *p, int len);\r
-static void wpng_cleanup(void);\r
-\r
-#ifdef DOS_OS2_W32\r
- static char *dos_kbd_gets(char *buf, int len);\r
-#endif\r
-\r
-\r
-\r
-static mainprog_info wpng_info; /* lone global */\r
-\r
-\r
-\r
-int main(int argc, char **argv)\r
-{\r
-#ifndef DOS_OS2_W32\r
- FILE *keybd;\r
-#endif\r
-#ifdef sgi\r
- FILE *tmpfile; /* or we could just use keybd, since no overlap */\r
- char tmpline[80];\r
-#endif\r
- char *inname = NULL, outname[256];\r
- char *p, pnmchar, pnmline[256];\r
- char *bgstr, *textbuf = NULL;\r
- ulg rowbytes;\r
- int rc, len = 0;\r
- int error = 0;\r
- int text = FALSE;\r
- int maxval;\r
- double LUT_exponent; /* just the lookup table */\r
- double CRT_exponent = 2.2; /* just the monitor */\r
- double default_display_exponent; /* whole display system */\r
- double default_gamma = 0.0;\r
-\r
-\r
- wpng_info.infile = NULL;\r
- wpng_info.outfile = NULL;\r
- wpng_info.image_data = NULL;\r
- wpng_info.row_pointers = NULL;\r
- wpng_info.filter = FALSE;\r
- wpng_info.interlaced = FALSE;\r
- wpng_info.have_bg = FALSE;\r
- wpng_info.have_time = FALSE;\r
- wpng_info.have_text = 0;\r
- wpng_info.gamma = 0.0;\r
-\r
-\r
- /* First get the default value for our display-system exponent, i.e.,\r
- * the product of the CRT exponent and the exponent corresponding to\r
- * the frame-buffer's lookup table (LUT), if any. If the PNM image\r
- * looks correct on the user's display system, its file gamma is the\r
- * inverse of this value. (Note that this is not an exhaustive list\r
- * of LUT values--e.g., OpenStep has a lot of weird ones--but it should\r
- * cover 99% of the current possibilities. This section must ensure\r
- * that default_display_exponent is positive.) */\r
-\r
-#if defined(NeXT)\r
- /* third-party utilities can modify the default LUT exponent */\r
- LUT_exponent = 1.0 / 2.2;\r
- /*\r
- if (some_next_function_that_returns_gamma(&next_gamma))\r
- LUT_exponent = 1.0 / next_gamma;\r
- */\r
-#elif defined(sgi)\r
- LUT_exponent = 1.0 / 1.7;\r
- /* there doesn't seem to be any documented function to\r
- * get the "gamma" value, so we do it the hard way */\r
- tmpfile = fopen("/etc/config/system.glGammaVal", "r");\r
- if (tmpfile) {\r
- double sgi_gamma;\r
-\r
- fgets(tmpline, 80, tmpfile);\r
- fclose(tmpfile);\r
- sgi_gamma = atof(tmpline);\r
- if (sgi_gamma > 0.0)\r
- LUT_exponent = 1.0 / sgi_gamma;\r
- }\r
-#elif defined(Macintosh)\r
- LUT_exponent = 1.8 / 2.61;\r
- /*\r
- if (some_mac_function_that_returns_gamma(&mac_gamma))\r
- LUT_exponent = mac_gamma / 2.61;\r
- */\r
-#else\r
- LUT_exponent = 1.0; /* assume no LUT: most PCs */\r
-#endif\r
-\r
- /* the defaults above give 1.0, 1.3, 1.5 and 2.2, respectively: */\r
- default_display_exponent = LUT_exponent * CRT_exponent;\r
-\r
-\r
- /* If the user has set the SCREEN_GAMMA environment variable as suggested\r
- * (somewhat imprecisely) in the libpng documentation, use that; otherwise\r
- * use the default value we just calculated. Either way, the user may\r
- * override this via a command-line option. */\r
-\r
- if ((p = getenv("SCREEN_GAMMA")) != NULL) {\r
- double exponent = atof(p);\r
-\r
- if (exponent > 0.0)\r
- default_gamma = 1.0 / exponent;\r
- }\r
-\r
- if (default_gamma == 0.0)\r
- default_gamma = 1.0 / default_display_exponent;\r
-\r
-\r
- /* Now parse the command line for options and the PNM filename. */\r
-\r
- while (*++argv && !error) {\r
- if (!strncmp(*argv, "-i", 2)) {\r
- wpng_info.interlaced = TRUE;\r
- } else if (!strncmp(*argv, "-time", 3)) {\r
- wpng_info.modtime = time(NULL);\r
- wpng_info.have_time = TRUE;\r
- } else if (!strncmp(*argv, "-text", 3)) {\r
- text = TRUE;\r
- } else if (!strncmp(*argv, "-gamma", 2)) {\r
- if (!*++argv)\r
- ++error;\r
- else {\r
- wpng_info.gamma = atof(*argv);\r
- if (wpng_info.gamma <= 0.0)\r
- ++error;\r
- else if (wpng_info.gamma > 1.01)\r
- fprintf(stderr, PROGNAME\r
- " warning: file gammas are usually less than 1.0\n");\r
- }\r
- } else if (!strncmp(*argv, "-bgcolor", 4)) {\r
- if (!*++argv)\r
- ++error;\r
- else {\r
- bgstr = *argv;\r
- if (strlen(bgstr) != 7 || bgstr[0] != '#')\r
- ++error;\r
- else {\r
- unsigned r, g, b; /* this way quiets compiler warnings */\r
-\r
- sscanf(bgstr+1, "%2x%2x%2x", &r, &g, &b);\r
- wpng_info.bg_red = (uch)r;\r
- wpng_info.bg_green = (uch)g;\r
- wpng_info.bg_blue = (uch)b;\r
- wpng_info.have_bg = TRUE;\r
- }\r
- }\r
- } else {\r
- if (**argv != '-') {\r
- inname = *argv;\r
- if (argv[1]) /* shouldn't be any more args after filename */\r
- ++error;\r
- } else\r
- ++error; /* not expecting any other options */\r
- }\r
- }\r
-\r
-\r
- /* open the input and output files, or register an error and abort */\r
-\r
- if (!inname) {\r
- if (isatty(0)) {\r
- fprintf(stderr, PROGNAME\r
- ": must give input filename or provide image data via stdin\n");\r
- ++error;\r
- } else {\r
-#ifdef DOS_OS2_W32\r
- /* some buggy C libraries require BOTH setmode() and fdopen(bin) */\r
- setmode(fileno(stdin), O_BINARY);\r
- setmode(fileno(stdout), O_BINARY);\r
-#endif\r
- if ((wpng_info.infile = fdopen(fileno(stdin), "rb")) == NULL) {\r
- fprintf(stderr, PROGNAME\r
- ": unable to reopen stdin in binary mode\n");\r
- ++error;\r
- } else\r
- if ((wpng_info.outfile = fdopen(fileno(stdout), "wb")) == NULL) {\r
- fprintf(stderr, PROGNAME\r
- ": unable to reopen stdout in binary mode\n");\r
- fclose(wpng_info.infile);\r
- ++error;\r
- } else\r
- wpng_info.filter = TRUE;\r
- }\r
- } else if ((len = strlen(inname)) > 250) {\r
- fprintf(stderr, PROGNAME ": input filename is too long [%d chars]\n",\r
- len);\r
- ++error;\r
- } else if (!(wpng_info.infile = fopen(inname, "rb"))) {\r
- fprintf(stderr, PROGNAME ": can't open input file [%s]\n", inname);\r
- ++error;\r
- }\r
-\r
- if (!error) {\r
- fgets(pnmline, 256, wpng_info.infile);\r
- if (pnmline[0] != 'P' || ((pnmchar = pnmline[1]) != '5' &&\r
- pnmchar != '6' && pnmchar != '8'))\r
- {\r
- fprintf(stderr, PROGNAME\r
- ": input file [%s] is not a binary PGM, PPM or PAM file\n",\r
- inname);\r
- ++error;\r
- } else {\r
- wpng_info.pnmtype = (int)(pnmchar - '0');\r
- if (wpng_info.pnmtype != 8)\r
- wpng_info.have_bg = FALSE; /* no need for bg if opaque */\r
- do {\r
- fgets(pnmline, 256, wpng_info.infile); /* lose any comments */\r
- } while (pnmline[0] == '#');\r
- sscanf(pnmline, "%ld %ld", &wpng_info.width, &wpng_info.height);\r
- do {\r
- fgets(pnmline, 256, wpng_info.infile); /* more comment lines */\r
- } while (pnmline[0] == '#');\r
- sscanf(pnmline, "%d", &maxval);\r
- if (wpng_info.width <= 0L || wpng_info.height <= 0L ||\r
- maxval != 255)\r
- {\r
- fprintf(stderr, PROGNAME\r
- ": only positive width/height, maxval == 255 allowed \n");\r
- ++error;\r
- }\r
- wpng_info.sample_depth = 8; /* <==> maxval 255 */\r
-\r
- if (!wpng_info.filter) {\r
- /* make outname from inname */\r
- if ((p = strrchr(inname, '.')) == NULL ||\r
- (p - inname) != (len - 4))\r
- {\r
- strcpy(outname, inname);\r
- strcpy(outname+len, ".png");\r
- } else {\r
- len -= 4;\r
- strncpy(outname, inname, len);\r
- strcpy(outname+len, ".png");\r
- }\r
- /* check if outname already exists; if not, open */\r
- if ((wpng_info.outfile = fopen(outname, "rb")) != NULL) {\r
- fprintf(stderr, PROGNAME ": output file exists [%s]\n",\r
- outname);\r
- fclose(wpng_info.outfile);\r
- ++error;\r
- } else if (!(wpng_info.outfile = fopen(outname, "wb"))) {\r
- fprintf(stderr, PROGNAME ": can't open output file [%s]\n",\r
- outname);\r
- ++error;\r
- }\r
- }\r
- }\r
- if (error) {\r
- fclose(wpng_info.infile);\r
- wpng_info.infile = NULL;\r
- if (wpng_info.filter) {\r
- fclose(wpng_info.outfile);\r
- wpng_info.outfile = NULL;\r
- }\r
- }\r
- }\r
-\r
-\r
- /* if we had any errors, print usage and die horrible death...arrr! */\r
-\r
- if (error) {\r
- fprintf(stderr, "\n%s %s: %s\n", PROGNAME, VERSION, APPNAME);\r
- writepng_version_info();\r
- fprintf(stderr, "\n"\r
-"Usage: %s [-gamma exp] [-bgcolor bg] [-text] [-time] [-interlace] pnmfile\n"\r
-"or: ... | %s [-gamma exp] [-bgcolor bg] [-text] [-time] [-interlace] | ...\n"\r
- " exp \ttransfer-function exponent (``gamma'') of the image in\n"\r
- "\t\t floating-point format (e.g., ``%.5f''); if image looks\n"\r
- "\t\t correct on given display system, image gamma is equal to\n"\r
- "\t\t inverse of display-system exponent, i.e., 1 / (LUT * CRT)\n"\r
- "\t\t (where LUT = lookup-table exponent and CRT = CRT exponent;\n"\r
- "\t\t first varies, second is usually 2.2, all are positive)\n"\r
- " bg \tdesired background color for alpha-channel images, in\n"\r
- "\t\t 7-character hex RGB format (e.g., ``#ff7700'' for orange:\n"\r
- "\t\t same as HTML colors)\n"\r
- " -text\tprompt interactively for text info (tEXt chunks)\n"\r
- " -time\tinclude a tIME chunk (last modification time)\n"\r
- " -interlace\twrite interlaced PNG image\n"\r
- "\n"\r
-"pnmfile or stdin must be a binary PGM (`P5'), PPM (`P6') or (extremely\n"\r
-"unofficial and unsupported!) PAM (`P8') file. Currently it is required\n"\r
-"to have maxval == 255 (i.e., no scaling). If pnmfile is specified, it\n"\r
-"is converted to the corresponding PNG file with the same base name but a\n"\r
-"``.png'' extension; files read from stdin are converted and sent to stdout.\n"\r
-"The conversion is progressive (low memory usage) unless interlacing is\n"\r
-"requested; in that case the whole image will be buffered in memory and\n"\r
-"written in one call.\n"\r
- "\n", PROGNAME, PROGNAME, default_gamma);\r
- exit(1);\r
- }\r
-\r
-\r
- /* prepare the text buffers for libpng's use; note that even though\r
- * PNG's png_text struct includes a length field, we don't have to fill\r
- * it out */\r
-\r
- if (text &&\r
-#ifndef DOS_OS2_W32\r
- (keybd = fdopen(fileno(stderr), "r")) != NULL &&\r
-#endif\r
- (textbuf = (char *)malloc((5 + 9)*75)) != NULL)\r
- {\r
- int i, valid, result;\r
-\r
- fprintf(stderr,\r
- "Enter text info (no more than 72 characters per line);\n");\r
- fprintf(stderr, "to skip a field, hit the <Enter> key.\n");\r
- /* note: just <Enter> leaves len == 1 */\r
-\r
- do {\r
- valid = TRUE;\r
- p = textbuf + TEXT_TITLE_OFFSET;\r
- fprintf(stderr, " Title: ");\r
- fflush(stderr);\r
- if (FGETS(p, 74, keybd) && (len = strlen(p)) > 1) {\r
- if (p[len-1] == '\n')\r
- p[--len] = '\0';\r
- wpng_info.title = p;\r
- wpng_info.have_text |= TEXT_TITLE;\r
- if ((result = wpng_isvalid_latin1((uch *)p, len)) >= 0) {\r
- fprintf(stderr, " " PROGNAME " warning: character code"\r
- " %u is %sdiscouraged by the PNG\n specification "\r
- "[first occurrence was at character position #%d]\n",\r
- (unsigned)p[result], (p[result] == 27)? "strongly " : "",\r
- result+1);\r
- fflush(stderr);\r
-#ifdef FORBID_LATIN1_CTRL\r
- wpng_info.have_text &= ~TEXT_TITLE;\r
- valid = FALSE;\r
-#else\r
- if (p[result] == 27) { /* escape character */\r
- wpng_info.have_text &= ~TEXT_TITLE;\r
- valid = FALSE;\r
- }\r
-#endif\r
- }\r
- }\r
- } while (!valid);\r
-\r
- do {\r
- valid = TRUE;\r
- p = textbuf + TEXT_AUTHOR_OFFSET;\r
- fprintf(stderr, " Author: ");\r
- fflush(stderr);\r
- if (FGETS(p, 74, keybd) && (len = strlen(p)) > 1) {\r
- if (p[len-1] == '\n')\r
- p[--len] = '\0';\r
- wpng_info.author = p;\r
- wpng_info.have_text |= TEXT_AUTHOR;\r
- if ((result = wpng_isvalid_latin1((uch *)p, len)) >= 0) {\r
- fprintf(stderr, " " PROGNAME " warning: character code"\r
- " %u is %sdiscouraged by the PNG\n specification "\r
- "[first occurrence was at character position #%d]\n",\r
- (unsigned)p[result], (p[result] == 27)? "strongly " : "",\r
- result+1);\r
- fflush(stderr);\r
-#ifdef FORBID_LATIN1_CTRL\r
- wpng_info.have_text &= ~TEXT_AUTHOR;\r
- valid = FALSE;\r
-#else\r
- if (p[result] == 27) { /* escape character */\r
- wpng_info.have_text &= ~TEXT_AUTHOR;\r
- valid = FALSE;\r
- }\r
-#endif\r
- }\r
- }\r
- } while (!valid);\r
-\r
- do {\r
- valid = TRUE;\r
- p = textbuf + TEXT_DESC_OFFSET;\r
- fprintf(stderr, " Description (up to 9 lines):\n");\r
- for (i = 1; i < 10; ++i) {\r
- fprintf(stderr, " [%d] ", i);\r
- fflush(stderr);\r
- if (FGETS(p, 74, keybd) && (len = strlen(p)) > 1)\r
- p += len; /* now points at NULL; char before is newline */\r
- else\r
- break;\r
- }\r
- if ((len = p - (textbuf + TEXT_DESC_OFFSET)) > 1) {\r
- if (p[-1] == '\n') {\r
- p[-1] = '\0';\r
- --len;\r
- }\r
- wpng_info.desc = textbuf + TEXT_DESC_OFFSET;\r
- wpng_info.have_text |= TEXT_DESC;\r
- p = textbuf + TEXT_DESC_OFFSET;\r
- if ((result = wpng_isvalid_latin1((uch *)p, len)) >= 0) {\r
- fprintf(stderr, " " PROGNAME " warning: character code"\r
- " %u is %sdiscouraged by the PNG\n specification "\r
- "[first occurrence was at character position #%d]\n",\r
- (unsigned)p[result], (p[result] == 27)? "strongly " : "",\r
- result+1);\r
- fflush(stderr);\r
-#ifdef FORBID_LATIN1_CTRL\r
- wpng_info.have_text &= ~TEXT_DESC;\r
- valid = FALSE;\r
-#else\r
- if (p[result] == 27) { /* escape character */\r
- wpng_info.have_text &= ~TEXT_DESC;\r
- valid = FALSE;\r
- }\r
-#endif\r
- }\r
- }\r
- } while (!valid);\r
-\r
- do {\r
- valid = TRUE;\r
- p = textbuf + TEXT_COPY_OFFSET;\r
- fprintf(stderr, " Copyright: ");\r
- fflush(stderr);\r
- if (FGETS(p, 74, keybd) && (len = strlen(p)) > 1) {\r
- if (p[len-1] == '\n')\r
- p[--len] = '\0';\r
- wpng_info.copyright = p;\r
- wpng_info.have_text |= TEXT_COPY;\r
- if ((result = wpng_isvalid_latin1((uch *)p, len)) >= 0) {\r
- fprintf(stderr, " " PROGNAME " warning: character code"\r
- " %u is %sdiscouraged by the PNG\n specification "\r
- "[first occurrence was at character position #%d]\n",\r
- (unsigned)p[result], (p[result] == 27)? "strongly " : "",\r
- result+1);\r
- fflush(stderr);\r
-#ifdef FORBID_LATIN1_CTRL\r
- wpng_info.have_text &= ~TEXT_COPY;\r
- valid = FALSE;\r
-#else\r
- if (p[result] == 27) { /* escape character */\r
- wpng_info.have_text &= ~TEXT_COPY;\r
- valid = FALSE;\r
- }\r
-#endif\r
- }\r
- }\r
- } while (!valid);\r
-\r
- do {\r
- valid = TRUE;\r
- p = textbuf + TEXT_EMAIL_OFFSET;\r
- fprintf(stderr, " E-mail: ");\r
- fflush(stderr);\r
- if (FGETS(p, 74, keybd) && (len = strlen(p)) > 1) {\r
- if (p[len-1] == '\n')\r
- p[--len] = '\0';\r
- wpng_info.email = p;\r
- wpng_info.have_text |= TEXT_EMAIL;\r
- if ((result = wpng_isvalid_latin1((uch *)p, len)) >= 0) {\r
- fprintf(stderr, " " PROGNAME " warning: character code"\r
- " %u is %sdiscouraged by the PNG\n specification "\r
- "[first occurrence was at character position #%d]\n",\r
- (unsigned)p[result], (p[result] == 27)? "strongly " : "",\r
- result+1);\r
- fflush(stderr);\r
-#ifdef FORBID_LATIN1_CTRL\r
- wpng_info.have_text &= ~TEXT_EMAIL;\r
- valid = FALSE;\r
-#else\r
- if (p[result] == 27) { /* escape character */\r
- wpng_info.have_text &= ~TEXT_EMAIL;\r
- valid = FALSE;\r
- }\r
-#endif\r
- }\r
- }\r
- } while (!valid);\r
-\r
- do {\r
- valid = TRUE;\r
- p = textbuf + TEXT_URL_OFFSET;\r
- fprintf(stderr, " URL: ");\r
- fflush(stderr);\r
- if (FGETS(p, 74, keybd) && (len = strlen(p)) > 1) {\r
- if (p[len-1] == '\n')\r
- p[--len] = '\0';\r
- wpng_info.url = p;\r
- wpng_info.have_text |= TEXT_URL;\r
- if ((result = wpng_isvalid_latin1((uch *)p, len)) >= 0) {\r
- fprintf(stderr, " " PROGNAME " warning: character code"\r
- " %u is %sdiscouraged by the PNG\n specification "\r
- "[first occurrence was at character position #%d]\n",\r
- (unsigned)p[result], (p[result] == 27)? "strongly " : "",\r
- result+1);\r
- fflush(stderr);\r
-#ifdef FORBID_LATIN1_CTRL\r
- wpng_info.have_text &= ~TEXT_URL;\r
- valid = FALSE;\r
-#else\r
- if (p[result] == 27) { /* escape character */\r
- wpng_info.have_text &= ~TEXT_URL;\r
- valid = FALSE;\r
- }\r
-#endif\r
- }\r
- }\r
- } while (!valid);\r
-\r
-#ifndef DOS_OS2_W32\r
- fclose(keybd);\r
-#endif\r
-\r
- } else if (text) {\r
- fprintf(stderr, PROGNAME ": unable to allocate memory for text\n");\r
- text = FALSE;\r
- wpng_info.have_text = 0;\r
- }\r
-\r
-\r
- /* allocate libpng stuff, initialize transformations, write pre-IDAT data */\r
-\r
- if ((rc = writepng_init(&wpng_info)) != 0) {\r
- switch (rc) {\r
- case 2:\r
- fprintf(stderr, PROGNAME\r
- ": libpng initialization problem (longjmp)\n");\r
- break;\r
- case 4:\r
- fprintf(stderr, PROGNAME ": insufficient memory\n");\r
- break;\r
- case 11:\r
- fprintf(stderr, PROGNAME\r
- ": internal logic error (unexpected PNM type)\n");\r
- break;\r
- default:\r
- fprintf(stderr, PROGNAME\r
- ": unknown writepng_init() error\n");\r
- break;\r
- }\r
- exit(rc);\r
- }\r
-\r
-\r
- /* free textbuf, since it's a completely local variable and all text info\r
- * has just been written to the PNG file */\r
-\r
- if (text && textbuf) {\r
- free(textbuf);\r
- textbuf = NULL;\r
- }\r
-\r
-\r
- /* calculate rowbytes on basis of image type; note that this becomes much\r
- * more complicated if we choose to support PBM type, ASCII PNM types, or\r
- * 16-bit-per-sample binary data [currently not an official NetPBM type] */\r
-\r
- if (wpng_info.pnmtype == 5)\r
- rowbytes = wpng_info.width;\r
- else if (wpng_info.pnmtype == 6)\r
- rowbytes = wpng_info.width * 3;\r
- else /* if (wpng_info.pnmtype == 8) */\r
- rowbytes = wpng_info.width * 4;\r
-\r
-\r
- /* read and write the image, either in its entirety (if writing interlaced\r
- * PNG) or row by row (if non-interlaced) */\r
-\r
- fprintf(stderr, "Encoding image data...\n");\r
- fflush(stderr);\r
-\r
- if (wpng_info.interlaced) {\r
- long i;\r
- ulg bytes;\r
- ulg image_bytes = rowbytes * wpng_info.height; /* overflow? */\r
-\r
- wpng_info.image_data = (uch *)malloc(image_bytes);\r
- wpng_info.row_pointers = (uch **)malloc(wpng_info.height*sizeof(uch *));\r
- if (wpng_info.image_data == NULL || wpng_info.row_pointers == NULL) {\r
- fprintf(stderr, PROGNAME ": insufficient memory for image data\n");\r
- writepng_cleanup(&wpng_info);\r
- wpng_cleanup();\r
- exit(5);\r
- }\r
- for (i = 0; i < wpng_info.height; ++i)\r
- wpng_info.row_pointers[i] = wpng_info.image_data + i*rowbytes;\r
- bytes = fread(wpng_info.image_data, 1, image_bytes, wpng_info.infile);\r
- if (bytes != image_bytes) {\r
- fprintf(stderr, PROGNAME ": expected %lu bytes, got %lu bytes\n",\r
- image_bytes, bytes);\r
- fprintf(stderr, " (continuing anyway)\n");\r
- }\r
- if (writepng_encode_image(&wpng_info) != 0) {\r
- fprintf(stderr, PROGNAME\r
- ": libpng problem (longjmp) while writing image data\n");\r
- writepng_cleanup(&wpng_info);\r
- wpng_cleanup();\r
- exit(2);\r
- }\r
-\r
- } else /* not interlaced: write progressively (row by row) */ {\r
- long j;\r
- ulg bytes;\r
-\r
- wpng_info.image_data = (uch *)malloc(rowbytes);\r
- if (wpng_info.image_data == NULL) {\r
- fprintf(stderr, PROGNAME ": insufficient memory for row data\n");\r
- writepng_cleanup(&wpng_info);\r
- wpng_cleanup();\r
- exit(5);\r
- }\r
- error = 0;\r
- for (j = wpng_info.height; j > 0L; --j) {\r
- bytes = fread(wpng_info.image_data, 1, rowbytes, wpng_info.infile);\r
- if (bytes != rowbytes) {\r
- fprintf(stderr, PROGNAME\r
- ": expected %lu bytes, got %lu bytes (row %ld)\n", rowbytes,\r
- bytes, wpng_info.height-j);\r
- ++error;\r
- break;\r
- }\r
- if (writepng_encode_row(&wpng_info) != 0) {\r
- fprintf(stderr, PROGNAME\r
- ": libpng problem (longjmp) while writing row %ld\n",\r
- wpng_info.height-j);\r
- ++error;\r
- break;\r
- }\r
- }\r
- if (error) {\r
- writepng_cleanup(&wpng_info);\r
- wpng_cleanup();\r
- exit(2);\r
- }\r
- if (writepng_encode_finish(&wpng_info) != 0) {\r
- fprintf(stderr, PROGNAME ": error on final libpng call\n");\r
- writepng_cleanup(&wpng_info);\r
- wpng_cleanup();\r
- exit(2);\r
- }\r
- }\r
-\r
-\r
- /* OK, we're done (successfully): clean up all resources and quit */\r
-\r
- fprintf(stderr, "Done.\n");\r
- fflush(stderr);\r
-\r
- writepng_cleanup(&wpng_info);\r
- wpng_cleanup();\r
-\r
- return 0;\r
-}\r
-\r
-\r
-\r
-\r
-\r
-static int wpng_isvalid_latin1(uch *p, int len)\r
-{\r
- int i, result = -1;\r
-\r
- for (i = 0; i < len; ++i) {\r
- if (p[i] == 10 || (p[i] > 31 && p[i] < 127) || p[i] > 160)\r
- continue; /* character is completely OK */\r
- if (result < 0 || (p[result] != 27 && p[i] == 27))\r
- result = i; /* mark location of first questionable one */\r
- } /* or of first escape character (bad) */\r
-\r
- return result;\r
-}\r
-\r
-\r
-\r
-\r
-\r
-static void wpng_cleanup(void)\r
-{\r
- if (wpng_info.outfile) {\r
- fclose(wpng_info.outfile);\r
- wpng_info.outfile = NULL;\r
- }\r
-\r
- if (wpng_info.infile) {\r
- fclose(wpng_info.infile);\r
- wpng_info.infile = NULL;\r
- }\r
-\r
- if (wpng_info.image_data) {\r
- free(wpng_info.image_data);\r
- wpng_info.image_data = NULL;\r
- }\r
-\r
- if (wpng_info.row_pointers) {\r
- free(wpng_info.row_pointers);\r
- wpng_info.row_pointers = NULL;\r
- }\r
-}\r
-\r
-\r
-\r
-\r
-#ifdef DOS_OS2_W32\r
-\r
-static char *dos_kbd_gets(char *buf, int len)\r
-{\r
- int ch, count=0;\r
-\r
- do {\r
- buf[count++] = ch = getche();\r
- } while (ch != '\r' && count < len-1);\r
-\r
- buf[count--] = '\0'; /* terminate string */\r
- if (buf[count] == '\r') /* Enter key makes CR, so change to newline */\r
- buf[count] = '\n';\r
-\r
- fprintf(stderr, "\n"); /* Enter key does *not* cause a newline */\r
- fflush(stderr);\r
-\r
- return buf;\r
-}\r
-\r
-#endif /* DOS_OS2_W32 */\r