From d96a823670f86f0f74c307ad03d3ec2982bf38de Mon Sep 17 00:00:00 2001 From: Lee Howard Date: Tue, 14 Dec 2010 02:03:24 +0000 Subject: [PATCH] * tools/tiffcrop.c: new release by Richard Nolde http://bugzilla.maptools.org/show_bug.cgi?id=2004 --- ChangeLog | 5 ++ tools/tiffcrop.c | 164 +++++++++++++++++++++++++++-------------------- 2 files changed, 101 insertions(+), 68 deletions(-) diff --git a/ChangeLog b/ChangeLog index c3099705..abae57ff 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2010-12-13 Lee Howard + + * tools/tiffcrop.c: new release by Richard Nolde + http://bugzilla.maptools.org/show_bug.cgi?id=2004 + 2010-12-12 Lee Howard * tools/tiff2pdf.c: fix colors for images with RGBA diff --git a/tools/tiffcrop.c b/tools/tiffcrop.c index 509c7b99..9cd5d86f 100644 --- a/tools/tiffcrop.c +++ b/tools/tiffcrop.c @@ -1,4 +1,4 @@ -/* $Id: tiffcrop.c,v 1.19 2010-07-13 15:51:39 bfriesen Exp $ */ +/* $Id: tiffcrop.c,v 1.20 2010-12-14 02:03:24 faxguy Exp $ */ /* tiffcrop.c -- a port of tiffcp.c extended to include manipulations of * the image data through additional options listed below @@ -6,16 +6,16 @@ * Original code: * Copyright (c) 1988-1997 Sam Leffler * Copyright (c) 1991-1997 Silicon Graphics, Inc. - * Additions (c) Richard Nolde 2006-2010 + * Additions (c) Richard Nolde 2006-2010 * - * Permission to use, copy, modify, distribute, and sell this software and + * Permission to use, copy, modify, distribute, and sell this software and * its documentation for any purpose is hereby granted without fee, provided * that (i) the above copyright notices and this permission notice appear in * all copies of the software and related documentation, and (ii) the names of * Sam Leffler and Silicon Graphics may not be used in any advertising or * publicity relating to the software without the specific, prior written * permission of Sam Leffler and Silicon Graphics. - * + * * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. @@ -107,8 +107,8 @@ * of messages to monitor progess without enabling dump logs. */ -static char tiffcrop_version_id[] = "2.3"; -static char tiffcrop_rev_date[] = "07-12-2010"; +static char tiffcrop_version_id[] = "2.4"; +static char tiffcrop_rev_date[] = "12-13-2010"; #include "tif_config.h" #include "tiffiop.h" @@ -134,6 +134,10 @@ static char tiffcrop_rev_date[] = "07-12-2010"; extern int getopt(int, char**, char*); #endif +#ifdef NEED_LIBPORT +# include "libport.h" +#endif + #include "tiffio.h" #if defined(VMS) @@ -1475,7 +1479,7 @@ cpTag(TIFF* in, TIFF* out, uint16 tag, uint16 count, TIFFDataType type) break; default: TIFFError(TIFFFileName(in), - "Data type %d is not supported, tag %d skipped.", + "Data type %d is not supported, tag %d skipped", tag, type); } } @@ -1722,13 +1726,13 @@ void process_command_opts (int argc, char *argv[], char *mp, char *mode, uint32 dump->level = atoi(opt_offset + 1); /* Look for input data dump file name */ if (strncmp (opt_ptr, "in", 2) == 0) - { + { strncpy (dump->infilename, opt_offset + 1, PATH_MAX - 20); dump->infilename[PATH_MAX - 20] = '\0'; } /* Look for output data dump file name */ if (strncmp (opt_ptr, "out", 3) == 0) - { + { strncpy (dump->outfilename, opt_offset + 1, PATH_MAX - 20); dump->outfilename[PATH_MAX - 20] = '\0'; } @@ -2065,24 +2069,24 @@ update_output_file (TIFF **tiffout, char *mode, int autoindex, else strncpy (export_ext, ".tiff", 5); export_ext[5] = '\0'; - - /* MAX_EXPORT_PAGES limited to 6 digits to prevent string overflow of pathname */ - if (findex > MAX_EXPORT_PAGES) - { - TIFFError("update_output_file", "Maximum of %d pages per file exceeded.\n", MAX_EXPORT_PAGES); - return 1; - } - - sprintf (filenum, "-%03d%s", findex, export_ext); - filenum[14] = '\0'; - strncat (exportname, filenum, 15); - } - exportname[PATH_MAX - 1] = '\0'; - - *tiffout = TIFFOpen(exportname, mode); + + /* MAX_EXPORT_PAGES limited to 6 digits to prevent string overflow of pathname */ + if (findex > MAX_EXPORT_PAGES) + { + TIFFError("update_output_file", "Maximum of %d pages per file exceeded", MAX_EXPORT_PAGES); + return 1; + } + + sprintf (filenum, "-%03d%s", findex, export_ext); + filenum[14] = '\0'; + strncat (exportname, filenum, 15); + } + exportname[PATH_MAX - 1] = '\0'; + + *tiffout = TIFFOpen(exportname, mode); if (*tiffout == NULL) { - TIFFError("update_output_file", "Unable to open output file %s\n", exportname); + TIFFError("update_output_file", "Unable to open output file %s", exportname); return 1; } *page = 0; @@ -2133,7 +2137,7 @@ main(int argc, char* argv[]) unsigned int end_of_input = FALSE; int seg, length; char temp_filename[PATH_MAX + 1]; - memset (temp_filename, '\0', PATH_MAX + 1); + little_endian = *((unsigned char *)&little_endian) & '1'; initImageData(&image); @@ -2224,6 +2228,9 @@ main(int argc, char* argv[]) if (dump.infile != NULL) fclose (dump.infile); + /* dump.infilename is guaranteed to be NUL termimated and have 20 bytes + fewer than PATH_MAX */ + memset (temp_filename, '\0', PATH_MAX + 1); sprintf (temp_filename, "%s-read-%03d.%s", dump.infilename, dump_images, (dump.format == DUMP_TEXT) ? "txt" : "raw"); if ((dump.infile = fopen(temp_filename, dump.mode)) == NULL) @@ -2240,6 +2247,9 @@ main(int argc, char* argv[]) if (dump.outfile != NULL) fclose (dump.outfile); + /* dump.outfilename is guaranteed to be NUL termimated and have 20 bytes + fewer than PATH_MAX */ + memset (temp_filename, '\0', PATH_MAX + 1); sprintf (temp_filename, "%s-write-%03d.%s", dump.outfilename, dump_images, (dump.format == DUMP_TEXT) ? "txt" : "raw"); if ((dump.outfile = fopen(temp_filename, dump.mode)) == NULL) @@ -2400,7 +2410,7 @@ static int dump_data (FILE *dumpfile, int format, char *dump_tag, unsigned char if (dumpfile == NULL) { - TIFFError ("", "Invalid FILE pointer for dump file\n"); + TIFFError ("", "Invalid FILE pointer for dump file"); return (1); } @@ -2423,7 +2433,7 @@ static int dump_data (FILE *dumpfile, int format, char *dump_tag, unsigned char { if ((fwrite (data, 1, count, dumpfile)) != count) { - TIFFError ("", "Unable to write binary data to dump file\n"); + TIFFError ("", "Unable to write binary data to dump file"); return (1); } } @@ -2439,7 +2449,7 @@ static int dump_byte (FILE *dumpfile, int format, char *dump_tag, unsigned char if (dumpfile == NULL) { - TIFFError ("", "Invalid FILE pointer for dump file\n"); + TIFFError ("", "Invalid FILE pointer for dump file"); return (1); } @@ -2458,7 +2468,7 @@ static int dump_byte (FILE *dumpfile, int format, char *dump_tag, unsigned char { if ((fwrite (&data, 1, 1, dumpfile)) != 1) { - TIFFError ("", "Unable to write binary data to dump file\n"); + TIFFError ("", "Unable to write binary data to dump file"); return (1); } } @@ -2474,7 +2484,7 @@ static int dump_short (FILE *dumpfile, int format, char *dump_tag, uint16 data) if (dumpfile == NULL) { - TIFFError ("", "Invalid FILE pointer for dump file\n"); + TIFFError ("", "Invalid FILE pointer for dump file"); return (1); } @@ -2495,7 +2505,7 @@ static int dump_short (FILE *dumpfile, int format, char *dump_tag, uint16 data) { if ((fwrite (&data, 2, 1, dumpfile)) != 2) { - TIFFError ("", "Unable to write binary data to dump file\n"); + TIFFError ("", "Unable to write binary data to dump file"); return (1); } } @@ -2511,7 +2521,7 @@ static int dump_long (FILE *dumpfile, int format, char *dump_tag, uint32 data) if (dumpfile == NULL) { - TIFFError ("", "Invalid FILE pointer for dump file\n"); + TIFFError ("", "Invalid FILE pointer for dump file"); return (1); } @@ -2532,7 +2542,7 @@ static int dump_long (FILE *dumpfile, int format, char *dump_tag, uint32 data) { if ((fwrite (&data, 4, 1, dumpfile)) != 4) { - TIFFError ("", "Unable to write binary data to dump file\n"); + TIFFError ("", "Unable to write binary data to dump file"); return (1); } } @@ -2547,7 +2557,7 @@ static int dump_wide (FILE *dumpfile, int format, char *dump_tag, uint64 data) if (dumpfile == NULL) { - TIFFError ("", "Invalid FILE pointer for dump file\n"); + TIFFError ("", "Invalid FILE pointer for dump file"); return (1); } @@ -2568,7 +2578,7 @@ static int dump_wide (FILE *dumpfile, int format, char *dump_tag, uint64 data) { if ((fwrite (&data, 8, 1, dumpfile)) != 8) { - TIFFError ("", "Unable to write binary data to dump file\n"); + TIFFError ("", "Unable to write binary data to dump file"); return (1); } } @@ -2597,7 +2607,7 @@ static int dump_buffer (FILE* dumpfile, int format, uint32 rows, uint32 width, if (dumpfile == NULL) { - TIFFError ("", "Invalid FILE pointer for dump file\n"); + TIFFError ("", "Invalid FILE pointer for dump file"); return (1); } @@ -5265,8 +5275,8 @@ getCropOffsets(struct image_data *image, struct crop_mask *crop, struct dump_opt { struct offset offsets; int i; - int32 test2; - uint32 test, seg, total, need_buff = 0; + int32 test; + uint32 seg, total, need_buff = 0; uint32 buffsize; uint32 zwidth, zlength; @@ -5353,12 +5363,17 @@ getCropOffsets(struct image_data *image, struct crop_mask *crop, struct dump_opt crop->regionlist[i].x1 = offsets.startx + (uint32)(offsets.crop_width * 1.0 * (seg - 1) / total); - test = offsets.startx + - (uint32)(offsets.crop_width * 1.0 * seg / total); - if (test > image->width - 1) - crop->regionlist[i].x2 = image->width - 1; + test = (int32)offsets.startx + + (int32)(offsets.crop_width * 1.0 * seg / total); + if (test < 1 ) + crop->regionlist[i].x2 = 0; else - crop->regionlist[i].x2 = test - 1; + { + if (test > (int32)(image->width - 1)) + crop->regionlist[i].x2 = image->width - 1; + else + crop->regionlist[i].x2 = test - 1; + } zwidth = crop->regionlist[i].x2 - crop->regionlist[i].x1 + 1; /* This is passed to extractCropZone or extractCompositeZones */ @@ -5373,17 +5388,22 @@ getCropOffsets(struct image_data *image, struct crop_mask *crop, struct dump_opt crop->regionlist[i].x1 = offsets.startx; crop->regionlist[i].x2 = offsets.endx; - test2 = offsets.endy - (uint32)(offsets.crop_length * 1.0 * seg / total); - if (test2 < 1 ) + test = offsets.endy - (uint32)(offsets.crop_length * 1.0 * seg / total); + if (test < 1 ) crop->regionlist[i].y1 = 0; else - crop->regionlist[i].y1 = test2 + 1; + crop->regionlist[i].y1 = test + 1; - test = offsets.endy - (uint32)(offsets.crop_length * 1.0 * (seg - 1) / total); - if (test > (image->length - 1)) - crop->regionlist[i].y2 = image->length - 1; - else - crop->regionlist[i].y2 = test; + test = offsets.endy - (offsets.crop_length * 1.0 * (seg - 1) / total); + if (test < 1 ) + crop->regionlist[i].y2 = 0; + else + { + if (test > (int32)(image->length - 1)) + crop->regionlist[i].y2 = image->length - 1; + else + crop->regionlist[i].y2 = test; + } zlength = crop->regionlist[i].y2 - crop->regionlist[i].y1 + 1; /* This is passed to extractCropZone or extractCompositeZones */ @@ -5401,12 +5421,16 @@ getCropOffsets(struct image_data *image, struct crop_mask *crop, struct dump_opt crop->regionlist[i].x1 = offsets.startx + (uint32)(offsets.crop_width * (total - seg) * 1.0 / total); test = offsets.startx + - (uint32)(offsets.crop_width * (total - seg + 1) * 1.0 / total); - - if (test > image->width - 1) - crop->regionlist[i].x2 = image->width - 1; + (offsets.crop_width * (total - seg + 1) * 1.0 / total); + if (test < 1 ) + crop->regionlist[i].x2 = 0; else - crop->regionlist[i].x2 = test - 1; + { + if (test > (int32)(image->width - 1)) + crop->regionlist[i].x2 = image->width - 1; + else + crop->regionlist[i].x2 = test - 1; + } zwidth = crop->regionlist[i].x2 - crop->regionlist[i].x1 + 1; /* This is passed to extractCropZone or extractCompositeZones */ @@ -5424,10 +5448,15 @@ getCropOffsets(struct image_data *image, struct crop_mask *crop, struct dump_opt crop->regionlist[i].y1 = offsets.starty + (uint32)(offsets.crop_length * 1.0 * (seg - 1) / total); test = offsets.starty + (uint32)(offsets.crop_length * 1.0 * seg / total); - if (test > image->length - 1) - crop->regionlist[i].y2 = image->length - 1; + if (test < 1 ) + crop->regionlist[i].y2 = 0; else - crop->regionlist[i].y2 = test - 1; + { + if (test > (int32)(image->length - 1)) + crop->regionlist[i].y2 = image->length - 1; + else + crop->regionlist[i].y2 = test - 1; + } zlength = crop->regionlist[i].y2 - crop->regionlist[i].y1 + 1; /* This is passed to extractCropZone or extractCompositeZones */ @@ -5529,10 +5558,10 @@ computeOutputPixelOffsets (struct crop_mask *crop, struct image_data *image, if (dump->debug) { TIFFError("", "Page size: %s, Vres: %3.2f, Hres: %3.2f, " - "Hmargin: %3.2f, Vmargin: %3.2f\n", + "Hmargin: %3.2f, Vmargin: %3.2f", page->name, page->vres, page->hres, page->hmargin, page->vmargin); - TIFFError("", "Res_unit: %d, Scale: %3.2f, Page width: %3.2f, length: %3.2f\n", + TIFFError("", "Res_unit: %d, Scale: %3.2f, Page width: %3.2f, length: %3.2f", page->res_unit, scale, pwidth, plength); } @@ -5948,7 +5977,7 @@ loadImage(TIFF* in, struct image_data *image, struct dump_opts *dump, unsigned c jpegcolormode = JPEGCOLORMODE_RGB; TIFFSetField(in, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB); } - /* The clause up to the read statement are taken from Tom Lane's tiffcp patch */ + /* The clause up to the read statement is taken from Tom Lane's tiffcp patch */ else { /* Otherwise, can't handle subsampled input */ if (input_photometric == PHOTOMETRIC_YCBCR) @@ -5958,14 +5987,13 @@ loadImage(TIFF* in, struct image_data *image, struct dump_opts *dump, unsigned c if (subsampling_horiz != 1 || subsampling_vert != 1) { TIFFError("loadImage", - "Can't copy/convert subsampled image with subsampling %d horiz %d vert.\n", + "Can't copy/convert subsampled image with subsampling %d horiz %d vert", subsampling_horiz, subsampling_vert); return (-1); } } } - read_buff = *read_ptr; if (!read_buff) read_buff = (unsigned char *)_TIFFmalloc(buffsize); @@ -6514,7 +6542,7 @@ extractImageSection(struct image_data *image, struct pageseg *section, { if ((bitarray = (char *)malloc(img_width)) == NULL) { - TIFFError ("", "DEBUG: Unable to allocate debugging bitarray\n"); + TIFFError ("", "DEBUG: Unable to allocate debugging bitarray"); return (-1); } } @@ -6551,7 +6579,7 @@ extractImageSection(struct image_data *image, struct pageseg *section, src_offset = row_offset + col_offset; #ifdef DEVELMODE - TIFFError ("", "Src offset: %8d, Dst offset: %8d\n", src_offset, dst_offset); + TIFFError ("", "Src offset: %8d, Dst offset: %8d", src_offset, dst_offset); #endif _TIFFmemcpy (sect_buff + dst_offset, src_buff + src_offset, full_bytes); dst_offset += full_bytes; @@ -7033,7 +7061,7 @@ writeSingleSection(TIFF *in, TIFF *out, struct image_data *image, } else { - TIFFError("writeCroppedImage", + TIFFError("writeSingleSection", "JPEG compression requires 8 or 12 bits per sample"); return (-1); }