📄 wpng.c
字号:
} } else { if (**argv != '-') { inname = *argv; if (argv[1]) /* shouldn't be any more args after filename */ ++error; } else ++error; /* not expecting any other options */ } } /* open the input and output files, or register an error and abort */ if (!inname) { if (isatty(0)) { fprintf(stderr, PROGNAME ": must give input filename or provide image data via stdin\n"); ++error; } else {#ifdef DOS_OS2_W32 /* some buggy C libraries require BOTH setmode() and fdopen(bin) */ setmode(fileno(stdin), O_BINARY); setmode(fileno(stdout), O_BINARY);#endif if ((wpng_info.infile = fdopen(fileno(stdin), "rb")) == NULL) { fprintf(stderr, PROGNAME ": unable to reopen stdin in binary mode\n"); ++error; } else if ((wpng_info.outfile = fdopen(fileno(stdout), "wb")) == NULL) { fprintf(stderr, PROGNAME ": unable to reopen stdout in binary mode\n"); fclose(wpng_info.infile); ++error; } else wpng_info.filter = TRUE; } } else if ((len = strlen(inname)) > 250) { fprintf(stderr, PROGNAME ": input filename is too long [%d chars]\n", len); ++error; } else if (!(wpng_info.infile = fopen(inname, "rb"))) { fprintf(stderr, PROGNAME ": can't open input file [%s]\n", inname); ++error; } if (!error) { fgets(pnmline, 256, wpng_info.infile); if (pnmline[0] != 'P' || ((pnmchar = pnmline[1]) != '5' && pnmchar != '6' && pnmchar != '8')) { fprintf(stderr, PROGNAME ": input file [%s] is not a binary PGM, PPM or PAM file\n", inname); ++error; } else { wpng_info.pnmtype = (int)(pnmchar - '0'); if (wpng_info.pnmtype != 8) wpng_info.have_bg = FALSE; /* no need for bg if opaque */ do { fgets(pnmline, 256, wpng_info.infile); /* lose any comments */ } while (pnmline[0] == '#'); sscanf(pnmline, "%ld %ld", &wpng_info.width, &wpng_info.height); do { fgets(pnmline, 256, wpng_info.infile); /* more comment lines */ } while (pnmline[0] == '#'); sscanf(pnmline, "%d", &maxval); if (wpng_info.width <= 0L || wpng_info.height <= 0L || maxval != 255) { fprintf(stderr, PROGNAME ": only positive width/height, maxval == 255 allowed \n"); ++error; } wpng_info.sample_depth = 8; /* <==> maxval 255 */ if (!wpng_info.filter) { /* make outname from inname */ if ((p = strrchr(inname, '.')) == NULL || (p - inname) != (len - 4)) { strcpy(outname, inname); strcpy(outname+len, ".png"); } else { len -= 4; strncpy(outname, inname, len); strcpy(outname+len, ".png"); } /* check if outname already exists; if not, open */ if ((wpng_info.outfile = fopen(outname, "rb")) != NULL) { fprintf(stderr, PROGNAME ": output file exists [%s]\n", outname); fclose(wpng_info.outfile); ++error; } else if (!(wpng_info.outfile = fopen(outname, "wb"))) { fprintf(stderr, PROGNAME ": can't open output file [%s]\n", outname); ++error; } } } if (error) { fclose(wpng_info.infile); wpng_info.infile = NULL; if (wpng_info.filter) { fclose(wpng_info.outfile); wpng_info.outfile = NULL; } } } /* if we had any errors, print usage and die horrible death...arrr! */ if (error) { fprintf(stderr, "\n%s %s: %s\n", PROGNAME, VERSION, APPNAME); writepng_version_info(); fprintf(stderr, "\n""Usage: %s [-gamma exp] [-bgcolor bg] [-text] [-time] [-interlace] pnmfile\n""or: ... | %s [-gamma exp] [-bgcolor bg] [-text] [-time] [-interlace] | ...\n" " exp \ttransfer-function exponent (``gamma'') of the image in\n" "\t\t floating-point format (e.g., ``%.5f''); if image looks\n" "\t\t correct on given display system, image gamma is equal to\n" "\t\t inverse of display-system exponent, i.e., 1 / (LUT * CRT)\n" "\t\t (where LUT = lookup-table exponent and CRT = CRT exponent;\n" "\t\t first varies, second is usually 2.2, all are positive)\n" " bg \tdesired background color for alpha-channel images, in\n" "\t\t 7-character hex RGB format (e.g., ``#ff7700'' for orange:\n" "\t\t same as HTML colors)\n" " -text\tprompt interactively for text info (tEXt chunks)\n" " -time\tinclude a tIME chunk (last modification time)\n" " -interlace\twrite interlaced PNG image\n" "\n""pnmfile or stdin must be a binary PGM (`P5'), PPM (`P6') or (extremely\n""unofficial and unsupported!) PAM (`P8') file. Currently it is required\n""to have maxval == 255 (i.e., no scaling). If pnmfile is specified, it\n""is converted to the corresponding PNG file with the same base name but a\n""``.png'' extension; files read from stdin are converted and sent to stdout.\n""The conversion is progressive (low memory usage) unless interlacing is\n""requested; in that case the whole image will be buffered in memory and\n""written in one call.\n" "\n", PROGNAME, PROGNAME, default_gamma); exit(1); } /* prepare the text buffers for libpng's use; note that even though * PNG's png_text struct includes a length field, we don't have to fill * it out */ if (text &&#ifndef DOS_OS2_W32 (keybd = fdopen(fileno(stderr), "r")) != NULL &&#endif (textbuf = (char *)malloc((5 + 9)*75)) != NULL) { int i, valid, result; fprintf(stderr, "Enter text info (no more than 72 characters per line);\n"); fprintf(stderr, "to skip a field, hit the <Enter> key.\n"); /* note: just <Enter> leaves len == 1 */ do { valid = TRUE; p = textbuf + TEXT_TITLE_OFFSET; fprintf(stderr, " Title: "); fflush(stderr); if (FGETS(p, 74, keybd) && (len = strlen(p)) > 1) { if (p[len-1] == '\n') p[--len] = '\0'; wpng_info.title = p; wpng_info.have_text |= TEXT_TITLE; if ((result = wpng_isvalid_latin1((uch *)p, len)) >= 0) { fprintf(stderr, " " PROGNAME " warning: character code" " %u is %sdiscouraged by the PNG\n specification " "[first occurrence was at character position #%d]\n", (unsigned)p[result], (p[result] == 27)? "strongly " : "", result+1); fflush(stderr);#ifdef FORBID_LATIN1_CTRL wpng_info.have_text &= ~TEXT_TITLE; valid = FALSE;#else if (p[result] == 27) { /* escape character */ wpng_info.have_text &= ~TEXT_TITLE; valid = FALSE; }#endif } } } while (!valid); do { valid = TRUE; p = textbuf + TEXT_AUTHOR_OFFSET; fprintf(stderr, " Author: "); fflush(stderr); if (FGETS(p, 74, keybd) && (len = strlen(p)) > 1) { if (p[len-1] == '\n') p[--len] = '\0'; wpng_info.author = p; wpng_info.have_text |= TEXT_AUTHOR; if ((result = wpng_isvalid_latin1((uch *)p, len)) >= 0) { fprintf(stderr, " " PROGNAME " warning: character code" " %u is %sdiscouraged by the PNG\n specification " "[first occurrence was at character position #%d]\n", (unsigned)p[result], (p[result] == 27)? "strongly " : "", result+1); fflush(stderr);#ifdef FORBID_LATIN1_CTRL wpng_info.have_text &= ~TEXT_AUTHOR; valid = FALSE;#else if (p[result] == 27) { /* escape character */ wpng_info.have_text &= ~TEXT_AUTHOR; valid = FALSE; }#endif } } } while (!valid); do { valid = TRUE; p = textbuf + TEXT_DESC_OFFSET; fprintf(stderr, " Description (up to 9 lines):\n"); for (i = 1; i < 10; ++i) { fprintf(stderr, " [%d] ", i); fflush(stderr); if (FGETS(p, 74, keybd) && (len = strlen(p)) > 1) p += len; /* now points at NULL; char before is newline */ else break; } if ((len = p - (textbuf + TEXT_DESC_OFFSET)) > 1) { if (p[-1] == '\n') { p[-1] = '\0'; --len; } wpng_info.desc = textbuf + TEXT_DESC_OFFSET; wpng_info.have_text |= TEXT_DESC; p = textbuf + TEXT_DESC_OFFSET; if ((result = wpng_isvalid_latin1((uch *)p, len)) >= 0) { fprintf(stderr, " " PROGNAME " warning: character code" " %u is %sdiscouraged by the PNG\n specification " "[first occurrence was at character position #%d]\n", (unsigned)p[result], (p[result] == 27)? "strongly " : "", result+1); fflush(stderr);#ifdef FORBID_LATIN1_CTRL wpng_info.have_text &= ~TEXT_DESC; valid = FALSE;#else if (p[result] == 27) { /* escape character */ wpng_info.have_text &= ~TEXT_DESC; valid = FALSE; }#endif } } } while (!valid); do { valid = TRUE; p = textbuf + TEXT_COPY_OFFSET; fprintf(stderr, " Copyright: "); fflush(stderr); if (FGETS(p, 74, keybd) && (len = strlen(p)) > 1) { if (p[len-1] == '\n') p[--len] = '\0'; wpng_info.copyright = p; wpng_info.have_text |= TEXT_COPY; if ((result = wpng_isvalid_latin1((uch *)p, len)) >= 0) { fprintf(stderr, " " PROGNAME " warning: character code" " %u is %sdiscouraged by the PNG\n specification " "[first occurrence was at character position #%d]\n", (unsigned)p[result], (p[result] == 27)? "strongly " : "", result+1); fflush(stderr);#ifdef FORBID_LATIN1_CTRL wpng_info.have_text &= ~TEXT_COPY; valid = FALSE;#else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -