| 1 |
/* clang-format off */ |
| 2 |
/* tiffcrop.c -- a port of tiffcp.c extended to include manipulations of |
| 3 |
* the image data through additional options listed below |
| 4 |
* |
| 5 |
* Original code: |
| 6 |
* Copyright (c) 1988-1997 Sam Leffler |
| 7 |
* Copyright (c) 1991-1997 Silicon Graphics, Inc. |
| 8 |
* Additions (c) Richard Nolde 2006-2010 |
| 9 |
* |
| 10 |
* Permission to use, copy, modify, distribute, and sell this software and |
| 11 |
* its documentation for any purpose is hereby granted without fee, provided |
| 12 |
* that (i) the above copyright notices and this permission notice appear in |
| 13 |
* all copies of the software and related documentation, and (ii) the names of |
| 14 |
* Sam Leffler and Silicon Graphics may not be used in any advertising or |
| 15 |
* publicity relating to the software without the specific, prior written |
| 16 |
* permission of Sam Leffler and Silicon Graphics. |
| 17 |
* |
| 18 |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 19 |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 20 |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 21 |
* |
| 22 |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS OR ANY OTHER COPYRIGHT |
| 23 |
* HOLDERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL |
| 24 |
* DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 25 |
* DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND |
| 26 |
* ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE |
| 27 |
* OR PERFORMANCE OF THIS SOFTWARE. |
| 28 |
* |
| 29 |
* Some portions of the current code are derived from tiffcp, primarily in |
| 30 |
* the areas of lowlevel reading and writing of TAGS, scanlines and tiles though |
| 31 |
* some of the original functions have been extended to support arbitrary bit |
| 32 |
* depths. These functions are presented at the top of this file. |
| 33 |
* |
| 34 |
* Add support for the options below to extract sections of image(s) |
| 35 |
* and to modify the whole image or selected portions of each image by |
| 36 |
* rotations, mirroring, and colorscale/colormap inversion of selected |
| 37 |
* types of TIFF images when appropriate. Some color model dependent |
| 38 |
* functions are restricted to bilevel or 8 bit per sample data. |
| 39 |
* See the man page for the full explanations. |
| 40 |
* |
| 41 |
* New Options: |
| 42 |
* -h Display the syntax guide. |
| 43 |
* -v Report the version and last build date for tiffcrop |
| 44 |
* and libtiff. |
| 45 |
* -z x1,y1,x2,y2:x3,y3,x4,y4:..xN,yN,xN + 1, yN + 1 |
| 46 |
* Specify a series of coordinates to define rectangular |
| 47 |
* regions by the top left and lower right corners. |
| 48 |
* -e c|d|i|m|s export mode for images and selections from input images |
| 49 |
* combined All images and selections are written to a single |
| 50 |
* file (default) with multiple selections from one image |
| 51 |
* combined into a single image |
| 52 |
* divided All images and selections are written to a single file |
| 53 |
* with each selection from one image written to a new image |
| 54 |
* image Each input image is written to a new file |
| 55 |
* (numeric filename sequence) with multiple selections from |
| 56 |
* the image combined into one image |
| 57 |
* multiple Each input image is written to a new file |
| 58 |
* (numeric filename sequence) with each selection from |
| 59 |
* the image written to a new image |
| 60 |
* separated Individual selections from each image are written |
| 61 |
* to separate files |
| 62 |
* -U units [in, cm, px ] inches, centimeters or pixels |
| 63 |
* -H # Set horizontal resolution of output images to # |
| 64 |
* -V # Set vertical resolution of output images to # |
| 65 |
* -J # Horizontal margin of output page to # expressed in current |
| 66 |
* units when sectioning image into columns x rows |
| 67 |
* using the -S cols:rows option. |
| 68 |
* -K # Vertical margin of output page to # expressed in current |
| 69 |
* units when sectioning image into columns x rows |
| 70 |
* using the -S cols:rows option. |
| 71 |
* -X # Horizontal dimension of region to extract expressed in current |
| 72 |
* units, relative to the specified origin reference 'edge' left |
| 73 |
* (default for X) or right. |
| 74 |
* -Y # Vertical dimension of region to extract expressed in current |
| 75 |
* units, relative to the specified origin reference 'edge' top |
| 76 |
* (default for Y) or bottom. |
| 77 |
* -O orient Orientation for output image, portrait, landscape, auto |
| 78 |
* -P page Page size for output image segments, eg letter, legal, tabloid, |
| 79 |
* etc. |
| 80 |
* -S cols:rows Divide the image into equal sized segments using cols across |
| 81 |
* and rows down |
| 82 |
* -E t|l|r|b Edge to use as origin (i.e. 'side' of the image not 'corner') |
| 83 |
* top = width from left, zones from top to bottom (default) |
| 84 |
* bottom = width from left, zones from bottom to top |
| 85 |
* left = zones from left to right, length from top |
| 86 |
* right = zones from right to left, length from top |
| 87 |
* -m #,#,#,# Margins from edges for selection: top, left, bottom, right |
| 88 |
* (commas separated) |
| 89 |
* -Z #:#,#:# Zones of the image designated as zone X of Y, |
| 90 |
* eg 1:3 would be first of three equal portions measured |
| 91 |
* from reference edge (i.e. 'side' not corner) |
| 92 |
* -N odd|even|#,#-#,#|last |
| 93 |
* Select sequences and/or ranges of images within file |
| 94 |
* to process. The words odd or even may be used to specify |
| 95 |
* all odd or even numbered images the word last may be used |
| 96 |
* in place of a number in the sequence to indicate the final |
| 97 |
* image in the file without knowing how many images there are. |
| 98 |
* -R # Rotate image or crop selection by 90,180,or 270 degrees |
| 99 |
* clockwise |
| 100 |
* -F h|v Flip (mirror) image or crop selection horizontally |
| 101 |
* or vertically |
| 102 |
* -I [black|white|data|both] |
| 103 |
* Invert color space, eg dark to light for bilevel and |
| 104 |
* grayscale images If argument is white or black, set the |
| 105 |
* PHOTOMETRIC_INTERPRETATION tag to MinIsBlack or MinIsWhite |
| 106 |
* without altering the image data. If the argument is data |
| 107 |
* or both, the image data are modified: |
| 108 |
* both inverts the data and the PHOTOMETRIC_INTERPRETATION tag, |
| 109 |
* data inverts the data but not the PHOTOMETRIC_INTERPRETATION tag |
| 110 |
* -D input:<filename1>,output:<filename2>,format:<raw|txt>,level:N,debug:N |
| 111 |
* Dump raw data for input and/or output images to individual |
| 112 |
* files in raw (binary) format or text (ASCII) representing |
| 113 |
* binary data as strings of 1s and 0s. The filename arguments |
| 114 |
* are used as stems from which individual files are created for |
| 115 |
* each image. Text format includes annotations for image |
| 116 |
* parameters and scanline info. Level selects which functions |
| 117 |
* dump data, with higher numbers selecting lower level, |
| 118 |
* scanline level routines. Debug reports a limited set |
| 119 |
* of messages to monitor progress without enabling dump logs. |
| 120 |
* |
| 121 |
* Note 1: The (-X|-Y), -Z, -z and -S options are mutually exclusive. |
| 122 |
* In no case should the options be applied to a given |
| 123 |
* selection successively. |
| 124 |
* Note 2: Any of the -X, -Y, -Z and -z options together with other |
| 125 |
* PAGE_MODE_x options such as -H, -V, -P, -J or -K are not supported |
| 126 |
* and may cause buffer overflows. |
| 127 |
*/ |
| 128 |
/* clang-format on */ |
| 129 |
|
| 130 |
#include "libport.h" |
| 131 |
#include "tif_config.h" |
| 132 |
#include "tiffiop.h" |
| 133 |
|
| 134 |
#include <assert.h> |
| 135 |
#include <ctype.h> |
| 136 |
#include <limits.h> |
| 137 |
#include <math.h> |
| 138 |
#include <stdint.h> |
| 139 |
#include <stdio.h> |
| 140 |
#include <stdlib.h> |
| 141 |
#include <string.h> |
| 142 |
#include <sys/stat.h> |
| 143 |
|
| 144 |
#ifdef HAVE_UNISTD_H |
| 145 |
#include <unistd.h> |
| 146 |
#endif |
| 147 |
|
| 148 |
#ifndef EXIT_SUCCESS |
| 149 |
#define EXIT_SUCCESS 0 |
| 150 |
#endif |
| 151 |
#ifndef EXIT_FAILURE |
| 152 |
#define EXIT_FAILURE 1 |
| 153 |
#endif |
| 154 |
|
| 155 |
#include "tiffio.h" |
| 156 |
|
| 157 |
#ifndef PATH_MAX |
| 158 |
#define PATH_MAX 1024 |
| 159 |
#endif |
| 160 |
|
| 161 |
#define TRUE 1 |
| 162 |
#define FALSE 0 |
| 163 |
|
| 164 |
#ifndef TIFFhowmany |
| 165 |
#define TIFFhowmany(x, y) \ |
| 166 |
((((uint32_t)(x)) + (((uint32_t)(y)) - 1)) / ((uint32_t)(y))) |
| 167 |
#define TIFFhowmany8(x) \ |
| 168 |
(((x)&0x07) ? ((uint32_t)(x) >> 3) + 1 : (uint32_t)(x) >> 3) |
| 169 |
#endif |
| 170 |
|
| 171 |
/* |
| 172 |
* Definitions and data structures required to support cropping and image |
| 173 |
* manipulations. |
| 174 |
*/ |
| 175 |
|
| 176 |
#define EDGE_TOP 1 |
| 177 |
#define EDGE_LEFT 2 |
| 178 |
#define EDGE_BOTTOM 3 |
| 179 |
#define EDGE_RIGHT 4 |
| 180 |
#define EDGE_CENTER 5 |
| 181 |
|
| 182 |
#define MIRROR_HORIZ 1 |
| 183 |
#define MIRROR_VERT 2 |
| 184 |
#define MIRROR_BOTH 3 |
| 185 |
#define ROTATECW_90 8 |
| 186 |
#define ROTATECW_180 16 |
| 187 |
#define ROTATECW_270 32 |
| 188 |
#define ROTATE_ANY (ROTATECW_90 | ROTATECW_180 | ROTATECW_270) |
| 189 |
|
| 190 |
#define CROP_NONE \ |
| 191 |
0 /* "-S" -> Page_MODE_ROWSCOLS and page->rows/->cols != 0 \ |
| 192 |
*/ |
| 193 |
#define CROP_MARGINS 1 /* "-m" */ |
| 194 |
#define CROP_WIDTH 2 /* "-X" */ |
| 195 |
#define CROP_LENGTH 4 /* "-Y" */ |
| 196 |
#define CROP_ZONES 8 /* "-Z" */ |
| 197 |
#define CROP_REGIONS 16 /* "-z" */ |
| 198 |
#define CROP_ROTATE 32 |
| 199 |
#define CROP_MIRROR 64 |
| 200 |
#define CROP_INVERT 128 |
| 201 |
|
| 202 |
/* Modes for writing out images and selections */ |
| 203 |
#define ONE_FILE_COMPOSITE 0 /* One file, sections combined sections */ |
| 204 |
#define ONE_FILE_SEPARATED 1 /* One file, sections to new IFDs */ |
| 205 |
#define FILE_PER_IMAGE_COMPOSITE 2 /* One file per image, combined sections */ |
| 206 |
#define FILE_PER_IMAGE_SEPARATED 3 /* One file per input image */ |
| 207 |
#define FILE_PER_SELECTION 4 /* One file per selection */ |
| 208 |
|
| 209 |
#define COMPOSITE_IMAGES 0 /* Selections combined into one image */ |
| 210 |
#define SEPARATED_IMAGES 1 /* Selections saved to separate images */ |
| 211 |
|
| 212 |
#define STRIP 1 |
| 213 |
#define TILE 2 |
| 214 |
|
| 215 |
#define MAX_REGIONS 8 /* number of regions to extract from a single page */ |
| 216 |
#define MAX_OUTBUFFS 8 /* must match larger of zones or regions */ |
| 217 |
#define MAX_SECTIONS 32 /* number of sections per page to write to output */ |
| 218 |
#define MAX_IMAGES \ |
| 219 |
2048 /* number of images in descrete list, not in the file \ |
| 220 |
*/ |
| 221 |
#define MAX_SAMPLES 8 /* maximum number of samples per pixel supported */ |
| 222 |
#define MAX_BITS_PER_SAMPLE 64 /* maximum bit depth supported */ |
| 223 |
#define MAX_EXPORT_PAGES 999999 /* maximum number of export pages per file */ |
| 224 |
|
| 225 |
#define DUMP_NONE 0 |
| 226 |
#define DUMP_TEXT 1 |
| 227 |
#define DUMP_RAW 2 |
| 228 |
|
| 229 |
#define TIFF_DIR_MAX 65534 |
| 230 |
|
| 231 |
/* To avoid mode buffer overflow */ |
| 232 |
#define MAX_MODESTRING_LEN 9 |
| 233 |
|
| 234 |
/* Some conversion subroutines require image buffers, which are at least 3 bytes |
| 235 |
* larger than the necessary size for the image itself. */ |
| 236 |
#define NUM_BUFF_OVERSIZE_BYTES 3 |
| 237 |
|
| 238 |
/* Offsets into buffer for margins and fixed width and length segments */ |
| 239 |
struct offset |
| 240 |
{ |
| 241 |
uint32_t tmargin; |
| 242 |
uint32_t lmargin; |
| 243 |
uint32_t bmargin; |
| 244 |
uint32_t rmargin; |
| 245 |
uint32_t crop_width; |
| 246 |
uint32_t crop_length; |
| 247 |
uint32_t startx; |
| 248 |
uint32_t endx; |
| 249 |
uint32_t starty; |
| 250 |
uint32_t endy; |
| 251 |
}; |
| 252 |
|
| 253 |
/* Description of a zone within the image. Position 1 of 3 zones would be |
| 254 |
* the first third of the image. These are computed after margins and |
| 255 |
* width/length requests are applied so that you can extract multiple |
| 256 |
* zones from within a larger region for OCR or barcode recognition. |
| 257 |
*/ |
| 258 |
|
| 259 |
struct buffinfo |
| 260 |
{ |
| 261 |
size_t size; /* size of this buffer */ |
| 262 |
unsigned char *buffer; /* address of the allocated buffer */ |
| 263 |
}; |
| 264 |
|
| 265 |
struct zone |
| 266 |
{ |
| 267 |
int position; /* ordinal of segment to be extracted */ |
| 268 |
int total; /* total equal sized divisions of crop area */ |
| 269 |
}; |
| 270 |
|
| 271 |
struct pageseg |
| 272 |
{ |
| 273 |
uint32_t x1; /* index of left edge */ |
| 274 |
uint32_t x2; /* index of right edge */ |
| 275 |
uint32_t y1; /* index of top edge */ |
| 276 |
uint32_t y2; /* index of bottom edge */ |
| 277 |
int position; /* ordinal of segment to be extracted */ |
| 278 |
int total; /* total equal sized divisions of crop area */ |
| 279 |
uint32_t buffsize; /* size of buffer needed to hold the cropped zone */ |
| 280 |
}; |
| 281 |
|
| 282 |
struct coordpairs |
| 283 |
{ |
| 284 |
double X1; /* index of left edge in current units */ |
| 285 |
double X2; /* index of right edge in current units */ |
| 286 |
double Y1; /* index of top edge in current units */ |
| 287 |
double Y2; /* index of bottom edge in current units */ |
| 288 |
}; |
| 289 |
|
| 290 |
struct region |
| 291 |
{ |
| 292 |
uint32_t x1; /* pixel offset of left edge */ |
| 293 |
uint32_t x2; /* pixel offset of right edge */ |
| 294 |
uint32_t y1; /* pixel offset of top edge */ |
| 295 |
uint32_t y2; /* picel offset of bottom edge */ |
| 296 |
uint32_t width; /* width in pixels */ |
| 297 |
uint32_t length; /* length in pixels */ |
| 298 |
uint32_t buffsize; /* size of buffer needed to hold the cropped region */ |
| 299 |
}; |
| 300 |
|
| 301 |
/* Cropping parameters from command line and image data |
| 302 |
* Note: This should be renamed to proc_opts and expanded to include all current |
| 303 |
* globals if possible, but each function that accesses global variables will |
| 304 |
* have to be redone. |
| 305 |
*/ |
| 306 |
struct crop_mask |
| 307 |
{ |
| 308 |
double |
| 309 |
width; /* Selection width for master crop region in requested units */ |
| 310 |
double |
| 311 |
length; /* Selection length for master crop region in requesed units */ |
| 312 |
double margins[4]; /* Top, left, bottom, right margins */ |
| 313 |
float xres; /* Horizontal resolution read from image*/ |
| 314 |
float yres; /* Vertical resolution read from image */ |
| 315 |
uint32_t combined_width; /* Width of combined cropped zones */ |
| 316 |
uint32_t combined_length; /* Length of combined cropped zones */ |
| 317 |
uint32_t |
| 318 |
bufftotal; /* Size of buffer needed to hold all the cropped region */ |
| 319 |
uint16_t img_mode; /* Composite or separate images created from zones or |
| 320 |
regions */ |
| 321 |
uint16_t |
| 322 |
exp_mode; /* Export input images or selections to one or more files */ |
| 323 |
uint16_t crop_mode; /* Crop options to be applied */ |
| 324 |
uint16_t res_unit; /* Resolution unit for margins and selections */ |
| 325 |
uint16_t |
| 326 |
edge_ref; /* Reference edge for sections extraction and combination */ |
| 327 |
uint16_t rotation; /* Clockwise rotation of the extracted region or image */ |
| 328 |
uint16_t mirror; /* Mirror extracted region or image horizontally or |
| 329 |
vertically */ |
| 330 |
uint16_t invert; /* Invert the color map of image or region */ |
| 331 |
uint16_t photometric; /* Status of photometric interpretation for inverted |
| 332 |
image */ |
| 333 |
uint16_t selections; /* Number of regions or zones selected */ |
| 334 |
uint16_t regions; /* Number of regions delimited by corner coordinates */ |
| 335 |
struct region |
| 336 |
regionlist[MAX_REGIONS]; /* Regions within page or master crop region */ |
| 337 |
uint16_t zones; /* Number of zones delimited by Ordinal:Total requested */ |
| 338 |
struct zone zonelist[MAX_REGIONS]; /* Zones indices to define a region */ |
| 339 |
struct coordpairs corners[MAX_REGIONS]; /* Coordinates of upper left and |
| 340 |
lower right corner */ |
| 341 |
}; |
| 342 |
|
| 343 |
#define MAX_PAPERNAMES \ |
| 344 |
(sizeof(PaperTable) / sizeof(PaperTable[0])) /* was 49 \ |
| 345 |
*/ |
| 346 |
#define MAX_PAPERNAME_LENGTH 15 |
| 347 |
#define DEFAULT_RESUNIT RESUNIT_INCH |
| 348 |
#define DEFAULT_PAGE_HEIGHT 14.0 |
| 349 |
#define DEFAULT_PAGE_WIDTH 8.5 |
| 350 |
#define DEFAULT_RESOLUTION 300 |
| 351 |
#define DEFAULT_PAPER_SIZE "legal" |
| 352 |
|
| 353 |
#define ORIENTATION_NONE 0 |
| 354 |
#define ORIENTATION_PORTRAIT 1 |
| 355 |
#define ORIENTATION_LANDSCAPE 2 |
| 356 |
#define ORIENTATION_SEASCAPE 4 |
| 357 |
#define ORIENTATION_AUTO 16 |
| 358 |
|
| 359 |
#define PAGE_MODE_NONE 0 |
| 360 |
#define PAGE_MODE_RESOLUTION 1 |
| 361 |
#define PAGE_MODE_PAPERSIZE 2 |
| 362 |
#define PAGE_MODE_MARGINS 4 |
| 363 |
#define PAGE_MODE_ROWSCOLS 8 /* for -S option */ |
| 364 |
|
| 365 |
#define INVERT_DATA_ONLY 10 |
| 366 |
#define INVERT_DATA_AND_TAG 11 |
| 367 |
|
| 368 |
struct paperdef |
| 369 |
{ |
| 370 |
char name[MAX_PAPERNAME_LENGTH]; |
| 371 |
double width; |
| 372 |
double length; |
| 373 |
double asratio; |
| 374 |
}; |
| 375 |
|
| 376 |
/* European page sizes corrected from update sent by |
| 377 |
* thomas . jarosch @ intra2net . com on 5/7/2010 |
| 378 |
* Paper Size Width Length Aspect Ratio */ |
| 379 |
static const struct paperdef PaperTable[/*MAX_PAPERNAMES*/] = { |
| 380 |
{"default", 8.500, 14.000, 0.607}, |
| 381 |
{"pa4", 8.264, 11.000, 0.751}, |
| 382 |
{"letter", 8.500, 11.000, 0.773}, |
| 383 |
{"legal", 8.500, 14.000, 0.607}, |
| 384 |
{"half-letter", 8.500, 5.514, 1.542}, |
| 385 |
{"executive", 7.264, 10.528, 0.690}, |
| 386 |
{"tabloid", 11.000, 17.000, 0.647}, |
| 387 |
{"11x17", 11.000, 17.000, 0.647}, |
| 388 |
{"ledger", 17.000, 11.000, 1.545}, |
| 389 |
{"archa", 9.000, 12.000, 0.750}, |
| 390 |
{"archb", 12.000, 18.000, 0.667}, |
| 391 |
{"archc", 18.000, 24.000, 0.750}, |
| 392 |
{"archd", 24.000, 36.000, 0.667}, |
| 393 |
{"arche", 36.000, 48.000, 0.750}, |
| 394 |
{"csheet", 17.000, 22.000, 0.773}, |
| 395 |
{"dsheet", 22.000, 34.000, 0.647}, |
| 396 |
{"esheet", 34.000, 44.000, 0.773}, |
| 397 |
{"superb", 11.708, 17.042, 0.687}, |
| 398 |
{"commercial", 4.139, 9.528, 0.434}, |
| 399 |
{"monarch", 3.889, 7.528, 0.517}, |
| 400 |
{"envelope-dl", 4.333, 8.681, 0.499}, |
| 401 |
{"envelope-c5", 6.389, 9.028, 0.708}, |
| 402 |
{"europostcard", 4.139, 5.833, 0.710}, |
| 403 |
{"a0", 33.110, 46.811, 0.707}, |
| 404 |
{"a1", 23.386, 33.110, 0.706}, |
| 405 |
{"a2", 16.535, 23.386, 0.707}, |
| 406 |
{"a3", 11.693, 16.535, 0.707}, |
| 407 |
{"a4", 8.268, 11.693, 0.707}, |
| 408 |
{"a5", 5.827, 8.268, 0.705}, |
| 409 |
{"a6", 4.134, 5.827, 0.709}, |
| 410 |
{"a7", 2.913, 4.134, 0.705}, |
| 411 |
{"a8", 2.047, 2.913, 0.703}, |
| 412 |
{"a9", 1.457, 2.047, 0.712}, |
| 413 |
{"a10", 1.024, 1.457, 0.703}, |
| 414 |
{"b0", 39.370, 55.669, 0.707}, |
| 415 |
{"b1", 27.835, 39.370, 0.707}, |
| 416 |
{"b2", 19.685, 27.835, 0.707}, |
| 417 |
{"b3", 13.898, 19.685, 0.706}, |
| 418 |
{"b4", 9.843, 13.898, 0.708}, |
| 419 |
{"b5", 6.929, 9.843, 0.704}, |
| 420 |
{"b6", 4.921, 6.929, 0.710}, |
| 421 |
{"c0", 36.102, 51.063, 0.707}, |
| 422 |
{"c1", 25.512, 36.102, 0.707}, |
| 423 |
{"c2", 18.031, 25.512, 0.707}, |
| 424 |
{"c3", 12.756, 18.031, 0.707}, |
| 425 |
{"c4", 9.016, 12.756, 0.707}, |
| 426 |
{"c5", 6.378, 9.016, 0.707}, |
| 427 |
{"c6", 4.488, 6.378, 0.704}, |
| 428 |
{"", 0.000, 0.000, 1.000}}; |
| 429 |
|
| 430 |
/* Structure to define input image parameters */ |
| 431 |
struct image_data |
| 432 |
{ |
| 433 |
float xres; |
| 434 |
float yres; |
| 435 |
uint32_t width; |
| 436 |
uint32_t length; |
| 437 |
uint16_t res_unit; |
| 438 |
uint16_t bps; |
| 439 |
uint16_t spp; |
| 440 |
uint16_t planar; |
| 441 |
uint16_t photometric; |
| 442 |
uint16_t orientation; |
| 443 |
uint16_t compression; |
| 444 |
uint16_t adjustments; |
| 445 |
}; |
| 446 |
|
| 447 |
/* Structure to define the output image modifiers */ |
| 448 |
struct pagedef |
| 449 |
{ |
| 450 |
char name[16]; |
| 451 |
double width; /* width in pixels */ |
| 452 |
double length; /* length in pixels */ |
| 453 |
double hmargin; /* margins to subtract from width of sections */ |
| 454 |
double vmargin; /* margins to subtract from height of sections */ |
| 455 |
double hres; /* horizontal resolution for output */ |
| 456 |
double vres; /* vertical resolution for output */ |
| 457 |
uint32_t mode; /* bitmask of modifiers to page format */ |
| 458 |
uint16_t res_unit; /* resolution unit for output image */ |
| 459 |
unsigned int rows; /* number of section rows */ |
| 460 |
unsigned int cols; /* number of section cols */ |
| 461 |
unsigned int orient; /* portrait, landscape, seascape, auto */ |
| 462 |
}; |
| 463 |
|
| 464 |
struct dump_opts |
| 465 |
{ |
| 466 |
int debug; |
| 467 |
int format; |
| 468 |
int level; |
| 469 |
char mode[4]; |
| 470 |
char infilename[PATH_MAX + 1]; |
| 471 |
char outfilename[PATH_MAX + 1]; |
| 472 |
FILE *infile; |
| 473 |
FILE *outfile; |
| 474 |
}; |
| 475 |
|
| 476 |
/* globals */ |
| 477 |
static int outtiled = -1; |
| 478 |
static uint32_t tilewidth = 0; |
| 479 |
static uint32_t tilelength = 0; |
| 480 |
|
| 481 |
static uint16_t config = 0; |
| 482 |
static uint16_t compression = 0; |
| 483 |
static uint16_t predictor = 0; |
| 484 |
static uint16_t fillorder = 0; |
| 485 |
static uint32_t rowsperstrip = 0; |
| 486 |
static uint32_t g3opts = 0; |
| 487 |
static int ignore = FALSE; /* if true, ignore read errors */ |
| 488 |
static uint32_t defg3opts = (uint32_t)-1; |
| 489 |
static int quality = 100; /* JPEG quality */ |
| 490 |
static int jpegcolormode = -1; /* means YCbCr or to not convert */ |
| 491 |
static uint16_t defcompression = (uint16_t)-1; |
| 492 |
static uint16_t defpredictor = (uint16_t)-1; |
| 493 |
static int pageNum = 0; |
| 494 |
static int little_endian = 1; |
| 495 |
static tmsize_t check_buffsize = 0; |
| 496 |
|
| 497 |
/* Functions adapted from tiffcp with additions or significant modifications */ |
| 498 |
static int readContigStripsIntoBuffer(TIFF *, uint8_t *); |
| 499 |
static int readSeparateStripsIntoBuffer(TIFF *, uint8_t *, uint32_t, uint32_t, |
| 500 |
tsample_t, struct dump_opts *); |
| 501 |
static int readContigTilesIntoBuffer(TIFF *, uint8_t *, uint32_t, uint32_t, |
| 502 |
uint32_t, uint32_t, tsample_t, uint16_t); |
| 503 |
static int readSeparateTilesIntoBuffer(TIFF *, uint8_t *, uint32_t, uint32_t, |
| 504 |
uint32_t, uint32_t, tsample_t, uint16_t); |
| 505 |
static int writeBufferToContigStrips(TIFF *, uint8_t *, uint32_t); |
| 506 |
static int writeBufferToContigTiles(TIFF *, uint8_t *, uint32_t, uint32_t, |
| 507 |
tsample_t, struct dump_opts *); |
| 508 |
static int writeBufferToSeparateStrips(TIFF *, uint8_t *, uint32_t, uint32_t, |
| 509 |
tsample_t, struct dump_opts *); |
| 510 |
static int writeBufferToSeparateTiles(TIFF *, uint8_t *, uint32_t, uint32_t, |
| 511 |
tsample_t, struct dump_opts *); |
| 512 |
static int extractContigSamplesToBuffer(uint8_t *, uint8_t *, uint32_t, |
| 513 |
uint32_t, tsample_t, uint16_t, uint16_t, |
| 514 |
struct dump_opts *); |
| 515 |
static int processCompressOptions(char *); |
| 516 |
static void usage(int code); |
| 517 |
|
| 518 |
/* All other functions by Richard Nolde, not found in tiffcp */ |
| 519 |
static void initImageData(struct image_data *); |
| 520 |
static void initCropMasks(struct crop_mask *); |
| 521 |
static void initPageSetup(struct pagedef *, struct pageseg *, |
| 522 |
struct buffinfo[]); |
| 523 |
static void initDumpOptions(struct dump_opts *); |
| 524 |
|
| 525 |
/* Command line and file naming functions */ |
| 526 |
void process_command_opts(int, char *[], char *, char *, uint32_t *, uint16_t *, |
| 527 |
uint16_t *, uint32_t *, uint32_t *, uint32_t *, |
| 528 |
struct crop_mask *, struct pagedef *, |
| 529 |
struct dump_opts *, unsigned int *, unsigned int *); |
| 530 |
static int update_output_file(TIFF **, char *, int, char *, unsigned int *); |
| 531 |
|
| 532 |
/* * High level functions for whole image manipulation */ |
| 533 |
static int get_page_geometry(char *, struct pagedef *); |
| 534 |
static int computeInputPixelOffsets(struct crop_mask *, struct image_data *, |
| 535 |
struct offset *); |
| 536 |
static int computeOutputPixelOffsets(struct crop_mask *, struct image_data *, |
| 537 |
struct pagedef *, struct pageseg *, |
| 538 |
struct dump_opts *); |
| 539 |
static int loadImage(TIFF *, struct image_data *, struct dump_opts *, |
| 540 |
unsigned char **); |
| 541 |
static int correct_orientation(struct image_data *, unsigned char **); |
| 542 |
static int getCropOffsets(struct image_data *, struct crop_mask *, |
| 543 |
struct dump_opts *); |
| 544 |
static int processCropSelections(struct image_data *, struct crop_mask *, |
| 545 |
unsigned char **, struct buffinfo[]); |
| 546 |
static int writeSelections(TIFF *, TIFF **, struct crop_mask *, |
| 547 |
struct image_data *, struct dump_opts *, |
| 548 |
struct buffinfo[], char *, char *, unsigned int *, |
| 549 |
unsigned int); |
| 550 |
|
| 551 |
/* Section functions */ |
| 552 |
static int createImageSection(uint32_t, unsigned char **); |
| 553 |
static int extractImageSection(struct image_data *, struct pageseg *, |
| 554 |
unsigned char *, unsigned char *); |
| 555 |
static int writeSingleSection(TIFF *, TIFF *, struct image_data *, |
| 556 |
struct dump_opts *, uint32_t, uint32_t, double, |
| 557 |
double, unsigned char *); |
| 558 |
static int writeImageSections(TIFF *, TIFF *, struct image_data *, |
| 559 |
struct pagedef *, struct pageseg *, |
| 560 |
struct dump_opts *, unsigned char *, |
| 561 |
unsigned char **); |
| 562 |
/* Whole image functions */ |
| 563 |
static int createCroppedImage(struct image_data *, struct crop_mask *, |
| 564 |
unsigned char **, unsigned char **); |
| 565 |
static int writeCroppedImage(TIFF *, TIFF *, struct image_data *image, |
| 566 |
struct dump_opts *dump, uint32_t, uint32_t, |
| 567 |
unsigned char *, int, int); |
| 568 |
|
| 569 |
/* Image manipulation functions */ |
| 570 |
static int rotateContigSamples8bits(uint16_t, uint16_t, uint16_t, uint32_t, |
| 571 |
uint32_t, uint32_t, uint8_t *, uint8_t *); |
| 572 |
static int rotateContigSamples16bits(uint16_t, uint16_t, uint16_t, uint32_t, |
| 573 |
uint32_t, uint32_t, uint8_t *, uint8_t *); |
| 574 |
static int rotateContigSamples24bits(uint16_t, uint16_t, uint16_t, uint32_t, |
| 575 |
uint32_t, uint32_t, uint8_t *, uint8_t *); |
| 576 |
static int rotateContigSamples32bits(uint16_t, uint16_t, uint16_t, uint32_t, |
| 577 |
uint32_t, uint32_t, uint8_t *, uint8_t *); |
| 578 |
static int rotateImage(uint16_t, struct image_data *, uint32_t *, uint32_t *, |
| 579 |
unsigned char **, size_t *, int); |
| 580 |
static int mirrorImage(uint16_t, uint16_t, uint16_t, uint32_t, uint32_t, |
| 581 |
unsigned char *); |
| 582 |
static int invertImage(uint16_t, uint16_t, uint16_t, uint32_t, uint32_t, |
| 583 |
unsigned char *); |
| 584 |
|
| 585 |
/* Functions to reverse the sequence of samples in a scanline */ |
| 586 |
static int reverseSamples8bits(uint16_t, uint16_t, uint32_t, uint8_t *, |
| 587 |
uint8_t *); |
| 588 |
static int reverseSamples16bits(uint16_t, uint16_t, uint32_t, uint8_t *, |
| 589 |
uint8_t *); |
| 590 |
static int reverseSamples24bits(uint16_t, uint16_t, uint32_t, uint8_t *, |
| 591 |
uint8_t *); |
| 592 |
static int reverseSamples32bits(uint16_t, uint16_t, uint32_t, uint8_t *, |
| 593 |
uint8_t *); |
| 594 |
static int reverseSamplesBytes(uint16_t, uint16_t, uint32_t, uint8_t *, |
| 595 |
uint8_t *); |
| 596 |
|
| 597 |
/* Functions for manipulating individual samples in an image */ |
| 598 |
static int extractSeparateRegion(struct image_data *, struct crop_mask *, |
| 599 |
unsigned char *, unsigned char *, int); |
| 600 |
static int extractCompositeRegions(struct image_data *, struct crop_mask *, |
| 601 |
unsigned char *, unsigned char *); |
| 602 |
static int extractContigSamples8bits(uint8_t *, uint8_t *, uint32_t, tsample_t, |
| 603 |
uint16_t, uint16_t, tsample_t, uint32_t, |
| 604 |
uint32_t); |
| 605 |
static int extractContigSamples16bits(uint8_t *, uint8_t *, uint32_t, tsample_t, |
| 606 |
uint16_t, uint16_t, tsample_t, uint32_t, |
| 607 |
uint32_t); |
| 608 |
static int extractContigSamples24bits(uint8_t *, uint8_t *, uint32_t, tsample_t, |
| 609 |
uint16_t, uint16_t, tsample_t, uint32_t, |
| 610 |
uint32_t); |
| 611 |
static int extractContigSamples32bits(uint8_t *, uint8_t *, uint32_t, tsample_t, |
| 612 |
uint16_t, uint16_t, tsample_t, uint32_t, |
| 613 |
uint32_t); |
| 614 |
static int extractContigSamplesBytes(uint8_t *, uint8_t *, uint32_t, tsample_t, |
| 615 |
uint16_t, uint16_t, tsample_t, uint32_t, |
| 616 |
uint32_t); |
| 617 |
static int extractContigSamplesShifted8bits(uint8_t *, uint8_t *, uint32_t, |
| 618 |
tsample_t, uint16_t, uint16_t, |
| 619 |
tsample_t, uint32_t, uint32_t, int); |
| 620 |
static int extractContigSamplesShifted16bits(uint8_t *, uint8_t *, uint32_t, |
| 621 |
tsample_t, uint16_t, uint16_t, |
| 622 |
tsample_t, uint32_t, uint32_t, |
| 623 |
int); |
| 624 |
static int extractContigSamplesShifted24bits(uint8_t *, uint8_t *, uint32_t, |
| 625 |
tsample_t, uint16_t, uint16_t, |
| 626 |
tsample_t, uint32_t, uint32_t, |
| 627 |
int); |
| 628 |
static int extractContigSamplesShifted32bits(uint8_t *, uint8_t *, uint32_t, |
| 629 |
tsample_t, uint16_t, uint16_t, |
| 630 |
tsample_t, uint32_t, uint32_t, |
| 631 |
int); |
| 632 |
static int extractContigSamplesToTileBuffer(uint8_t *, uint8_t *, uint32_t, |
| 633 |
uint32_t, uint32_t, uint32_t, |
| 634 |
tsample_t, uint16_t, uint16_t, |
| 635 |
uint16_t, struct dump_opts *); |
| 636 |
|
| 637 |
/* Functions to combine separate planes into interleaved planes */ |
| 638 |
static int combineSeparateSamples8bits(uint8_t *[], uint8_t *, uint32_t, |
| 639 |
uint32_t, uint16_t, uint16_t, FILE *, |
| 640 |
int, int); |
| 641 |
static int combineSeparateSamples16bits(uint8_t *[], uint8_t *, uint32_t, |
| 642 |
uint32_t, uint16_t, uint16_t, FILE *, |
| 643 |
int, int); |
| 644 |
static int combineSeparateSamples24bits(uint8_t *[], uint8_t *, uint32_t, |
| 645 |
uint32_t, uint16_t, uint16_t, FILE *, |
| 646 |
int, int); |
| 647 |
static int combineSeparateSamples32bits(uint8_t *[], uint8_t *, uint32_t, |
| 648 |
uint32_t, uint16_t, uint16_t, FILE *, |
| 649 |
int, int); |
| 650 |
static int combineSeparateSamplesBytes(unsigned char *[], unsigned char *, |
| 651 |
uint32_t, uint32_t, tsample_t, uint16_t, |
| 652 |
FILE *, int, int); |
| 653 |
|
| 654 |
static int combineSeparateTileSamples8bits(uint8_t *[], uint8_t *, uint32_t, |
| 655 |
uint32_t, uint32_t, uint32_t, |
| 656 |
uint16_t, uint16_t, FILE *, int, |
| 657 |
int); |
| 658 |
static int combineSeparateTileSamples16bits(uint8_t *[], uint8_t *, uint32_t, |
| 659 |
uint32_t, uint32_t, uint32_t, |
| 660 |
uint16_t, uint16_t, FILE *, int, |
| 661 |
int); |
| 662 |
static int combineSeparateTileSamples24bits(uint8_t *[], uint8_t *, uint32_t, |
| 663 |
uint32_t, uint32_t, uint32_t, |
| 664 |
uint16_t, uint16_t, FILE *, int, |
| 665 |
int); |
| 666 |
static int combineSeparateTileSamples32bits(uint8_t *[], uint8_t *, uint32_t, |
| 667 |
uint32_t, uint32_t, uint32_t, |
| 668 |
uint16_t, uint16_t, FILE *, int, |
| 669 |
int); |
| 670 |
static int combineSeparateTileSamplesBytes(unsigned char *[], unsigned char *, |
| 671 |
uint32_t, uint32_t, uint32_t, |
| 672 |
uint32_t, tsample_t, uint16_t, |
| 673 |
FILE *, int, int); |
| 674 |
|
| 675 |
/* Dump functions for debugging */ |
| 676 |
static void dump_info(FILE *, int, char *, char *, ...); |
| 677 |
static int dump_data(FILE *, int, char *, unsigned char *, uint32_t); |
| 678 |
static int dump_byte(FILE *, int, char *, unsigned char); |
| 679 |
static int dump_short(FILE *, int, char *, uint16_t); |
| 680 |
static int dump_long(FILE *, int, char *, uint32_t); |
| 681 |
static int dump_wide(FILE *, int, char *, uint64_t); |
| 682 |
static int dump_buffer(FILE *, int, uint32_t, uint32_t, uint32_t, |
| 683 |
unsigned char *); |
| 684 |
|
| 685 |
/* End function declarations */ |
| 686 |
/* Functions derived in whole or in part from tiffcp */ |
| 687 |
/* The following functions are taken largely intact from tiffcp */ |
| 688 |
|
| 689 |
#define DEFAULT_MAX_MALLOC (256 * 1024 * 1024) |
| 690 |
|
| 691 |
/* malloc size limit (in bytes) |
| 692 |
* disabled when set to 0 */ |
| 693 |
static tmsize_t maxMalloc = DEFAULT_MAX_MALLOC; |
| 694 |
|
| 695 |
/** |
| 696 |
* This custom malloc function enforce a maximum allocation size |
| 697 |
*/ |
| 698 |
static void *limitMalloc(tmsize_t s) |
| 699 |
{ |
| 700 |
if (maxMalloc && (s > maxMalloc)) |
| 701 |
{ |
| 702 |
fprintf(stderr, |
| 703 |
"MemoryLimitError: allocation of %" PRIu64 |
| 704 |
" bytes is forbidden. Limit is %" PRIu64 ".\n", |
| 705 |
(uint64_t)s, (uint64_t)maxMalloc); |
| 706 |
fprintf(stderr, " use -k option to change limit.\n"); |
| 707 |
return NULL; |
| 708 |
} |
| 709 |
return _TIFFmalloc(s); |
| 710 |
} |
| 711 |
|
| 712 |
static const char usage_info[] = |
| 713 |
"Copy, crop, convert, extract, and/or process TIFF files\n\n" |
| 714 |
"usage: tiffcrop [options] source1 ... sourceN destination\n" |
| 715 |
"where options are:\n" |
| 716 |
" -h Print this syntax listing\n" |
| 717 |
" -v Print tiffcrop version identifier and last revision date\n" |
| 718 |
" \n" |
| 719 |
" -a Append to output instead of overwriting\n" |
| 720 |
" -B Force output to be written with Big - Endian byte order.\n" |
| 721 |
" -L Force output to be written with Little-Endian byte order.\n" |
| 722 |
" -M Suppress the use of memory-mapped files when reading images.\n" |
| 723 |
" -C Suppress the use of \"strip chopping\" when reading images that " |
| 724 |
"have a single strip/tile of uncompressed data.\n" |
| 725 |
" \n" |
| 726 |
" -d offset Set initial directory offset, counting first image as one, " |
| 727 |
"not zero\n" |
| 728 |
" -p contig Pack samples contiguously (e.g. RGBRGB...)\n" |
| 729 |
" -p separate Store samples separately (e.g. RRR...GGG...BBB...)\n" |
| 730 |
" -s Write output in strips\n" |
| 731 |
" -t Write output in tiles\n" |
| 732 |
" -i Ignore read errors\n" |
| 733 |
" -k size Set the memory allocation limit in MiB. 0 to disable limit\n" |
| 734 |
" \n" |
| 735 |
" -r # Make each strip have no more than # rows\n" |
| 736 |
" -w # Set output tile width (pixels)\n" |
| 737 |
" -l # Set output tile length (pixels)\n" |
| 738 |
" \n" |
| 739 |
" -f lsb2msb Force lsb-to-msb FillOrder for output\n" |
| 740 |
" -f msb2lsb Force msb-to-lsb FillOrder for output\n" |
| 741 |
"\n" |
| 742 |
#ifdef LZW_SUPPORT |
| 743 |
" -c lzw[:opts] Compress output with Lempel-Ziv & Welch encoding\n" |
| 744 |
/* " LZW options:\n" */ |
| 745 |
" # Set predictor value\n" |
| 746 |
" For example, -c lzw:2 for LZW-encoded data with horizontal " |
| 747 |
"differencing\n" |
| 748 |
#endif |
| 749 |
#ifdef ZIP_SUPPORT |
| 750 |
" -c zip[:opts] Compress output with deflate encoding\n" |
| 751 |
/* " Deflate (ZIP) options:\n" */ |
| 752 |
" # Set predictor value\n" |
| 753 |
#endif |
| 754 |
#ifdef JPEG_SUPPORT |
| 755 |
" -c jpeg[:opts] Compress output with JPEG encoding\n" |
| 756 |
/* " JPEG options:\n" */ |
| 757 |
" # Set compression quality level (0-100, default 100)\n" |
| 758 |
" raw Output same colorspace image as input\n" |
| 759 |
" rgb Output color image as RGB (default is YCbCr)\n" |
| 760 |
" For example, -c jpeg:raw:50 for JPEG-encoded with 50% comp. " |
| 761 |
"quality and the same colorspace\n" |
| 762 |
#endif |
| 763 |
#ifdef PACKBITS_SUPPORT |
| 764 |
" -c packbits Compress output with packbits encoding\n" |
| 765 |
#endif |
| 766 |
#ifdef CCITT_SUPPORT |
| 767 |
" -c g3[:opts] Compress output with CCITT Group 3 encoding\n" |
| 768 |
/* " CCITT Group 3 options:\n" */ |
| 769 |
" 1d Use default CCITT Group 3 1D-encoding\n" |
| 770 |
" 2d Use optional CCITT Group 3 2D-encoding\n" |
| 771 |
" fill Byte-align EOL codes\n" |
| 772 |
" For example, -c g3:2d:fill for G3-2D-encoded data with byte-aligned " |
| 773 |
"EOLs\n" |
| 774 |
" -c g4 Compress output with CCITT Group 4 encoding\n" |
| 775 |
#endif |
| 776 |
#if defined(LZW_SUPPORT) || defined(ZIP_SUPPORT) || defined(JPEG_SUPPORT) || \ |
| 777 |
defined(PACKBITS_SUPPORT) || defined(CCITT_SUPPORT) |
| 778 |
" -c none Use no compression algorithm on output\n" |
| 779 |
#endif |
| 780 |
"\n" |
| 781 |
"Page and selection options:\n" |
| 782 |
" -N odd|even|#,#-#,#|last sequences and ranges of images within " |
| 783 |
"file to process\n" |
| 784 |
" The words odd or even may be used to specify all odd or even " |
| 785 |
"numbered images.\n" |
| 786 |
" The word last may be used in place of a number in the " |
| 787 |
"sequence to indicate.\n" |
| 788 |
" The final image in the file without knowing how many images " |
| 789 |
"there are.\n" |
| 790 |
" Numbers are counted from one even though TIFF IFDs are " |
| 791 |
"counted from zero.\n" |
| 792 |
"\n" |
| 793 |
" -E t|l|r|b edge to use as origin for width and length of crop region\n" |
| 794 |
" -U units [in, cm, px ] inches, centimeters or pixels\n" |
| 795 |
" \n" |
| 796 |
" -m #,#,#,# margins from edges for selection: top, left, bottom, right " |
| 797 |
"separated by commas\n" |
| 798 |
" -X # horizontal dimension of region to extract expressed in " |
| 799 |
"current units\n" |
| 800 |
" -Y # vertical dimension of region to extract expressed in current " |
| 801 |
"units\n" |
| 802 |
" -Z #:#,#:# zones of the image designated as position X of Y,\n" |
| 803 |
" eg 1:3 would be first of three equal portions measured from " |
| 804 |
"reference edge\n" |
| 805 |
" -z x1,y1,x2,y2:...:xN,yN,xN+1,yN+1\n" |
| 806 |
" regions of the image designated by upper left and lower " |
| 807 |
"right coordinates\n" |
| 808 |
"\n" |
| 809 |
"Export grouping options:\n" |
| 810 |
" -e c|d|i|m|s export mode for images and selections from input " |
| 811 |
"images.\n" |
| 812 |
" When exporting a composite image from multiple zones or " |
| 813 |
"regions\n" |
| 814 |
" (combined and image modes), the selections must have " |
| 815 |
"equal sizes\n" |
| 816 |
" for the axis perpendicular to the edge specified with " |
| 817 |
"-E.\n" |
| 818 |
" c|combined All images and selections are written to a single file " |
| 819 |
"(default).\n" |
| 820 |
" with multiple selections from one image combined into a " |
| 821 |
"single image.\n" |
| 822 |
" d|divided All images and selections are written to a single file\n" |
| 823 |
" with each selection from one image written to a new " |
| 824 |
"image.\n" |
| 825 |
" i|image Each input image is written to a new file (numeric " |
| 826 |
"filename sequence)\n" |
| 827 |
" with multiple selections from the image combined into " |
| 828 |
"one image.\n" |
| 829 |
" m|multiple Each input image is written to a new file (numeric " |
| 830 |
"filename sequence)\n" |
| 831 |
" with each selection from the image written to a new " |
| 832 |
"image.\n" |
| 833 |
" s|separated Individual selections from each image are written to " |
| 834 |
"separate files.\n" |
| 835 |
"\n" |
| 836 |
"Output options:\n" |
| 837 |
" -H # Set horizontal resolution of output images to #\n" |
| 838 |
" -V # Set vertical resolution of output images to #\n" |
| 839 |
" -J # Set horizontal margin of output page to # expressed in " |
| 840 |
"current units\n" |
| 841 |
" when sectioning image into columns x rows using the -S " |
| 842 |
"cols:rows option\n" |
| 843 |
" -K # Set verticalal margin of output page to # expressed in " |
| 844 |
"current units\n" |
| 845 |
" when sectioning image into columns x rows using the -S " |
| 846 |
"cols:rows option\n" |
| 847 |
" \n" |
| 848 |
" -O orient orientation for output image, portrait, landscape, auto\n" |
| 849 |
" -P page page size for output image segments, eg letter, legal, " |
| 850 |
"tabloid, etc\n" |
| 851 |
" use #.#x#.# to specify a custom page size in the currently " |
| 852 |
"defined units\n" |
| 853 |
" where #.# represents the width and length\n" |
| 854 |
" -S cols:rows Divide the image into equal sized segments using cols " |
| 855 |
"across and rows down.\n" |
| 856 |
"\n" |
| 857 |
" -F hor|vert|both\n" |
| 858 |
" flip (mirror) image or region horizontally, vertically, or " |
| 859 |
"both\n" |
| 860 |
" -R # [90,180,or 270] degrees clockwise rotation of image or " |
| 861 |
"extracted region\n" |
| 862 |
" -I [black|white|data|both]\n" |
| 863 |
" invert color space, eg dark to light for bilevel and " |
| 864 |
"grayscale images\n" |
| 865 |
" If argument is white or black, set the " |
| 866 |
"PHOTOMETRIC_INTERPRETATION \n" |
| 867 |
" tag to MinIsBlack or MinIsWhite without altering the image " |
| 868 |
"data\n" |
| 869 |
" If the argument is data or both, the image data are " |
| 870 |
"modified:\n" |
| 871 |
" both inverts the data and the PHOTOMETRIC_INTERPRETATION " |
| 872 |
"tag,\n" |
| 873 |
" data inverts the data but not the PHOTOMETRIC_INTERPRETATION " |
| 874 |
"tag\n" |
| 875 |
"\n" |
| 876 |
"-D opt1:value1,opt2:value2,opt3:value3:opt4:value4\n" |
| 877 |
" Debug/dump program progress and/or data to non-TIFF files.\n" |
| 878 |
" Options include the following and must be joined as a comma\n" |
| 879 |
" separate list. The use of this option is generally limited " |
| 880 |
"to\n" |
| 881 |
" program debugging and development of future options.\n" |
| 882 |
"\n" |
| 883 |
" debug:N Display limited program progress indicators where larger N\n" |
| 884 |
" increase the level of detail. Note: Tiffcrop may be compiled " |
| 885 |
"with\n" |
| 886 |
" -DDEVELMODE to enable additional very low level debug " |
| 887 |
"reporting.\n" |
| 888 |
"\n" |
| 889 |
" Format:txt|raw Format any logged data as ASCII text or raw binary \n" |
| 890 |
" values. ASCII text dumps include strings of ones and zeroes\n" |
| 891 |
" representing the binary values in the image data plus " |
| 892 |
"identifying headers.\n" |
| 893 |
"\n" |
| 894 |
" level:N Specify the level of detail presented in the dump files.\n" |
| 895 |
" This can vary from dumps of the entire input or output image " |
| 896 |
"data to dumps\n" |
| 897 |
" of data processed by specific functions. Current range of " |
| 898 |
"levels is 1 to 3.\n" |
| 899 |
"\n" |
| 900 |
" input:full-path-to-directory/input-dumpname\n" |
| 901 |
"\n" |
| 902 |
" output:full-path-to-directory/output-dumpnaem\n" |
| 903 |
"\n" |
| 904 |
" When dump files are being written, each image will be " |
| 905 |
"written to a separate\n" |
| 906 |
" file with the name built by adding a numeric sequence value " |
| 907 |
"to the dumpname\n" |
| 908 |
" and an extension of .txt for ASCII dumps or .bin for binary " |
| 909 |
"dumps.\n" |
| 910 |
"\n" |
| 911 |
" The four debug/dump options are independent, though it makes " |
| 912 |
"little sense to\n" |
| 913 |
" specify a dump file without specifying a detail level.\n" |
| 914 |
"\n" |
| 915 |
"Note 1: The (-X|-Y), -Z, -z and -S options are mutually exclusive.\n" |
| 916 |
" In no case should the options be applied to a given " |
| 917 |
"selection successively.\n" |
| 918 |
"\n" |
| 919 |
"Note 2: Any of the -X, -Y, -Z and -z options together with other " |
| 920 |
"PAGE_MODE_x options\n" |
| 921 |
" such as - H, -V, -P, -J or -K are not supported and may " |
| 922 |
"cause buffer overflows.\n" |
| 923 |
"\n"; |
| 924 |
|
| 925 |
/* This function could be modified to pass starting sample offset |
| 926 |
* and number of samples as args to select fewer than spp |
| 927 |
* from input image. These would then be passed to individual |
| 928 |
* extractContigSampleXX routines. |
| 929 |
*/ |
| 930 |
static int readContigTilesIntoBuffer(TIFF *in, uint8_t *buf, |
| 931 |
uint32_t imagelength, uint32_t imagewidth, |
| 932 |
uint32_t tw, uint32_t tl, tsample_t spp, |
| 933 |
uint16_t bps) |
| 934 |
{ |
| 935 |
int status = 1; |
| 936 |
tsample_t sample = 0; |
| 937 |
tsample_t count = spp; |
| 938 |
uint32_t row, col, trow; |
| 939 |
uint32_t nrow, ncol; |
| 940 |
uint32_t dst_rowsize, shift_width; |
| 941 |
uint32_t bytes_per_sample, bytes_per_pixel; |
| 942 |
uint32_t trailing_bits, prev_trailing_bits; |
| 943 |
tmsize_t tile_rowsize = TIFFTileRowSize(in); |
| 944 |
tmsize_t src_offset, dst_offset; |
| 945 |
uint32_t row_offset, col_offset; |
| 946 |
uint8_t *bufp = (uint8_t *)buf; |
| 947 |
unsigned char *src = NULL; |
| 948 |
unsigned char *dst = NULL; |
| 949 |
tsize_t tbytes = 0, tile_buffsize = 0; |
| 950 |
tsize_t tilesize = TIFFTileSize(in); |
| 951 |
unsigned char *tilebuf = NULL; |
| 952 |
|
| 953 |
bytes_per_sample = (bps + 7) / 8; |
| 954 |
bytes_per_pixel = ((bps * spp) + 7) / 8; |
| 955 |
|
| 956 |
if ((bps % 8) == 0) |
| 957 |
shift_width = 0; |
| 958 |
else |
| 959 |
{ |
| 960 |
if (bytes_per_pixel < (bytes_per_sample + 1)) |
| 961 |
shift_width = bytes_per_pixel; |
| 962 |
else |
| 963 |
shift_width = bytes_per_sample + 1; |
| 964 |
} |
| 965 |
|
| 966 |
tile_buffsize = tilesize; |
| 967 |
if (tilesize == 0 || tile_rowsize == 0) |
| 968 |
{ |
| 969 |
TIFFError("readContigTilesIntoBuffer", |
| 970 |
"Tile size or tile rowsize is zero"); |
| 971 |
exit(EXIT_FAILURE); |
| 972 |
} |
| 973 |
|
| 974 |
if (tilesize < (tsize_t)(tl * tile_rowsize)) |
| 975 |
{ |
| 976 |
#ifdef DEBUG2 |
| 977 |
TIFFError("readContigTilesIntoBuffer", |
| 978 |
"Tilesize %" PRId64 |
| 979 |
" is too small, using alternate calculation %" PRIu64, |
| 980 |
tilesize, tl * tile_rowsize); |
| 981 |
#endif |
| 982 |
if (tile_rowsize != 0 && |
| 983 |
(tmsize_t)tl > (TIFF_TMSIZE_T_MAX / tile_rowsize)) |
| 984 |
{ |
| 985 |
TIFFError("readContigTilesIntoBuffer", |
| 986 |
"Integer overflow when calculating buffer size."); |
| 987 |
exit(EXIT_FAILURE); |
| 988 |
} |
| 989 |
tile_buffsize = tl * tile_rowsize; |
| 990 |
} |
| 991 |
|
| 992 |
/* Add 3 padding bytes for extractContigSamplesShifted32bits */ |
| 993 |
if ((size_t)tile_buffsize > TIFF_TMSIZE_T_MAX - NUM_BUFF_OVERSIZE_BYTES) |
| 994 |
{ |
| 995 |
TIFFError("readContigTilesIntoBuffer", |
| 996 |
"Integer overflow when calculating buffer size."); |
| 997 |
exit(EXIT_FAILURE); |
| 998 |
} |
| 999 |
tilebuf = limitMalloc(tile_buffsize + NUM_BUFF_OVERSIZE_BYTES); |
| 1000 |
if (tilebuf == 0) |
| 1001 |
return 0; |
| 1002 |
tilebuf[tile_buffsize] = 0; |
| 1003 |
tilebuf[tile_buffsize + 1] = 0; |
| 1004 |
tilebuf[tile_buffsize + 2] = 0; |
| 1005 |
|
| 1006 |
dst_rowsize = ((imagewidth * bps * spp) + 7) / 8; |
| 1007 |
for (row = 0; row < imagelength; row += tl) |
| 1008 |
{ |
| 1009 |
nrow = (row + tl > imagelength) ? imagelength - row : tl; |
| 1010 |
for (col = 0; col < imagewidth; col += tw) |
| 1011 |
{ |
| 1012 |
tbytes = TIFFReadTile(in, tilebuf, col, row, 0, 0); |
| 1013 |
if (tbytes < tilesize && !ignore) |
| 1014 |
{ |
| 1015 |
TIFFError(TIFFFileName(in), |
| 1016 |
"Error, can't read tile at row %" PRIu32 |
| 1017 |
" col %" PRIu32 ", Read %" TIFF_SSIZE_FORMAT |
| 1018 |
" bytes of %" TIFF_SSIZE_FORMAT, |
| 1019 |
col, row, tbytes, tilesize); |
| 1020 |
status = 0; |
| 1021 |
_TIFFfree(tilebuf); |
| 1022 |
return status; |
| 1023 |
} |
| 1024 |
|
| 1025 |
row_offset = row * dst_rowsize; |
| 1026 |
col_offset = ((col * bps * spp) + 7) / 8; |
| 1027 |
bufp = buf + row_offset + col_offset; |
| 1028 |
|
| 1029 |
if (col + tw > imagewidth) |
| 1030 |
ncol = imagewidth - col; |
| 1031 |
else |
| 1032 |
ncol = tw; |
| 1033 |
|
| 1034 |
/* Each tile scanline will start on a byte boundary but it |
| 1035 |
* has to be merged into the scanline for the entire |
| 1036 |
* image buffer and the previous segment may not have |
| 1037 |
* ended on a byte boundary |
| 1038 |
*/ |
| 1039 |
/* Optimization for common bit depths, all samples */ |
| 1040 |
if (((bps % 8) == 0) && (count == spp)) |
| 1041 |
{ |
| 1042 |
for (trow = 0; trow < nrow; trow++) |
| 1043 |
{ |
| 1044 |
src_offset = trow * tile_rowsize; |
| 1045 |
_TIFFmemcpy(bufp, tilebuf + src_offset, |
| 1046 |
(ncol * spp * bps) / 8); |
| 1047 |
bufp += (imagewidth * bps * spp) / 8; |
| 1048 |
} |
| 1049 |
} |
| 1050 |
else |
| 1051 |
{ |
| 1052 |
/* Bit depths not a multiple of 8 and/or extract fewer than spp |
| 1053 |
* samples */ |
| 1054 |
prev_trailing_bits = trailing_bits = 0; |
| 1055 |
trailing_bits = (ncol * bps * spp) % 8; |
| 1056 |
|
| 1057 |
/* for (trow = 0; tl < nrow; trow++) */ |
| 1058 |
for (trow = 0; trow < nrow; trow++) |
| 1059 |
{ |
| 1060 |
src_offset = trow * tile_rowsize; |
| 1061 |
src = tilebuf + src_offset; |
| 1062 |
dst_offset = (row + trow) * (tmsize_t)dst_rowsize; |
| 1063 |
dst = buf + dst_offset + col_offset; |
| 1064 |
switch (shift_width) |
| 1065 |
{ |
| 1066 |
case 0: |
| 1067 |
if (extractContigSamplesBytes(src, dst, ncol, |
| 1068 |
sample, spp, bps, |
| 1069 |
count, 0, ncol)) |
| 1070 |
{ |
| 1071 |
TIFFError("readContigTilesIntoBuffer", |
| 1072 |
"Unable to extract row %" PRIu32 |
| 1073 |
" from tile %" PRIu32, |
| 1074 |
row, TIFFCurrentTile(in)); |
| 1075 |
return 1; |
| 1076 |
} |
| 1077 |
break; |
| 1078 |
case 1: |
| 1079 |
if (bps == 1) |
| 1080 |
{ |
| 1081 |
if (extractContigSamplesShifted8bits( |
| 1082 |
src, dst, ncol, sample, spp, bps, count, |
| 1083 |
0, ncol, prev_trailing_bits)) |
| 1084 |
{ |
| 1085 |
TIFFError("readContigTilesIntoBuffer", |
| 1086 |
"Unable to extract row %" PRIu32 |
| 1087 |
" from tile %" PRIu32, |
| 1088 |
row, TIFFCurrentTile(in)); |
| 1089 |
return 1; |
| 1090 |
} |
| 1091 |
break; |
| 1092 |
} |
| 1093 |
else if (extractContigSamplesShifted16bits( |
| 1094 |
src, dst, ncol, sample, spp, bps, |
| 1095 |
count, 0, ncol, prev_trailing_bits)) |
| 1096 |
{ |
| 1097 |
TIFFError("readContigTilesIntoBuffer", |
| 1098 |
"Unable to extract row %" PRIu32 |
| 1099 |
" from tile %" PRIu32, |
| 1100 |
row, TIFFCurrentTile(in)); |
| 1101 |
return 1; |
| 1102 |
} |
| 1103 |
break; |
| 1104 |
case 2: |
| 1105 |
if (extractContigSamplesShifted24bits( |
| 1106 |
src, dst, ncol, sample, spp, bps, count, 0, |
| 1107 |
ncol, prev_trailing_bits)) |
| 1108 |
{ |
| 1109 |
TIFFError("readContigTilesIntoBuffer", |
| 1110 |
"Unable to extract row %" PRIu32 |
| 1111 |
" from tile %" PRIu32, |
| 1112 |
row, TIFFCurrentTile(in)); |
| 1113 |
return 1; |
| 1114 |
} |
| 1115 |
break; |
| 1116 |
case 3: |
| 1117 |
case 4: |
| 1118 |
case 5: |
| 1119 |
if (extractContigSamplesShifted32bits( |
| 1120 |
src, dst, ncol, sample, spp, bps, count, 0, |
| 1121 |
ncol, prev_trailing_bits)) |
| 1122 |
{ |
| 1123 |
TIFFError("readContigTilesIntoBuffer", |
| 1124 |
"Unable to extract row %" PRIu32 |
| 1125 |
" from tile %" PRIu32, |
| 1126 |
row, TIFFCurrentTile(in)); |
| 1127 |
return 1; |
| 1128 |
} |
| 1129 |
break; |
| 1130 |
default: |
| 1131 |
TIFFError("readContigTilesIntoBuffer", |
| 1132 |
"Unsupported bit depth %" PRIu16, bps); |
| 1133 |
return 1; |
| 1134 |
} |
| 1135 |
} |
| 1136 |
} |
| 1137 |
} |
| 1138 |
} |
| 1139 |
|
| 1140 |
_TIFFfree(tilebuf); |
| 1141 |
return status; |
| 1142 |
} |
| 1143 |
|
| 1144 |
static int readSeparateTilesIntoBuffer(TIFF *in, uint8_t *obuf, |
| 1145 |
uint32_t imagelength, |
| 1146 |
uint32_t imagewidth, uint32_t tw, |
| 1147 |
uint32_t tl, uint16_t spp, uint16_t bps) |
| 1148 |
{ |
| 1149 |
int i, status = 1, sample; |
| 1150 |
int shift_width, bytes_per_pixel; |
| 1151 |
uint16_t bytes_per_sample; |
| 1152 |
uint32_t row, col; /* Current row and col of image */ |
| 1153 |
uint32_t nrow, ncol; /* Number of rows and cols in current tile */ |
| 1154 |
uint32_t row_offset, col_offset; /* Output buffer offsets */ |
| 1155 |
tsize_t tbytes = 0, tilesize = TIFFTileSize(in); |
| 1156 |
tsample_t s; |
| 1157 |
uint8_t *bufp = (uint8_t *)obuf; |
| 1158 |
unsigned char *srcbuffs[MAX_SAMPLES]; |
| 1159 |
unsigned char *tbuff = NULL; |
| 1160 |
|
| 1161 |
bytes_per_sample = (bps + 7) / 8; |
| 1162 |
|
| 1163 |
for (sample = 0; (sample < spp) && (sample < MAX_SAMPLES); sample++) |
| 1164 |
{ |
| 1165 |
srcbuffs[sample] = NULL; |
| 1166 |
tbuff = |
| 1167 |
(unsigned char *)limitMalloc(tilesize + NUM_BUFF_OVERSIZE_BYTES); |
| 1168 |
if (!tbuff) |
| 1169 |
{ |
| 1170 |
TIFFError("readSeparateTilesIntoBuffer", |
| 1171 |
"Unable to allocate tile read buffer for sample %d", |
| 1172 |
sample); |
| 1173 |
for (i = 0; i < sample; i++) |
| 1174 |
_TIFFfree(srcbuffs[i]); |
| 1175 |
return 0; |
| 1176 |
} |
| 1177 |
srcbuffs[sample] = tbuff; |
| 1178 |
} |
| 1179 |
/* Each tile contains only the data for a single plane |
| 1180 |
* arranged in scanlines of tw * bytes_per_sample bytes. |
| 1181 |
*/ |
| 1182 |
for (row = 0; row < imagelength; row += tl) |
| 1183 |
{ |
| 1184 |
nrow = (row + tl > imagelength) ? imagelength - row : tl; |
| 1185 |
for (col = 0; col < imagewidth; col += tw) |
| 1186 |
{ |
| 1187 |
for (s = 0; s < spp && s < MAX_SAMPLES; s++) |
| 1188 |
{ /* Read each plane of a tile set into srcbuffs[s] */ |
| 1189 |
tbytes = TIFFReadTile(in, srcbuffs[s], col, row, 0, s); |
| 1190 |
if (tbytes < 0 && !ignore) |
| 1191 |
{ |
| 1192 |
TIFFError(TIFFFileName(in), |
| 1193 |
"Error, can't read tile for row %" PRIu32 |
| 1194 |
" col %" PRIu32 ", " |
| 1195 |
"sample %" PRIu16, |
| 1196 |
col, row, s); |
| 1197 |
status = 0; |
| 1198 |
for (sample = 0; (sample < spp) && (sample < MAX_SAMPLES); |
| 1199 |
sample++) |
| 1200 |
{ |
| 1201 |
tbuff = srcbuffs[sample]; |
| 1202 |
if (tbuff != NULL) |
| 1203 |
_TIFFfree(tbuff); |
| 1204 |
} |
| 1205 |
return status; |
| 1206 |
} |
| 1207 |
} |
| 1208 |
/* Tiles on the right edge may be padded out to tw |
| 1209 |
* which must be a multiple of 16. |
| 1210 |
* Ncol represents the visible (non padding) portion. |
| 1211 |
*/ |
| 1212 |
if (col + tw > imagewidth) |
| 1213 |
ncol = imagewidth - col; |
| 1214 |
else |
| 1215 |
ncol = tw; |
| 1216 |
|
| 1217 |
row_offset = row * (((imagewidth * spp * bps) + 7) / 8); |
| 1218 |
col_offset = ((col * spp * bps) + 7) / 8; |
| 1219 |
bufp = obuf + row_offset + col_offset; |
| 1220 |
|
| 1221 |
if ((bps % 8) == 0) |
| 1222 |
{ |
| 1223 |
if (combineSeparateTileSamplesBytes(srcbuffs, bufp, ncol, nrow, |
| 1224 |
imagewidth, tw, spp, bps, |
| 1225 |
NULL, 0, 0)) |
| 1226 |
{ |
| 1227 |
status = 0; |
| 1228 |
break; |
| 1229 |
} |
| 1230 |
} |
| 1231 |
else |
| 1232 |
{ |
| 1233 |
bytes_per_pixel = ((bps * spp) + 7) / 8; |
| 1234 |
if (bytes_per_pixel < (bytes_per_sample + 1)) |
| 1235 |
shift_width = bytes_per_pixel; |
| 1236 |
else |
| 1237 |
shift_width = bytes_per_sample + 1; |
| 1238 |
|
| 1239 |
switch (shift_width) |
| 1240 |
{ |
| 1241 |
case 1: |
| 1242 |
if (combineSeparateTileSamples8bits( |
| 1243 |
srcbuffs, bufp, ncol, nrow, imagewidth, tw, spp, |
| 1244 |
bps, NULL, 0, 0)) |
| 1245 |
{ |
| 1246 |
status = 0; |
| 1247 |
break; |
| 1248 |
} |
| 1249 |
break; |
| 1250 |
case 2: |
| 1251 |
if (combineSeparateTileSamples16bits( |
| 1252 |
srcbuffs, bufp, ncol, nrow, imagewidth, tw, spp, |
| 1253 |
bps, NULL, 0, 0)) |
| 1254 |
{ |
| 1255 |
status = 0; |
| 1256 |
break; |
| 1257 |
} |
| 1258 |
break; |
| 1259 |
case 3: |
| 1260 |
if (combineSeparateTileSamples24bits( |
| 1261 |
srcbuffs, bufp, ncol, nrow, imagewidth, tw, spp, |
| 1262 |
bps, NULL, 0, 0)) |
| 1263 |
{ |
| 1264 |
status = 0; |
| 1265 |
break; |
| 1266 |
} |
| 1267 |
break; |
| 1268 |
case 4: |
| 1269 |
case 5: |
| 1270 |
case 6: |
| 1271 |
case 7: |
| 1272 |
case 8: |
| 1273 |
if (combineSeparateTileSamples32bits( |
| 1274 |
srcbuffs, bufp, ncol, nrow, imagewidth, tw, spp, |
| 1275 |
bps, NULL, 0, 0)) |
| 1276 |
{ |
| 1277 |
status = 0; |
| 1278 |
break; |
| 1279 |
} |
| 1280 |
break; |
| 1281 |
default: |
| 1282 |
TIFFError("readSeparateTilesIntoBuffer", |
| 1283 |
"Unsupported bit depth: %" PRIu16, bps); |
| 1284 |
status = 0; |
| 1285 |
break; |
| 1286 |
} |
| 1287 |
} |
| 1288 |
} |
| 1289 |
} |
| 1290 |
|
| 1291 |
for (sample = 0; (sample < spp) && (sample < MAX_SAMPLES); sample++) |
| 1292 |
{ |
| 1293 |
tbuff = srcbuffs[sample]; |
| 1294 |
if (tbuff != NULL) |
| 1295 |
_TIFFfree(tbuff); |
| 1296 |
} |
| 1297 |
|
| 1298 |
return status; |
| 1299 |
} |
| 1300 |
|
| 1301 |
static int writeBufferToContigStrips(TIFF *out, uint8_t *buf, |
| 1302 |
uint32_t imagelength) |
| 1303 |
{ |
| 1304 |
uint32_t row, nrows, rowsperstrip; |
| 1305 |
tstrip_t strip = 0; |
| 1306 |
tsize_t stripsize; |
| 1307 |
|
| 1308 |
TIFFGetFieldDefaulted(out, TIFFTAG_ROWSPERSTRIP, &rowsperstrip); |
| 1309 |
for (row = 0; row < imagelength; row += rowsperstrip) |
| 1310 |
{ |
| 1311 |
nrows = (row + rowsperstrip > imagelength) ? imagelength - row |
| 1312 |
: rowsperstrip; |
| 1313 |
stripsize = TIFFVStripSize(out, nrows); |
| 1314 |
if (TIFFWriteEncodedStrip(out, strip++, buf, stripsize) < 0) |
| 1315 |
{ |
| 1316 |
TIFFError(TIFFFileName(out), "Error, can't write strip %" PRIu32, |
| 1317 |
strip - 1); |
| 1318 |
return 1; |
| 1319 |
} |
| 1320 |
buf += stripsize; |
| 1321 |
} |
| 1322 |
|
| 1323 |
return 0; |
| 1324 |
} |
| 1325 |
|
| 1326 |
/* Abandon plans to modify code so that plannar orientation separate images |
| 1327 |
* do not have all samples for each channel written before all samples |
| 1328 |
* for the next channel have been abandoned. |
| 1329 |
* Libtiff internals seem to depend on all data for a given sample |
| 1330 |
* being contiguous within a strip or tile when PLANAR_CONFIG is |
| 1331 |
* separate. All strips or tiles of a given plane are written |
| 1332 |
* before any strips or tiles of a different plane are stored. |
| 1333 |
*/ |
| 1334 |
static int writeBufferToSeparateStrips(TIFF *out, uint8_t *buf, uint32_t length, |
| 1335 |
uint32_t width, uint16_t spp, |
| 1336 |
struct dump_opts *dump) |
| 1337 |
{ |
| 1338 |
uint8_t *src; |
| 1339 |
uint16_t bps; |
| 1340 |
uint32_t row, nrows, rowsize, rowsperstrip; |
| 1341 |
uint32_t bytes_per_sample; |
| 1342 |
tsample_t s; |
| 1343 |
tstrip_t strip = 0; |
| 1344 |
tsize_t stripsize = TIFFStripSize(out); |
| 1345 |
tsize_t rowstripsize, scanlinesize = TIFFScanlineSize(out); |
| 1346 |
tdata_t obuf; |
| 1347 |
|
| 1348 |
(void)TIFFGetFieldDefaulted(out, TIFFTAG_ROWSPERSTRIP, &rowsperstrip); |
| 1349 |
(void)TIFFGetFieldDefaulted(out, TIFFTAG_BITSPERSAMPLE, &bps); |
| 1350 |
bytes_per_sample = (bps + 7) / 8; |
| 1351 |
if (width == 0 || (uint32_t)bps * (uint32_t)spp > UINT32_MAX / width || |
| 1352 |
bps * spp * width > UINT32_MAX - 7U) |
| 1353 |
{ |
| 1354 |
TIFFError( |
| 1355 |
TIFFFileName(out), |
| 1356 |
"Error, uint32_t overflow when computing (bps * spp * width) + 7"); |
| 1357 |
return 1; |
| 1358 |
} |
| 1359 |
rowsize = |
| 1360 |
((bps * spp * width) + 7U) / 8; /* source has interleaved samples */ |
| 1361 |
if (bytes_per_sample == 0 || rowsperstrip > UINT32_MAX / bytes_per_sample || |
| 1362 |
rowsperstrip * bytes_per_sample > UINT32_MAX / (width + 1)) |
| 1363 |
{ |
| 1364 |
TIFFError(TIFFFileName(out), |
| 1365 |
"Error, uint32_t overflow when computing rowsperstrip * " |
| 1366 |
"bytes_per_sample * (width + 1)"); |
| 1367 |
return 1; |
| 1368 |
} |
| 1369 |
rowstripsize = (tsize_t)rowsperstrip * bytes_per_sample * (width + 1); |
| 1370 |
|
| 1371 |
/* Add 3 padding bytes for extractContigSamples32bits */ |
| 1372 |
obuf = limitMalloc(rowstripsize + NUM_BUFF_OVERSIZE_BYTES); |
| 1373 |
if (obuf == NULL) |
| 1374 |
return 1; |
| 1375 |
|
| 1376 |
for (s = 0; s < spp; s++) |
| 1377 |
{ |
| 1378 |
for (row = 0; row < length; row += rowsperstrip) |
| 1379 |
{ |
| 1380 |
nrows = (row + rowsperstrip > length) ? length - row : rowsperstrip; |
| 1381 |
|
| 1382 |
stripsize = TIFFVStripSize(out, nrows); |
| 1383 |
src = buf + (row * rowsize); |
| 1384 |
memset(obuf, '\0', rowstripsize + NUM_BUFF_OVERSIZE_BYTES); |
| 1385 |
if (extractContigSamplesToBuffer(obuf, src, nrows, width, s, spp, |
| 1386 |
bps, dump)) |
| 1387 |
{ |
| 1388 |
_TIFFfree(obuf); |
| 1389 |
return 1; |
| 1390 |
} |
| 1391 |
if ((dump->outfile != NULL) && (dump->level == 1)) |
| 1392 |
{ |
| 1393 |
if ((uint64_t)scanlinesize > 0x0ffffffffULL) |
| 1394 |
{ |
| 1395 |
dump_info(dump->infile, dump->format, "loadImage", |
| 1396 |
"Attention: scanlinesize %" PRIu64 |
| 1397 |
" is larger than UINT32_MAX.\nFollowing dump " |
| 1398 |
"might be wrong.", |
| 1399 |
(uint64_t)scanlinesize); |
| 1400 |
} |
| 1401 |
dump_info( |
| 1402 |
dump->outfile, dump->format, "", |
| 1403 |
"Sample %2d, Strip: %2d, bytes: %4zd, Row %4d, bytes: " |
| 1404 |
"%4d, Input offset: %6zd", |
| 1405 |
s + 1, strip + 1, stripsize, row + 1, |
| 1406 |
(uint32_t)scanlinesize, src - buf); |
| 1407 |
dump_buffer(dump->outfile, dump->format, nrows, |
| 1408 |
(uint32_t)scanlinesize, row, obuf); |
| 1409 |
} |
| 1410 |
|
| 1411 |
if (TIFFWriteEncodedStrip(out, strip++, obuf, stripsize) < 0) |
| 1412 |
{ |
| 1413 |
TIFFError(TIFFFileName(out), |
| 1414 |
"Error, can't write strip %" PRIu32, strip - 1); |
| 1415 |
_TIFFfree(obuf); |
| 1416 |
return 1; |
| 1417 |
} |
| 1418 |
} |
| 1419 |
} |
| 1420 |
|
| 1421 |
_TIFFfree(obuf); |
| 1422 |
return 0; |
| 1423 |
} |
| 1424 |
|
| 1425 |
/* Extract all planes from contiguous buffer into a single tile buffer |
| 1426 |
* to be written out as a tile. |
| 1427 |
*/ |
| 1428 |
static int writeBufferToContigTiles(TIFF *out, uint8_t *buf, |
| 1429 |
uint32_t imagelength, uint32_t imagewidth, |
| 1430 |
tsample_t spp, struct dump_opts *dump) |
| 1431 |
{ |
| 1432 |
uint16_t bps; |
| 1433 |
uint32_t tl, tw; |
| 1434 |
uint32_t row, col, nrow, ncol; |
| 1435 |
uint32_t src_rowsize, col_offset; |
| 1436 |
tmsize_t tile_rowsize = TIFFTileRowSize(out); |
| 1437 |
uint8_t *bufp = (uint8_t *)buf; |
| 1438 |
tsize_t tile_buffsize = 0; |
| 1439 |
tsize_t tilesize = TIFFTileSize(out); |
| 1440 |
unsigned char *tilebuf = NULL; |
| 1441 |
|
| 1442 |
if (!TIFFGetField(out, TIFFTAG_TILELENGTH, &tl) || |
| 1443 |
!TIFFGetField(out, TIFFTAG_TILEWIDTH, &tw) || |
| 1444 |
!TIFFGetField(out, TIFFTAG_BITSPERSAMPLE, &bps)) |
| 1445 |
return 1; |
| 1446 |
|
| 1447 |
if (tilesize == 0 || tile_rowsize == 0 || tl == 0 || tw == 0) |
| 1448 |
{ |
| 1449 |
TIFFError( |
| 1450 |
"writeBufferToContigTiles", |
| 1451 |
"Tile size, tile row size, tile width, or tile length is zero"); |
| 1452 |
exit(EXIT_FAILURE); |
| 1453 |
} |
| 1454 |
|
| 1455 |
tile_buffsize = tilesize; |
| 1456 |
if (tilesize < (tsize_t)(tl * tile_rowsize)) |
| 1457 |
{ |
| 1458 |
#ifdef DEBUG2 |
| 1459 |
TIFFError("writeBufferToContigTiles", |
| 1460 |
"Tilesize %" PRId64 |
| 1461 |
" is too small, using alternate calculation %" PRIu32, |
| 1462 |
tilesize, tl * tile_rowsize); |
| 1463 |
#endif |
| 1464 |
if (tile_rowsize != 0 && |
| 1465 |
(tmsize_t)tl > (TIFF_TMSIZE_T_MAX / tile_rowsize)) |
| 1466 |
{ |
| 1467 |
TIFFError("writeBufferToContigTiles", |
| 1468 |
"Integer overflow when calculating buffer size"); |
| 1469 |
exit(EXIT_FAILURE); |
| 1470 |
} |
| 1471 |
tile_buffsize = tl * tile_rowsize; |
| 1472 |
} |
| 1473 |
|
| 1474 |
if (imagewidth == 0 || |
| 1475 |
(uint32_t)bps * (uint32_t)spp > UINT32_MAX / imagewidth || |
| 1476 |
bps * spp * imagewidth > UINT32_MAX - 7U) |
| 1477 |
{ |
| 1478 |
TIFFError(TIFFFileName(out), "Error, uint32_t overflow when computing " |
| 1479 |
"(imagewidth * bps * spp) + 7"); |
| 1480 |
return 1; |
| 1481 |
} |
| 1482 |
src_rowsize = ((imagewidth * spp * bps) + 7U) / 8; |
| 1483 |
|
| 1484 |
/* Add 3 padding bytes for extractContigSamples32bits */ |
| 1485 |
tilebuf = limitMalloc(tile_buffsize + NUM_BUFF_OVERSIZE_BYTES); |
| 1486 |
if (tilebuf == 0) |
| 1487 |
return 1; |
| 1488 |
memset(tilebuf, 0, tile_buffsize + NUM_BUFF_OVERSIZE_BYTES); |
| 1489 |
for (row = 0; row < imagelength; row += tl) |
| 1490 |
{ |
| 1491 |
nrow = (row + tl > imagelength) ? imagelength - row : tl; |
| 1492 |
for (col = 0; col < imagewidth; col += tw) |
| 1493 |
{ |
| 1494 |
/* Calculate visible portion of tile. */ |
| 1495 |
if (col + tw > imagewidth) |
| 1496 |
ncol = imagewidth - col; |
| 1497 |
else |
| 1498 |
ncol = tw; |
| 1499 |
|
| 1500 |
col_offset = (((col * bps * spp) + 7) / 8); |
| 1501 |
bufp = buf + (row * src_rowsize) + col_offset; |
| 1502 |
if (extractContigSamplesToTileBuffer(tilebuf, bufp, nrow, ncol, |
| 1503 |
imagewidth, tw, 0, spp, spp, |
| 1504 |
bps, dump) > 0) |
| 1505 |
{ |
| 1506 |
TIFFError("writeBufferToContigTiles", |
| 1507 |
"Unable to extract data to tile for row %" PRIu32 |
| 1508 |
", col %" PRIu32, |
| 1509 |
row, col); |
| 1510 |
_TIFFfree(tilebuf); |
| 1511 |
return 1; |
| 1512 |
} |
| 1513 |
|
| 1514 |
if (TIFFWriteTile(out, tilebuf, col, row, 0, 0) < 0) |
| 1515 |
{ |
| 1516 |
TIFFError("writeBufferToContigTiles", |
| 1517 |
"Cannot write tile at %" PRIu32 " %" PRIu32, col, |
| 1518 |
row); |
| 1519 |
_TIFFfree(tilebuf); |
| 1520 |
return 1; |
| 1521 |
} |
| 1522 |
} |
| 1523 |
} |
| 1524 |
_TIFFfree(tilebuf); |
| 1525 |
|
| 1526 |
return 0; |
| 1527 |
} /* end writeBufferToContigTiles */ |
| 1528 |
|
| 1529 |
/* Extract each plane from contiguous buffer into a single tile buffer |
| 1530 |
* to be written out as a tile. |
| 1531 |
*/ |
| 1532 |
static int writeBufferToSeparateTiles(TIFF *out, uint8_t *buf, |
| 1533 |
uint32_t imagelength, uint32_t imagewidth, |
| 1534 |
tsample_t spp, struct dump_opts *dump) |
| 1535 |
{ |
| 1536 |
/* Add 3 padding bytes for extractContigSamples32bits */ |
| 1537 |
tdata_t obuf = limitMalloc(TIFFTileSize(out) + NUM_BUFF_OVERSIZE_BYTES); |
| 1538 |
uint32_t tl, tw; |
| 1539 |
uint32_t row, col, nrow, ncol; |
| 1540 |
uint32_t src_rowsize, col_offset; |
| 1541 |
uint16_t bps; |
| 1542 |
tsample_t s; |
| 1543 |
uint8_t *bufp = (uint8_t *)buf; |
| 1544 |
|
| 1545 |
if (obuf == NULL) |
| 1546 |
return 1; |
| 1547 |
memset(obuf, 0, TIFFTileSize(out) + NUM_BUFF_OVERSIZE_BYTES); |
| 1548 |
|
| 1549 |
if (!TIFFGetField(out, TIFFTAG_TILELENGTH, &tl) || |
| 1550 |
!TIFFGetField(out, TIFFTAG_TILEWIDTH, &tw) || |
| 1551 |
!TIFFGetField(out, TIFFTAG_BITSPERSAMPLE, &bps)) |
| 1552 |
{ |
| 1553 |
_TIFFfree(obuf); |
| 1554 |
return 1; |
| 1555 |
} |
| 1556 |
|
| 1557 |
if (imagewidth == 0 || |
| 1558 |
(uint32_t)bps * (uint32_t)spp > UINT32_MAX / imagewidth || |
| 1559 |
bps * spp * imagewidth > UINT32_MAX - 7) |
| 1560 |
{ |
| 1561 |
TIFFError(TIFFFileName(out), "Error, uint32_t overflow when computing " |
| 1562 |
"(imagewidth * bps * spp) + 7"); |
| 1563 |
_TIFFfree(obuf); |
| 1564 |
return 1; |
| 1565 |
} |
| 1566 |
src_rowsize = ((imagewidth * spp * bps) + 7U) / 8; |
| 1567 |
|
| 1568 |
for (row = 0; row < imagelength; row += tl) |
| 1569 |
{ |
| 1570 |
nrow = (row + tl > imagelength) ? imagelength - row : tl; |
| 1571 |
for (col = 0; col < imagewidth; col += tw) |
| 1572 |
{ |
| 1573 |
/* Calculate visible portion of tile. */ |
| 1574 |
if (col + tw > imagewidth) |
| 1575 |
ncol = imagewidth - col; |
| 1576 |
else |
| 1577 |
ncol = tw; |
| 1578 |
|
| 1579 |
col_offset = (((col * bps * spp) + 7) / 8); |
| 1580 |
bufp = buf + (row * src_rowsize) + col_offset; |
| 1581 |
|
| 1582 |
for (s = 0; s < spp; s++) |
| 1583 |
{ |
| 1584 |
if (extractContigSamplesToTileBuffer(obuf, bufp, nrow, ncol, |
| 1585 |
imagewidth, tw, s, 1, spp, |
| 1586 |
bps, dump) > 0) |
| 1587 |
{ |
| 1588 |
TIFFError("writeBufferToSeparateTiles", |
| 1589 |
"Unable to extract data to tile for row %" PRIu32 |
| 1590 |
", col %" PRIu32 " sample %" PRIu16, |
| 1591 |
row, col, s); |
| 1592 |
_TIFFfree(obuf); |
| 1593 |
return 1; |
| 1594 |
} |
| 1595 |
|
| 1596 |
if (TIFFWriteTile(out, obuf, col, row, 0, s) < 0) |
| 1597 |
{ |
| 1598 |
TIFFError("writeBufferToseparateTiles", |
| 1599 |
"Cannot write tile at %" PRIu32 " %" PRIu32 |
| 1600 |
" sample %" PRIu16, |
| 1601 |
col, row, s); |
| 1602 |
_TIFFfree(obuf); |
| 1603 |
return 1; |
| 1604 |
} |
| 1605 |
} |
| 1606 |
} |
| 1607 |
} |
| 1608 |
_TIFFfree(obuf); |
| 1609 |
|
| 1610 |
return 0; |
| 1611 |
} /* end writeBufferToSeparateTiles */ |
| 1612 |
|
| 1613 |
static void processG3Options(char *cp) |
| 1614 |
{ |
| 1615 |
if ((cp = strchr(cp, ':'))) |
| 1616 |
{ |
| 1617 |
if (defg3opts == (uint32_t)-1) |
| 1618 |
defg3opts = 0; |
| 1619 |
do |
| 1620 |
{ |
| 1621 |
cp++; |
| 1622 |
if (strneq(cp, "1d", 2)) |
| 1623 |
defg3opts &= ~GROUP3OPT_2DENCODING; |
| 1624 |
else if (strneq(cp, "2d", 2)) |
| 1625 |
defg3opts |= GROUP3OPT_2DENCODING; |
| 1626 |
else if (strneq(cp, "fill", 4)) |
| 1627 |
defg3opts |= GROUP3OPT_FILLBITS; |
| 1628 |
else |
| 1629 |
usage(EXIT_FAILURE); |
| 1630 |
} while ((cp = strchr(cp, ':'))); |
| 1631 |
} |
| 1632 |
} |
| 1633 |
|
| 1634 |
static int processCompressOptions(char *opt) |
| 1635 |
{ |
| 1636 |
char *cp = NULL; |
| 1637 |
|
| 1638 |
if (strneq(opt, "none", 4)) |
| 1639 |
{ |
| 1640 |
defcompression = COMPRESSION_NONE; |
| 1641 |
} |
| 1642 |
else if (streq(opt, "packbits")) |
| 1643 |
{ |
| 1644 |
defcompression = COMPRESSION_PACKBITS; |
| 1645 |
} |
| 1646 |
else if (strneq(opt, "jpeg", 4)) |
| 1647 |
{ |
| 1648 |
cp = strchr(opt, ':'); |
| 1649 |
defcompression = COMPRESSION_JPEG; |
| 1650 |
|
| 1651 |
while (cp) |
| 1652 |
{ |
| 1653 |
if (isdigit((int)cp[1])) |
| 1654 |
quality = atoi(cp + 1); |
| 1655 |
else if (strneq(cp + 1, "raw", 3)) |
| 1656 |
jpegcolormode = JPEGCOLORMODE_RAW; |
| 1657 |
else if (strneq(cp + 1, "rgb", 3)) |
| 1658 |
jpegcolormode = JPEGCOLORMODE_RGB; |
| 1659 |
else |
| 1660 |
usage(EXIT_FAILURE); |
| 1661 |
cp = strchr(cp + 1, ':'); |
| 1662 |
} |
| 1663 |
} |
| 1664 |
else if (strneq(opt, "g3", 2)) |
| 1665 |
{ |
| 1666 |
processG3Options(opt); |
| 1667 |
defcompression = COMPRESSION_CCITTFAX3; |
| 1668 |
} |
| 1669 |
else if (streq(opt, "g4")) |
| 1670 |
{ |
| 1671 |
defcompression = COMPRESSION_CCITTFAX4; |
| 1672 |
} |
| 1673 |
else if (strneq(opt, "lzw", 3)) |
| 1674 |
{ |
| 1675 |
cp = strchr(opt, ':'); |
| 1676 |
if (cp) |
| 1677 |
defpredictor = atoi(cp + 1); |
| 1678 |
defcompression = COMPRESSION_LZW; |
| 1679 |
} |
| 1680 |
else if (strneq(opt, "zip", 3)) |
| 1681 |
{ |
| 1682 |
cp = strchr(opt, ':'); |
| 1683 |
if (cp) |
| 1684 |
defpredictor = atoi(cp + 1); |
| 1685 |
defcompression = COMPRESSION_ADOBE_DEFLATE; |
| 1686 |
} |
| 1687 |
else |
| 1688 |
return (0); |
| 1689 |
|
| 1690 |
return (1); |
| 1691 |
} |
| 1692 |
|
| 1693 |
static void usage(int code) |
| 1694 |
{ |
| 1695 |
FILE *out = (code == EXIT_SUCCESS) ? stdout : stderr; |
| 1696 |
|
| 1697 |
fprintf(out, "\n%s\n\n", TIFFGetVersion()); |
| 1698 |
fprintf(out, "%s", usage_info); |
| 1699 |
exit(code); |
| 1700 |
} |
| 1701 |
|
| 1702 |
#define CopyField(tag, v) \ |
| 1703 |
if (TIFFGetField(in, tag, &v)) \ |
| 1704 |
TIFFSetField(out, tag, v) |
| 1705 |
#define CopyField2(tag, v1, v2) \ |
| 1706 |
if (TIFFGetField(in, tag, &v1, &v2)) \ |
| 1707 |
TIFFSetField(out, tag, v1, v2) |
| 1708 |
#define CopyField3(tag, v1, v2, v3) \ |
| 1709 |
if (TIFFGetField(in, tag, &v1, &v2, &v3)) \ |
| 1710 |
TIFFSetField(out, tag, v1, v2, v3) |
| 1711 |
#define CopyField4(tag, v1, v2, v3, v4) \ |
| 1712 |
if (TIFFGetField(in, tag, &v1, &v2, &v3, &v4)) \ |
| 1713 |
TIFFSetField(out, tag, v1, v2, v3, v4) |
| 1714 |
|
| 1715 |
static void cpTag(TIFF *in, TIFF *out, uint16_t tag, uint16_t count, |
| 1716 |
TIFFDataType type) |
| 1717 |
{ |
| 1718 |
switch (type) |
| 1719 |
{ |
| 1720 |
case TIFF_SHORT: |
| 1721 |
if (count == 1) |
| 1722 |
{ |
| 1723 |
uint16_t shortv; |
| 1724 |
CopyField(tag, shortv); |
| 1725 |
} |
| 1726 |
else if (count == 2) |
| 1727 |
{ |
| 1728 |
uint16_t shortv1, shortv2; |
| 1729 |
CopyField2(tag, shortv1, shortv2); |
| 1730 |
} |
| 1731 |
else if (count == 4) |
| 1732 |
{ |
| 1733 |
uint16_t *tr, *tg, *tb, *ta; |
| 1734 |
CopyField4(tag, tr, tg, tb, ta); |
| 1735 |
} |
| 1736 |
else if (count == (uint16_t)-1) |
| 1737 |
{ |
| 1738 |
uint16_t shortv1; |
| 1739 |
uint16_t *shortav; |
| 1740 |
CopyField2(tag, shortv1, shortav); |
| 1741 |
} |
| 1742 |
break; |
| 1743 |
case TIFF_LONG: |
| 1744 |
{ |
| 1745 |
uint32_t longv; |
| 1746 |
CopyField(tag, longv); |
| 1747 |
} |
| 1748 |
break; |
| 1749 |
case TIFF_RATIONAL: |
| 1750 |
if (count == 1) |
| 1751 |
{ |
| 1752 |
float floatv; |
| 1753 |
CopyField(tag, floatv); |
| 1754 |
} |
| 1755 |
else if (count == (uint16_t)-1) |
| 1756 |
{ |
| 1757 |
float *floatav; |
| 1758 |
CopyField(tag, floatav); |
| 1759 |
} |
| 1760 |
break; |
| 1761 |
case TIFF_ASCII: |
| 1762 |
{ |
| 1763 |
char *stringv; |
| 1764 |
CopyField(tag, stringv); |
| 1765 |
} |
| 1766 |
break; |
| 1767 |
case TIFF_DOUBLE: |
| 1768 |
if (count == 1) |
| 1769 |
{ |
| 1770 |
double doublev; |
| 1771 |
CopyField(tag, doublev); |
| 1772 |
} |
| 1773 |
else if (count == (uint16_t)-1) |
| 1774 |
{ |
| 1775 |
double *doubleav; |
| 1776 |
CopyField(tag, doubleav); |
| 1777 |
} |
| 1778 |
break; |
| 1779 |
default: |
| 1780 |
TIFFError(TIFFFileName(in), |
| 1781 |
"Data type %" PRIu16 " is not supported, tag %d skipped", |
| 1782 |
tag, type); |
| 1783 |
} |
| 1784 |
} |
| 1785 |
|
| 1786 |
static const struct cpTag |
| 1787 |
{ |
| 1788 |
uint16_t tag; |
| 1789 |
uint16_t count; |
| 1790 |
TIFFDataType type; |
| 1791 |
} tags[] = { |
| 1792 |
{TIFFTAG_SUBFILETYPE, 1, TIFF_LONG}, |
| 1793 |
{TIFFTAG_THRESHHOLDING, 1, TIFF_SHORT}, |
| 1794 |
{TIFFTAG_DOCUMENTNAME, 1, TIFF_ASCII}, |
| 1795 |
{TIFFTAG_IMAGEDESCRIPTION, 1, TIFF_ASCII}, |
| 1796 |
{TIFFTAG_MAKE, 1, TIFF_ASCII}, |
| 1797 |
{TIFFTAG_MODEL, 1, TIFF_ASCII}, |
| 1798 |
{TIFFTAG_MINSAMPLEVALUE, 1, TIFF_SHORT}, |
| 1799 |
{TIFFTAG_MAXSAMPLEVALUE, 1, TIFF_SHORT}, |
| 1800 |
{TIFFTAG_XRESOLUTION, 1, TIFF_RATIONAL}, |
| 1801 |
{TIFFTAG_YRESOLUTION, 1, TIFF_RATIONAL}, |
| 1802 |
{TIFFTAG_PAGENAME, 1, TIFF_ASCII}, |
| 1803 |
{TIFFTAG_XPOSITION, 1, TIFF_RATIONAL}, |
| 1804 |
{TIFFTAG_YPOSITION, 1, TIFF_RATIONAL}, |
| 1805 |
{TIFFTAG_RESOLUTIONUNIT, 1, TIFF_SHORT}, |
| 1806 |
{TIFFTAG_SOFTWARE, 1, TIFF_ASCII}, |
| 1807 |
{TIFFTAG_DATETIME, 1, TIFF_ASCII}, |
| 1808 |
{TIFFTAG_ARTIST, 1, TIFF_ASCII}, |
| 1809 |
{TIFFTAG_HOSTCOMPUTER, 1, TIFF_ASCII}, |
| 1810 |
{TIFFTAG_WHITEPOINT, (uint16_t)-1, TIFF_RATIONAL}, |
| 1811 |
{TIFFTAG_PRIMARYCHROMATICITIES, (uint16_t)-1, TIFF_RATIONAL}, |
| 1812 |
{TIFFTAG_HALFTONEHINTS, 2, TIFF_SHORT}, |
| 1813 |
{TIFFTAG_INKSET, 1, TIFF_SHORT}, |
| 1814 |
{TIFFTAG_DOTRANGE, 2, TIFF_SHORT}, |
| 1815 |
{TIFFTAG_TARGETPRINTER, 1, TIFF_ASCII}, |
| 1816 |
{TIFFTAG_SAMPLEFORMAT, 1, TIFF_SHORT}, |
| 1817 |
{TIFFTAG_YCBCRCOEFFICIENTS, (uint16_t)-1, TIFF_RATIONAL}, |
| 1818 |
{TIFFTAG_YCBCRSUBSAMPLING, 2, TIFF_SHORT}, |
| 1819 |
{TIFFTAG_YCBCRPOSITIONING, 1, TIFF_SHORT}, |
| 1820 |
{TIFFTAG_REFERENCEBLACKWHITE, (uint16_t)-1, TIFF_RATIONAL}, |
| 1821 |
{TIFFTAG_EXTRASAMPLES, (uint16_t)-1, TIFF_SHORT}, |
| 1822 |
{TIFFTAG_SMINSAMPLEVALUE, 1, TIFF_DOUBLE}, |
| 1823 |
{TIFFTAG_SMAXSAMPLEVALUE, 1, TIFF_DOUBLE}, |
| 1824 |
{TIFFTAG_STONITS, 1, TIFF_DOUBLE}, |
| 1825 |
}; |
| 1826 |
#define NTAGS (sizeof(tags) / sizeof(tags[0])) |
| 1827 |
|
| 1828 |
#define CopyTag(tag, count, type) cpTag(in, out, tag, count, type) |
| 1829 |
|
| 1830 |
/* Functions written by Richard Nolde, with exceptions noted. */ |
| 1831 |
void process_command_opts(int argc, char *argv[], char *mp, char *mode, |
| 1832 |
uint32_t *dirnum, uint16_t *defconfig, |
| 1833 |
uint16_t *deffillorder, uint32_t *deftilewidth, |
| 1834 |
uint32_t *deftilelength, uint32_t *defrowsperstrip, |
| 1835 |
struct crop_mask *crop_data, struct pagedef *page, |
| 1836 |
struct dump_opts *dump, unsigned int *imagelist, |
| 1837 |
unsigned int *image_count) |
| 1838 |
{ |
| 1839 |
int c, good_args = 0; |
| 1840 |
char *opt_offset = NULL; /* Position in string of value sought */ |
| 1841 |
char *opt_ptr = NULL; /* Pointer to next token in option set */ |
| 1842 |
char *sep = NULL; /* Pointer to a token separator */ |
| 1843 |
unsigned int i, j, start, end; |
| 1844 |
#if !HAVE_DECL_OPTARG |
| 1845 |
extern int optind; |
| 1846 |
extern char *optarg; |
| 1847 |
#endif |
| 1848 |
|
| 1849 |
*mp++ = 'w'; |
| 1850 |
*mp = '\0'; |
| 1851 |
while ((c = getopt(argc, argv, |
| 1852 |
"ac:d:e:f:hik:l:m:p:r:stvw:z:BCD:E:F:H:I:J:K:LMN:O:P:R:" |
| 1853 |
"S:U:V:X:Y:Z:")) != -1) |
| 1854 |
{ |
| 1855 |
good_args++; |
| 1856 |
switch (c) |
| 1857 |
{ |
| 1858 |
case 'a': |
| 1859 |
mode[0] = 'a'; /* append to output */ |
| 1860 |
break; |
| 1861 |
case 'c': |
| 1862 |
if (!processCompressOptions(optarg)) /* compression scheme */ |
| 1863 |
{ |
| 1864 |
TIFFError("Unknown compression option", "%s", optarg); |
| 1865 |
TIFFError("For valid options type", "tiffcrop -h"); |
| 1866 |
exit(EXIT_FAILURE); |
| 1867 |
} |
| 1868 |
break; |
| 1869 |
case 'd': |
| 1870 |
start = strtoul(optarg, NULL, 0); /* initial IFD offset */ |
| 1871 |
if (start == 0) |
| 1872 |
{ |
| 1873 |
TIFFError("", "Directory offset must be greater than zero"); |
| 1874 |
TIFFError("For valid options type", "tiffcrop -h"); |
| 1875 |
exit(EXIT_FAILURE); |
| 1876 |
} |
| 1877 |
*dirnum = start - 1; |
| 1878 |
break; |
| 1879 |
case 'e': |
| 1880 |
switch (tolower((int)optarg[0])) /* image export modes*/ |
| 1881 |
{ |
| 1882 |
case 'c': |
| 1883 |
crop_data->exp_mode = ONE_FILE_COMPOSITE; |
| 1884 |
crop_data->img_mode = COMPOSITE_IMAGES; |
| 1885 |
break; /* Composite */ |
| 1886 |
case 'd': |
| 1887 |
crop_data->exp_mode = ONE_FILE_SEPARATED; |
| 1888 |
crop_data->img_mode = SEPARATED_IMAGES; |
| 1889 |
break; /* Divided */ |
| 1890 |
case 'i': |
| 1891 |
crop_data->exp_mode = FILE_PER_IMAGE_COMPOSITE; |
| 1892 |
crop_data->img_mode = COMPOSITE_IMAGES; |
| 1893 |
break; /* Image */ |
| 1894 |
case 'm': |
| 1895 |
crop_data->exp_mode = FILE_PER_IMAGE_SEPARATED; |
| 1896 |
crop_data->img_mode = SEPARATED_IMAGES; |
| 1897 |
break; /* Multiple */ |
| 1898 |
case 's': |
| 1899 |
crop_data->exp_mode = FILE_PER_SELECTION; |
| 1900 |
crop_data->img_mode = SEPARATED_IMAGES; |
| 1901 |
break; /* Sections */ |
| 1902 |
default: |
| 1903 |
TIFFError("Unknown export mode", "%s", optarg); |
| 1904 |
TIFFError("For valid options type", "tiffcrop -h"); |
| 1905 |
exit(EXIT_FAILURE); |
| 1906 |
} |
| 1907 |
break; |
| 1908 |
case 'f': |
| 1909 |
if (streq(optarg, "lsb2msb")) /* fill order */ |
| 1910 |
*deffillorder = FILLORDER_LSB2MSB; |
| 1911 |
else if (streq(optarg, "msb2lsb")) |
| 1912 |
*deffillorder = FILLORDER_MSB2LSB; |
| 1913 |
else |
| 1914 |
{ |
| 1915 |
TIFFError("Unknown fill order", "%s", optarg); |
| 1916 |
TIFFError("For valid options type", "tiffcrop -h"); |
| 1917 |
exit(EXIT_FAILURE); |
| 1918 |
} |
| 1919 |
break; |
| 1920 |
case 'h': |
| 1921 |
usage(EXIT_SUCCESS); |
| 1922 |
break; |
| 1923 |
case 'i': |
| 1924 |
ignore = TRUE; /* ignore errors */ |
| 1925 |
break; |
| 1926 |
case 'k': |
| 1927 |
maxMalloc = (tmsize_t)strtoul(optarg, NULL, 0) << 20; |
| 1928 |
break; |
| 1929 |
case 'l': |
| 1930 |
outtiled = TRUE; /* tile length */ |
| 1931 |
*deftilelength = atoi(optarg); |
| 1932 |
break; |
| 1933 |
case 'p': /* planar configuration */ |
| 1934 |
if (streq(optarg, "separate")) |
| 1935 |
*defconfig = PLANARCONFIG_SEPARATE; |
| 1936 |
else if (streq(optarg, "contig")) |
| 1937 |
*defconfig = PLANARCONFIG_CONTIG; |
| 1938 |
else |
| 1939 |
{ |
| 1940 |
TIFFError("Unknown planar configuration", "%s", optarg); |
| 1941 |
TIFFError("For valid options type", "tiffcrop -h"); |
| 1942 |
exit(EXIT_FAILURE); |
| 1943 |
} |
| 1944 |
break; |
| 1945 |
case 'r': /* rows/strip */ |
| 1946 |
*defrowsperstrip = atol(optarg); |
| 1947 |
break; |
| 1948 |
case 's': /* generate stripped output */ |
| 1949 |
outtiled = FALSE; |
| 1950 |
break; |
| 1951 |
case 't': /* generate tiled output */ |
| 1952 |
outtiled = TRUE; |
| 1953 |
break; |
| 1954 |
case 'v': |
| 1955 |
printf("Library Release: %s\n", TIFFGetVersion()); |
| 1956 |
printf("Tiffcp code: Copyright (c) 1988-1997 Sam Leffler\n"); |
| 1957 |
printf(" : Copyright (c) 1991-1997 Silicon Graphics, " |
| 1958 |
"Inc\n"); |
| 1959 |
printf("Tiffcrop additions: Copyright (c) 2007-2010 Richard " |
| 1960 |
"Nolde\n"); |
| 1961 |
exit(EXIT_SUCCESS); |
| 1962 |
break; |
| 1963 |
case 'w': /* tile width */ |
| 1964 |
outtiled = TRUE; |
| 1965 |
*deftilewidth = atoi(optarg); |
| 1966 |
break; |
| 1967 |
case 'z': /* regions of an image specified as |
| 1968 |
x1,y1,x2,y2:x3,y3,x4,y4 etc */ |
| 1969 |
crop_data->crop_mode |= CROP_REGIONS; |
| 1970 |
for (i = 0, opt_ptr = strtok(optarg, ":"); |
| 1971 |
((opt_ptr != NULL) && (i < MAX_REGIONS)); |
| 1972 |
(opt_ptr = strtok(NULL, ":")), i++) |
| 1973 |
{ |
| 1974 |
crop_data->regions++; |
| 1975 |
if (sscanf(opt_ptr, "%lf,%lf,%lf,%lf", |
| 1976 |
&crop_data->corners[i].X1, |
| 1977 |
&crop_data->corners[i].Y1, |
| 1978 |
&crop_data->corners[i].X2, |
| 1979 |
&crop_data->corners[i].Y2) != 4) |
| 1980 |
{ |
| 1981 |
TIFFError("Unable to parse coordinates for region", |
| 1982 |
"%u %s", i, optarg); |
| 1983 |
TIFFError("For valid options type", "tiffcrop -h"); |
| 1984 |
exit(EXIT_FAILURE); |
| 1985 |
} |
| 1986 |
} |
| 1987 |
/* check for remaining elements over MAX_REGIONS */ |
| 1988 |
if ((opt_ptr != NULL) && (i >= MAX_REGIONS)) |
| 1989 |
{ |
| 1990 |
TIFFError("Region list exceeds limit of", "%d regions %s", |
| 1991 |
MAX_REGIONS, optarg); |
| 1992 |
TIFFError("For valid options type", "tiffcrop -h"); |
| 1993 |
exit(EXIT_FAILURE); |
| 1994 |
} |
| 1995 |
break; |
| 1996 |
/* options for file open modes */ |
| 1997 |
case 'B': |
| 1998 |
{ |
| 1999 |
if (mp < mode + MAX_MODESTRING_LEN) |
| 2000 |
{ |
| 2001 |
*mp++ = 'b'; |
| 2002 |
*mp = '\0'; |
| 2003 |
} |
| 2004 |
else |
| 2005 |
{ |
| 2006 |
TIFFError("To many options for output file open modes. " |
| 2007 |
"Maximum allowed ", |
| 2008 |
"%d", MAX_MODESTRING_LEN); |
| 2009 |
TIFFError("For valid options type", "tiffcrop -h"); |
| 2010 |
exit(EXIT_FAILURE); |
| 2011 |
} |
| 2012 |
} |
| 2013 |
break; |
| 2014 |
case 'L': |
| 2015 |
{ |
| 2016 |
if (mp < mode + MAX_MODESTRING_LEN) |
| 2017 |
{ |
| 2018 |
*mp++ = 'l'; |
| 2019 |
*mp = '\0'; |
| 2020 |
} |
| 2021 |
else |
| 2022 |
{ |
| 2023 |
TIFFError("To many options for output file open modes. " |
| 2024 |
"Maximum allowed ", |
| 2025 |
"%d", MAX_MODESTRING_LEN); |
| 2026 |
TIFFError("For valid options type", "tiffcrop -h"); |
| 2027 |
exit(EXIT_FAILURE); |
| 2028 |
} |
| 2029 |
} |
| 2030 |
break; |
| 2031 |
case 'M': |
| 2032 |
{ |
| 2033 |
if (mp < mode + MAX_MODESTRING_LEN) |
| 2034 |
{ |
| 2035 |
*mp++ = 'm'; |
| 2036 |
*mp = '\0'; |
| 2037 |
} |
| 2038 |
else |
| 2039 |
{ |
| 2040 |
TIFFError("To many options for output file open modes. " |
| 2041 |
"Maximum allowed ", |
| 2042 |
"%d", MAX_MODESTRING_LEN); |
| 2043 |
TIFFError("For valid options type", "tiffcrop -h"); |
| 2044 |
exit(EXIT_FAILURE); |
| 2045 |
} |
| 2046 |
} |
| 2047 |
break; |
| 2048 |
case 'C': |
| 2049 |
{ |
| 2050 |
if (mp < mode + MAX_MODESTRING_LEN) |
| 2051 |
{ |
| 2052 |
*mp++ = 'c'; |
| 2053 |
*mp = '\0'; |
| 2054 |
} |
| 2055 |
else |
| 2056 |
{ |
| 2057 |
TIFFError("To many options for output file open modes. " |
| 2058 |
"Maximum allowed ", |
| 2059 |
"%d", MAX_MODESTRING_LEN); |
| 2060 |
TIFFError("For valid options type", "tiffcrop -h"); |
| 2061 |
exit(EXIT_FAILURE); |
| 2062 |
} |
| 2063 |
} |
| 2064 |
break; |
| 2065 |
/* options for Debugging / data dump */ |
| 2066 |
case 'D': |
| 2067 |
for (i = 0, opt_ptr = strtok(optarg, ","); (opt_ptr != NULL); |
| 2068 |
(opt_ptr = strtok(NULL, ",")), i++) |
| 2069 |
{ |
| 2070 |
opt_offset = strpbrk(opt_ptr, ":="); |
| 2071 |
if (opt_offset == NULL) |
| 2072 |
{ |
| 2073 |
TIFFError("Invalid dump option", "%s", optarg); |
| 2074 |
TIFFError("For valid options type", "tiffcrop -h"); |
| 2075 |
exit(EXIT_FAILURE); |
| 2076 |
} |
| 2077 |
|
| 2078 |
*opt_offset = '\0'; |
| 2079 |
/* convert option to lowercase */ |
| 2080 |
end = (unsigned int)strlen(opt_ptr); |
| 2081 |
for (i = 0; i < end; i++) |
| 2082 |
*(opt_ptr + i) = tolower((int)*(opt_ptr + i)); |
| 2083 |
/* Look for dump format specification */ |
| 2084 |
if (strncmp(opt_ptr, "for", 3) == 0) |
| 2085 |
{ |
| 2086 |
/* convert value to lowercase */ |
| 2087 |
end = (unsigned int)strlen(opt_offset + 1); |
| 2088 |
for (i = 1; i <= end; i++) |
| 2089 |
*(opt_offset + i) = tolower((int)*(opt_offset + i)); |
| 2090 |
/* check dump format value */ |
| 2091 |
if (strncmp(opt_offset + 1, "txt", 3) == 0) |
| 2092 |
{ |
| 2093 |
dump->format = DUMP_TEXT; |
| 2094 |
strcpy(dump->mode, "w"); |
| 2095 |
} |
| 2096 |
else |
| 2097 |
{ |
| 2098 |
if (strncmp(opt_offset + 1, "raw", 3) == 0) |
| 2099 |
{ |
| 2100 |
dump->format = DUMP_RAW; |
| 2101 |
strcpy(dump->mode, "wb"); |
| 2102 |
} |
| 2103 |
else |
| 2104 |
{ |
| 2105 |
TIFFError("parse_command_opts", |
| 2106 |
"Unknown dump format %s", |
| 2107 |
opt_offset + 1); |
| 2108 |
TIFFError("For valid options type", |
| 2109 |
"tiffcrop -h"); |
| 2110 |
exit(EXIT_FAILURE); |
| 2111 |
} |
| 2112 |
} |
| 2113 |
} |
| 2114 |
else |
| 2115 |
{ /* Look for dump level specification */ |
| 2116 |
if (strncmp(opt_ptr, "lev", 3) == 0) |
| 2117 |
dump->level = atoi(opt_offset + 1); |
| 2118 |
/* Look for input data dump file name */ |
| 2119 |
if (strncmp(opt_ptr, "in", 2) == 0) |
| 2120 |
{ |
| 2121 |
strncpy(dump->infilename, opt_offset + 1, |
| 2122 |
PATH_MAX - 20); |
| 2123 |
dump->infilename[PATH_MAX - 20] = '\0'; |
| 2124 |
} |
| 2125 |
/* Look for output data dump file name */ |
| 2126 |
if (strncmp(opt_ptr, "out", 3) == 0) |
| 2127 |
{ |
| 2128 |
strncpy(dump->outfilename, opt_offset + 1, |
| 2129 |
PATH_MAX - 20); |
| 2130 |
dump->outfilename[PATH_MAX - 20] = '\0'; |
| 2131 |
} |
| 2132 |
if (strncmp(opt_ptr, "deb", 3) == 0) |
| 2133 |
dump->debug = atoi(opt_offset + 1); |
| 2134 |
} |
| 2135 |
} |
| 2136 |
if ((strlen(dump->infilename)) || (strlen(dump->outfilename))) |
| 2137 |
{ |
| 2138 |
if (dump->level == 1) |
| 2139 |
TIFFError("", "Defaulting to dump level 1, no data."); |
| 2140 |
if (dump->format == DUMP_NONE) |
| 2141 |
{ |
| 2142 |
TIFFError( |
| 2143 |
"", |
| 2144 |
"You must specify a dump format for dump files"); |
| 2145 |
TIFFError("For valid options type", "tiffcrop -h"); |
| 2146 |
exit(EXIT_FAILURE); |
| 2147 |
} |
| 2148 |
} |
| 2149 |
break; |
| 2150 |
|
| 2151 |
/* image manipulation routine options */ |
| 2152 |
case 'm': /* margins to exclude from selection, uppercase M was |
| 2153 |
already used */ |
| 2154 |
/* order of values must be TOP, LEFT, BOTTOM, RIGHT */ |
| 2155 |
crop_data->crop_mode |= CROP_MARGINS; |
| 2156 |
for (i = 0, opt_ptr = strtok(optarg, ",:"); |
| 2157 |
((opt_ptr != NULL) && (i < 4)); |
| 2158 |
(opt_ptr = strtok(NULL, ",:")), i++) |
| 2159 |
{ |
| 2160 |
crop_data->margins[i] = atof(opt_ptr); |
| 2161 |
} |
| 2162 |
break; |
| 2163 |
case 'E': /* edge reference */ |
| 2164 |
switch (tolower((int)optarg[0])) |
| 2165 |
{ |
| 2166 |
case 't': |
| 2167 |
crop_data->edge_ref = EDGE_TOP; |
| 2168 |
break; |
| 2169 |
case 'b': |
| 2170 |
crop_data->edge_ref = EDGE_BOTTOM; |
| 2171 |
break; |
| 2172 |
case 'l': |
| 2173 |
crop_data->edge_ref = EDGE_LEFT; |
| 2174 |
break; |
| 2175 |
case 'r': |
| 2176 |
crop_data->edge_ref = EDGE_RIGHT; |
| 2177 |
break; |
| 2178 |
default: |
| 2179 |
TIFFError("Edge reference must be top, bottom, left, " |
| 2180 |
"or right", |
| 2181 |
"%s", optarg); |
| 2182 |
TIFFError("For valid options type", "tiffcrop -h"); |
| 2183 |
exit(EXIT_FAILURE); |
| 2184 |
} |
| 2185 |
break; |
| 2186 |
case 'F': /* flip eg mirror image or cropped segment, M was already |
| 2187 |
used */ |
| 2188 |
crop_data->crop_mode |= CROP_MIRROR; |
| 2189 |
switch (tolower((int)optarg[0])) |
| 2190 |
{ |
| 2191 |
case 'h': |
| 2192 |
crop_data->mirror = MIRROR_HORIZ; |
| 2193 |
break; |
| 2194 |
case 'v': |
| 2195 |
crop_data->mirror = MIRROR_VERT; |
| 2196 |
break; |
| 2197 |
case 'b': |
| 2198 |
crop_data->mirror = MIRROR_BOTH; |
| 2199 |
break; |
| 2200 |
default: |
| 2201 |
TIFFError("Flip mode must be horiz, vert, or both", |
| 2202 |
"%s", optarg); |
| 2203 |
TIFFError("For valid options type", "tiffcrop -h"); |
| 2204 |
exit(EXIT_FAILURE); |
| 2205 |
} |
| 2206 |
break; |
| 2207 |
case 'H': /* set horizontal resolution to new value */ |
| 2208 |
page->hres = atof(optarg); |
| 2209 |
page->mode |= PAGE_MODE_RESOLUTION; |
| 2210 |
break; |
| 2211 |
case 'I': /* invert the color space, eg black to white */ |
| 2212 |
crop_data->crop_mode |= CROP_INVERT; |
| 2213 |
/* The PHOTOMETIC_INTERPRETATION tag may be updated */ |
| 2214 |
if (streq(optarg, "black")) |
| 2215 |
{ |
| 2216 |
crop_data->photometric = PHOTOMETRIC_MINISBLACK; |
| 2217 |
continue; |
| 2218 |
} |
| 2219 |
if (streq(optarg, "white")) |
| 2220 |
{ |
| 2221 |
crop_data->photometric = PHOTOMETRIC_MINISWHITE; |
| 2222 |
continue; |
| 2223 |
} |
| 2224 |
if (streq(optarg, "data")) |
| 2225 |
{ |
| 2226 |
crop_data->photometric = INVERT_DATA_ONLY; |
| 2227 |
continue; |
| 2228 |
} |
| 2229 |
if (streq(optarg, "both")) |
| 2230 |
{ |
| 2231 |
crop_data->photometric = INVERT_DATA_AND_TAG; |
| 2232 |
continue; |
| 2233 |
} |
| 2234 |
|
| 2235 |
TIFFError("Missing or unknown option for inverting " |
| 2236 |
"PHOTOMETRIC_INTERPRETATION", |
| 2237 |
"%s", optarg); |
| 2238 |
TIFFError("For valid options type", "tiffcrop -h"); |
| 2239 |
exit(EXIT_FAILURE); |
| 2240 |
break; |
| 2241 |
case 'J': /* horizontal margin for sectioned output pages */ |
| 2242 |
page->hmargin = atof(optarg); |
| 2243 |
page->mode |= PAGE_MODE_MARGINS; |
| 2244 |
break; |
| 2245 |
case 'K': /* vertical margin for sectioned output pages*/ |
| 2246 |
page->vmargin = atof(optarg); |
| 2247 |
page->mode |= PAGE_MODE_MARGINS; |
| 2248 |
break; |
| 2249 |
case 'N': /* list of images to process */ |
| 2250 |
for (i = 0, opt_ptr = strtok(optarg, ","); |
| 2251 |
((opt_ptr != NULL) && (i < MAX_IMAGES)); |
| 2252 |
(opt_ptr = strtok(NULL, ","))) |
| 2253 |
{ /* We do not know how many images are in file yet |
| 2254 |
* so we build a list to include the maximum allowed |
| 2255 |
* and follow it until we hit the end of the file. |
| 2256 |
* Image count is not accurate for odd, even, last |
| 2257 |
* so page numbers won't be valid either. |
| 2258 |
*/ |
| 2259 |
if (streq(opt_ptr, "odd")) |
| 2260 |
{ |
| 2261 |
for (j = 1; j <= MAX_IMAGES; j += 2) |
| 2262 |
imagelist[i++] = j; |
| 2263 |
*image_count = (MAX_IMAGES - 1) / 2; |
| 2264 |
break; |
| 2265 |
} |
| 2266 |
else |
| 2267 |
{ |
| 2268 |
if (streq(opt_ptr, "even")) |
| 2269 |
{ |
| 2270 |
for (j = 2; j <= MAX_IMAGES; j += 2) |
| 2271 |
imagelist[i++] = j; |
| 2272 |
*image_count = MAX_IMAGES / 2; |
| 2273 |
break; |
| 2274 |
} |
| 2275 |
else |
| 2276 |
{ |
| 2277 |
if (streq(opt_ptr, "last")) |
| 2278 |
imagelist[i++] = MAX_IMAGES; |
| 2279 |
else /* single value between commas */ |
| 2280 |
{ |
| 2281 |
sep = strpbrk(opt_ptr, ":-"); |
| 2282 |
if (!sep) |
| 2283 |
imagelist[i++] = atoi(opt_ptr); |
| 2284 |
else |
| 2285 |
{ |
| 2286 |
*sep = '\0'; |
| 2287 |
start = atoi(opt_ptr); |
| 2288 |
if (!strcmp((sep + 1), "last")) |
| 2289 |
end = MAX_IMAGES; |
| 2290 |
else |
| 2291 |
end = atoi(sep + 1); |
| 2292 |
for (j = start; |
| 2293 |
j <= end && j - start + i < MAX_IMAGES; |
| 2294 |
j++) |
| 2295 |
imagelist[i++] = j; |
| 2296 |
} |
| 2297 |
} |
| 2298 |
} |
| 2299 |
} |
| 2300 |
} |
| 2301 |
*image_count = i; |
| 2302 |
break; |
| 2303 |
case 'O': /* page orientation */ |
| 2304 |
switch (tolower((int)optarg[0])) |
| 2305 |
{ |
| 2306 |
case 'a': |
| 2307 |
page->orient = ORIENTATION_AUTO; |
| 2308 |
break; |
| 2309 |
case 'p': |
| 2310 |
page->orient = ORIENTATION_PORTRAIT; |
| 2311 |
break; |
| 2312 |
case 'l': |
| 2313 |
page->orient = ORIENTATION_LANDSCAPE; |
| 2314 |
break; |
| 2315 |
default: |
| 2316 |
TIFFError( |
| 2317 |
"Orientation must be portrait, landscape, or auto.", |
| 2318 |
"%s", optarg); |
| 2319 |
TIFFError("For valid options type", "tiffcrop -h"); |
| 2320 |
exit(EXIT_FAILURE); |
| 2321 |
} |
| 2322 |
break; |
| 2323 |
case 'P': /* page size selection */ |
| 2324 |
if (sscanf(optarg, "%lfx%lf", &page->width, &page->length) == 2) |
| 2325 |
{ |
| 2326 |
strcpy(page->name, "Custom"); |
| 2327 |
page->mode |= PAGE_MODE_PAPERSIZE; |
| 2328 |
break; |
| 2329 |
} |
| 2330 |
if (get_page_geometry(optarg, page)) |
| 2331 |
{ |
| 2332 |
if (!strcmp(optarg, "list")) |
| 2333 |
{ |
| 2334 |
TIFFError("", |
| 2335 |
"Name Width Length (in inches)"); |
| 2336 |
for (i = 0; i < MAX_PAPERNAMES - 1; i++) |
| 2337 |
TIFFError("", "%-15.15s %5.2f %5.2f", |
| 2338 |
PaperTable[i].name, PaperTable[i].width, |
| 2339 |
PaperTable[i].length); |
| 2340 |
exit(EXIT_FAILURE); |
| 2341 |
} |
| 2342 |
|
| 2343 |
TIFFError("Invalid paper size", "%s", optarg); |
| 2344 |
TIFFError("", "Select one of:"); |
| 2345 |
TIFFError("", "Name Width Length (in inches)"); |
| 2346 |
for (i = 0; i < MAX_PAPERNAMES - 1; i++) |
| 2347 |
TIFFError("", "%-15.15s %5.2f %5.2f", |
| 2348 |
PaperTable[i].name, PaperTable[i].width, |
| 2349 |
PaperTable[i].length); |
| 2350 |
exit(EXIT_FAILURE); |
| 2351 |
} |
| 2352 |
else |
| 2353 |
{ |
| 2354 |
page->mode |= PAGE_MODE_PAPERSIZE; |
| 2355 |
} |
| 2356 |
break; |
| 2357 |
case 'R': /* rotate image or cropped segment */ |
| 2358 |
crop_data->crop_mode |= CROP_ROTATE; |
| 2359 |
switch (strtoul(optarg, NULL, 0)) |
| 2360 |
{ |
| 2361 |
case 90: |
| 2362 |
crop_data->rotation = (uint16_t)90; |
| 2363 |
break; |
| 2364 |
case 180: |
| 2365 |
crop_data->rotation = (uint16_t)180; |
| 2366 |
break; |
| 2367 |
case 270: |
| 2368 |
crop_data->rotation = (uint16_t)270; |
| 2369 |
break; |
| 2370 |
default: |
| 2371 |
TIFFError("Rotation must be 90, 180, or 270 degrees " |
| 2372 |
"clockwise", |
| 2373 |
"%s", optarg); |
| 2374 |
TIFFError("For valid options type", "tiffcrop -h"); |
| 2375 |
exit(EXIT_FAILURE); |
| 2376 |
} |
| 2377 |
break; |
| 2378 |
case 'S': /* subdivide into Cols:Rows sections, eg 3:2 would be 3 |
| 2379 |
across and 2 down */ |
| 2380 |
sep = strpbrk(optarg, ",:"); |
| 2381 |
if (sep) |
| 2382 |
{ |
| 2383 |
*sep = '\0'; |
| 2384 |
page->cols = atoi(optarg); |
| 2385 |
page->rows = atoi(sep + 1); |
| 2386 |
} |
| 2387 |
else |
| 2388 |
{ |
| 2389 |
page->cols = atoi(optarg); |
| 2390 |
page->rows = atoi(optarg); |
| 2391 |
} |
| 2392 |
if ((page->cols * page->rows) > MAX_SECTIONS) |
| 2393 |
{ |
| 2394 |
TIFFError( |
| 2395 |
"Limit for subdivisions, ie rows x columns, exceeded", |
| 2396 |
"%d", MAX_SECTIONS); |
| 2397 |
exit(EXIT_FAILURE); |
| 2398 |
} |
| 2399 |
if ((page->cols * page->rows) < 1) |
| 2400 |
{ |
| 2401 |
TIFFError("No subdivisions", "%d", |
| 2402 |
(page->cols * page->rows)); |
| 2403 |
exit(EXIT_FAILURE); |
| 2404 |
} |
| 2405 |
page->mode |= PAGE_MODE_ROWSCOLS; |
| 2406 |
break; |
| 2407 |
case 'U': /* units for measurements and offsets */ |
| 2408 |
if (streq(optarg, "in")) |
| 2409 |
{ |
| 2410 |
crop_data->res_unit = RESUNIT_INCH; |
| 2411 |
page->res_unit = RESUNIT_INCH; |
| 2412 |
} |
| 2413 |
else if (streq(optarg, "cm")) |
| 2414 |
{ |
| 2415 |
crop_data->res_unit = RESUNIT_CENTIMETER; |
| 2416 |
page->res_unit = RESUNIT_CENTIMETER; |
| 2417 |
} |
| 2418 |
else if (streq(optarg, "px")) |
| 2419 |
{ |
| 2420 |
crop_data->res_unit = RESUNIT_NONE; |
| 2421 |
page->res_unit = RESUNIT_NONE; |
| 2422 |
} |
| 2423 |
else |
| 2424 |
{ |
| 2425 |
TIFFError("Illegal unit of measure", "%s", optarg); |
| 2426 |
TIFFError("For valid options type", "tiffcrop -h"); |
| 2427 |
exit(EXIT_FAILURE); |
| 2428 |
} |
| 2429 |
break; |
| 2430 |
case 'V': /* set vertical resolution to new value */ |
| 2431 |
page->vres = atof(optarg); |
| 2432 |
page->mode |= PAGE_MODE_RESOLUTION; |
| 2433 |
break; |
| 2434 |
case 'X': /* selection width */ |
| 2435 |
crop_data->crop_mode |= CROP_WIDTH; |
| 2436 |
crop_data->width = atof(optarg); |
| 2437 |
break; |
| 2438 |
case 'Y': /* selection length */ |
| 2439 |
crop_data->crop_mode |= CROP_LENGTH; |
| 2440 |
crop_data->length = atof(optarg); |
| 2441 |
break; |
| 2442 |
case 'Z': /* zones of an image X:Y read as zone X of Y */ |
| 2443 |
crop_data->crop_mode |= CROP_ZONES; |
| 2444 |
for (i = 0, opt_ptr = strtok(optarg, ","); |
| 2445 |
((opt_ptr != NULL) && (i < MAX_REGIONS)); |
| 2446 |
(opt_ptr = strtok(NULL, ",")), i++) |
| 2447 |
{ |
| 2448 |
crop_data->zones++; |
| 2449 |
opt_offset = strchr(opt_ptr, ':'); |
| 2450 |
if (!opt_offset) |
| 2451 |
{ |
| 2452 |
TIFFError("Wrong parameter syntax for -Z", |
| 2453 |
"tiffcrop -h"); |
| 2454 |
exit(EXIT_FAILURE); |
| 2455 |
} |
| 2456 |
*opt_offset = '\0'; |
| 2457 |
crop_data->zonelist[i].position = atoi(opt_ptr); |
| 2458 |
crop_data->zonelist[i].total = atoi(opt_offset + 1); |
| 2459 |
} |
| 2460 |
/* check for remaining elements over MAX_REGIONS */ |
| 2461 |
if ((opt_ptr != NULL) && (i >= MAX_REGIONS)) |
| 2462 |
{ |
| 2463 |
TIFFError("Zone list exceeds region limit", "%d", |
| 2464 |
MAX_REGIONS); |
| 2465 |
exit(EXIT_FAILURE); |
| 2466 |
} |
| 2467 |
break; |
| 2468 |
case '?': |
| 2469 |
TIFFError("For valid options type", "tiffcrop -h"); |
| 2470 |
exit(EXIT_FAILURE); |
| 2471 |
/*NOTREACHED*/ |
| 2472 |
} |
| 2473 |
} |
| 2474 |
/*-- Check for not allowed combinations (e.g. -X, -Y and -Z, -z and -S are |
| 2475 |
* mutually exclusive) --*/ |
| 2476 |
char XY, Z, R, S; |
| 2477 |
XY = ((crop_data->crop_mode & CROP_WIDTH) || |
| 2478 |
(crop_data->crop_mode & CROP_LENGTH)) |
| 2479 |
? 1 |
| 2480 |
: 0; |
| 2481 |
Z = (crop_data->crop_mode & CROP_ZONES) ? 1 : 0; |
| 2482 |
R = (crop_data->crop_mode & CROP_REGIONS) ? 1 : 0; |
| 2483 |
S = (page->mode & PAGE_MODE_ROWSCOLS) ? 1 : 0; |
| 2484 |
if (XY + Z + R + S > 1) |
| 2485 |
{ |
| 2486 |
TIFFError("tiffcrop input error", "The crop options(-X|-Y), -Z, -z and " |
| 2487 |
"-S are mutually exclusive.->exit"); |
| 2488 |
exit(EXIT_FAILURE); |
| 2489 |
} |
| 2490 |
|
| 2491 |
/* Check for not allowed combination: |
| 2492 |
* Any of the -X, -Y, -Z and -z options together with other PAGE_MODE_x |
| 2493 |
options |
| 2494 |
* such as -H, -V, -P, -J or -K are not supported and may cause buffer |
| 2495 |
overflows. |
| 2496 |
. */ |
| 2497 |
if ((XY + Z + R > 0) && page->mode != PAGE_MODE_NONE) |
| 2498 |
{ |
| 2499 |
TIFFError("tiffcrop input error", |
| 2500 |
"Any of the crop options -X, -Y, -Z and -z together with " |
| 2501 |
"other PAGE_MODE_x options such as - H, -V, -P, -J or -K is " |
| 2502 |
"not supported and may cause buffer overflows..->exit"); |
| 2503 |
exit(EXIT_FAILURE); |
| 2504 |
} |
| 2505 |
|
| 2506 |
} /* end process_command_opts */ |
| 2507 |
|
| 2508 |
/* Start a new output file if one has not been previously opened or |
| 2509 |
* autoindex is set to non-zero. Update page and file counters |
| 2510 |
* so TIFFTAG PAGENUM will be correct in image. |
| 2511 |
*/ |
| 2512 |
static int update_output_file(TIFF **tiffout, char *mode, int autoindex, |
| 2513 |
char *outname, unsigned int *page) |
| 2514 |
{ |
| 2515 |
static int findex = 0; /* file sequence indicator */ |
| 2516 |
size_t basename_len; |
| 2517 |
char *sep; |
| 2518 |
char export_ext[16]; |
| 2519 |
char exportname[PATH_MAX]; |
| 2520 |
|
| 2521 |
if (autoindex && (*tiffout != NULL)) |
| 2522 |
{ |
| 2523 |
/* Close any export file that was previously opened */ |
| 2524 |
TIFFClose(*tiffout); |
| 2525 |
*tiffout = NULL; |
| 2526 |
} |
| 2527 |
|
| 2528 |
memcpy(export_ext, ".tiff", 6); |
| 2529 |
memset(exportname, '\0', sizeof(exportname)); |
| 2530 |
|
| 2531 |
/* Leave room for page number portion of the new filename : |
| 2532 |
* hyphen + 6 digits + dot + 4 extension characters + null terminator */ |
| 2533 |
#define FILENUM_MAX_LENGTH (1 + 6 + 1 + 4 + 1) |
| 2534 |
strncpy(exportname, outname, sizeof(exportname) - FILENUM_MAX_LENGTH); |
| 2535 |
if (*tiffout == NULL) /* This is a new export file */ |
| 2536 |
{ |
| 2537 |
if (autoindex) |
| 2538 |
{ /* create a new filename for each export */ |
| 2539 |
findex++; |
| 2540 |
if ((sep = strstr(exportname, ".tif")) || |
| 2541 |
(sep = strstr(exportname, ".TIF"))) |
| 2542 |
{ |
| 2543 |
strncpy(export_ext, sep, 5); |
| 2544 |
*sep = '\0'; |
| 2545 |
} |
| 2546 |
else |
| 2547 |
memcpy(export_ext, ".tiff", 5); |
| 2548 |
export_ext[5] = '\0'; |
| 2549 |
basename_len = strlen(exportname); |
| 2550 |
|
| 2551 |
/* MAX_EXPORT_PAGES limited to 6 digits to prevent string overflow |
| 2552 |
* of pathname */ |
| 2553 |
if (findex > MAX_EXPORT_PAGES) |
| 2554 |
{ |
| 2555 |
TIFFError("update_output_file", |
| 2556 |
"Maximum of %d pages per file exceeded", |
| 2557 |
MAX_EXPORT_PAGES); |
| 2558 |
return 1; |
| 2559 |
} |
| 2560 |
|
| 2561 |
/* We previously assured that there will be space left */ |
| 2562 |
snprintf(exportname + basename_len, |
| 2563 |
sizeof(exportname) - basename_len, "-%03d%.5s", findex, |
| 2564 |
export_ext); |
| 2565 |
} |
| 2566 |
exportname[sizeof(exportname) - 1] = '\0'; |
| 2567 |
|
| 2568 |
TIFFOpenOptions *opts = TIFFOpenOptionsAlloc(); |
| 2569 |
if (opts == NULL) |
| 2570 |
{ |
| 2571 |
return 1; |
| 2572 |
} |
| 2573 |
TIFFOpenOptionsSetMaxSingleMemAlloc(opts, maxMalloc); |
| 2574 |
*tiffout = TIFFOpenExt(exportname, mode, opts); |
| 2575 |
TIFFOpenOptionsFree(opts); |
| 2576 |
if (*tiffout == NULL) |
| 2577 |
{ |
| 2578 |
TIFFError("update_output_file", "Unable to open output file %s", |
| 2579 |
exportname); |
| 2580 |
return 1; |
| 2581 |
} |
| 2582 |
*page = 0; |
| 2583 |
|
| 2584 |
return 0; |
| 2585 |
} |
| 2586 |
else |
| 2587 |
(*page)++; |
| 2588 |
|
| 2589 |
return 0; |
| 2590 |
} /* end update_output_file */ |
| 2591 |
|
| 2592 |
int main(int argc, char *argv[]) |
| 2593 |
{ |
| 2594 |
|
| 2595 |
#if !HAVE_DECL_OPTARG |
| 2596 |
extern int optind; |
| 2597 |
#endif |
| 2598 |
uint16_t defconfig = (uint16_t)-1; |
| 2599 |
uint16_t deffillorder = 0; |
| 2600 |
uint32_t deftilewidth = (uint32_t)0; |
| 2601 |
uint32_t deftilelength = (uint32_t)0; |
| 2602 |
uint32_t defrowsperstrip = (uint32_t)0; |
| 2603 |
uint32_t dirnum = 0; |
| 2604 |
|
| 2605 |
TIFF *in = NULL; |
| 2606 |
TIFF *out = NULL; |
| 2607 |
char mode[10]; |
| 2608 |
char *mp = mode; |
| 2609 |
|
| 2610 |
/** RJN additions **/ |
| 2611 |
struct image_data image; /* Image parameters for one image */ |
| 2612 |
struct crop_mask crop; /* Cropping parameters for all images */ |
| 2613 |
struct pagedef page; /* Page definition for output pages */ |
| 2614 |
struct pageseg sections[MAX_SECTIONS]; /* Sections of one output page */ |
| 2615 |
struct buffinfo |
| 2616 |
seg_buffs[MAX_SECTIONS]; /* Segment buffer sizes and pointers */ |
| 2617 |
struct dump_opts dump; /* Data dump options */ |
| 2618 |
unsigned char *read_buff = NULL; /* Input image data buffer */ |
| 2619 |
unsigned char *crop_buff = NULL; /* Crop area buffer */ |
| 2620 |
unsigned char *sect_buff = NULL; /* Image section buffer */ |
| 2621 |
unsigned char *sect_src = NULL; /* Image section buffer pointer */ |
| 2622 |
unsigned int imagelist[MAX_IMAGES + 1]; /* individually specified images */ |
| 2623 |
unsigned int image_count = 0; |
| 2624 |
unsigned int dump_images = 0; |
| 2625 |
unsigned int next_image = 0; |
| 2626 |
unsigned int next_page = 0; |
| 2627 |
unsigned int total_pages = 0; |
| 2628 |
unsigned int total_images = 0; |
| 2629 |
unsigned int end_of_input = FALSE; |
| 2630 |
int seg; |
| 2631 |
size_t length; |
| 2632 |
char temp_filename[PATH_MAX + 16]; /* Extra space keeps the compiler from |
| 2633 |
complaining */ |
| 2634 |
|
| 2635 |
assert(NUM_BUFF_OVERSIZE_BYTES >= 3); |
| 2636 |
|
| 2637 |
little_endian = *((unsigned char *)&little_endian) & '1'; |
| 2638 |
|
| 2639 |
initImageData(&image); |
| 2640 |
initCropMasks(&crop); |
| 2641 |
initPageSetup(&page, sections, seg_buffs); |
| 2642 |
initDumpOptions(&dump); |
| 2643 |
|
| 2644 |
process_command_opts(argc, argv, mp, mode, &dirnum, &defconfig, |
| 2645 |
&deffillorder, &deftilewidth, &deftilelength, |
| 2646 |
&defrowsperstrip, &crop, &page, &dump, imagelist, |
| 2647 |
&image_count); |
| 2648 |
|
| 2649 |
if (argc - optind < 2) |
| 2650 |
usage(EXIT_FAILURE); |
| 2651 |
|
| 2652 |
if ((argc - optind) == 2) |
| 2653 |
pageNum = -1; |
| 2654 |
|
| 2655 |
/* Read multiple input files and write to output file(s) */ |
| 2656 |
while (optind < argc - 1) |
| 2657 |
{ |
| 2658 |
|
| 2659 |
TIFFOpenOptions *opts = TIFFOpenOptionsAlloc(); |
| 2660 |
if (opts == NULL) |
| 2661 |
{ |
| 2662 |
return -3; |
| 2663 |
} |
| 2664 |
TIFFOpenOptionsSetMaxSingleMemAlloc(opts, maxMalloc); |
| 2665 |
in = TIFFOpenExt(argv[optind], "r", opts); |
| 2666 |
TIFFOpenOptionsFree(opts); |
| 2667 |
if (in == NULL) |
| 2668 |
return (-3); |
| 2669 |
|
| 2670 |
/* If only one input file is specified, we can use directory count */ |
| 2671 |
total_images = TIFFNumberOfDirectories(in); |
| 2672 |
if (total_images > TIFF_DIR_MAX) |
| 2673 |
{ |
| 2674 |
TIFFError(TIFFFileName(in), "File contains too many directories"); |
| 2675 |
if (out != NULL) |
| 2676 |
(void)TIFFClose(out); |
| 2677 |
return (1); |
| 2678 |
} |
| 2679 |
if (image_count == 0) |
| 2680 |
{ |
| 2681 |
dirnum = 0; |
| 2682 |
total_pages = total_images; /* Only valid with single input file */ |
| 2683 |
} |
| 2684 |
else |
| 2685 |
{ |
| 2686 |
dirnum = (tdir_t)(imagelist[next_image] - 1); |
| 2687 |
next_image++; |
| 2688 |
|
| 2689 |
/* Total pages only valid for enumerated list of pages not derived |
| 2690 |
* using odd, even, or last keywords. |
| 2691 |
*/ |
| 2692 |
if (image_count > total_images) |
| 2693 |
image_count = total_images; |
| 2694 |
|
| 2695 |
total_pages = image_count; |
| 2696 |
} |
| 2697 |
|
| 2698 |
/* MAX_IMAGES is used for special case "last" in selection list */ |
| 2699 |
if (dirnum == (MAX_IMAGES - 1)) |
| 2700 |
dirnum = total_images - 1; |
| 2701 |
|
| 2702 |
if (dirnum > (total_images)) |
| 2703 |
{ |
| 2704 |
TIFFError(TIFFFileName(in), |
| 2705 |
"Invalid image number %" PRIu32 |
| 2706 |
", File contains only %" PRIu32 " images", |
| 2707 |
dirnum + 1u, total_images); |
| 2708 |
if (out != NULL) |
| 2709 |
(void)TIFFClose(out); |
| 2710 |
return (1); |
| 2711 |
} |
| 2712 |
|
| 2713 |
if (dirnum != 0 && !TIFFSetDirectory(in, (tdir_t)dirnum)) |
| 2714 |
{ |
| 2715 |
TIFFError(TIFFFileName(in), |
| 2716 |
"Error, setting subdirectory at %" PRIu32, dirnum); |
| 2717 |
if (out != NULL) |
| 2718 |
(void)TIFFClose(out); |
| 2719 |
return (1); |
| 2720 |
} |
| 2721 |
|
| 2722 |
end_of_input = FALSE; |
| 2723 |
while (end_of_input == FALSE) |
| 2724 |
{ |
| 2725 |
config = defconfig; |
| 2726 |
compression = defcompression; |
| 2727 |
predictor = defpredictor; |
| 2728 |
fillorder = deffillorder; |
| 2729 |
rowsperstrip = defrowsperstrip; |
| 2730 |
tilewidth = deftilewidth; |
| 2731 |
tilelength = deftilelength; |
| 2732 |
g3opts = defg3opts; |
| 2733 |
|
| 2734 |
if (dump.format != DUMP_NONE) |
| 2735 |
{ |
| 2736 |
/* manage input and/or output dump files here */ |
| 2737 |
dump_images++; |
| 2738 |
length = strlen(dump.infilename); |
| 2739 |
if (length > 0) |
| 2740 |
{ |
| 2741 |
if (dump.infile != NULL) |
| 2742 |
fclose(dump.infile); |
| 2743 |
|
| 2744 |
/* dump.infilename is guaranteed to be NUL terminated and |
| 2745 |
have 20 bytes fewer than PATH_MAX */ |
| 2746 |
snprintf(temp_filename, sizeof(temp_filename), |
| 2747 |
"%s-read-%03d.%s", dump.infilename, dump_images, |
| 2748 |
(dump.format == DUMP_TEXT) ? "txt" : "raw"); |
| 2749 |
if ((dump.infile = fopen(temp_filename, dump.mode)) == NULL) |
| 2750 |
{ |
| 2751 |
TIFFError("Unable to open dump file for writing", "%s", |
| 2752 |
temp_filename); |
| 2753 |
exit(EXIT_FAILURE); |
| 2754 |
} |
| 2755 |
dump_info(dump.infile, dump.format, "Reading image", |
| 2756 |
"%u from %s", dump_images, TIFFFileName(in)); |
| 2757 |
} |
| 2758 |
length = strlen(dump.outfilename); |
| 2759 |
if (length > 0) |
| 2760 |
{ |
| 2761 |
if (dump.outfile != NULL) |
| 2762 |
fclose(dump.outfile); |
| 2763 |
|
| 2764 |
/* dump.outfilename is guaranteed to be NUL terminated and |
| 2765 |
have 20 bytes fewer than PATH_MAX */ |
| 2766 |
snprintf(temp_filename, sizeof(temp_filename), |
| 2767 |
"%s-write-%03d.%s", dump.outfilename, dump_images, |
| 2768 |
(dump.format == DUMP_TEXT) ? "txt" : "raw"); |
| 2769 |
if ((dump.outfile = fopen(temp_filename, dump.mode)) == |
| 2770 |
NULL) |
| 2771 |
{ |
| 2772 |
TIFFError("Unable to open dump file for writing", "%s", |
| 2773 |
temp_filename); |
| 2774 |
exit(EXIT_FAILURE); |
| 2775 |
} |
| 2776 |
dump_info(dump.outfile, dump.format, "Writing image", |
| 2777 |
"%u from %s", dump_images, TIFFFileName(in)); |
| 2778 |
} |
| 2779 |
} |
| 2780 |
|
| 2781 |
if (dump.debug) |
| 2782 |
TIFFError("main", "Reading image %4d of %4d total pages.", |
| 2783 |
dirnum + 1, total_pages); |
| 2784 |
|
| 2785 |
if (loadImage(in, &image, &dump, &read_buff)) |
| 2786 |
{ |
| 2787 |
TIFFError("main", "Unable to load source image"); |
| 2788 |
exit(EXIT_FAILURE); |
| 2789 |
} |
| 2790 |
|
| 2791 |
/* Correct the image orientation if it was not ORIENTATION_TOPLEFT. |
| 2792 |
*/ |
| 2793 |
if (image.adjustments != 0) |
| 2794 |
{ |
| 2795 |
if (correct_orientation(&image, &read_buff)) |
| 2796 |
TIFFError("main", "Unable to correct image orientation"); |
| 2797 |
} |
| 2798 |
|
| 2799 |
if (getCropOffsets(&image, &crop, &dump)) |
| 2800 |
{ |
| 2801 |
TIFFError("main", "Unable to define crop regions"); |
| 2802 |
exit(EXIT_FAILURE); |
| 2803 |
} |
| 2804 |
|
| 2805 |
/* Crop input image and copy zones and regions from input image into |
| 2806 |
* seg_buffs or crop_buff. */ |
| 2807 |
if (crop.selections > 0) |
| 2808 |
{ |
| 2809 |
if (processCropSelections(&image, &crop, &read_buff, seg_buffs)) |
| 2810 |
{ |
| 2811 |
TIFFError("main", "Unable to process image selections"); |
| 2812 |
exit(EXIT_FAILURE); |
| 2813 |
} |
| 2814 |
} |
| 2815 |
else /* Single image segment without zones or regions */ |
| 2816 |
{ |
| 2817 |
if (createCroppedImage(&image, &crop, &read_buff, &crop_buff)) |
| 2818 |
{ |
| 2819 |
TIFFError("main", "Unable to create output image"); |
| 2820 |
exit(EXIT_FAILURE); |
| 2821 |
} |
| 2822 |
} |
| 2823 |
/* Format and write selected image parts to output file(s). */ |
| 2824 |
if (page.mode == PAGE_MODE_NONE) |
| 2825 |
{ /* Whole image or sections not based on output page size */ |
| 2826 |
if (crop.selections > 0) |
| 2827 |
{ |
| 2828 |
if (writeSelections(in, &out, &crop, &image, &dump, |
| 2829 |
seg_buffs, mp, argv[argc - 1], |
| 2830 |
&next_page, total_pages)) |
| 2831 |
{ |
| 2832 |
TIFFError("main", |
| 2833 |
"Unable to write new image selections"); |
| 2834 |
exit(EXIT_FAILURE); |
| 2835 |
} |
| 2836 |
} |
| 2837 |
else /* One file all images and sections */ |
| 2838 |
{ |
| 2839 |
if (update_output_file(&out, mp, crop.exp_mode, |
| 2840 |
argv[argc - 1], &next_page)) |
| 2841 |
exit(EXIT_FAILURE); |
| 2842 |
if (writeCroppedImage(in, out, &image, &dump, |
| 2843 |
crop.combined_width, |
| 2844 |
crop.combined_length, crop_buff, |
| 2845 |
next_page, total_pages)) |
| 2846 |
{ |
| 2847 |
TIFFError("main", "Unable to write new image"); |
| 2848 |
exit(EXIT_FAILURE); |
| 2849 |
} |
| 2850 |
} |
| 2851 |
} |
| 2852 |
else |
| 2853 |
{ |
| 2854 |
/* If we used a crop buffer, our data is there, otherwise it is |
| 2855 |
* in the read_buffer |
| 2856 |
*/ |
| 2857 |
if (crop_buff != NULL) |
| 2858 |
sect_src = crop_buff; |
| 2859 |
else |
| 2860 |
sect_src = read_buff; |
| 2861 |
/* Break input image into pages or rows and columns */ |
| 2862 |
if (computeOutputPixelOffsets(&crop, &image, &page, sections, |
| 2863 |
&dump)) |
| 2864 |
{ |
| 2865 |
TIFFError("main", "Unable to compute output section data"); |
| 2866 |
exit(EXIT_FAILURE); |
| 2867 |
} |
| 2868 |
/* If there are multiple files on the command line, the final |
| 2869 |
* one is assumed to be the output filename into which the |
| 2870 |
* images are written. |
| 2871 |
*/ |
| 2872 |
if (update_output_file(&out, mp, crop.exp_mode, argv[argc - 1], |
| 2873 |
&next_page)) |
| 2874 |
exit(EXIT_FAILURE); |
| 2875 |
|
| 2876 |
if (writeImageSections(in, out, &image, &page, sections, &dump, |
| 2877 |
sect_src, §_buff)) |
| 2878 |
{ |
| 2879 |
TIFFError("main", "Unable to write image sections"); |
| 2880 |
exit(EXIT_FAILURE); |
| 2881 |
} |
| 2882 |
} |
| 2883 |
|
| 2884 |
/* No image list specified, just read the next image */ |
| 2885 |
if (image_count == 0) |
| 2886 |
dirnum++; |
| 2887 |
else |
| 2888 |
{ |
| 2889 |
dirnum = (tdir_t)(imagelist[next_image] - 1); |
| 2890 |
next_image++; |
| 2891 |
} |
| 2892 |
|
| 2893 |
if (dirnum == MAX_IMAGES - 1) |
| 2894 |
dirnum = TIFFNumberOfDirectories(in) - 1; |
| 2895 |
|
| 2896 |
if (!TIFFSetDirectory(in, (tdir_t)dirnum)) |
| 2897 |
end_of_input = TRUE; |
| 2898 |
} |
| 2899 |
TIFFClose(in); |
| 2900 |
optind++; |
| 2901 |
} |
| 2902 |
|
| 2903 |
/* If we did not use the read buffer as the crop buffer */ |
| 2904 |
if (read_buff) |
| 2905 |
_TIFFfree(read_buff); |
| 2906 |
|
| 2907 |
if (crop_buff) |
| 2908 |
_TIFFfree(crop_buff); |
| 2909 |
|
| 2910 |
if (sect_buff) |
| 2911 |
_TIFFfree(sect_buff); |
| 2912 |
|
| 2913 |
/* Clean up any segment buffers used for zones or regions */ |
| 2914 |
for (seg = 0; seg < crop.selections; seg++) |
| 2915 |
_TIFFfree(seg_buffs[seg].buffer); |
| 2916 |
|
| 2917 |
if (dump.format != DUMP_NONE) |
| 2918 |
{ |
| 2919 |
if (dump.infile != NULL) |
| 2920 |
fclose(dump.infile); |
| 2921 |
|
| 2922 |
if (dump.outfile != NULL) |
| 2923 |
{ |
| 2924 |
dump_info(dump.outfile, dump.format, "", "Completed run for %s", |
| 2925 |
TIFFFileName(out)); |
| 2926 |
fclose(dump.outfile); |
| 2927 |
} |
| 2928 |
} |
| 2929 |
|
| 2930 |
if (out != NULL) |
| 2931 |
{ |
| 2932 |
TIFFClose(out); |
| 2933 |
} |
| 2934 |
|
| 2935 |
return (0); |
| 2936 |
} /* end main */ |
| 2937 |
|
| 2938 |
/* Debugging functions */ |
| 2939 |
static int dump_data(FILE *dumpfile, int format, char *dump_tag, |
| 2940 |
unsigned char *data, uint32_t count) |
| 2941 |
{ |
| 2942 |
int j, k; |
| 2943 |
uint32_t i; |
| 2944 |
char dump_array[10]; |
| 2945 |
unsigned char bitset; |
| 2946 |
|
| 2947 |
if (dumpfile == NULL) |
| 2948 |
{ |
| 2949 |
TIFFError("", "Invalid FILE pointer for dump file"); |
| 2950 |
return (1); |
| 2951 |
} |
| 2952 |
|
| 2953 |
if (format == DUMP_TEXT) |
| 2954 |
{ |
| 2955 |
fprintf(dumpfile, " %s ", dump_tag); |
| 2956 |
for (i = 0; i < count; i++) |
| 2957 |
{ |
| 2958 |
for (j = 0, k = 7; j < 8; j++, k--) |
| 2959 |
{ |
| 2960 |
bitset = (*(data + i)) & (((unsigned char)1 << k)) ? 1 : 0; |
| 2961 |
sprintf(&dump_array[j], (bitset) ? "1" : "0"); |
| 2962 |
} |
| 2963 |
dump_array[8] = '\0'; |
| 2964 |
fprintf(dumpfile, " %s", dump_array); |
| 2965 |
} |
| 2966 |
fprintf(dumpfile, "\n"); |
| 2967 |
} |
| 2968 |
else |
| 2969 |
{ |
| 2970 |
if ((fwrite(data, 1, count, dumpfile)) != count) |
| 2971 |
{ |
| 2972 |
TIFFError("", "Unable to write binary data to dump file"); |
| 2973 |
return (1); |
| 2974 |
} |
| 2975 |
} |
| 2976 |
|
| 2977 |
return (0); |
| 2978 |
} |
| 2979 |
|
| 2980 |
static int dump_byte(FILE *dumpfile, int format, char *dump_tag, |
| 2981 |
unsigned char data) |
| 2982 |
{ |
| 2983 |
int j, k; |
| 2984 |
char dump_array[10]; |
| 2985 |
unsigned char bitset; |
| 2986 |
|
| 2987 |
if (dumpfile == NULL) |
| 2988 |
{ |
| 2989 |
TIFFError("", "Invalid FILE pointer for dump file"); |
| 2990 |
return (1); |
| 2991 |
} |
| 2992 |
|
| 2993 |
if (format == DUMP_TEXT) |
| 2994 |
{ |
| 2995 |
fprintf(dumpfile, " %s ", dump_tag); |
| 2996 |
for (j = 0, k = 7; j < 8; j++, k--) |
| 2997 |
{ |
| 2998 |
bitset = data & (((unsigned char)1 << k)) ? 1 : 0; |
| 2999 |
sprintf(&dump_array[j], (bitset) ? "1" : "0"); |
| 3000 |
} |
| 3001 |
dump_array[8] = '\0'; |
| 3002 |
fprintf(dumpfile, " %s\n", dump_array); |
| 3003 |
} |
| 3004 |
else |
| 3005 |
{ |
| 3006 |
if ((fwrite(&data, 1, 1, dumpfile)) != 1) |
| 3007 |
{ |
| 3008 |
TIFFError("", "Unable to write binary data to dump file"); |
| 3009 |
return (1); |
| 3010 |
} |
| 3011 |
} |
| 3012 |
|
| 3013 |
return (0); |
| 3014 |
} |
| 3015 |
|
| 3016 |
static int dump_short(FILE *dumpfile, int format, char *dump_tag, uint16_t data) |
| 3017 |
{ |
| 3018 |
int j, k; |
| 3019 |
char dump_array[20]; |
| 3020 |
unsigned char bitset; |
| 3021 |
|
| 3022 |
if (dumpfile == NULL) |
| 3023 |
{ |
| 3024 |
TIFFError("", "Invalid FILE pointer for dump file"); |
| 3025 |
return (1); |
| 3026 |
} |
| 3027 |
|
| 3028 |
if (format == DUMP_TEXT) |
| 3029 |
{ |
| 3030 |
fprintf(dumpfile, " %s ", dump_tag); |
| 3031 |
for (j = 0, k = 15; k >= 0; j++, k--) |
| 3032 |
{ |
| 3033 |
bitset = data & (((unsigned char)1 << k)) ? 1 : 0; |
| 3034 |
sprintf(&dump_array[j], (bitset) ? "1" : "0"); |
| 3035 |
if ((k % 8) == 0) |
| 3036 |
sprintf(&dump_array[++j], " "); |
| 3037 |
} |
| 3038 |
dump_array[17] = '\0'; |
| 3039 |
fprintf(dumpfile, " %s\n", dump_array); |
| 3040 |
} |
| 3041 |
else |
| 3042 |
{ |
| 3043 |
if ((fwrite(&data, 2, 1, dumpfile)) != 2) |
| 3044 |
{ |
| 3045 |
TIFFError("", "Unable to write binary data to dump file"); |
| 3046 |
return (1); |
| 3047 |
} |
| 3048 |
} |
| 3049 |
|
| 3050 |
return (0); |
| 3051 |
} |
| 3052 |
|
| 3053 |
static int dump_long(FILE *dumpfile, int format, char *dump_tag, uint32_t data) |
| 3054 |
{ |
| 3055 |
int j, k; |
| 3056 |
char dump_array[40]; |
| 3057 |
unsigned char bitset; |
| 3058 |
|
| 3059 |
if (dumpfile == NULL) |
| 3060 |
{ |
| 3061 |
TIFFError("", "Invalid FILE pointer for dump file"); |
| 3062 |
return (1); |
| 3063 |
} |
| 3064 |
|
| 3065 |
if (format == DUMP_TEXT) |
| 3066 |
{ |
| 3067 |
fprintf(dumpfile, " %s ", dump_tag); |
| 3068 |
for (j = 0, k = 31; k >= 0; j++, k--) |
| 3069 |
{ |
| 3070 |
bitset = data & (((uint32_t)1 << k)) ? 1 : 0; |
| 3071 |
sprintf(&dump_array[j], (bitset) ? "1" : "0"); |
| 3072 |
if ((k % 8) == 0) |
| 3073 |
sprintf(&dump_array[++j], " "); |
| 3074 |
} |
| 3075 |
dump_array[35] = '\0'; |
| 3076 |
fprintf(dumpfile, " %s\n", dump_array); |
| 3077 |
} |
| 3078 |
else |
| 3079 |
{ |
| 3080 |
if ((fwrite(&data, 4, 1, dumpfile)) != 4) |
| 3081 |
{ |
| 3082 |
TIFFError("", "Unable to write binary data to dump file"); |
| 3083 |
return (1); |
| 3084 |
} |
| 3085 |
} |
| 3086 |
return (0); |
| 3087 |
} |
| 3088 |
|
| 3089 |
static int dump_wide(FILE *dumpfile, int format, char *dump_tag, uint64_t data) |
| 3090 |
{ |
| 3091 |
int j, k; |
| 3092 |
char dump_array[80]; |
| 3093 |
unsigned char bitset; |
| 3094 |
|
| 3095 |
if (dumpfile == NULL) |
| 3096 |
{ |
| 3097 |
TIFFError("", "Invalid FILE pointer for dump file"); |
| 3098 |
return (1); |
| 3099 |
} |
| 3100 |
|
| 3101 |
if (format == DUMP_TEXT) |
| 3102 |
{ |
| 3103 |
fprintf(dumpfile, " %s ", dump_tag); |
| 3104 |
for (j = 0, k = 63; k >= 0; j++, k--) |
| 3105 |
{ |
| 3106 |
bitset = data & (((uint64_t)1 << k)) ? 1 : 0; |
| 3107 |
sprintf(&dump_array[j], (bitset) ? "1" : "0"); |
| 3108 |
if ((k % 8) == 0) |
| 3109 |
sprintf(&dump_array[++j], " "); |
| 3110 |
} |
| 3111 |
dump_array[71] = '\0'; |
| 3112 |
fprintf(dumpfile, " %s\n", dump_array); |
| 3113 |
} |
| 3114 |
else |
| 3115 |
{ |
| 3116 |
if ((fwrite(&data, 8, 1, dumpfile)) != 8) |
| 3117 |
{ |
| 3118 |
TIFFError("", "Unable to write binary data to dump file"); |
| 3119 |
return (1); |
| 3120 |
} |
| 3121 |
} |
| 3122 |
|
| 3123 |
return (0); |
| 3124 |
} |
| 3125 |
|
| 3126 |
static void dump_info(FILE *dumpfile, int format, char *prefix, char *msg, ...) |
| 3127 |
{ |
| 3128 |
if (format == DUMP_TEXT) |
| 3129 |
{ |
| 3130 |
va_list ap; |
| 3131 |
va_start(ap, msg); |
| 3132 |
fprintf(dumpfile, "%s ", prefix); |
| 3133 |
vfprintf(dumpfile, msg, ap); |
| 3134 |
fprintf(dumpfile, "\n"); |
| 3135 |
va_end(ap); |
| 3136 |
} |
| 3137 |
} |
| 3138 |
|
| 3139 |
static int dump_buffer(FILE *dumpfile, int format, uint32_t rows, |
| 3140 |
uint32_t width, uint32_t row, unsigned char *buff) |
| 3141 |
{ |
| 3142 |
int k; |
| 3143 |
uint32_t i; |
| 3144 |
unsigned char *dump_ptr; |
| 3145 |
|
| 3146 |
if (dumpfile == NULL) |
| 3147 |
{ |
| 3148 |
TIFFError("", "Invalid FILE pointer for dump file"); |
| 3149 |
return (1); |
| 3150 |
} |
| 3151 |
|
| 3152 |
for (i = 0; i < rows; i++) |
| 3153 |
{ |
| 3154 |
dump_ptr = buff + (i * width); |
| 3155 |
if (format == DUMP_TEXT) |
| 3156 |
dump_info(dumpfile, format, "", |
| 3157 |
"Row %4" PRIu32 ", %" PRIu32 " bytes at offset %" PRIu32, |
| 3158 |
row + i + 1u, width, row * width); |
| 3159 |
|
| 3160 |
for (k = width; k >= 10; k -= 10, dump_ptr += 10) |
| 3161 |
dump_data(dumpfile, format, "", dump_ptr, 10); |
| 3162 |
if (k > 0) |
| 3163 |
dump_data(dumpfile, format, "", dump_ptr, k); |
| 3164 |
} |
| 3165 |
return (0); |
| 3166 |
} |
| 3167 |
|
| 3168 |
/* Extract one or more samples from an interleaved buffer. If count == 1, |
| 3169 |
* only the sample plane indicated by sample will be extracted. If count > 1, |
| 3170 |
* count samples beginning at sample will be extracted. Portions of a |
| 3171 |
* scanline can be extracted by specifying a start and end value. |
| 3172 |
*/ |
| 3173 |
|
| 3174 |
static int extractContigSamplesBytes(uint8_t *in, uint8_t *out, uint32_t cols, |
| 3175 |
tsample_t sample, uint16_t spp, |
| 3176 |
uint16_t bps, tsample_t count, |
| 3177 |
uint32_t start, uint32_t end) |
| 3178 |
{ |
| 3179 |
int i, bytes_per_sample, sindex; |
| 3180 |
uint32_t col, dst_rowsize, bit_offset, numcols; |
| 3181 |
uint32_t src_byte /*, src_bit */; |
| 3182 |
uint8_t *src = in; |
| 3183 |
uint8_t *dst = out; |
| 3184 |
|
| 3185 |
if ((src == NULL) || (dst == NULL)) |
| 3186 |
{ |
| 3187 |
TIFFError("extractContigSamplesBytes", |
| 3188 |
"Invalid input or output buffer"); |
| 3189 |
return (1); |
| 3190 |
} |
| 3191 |
|
| 3192 |
/* Number of extracted columns shall be kept as (end-start + 1). Otherwise |
| 3193 |
* buffer-overflow might occur. 'start' and 'col' count from 0 to (cols-1) |
| 3194 |
* but 'end' is to be set one after the index of the last column to be |
| 3195 |
* copied! |
| 3196 |
*/ |
| 3197 |
if (end >= start) |
| 3198 |
numcols = end - start; |
| 3199 |
else |
| 3200 |
numcols = start - end; |
| 3201 |
if ((start > end) || (start > cols)) |
| 3202 |
{ |
| 3203 |
TIFFError("extractContigSamplesBytes", |
| 3204 |
"Invalid start column value %" PRIu32 " ignored", start); |
| 3205 |
start = 0; |
| 3206 |
} |
| 3207 |
if ((end == 0) || (end > cols)) |
| 3208 |
{ |
| 3209 |
TIFFError("extractContigSamplesBytes", |
| 3210 |
"Invalid end column value %" PRIu32 " ignored", end); |
| 3211 |
end = cols; |
| 3212 |
} |
| 3213 |
if ((end - start) > numcols) |
| 3214 |
{ |
| 3215 |
end = start + numcols; |
| 3216 |
} |
| 3217 |
|
| 3218 |
dst_rowsize = (bps * (end - start) * count) / 8; |
| 3219 |
|
| 3220 |
bytes_per_sample = (bps + 7) / 8; |
| 3221 |
/* Optimize case for copying all samples */ |
| 3222 |
if (count == spp) |
| 3223 |
{ |
| 3224 |
src = in + (start * spp * bytes_per_sample); |
| 3225 |
_TIFFmemcpy(dst, src, dst_rowsize); |
| 3226 |
} |
| 3227 |
else |
| 3228 |
{ |
| 3229 |
for (col = start; col < end; col++) |
| 3230 |
{ |
| 3231 |
for (sindex = sample; (sindex < spp) && (sindex < (sample + count)); |
| 3232 |
sindex++) |
| 3233 |
{ |
| 3234 |
bit_offset = col * bps * spp; |
| 3235 |
if (sindex == 0) |
| 3236 |
{ |
| 3237 |
src_byte = bit_offset / 8; |
| 3238 |
/* src_bit = bit_offset % 8; */ |
| 3239 |
} |
| 3240 |
else |
| 3241 |
{ |
| 3242 |
src_byte = (bit_offset + (sindex * bps)) / 8; |
| 3243 |
/* src_bit = (bit_offset + (sindex * bps)) % 8; */ |
| 3244 |
} |
| 3245 |
src = in + src_byte; |
| 3246 |
for (i = 0; i < bytes_per_sample; i++) |
| 3247 |
*dst++ = *src++; |
| 3248 |
} |
| 3249 |
} |
| 3250 |
} |
| 3251 |
|
| 3252 |
return (0); |
| 3253 |
} /* end extractContigSamplesBytes */ |
| 3254 |
|
| 3255 |
static int extractContigSamples8bits(uint8_t *in, uint8_t *out, uint32_t cols, |
| 3256 |
tsample_t sample, uint16_t spp, |
| 3257 |
uint16_t bps, tsample_t count, |
| 3258 |
uint32_t start, uint32_t end) |
| 3259 |
{ |
| 3260 |
int ready_bits = 0, sindex = 0; |
| 3261 |
uint32_t col, src_byte, src_bit, bit_offset, numcols; |
| 3262 |
uint8_t maskbits = 0, matchbits = 0; |
| 3263 |
uint8_t buff1 = 0, buff2 = 0; |
| 3264 |
uint8_t *src = in; |
| 3265 |
uint8_t *dst = out; |
| 3266 |
|
| 3267 |
if ((src == NULL) || (dst == NULL)) |
| 3268 |
{ |
| 3269 |
TIFFError("extractContigSamples8bits", |
| 3270 |
"Invalid input or output buffer"); |
| 3271 |
return (1); |
| 3272 |
} |
| 3273 |
|
| 3274 |
/* Number of extracted columns shall be kept as (end-start + 1). Otherwise |
| 3275 |
* buffer-overflow might occur. 'start' and 'col' count from 0 to (cols-1) |
| 3276 |
* but 'end' is to be set one after the index of the last column to be |
| 3277 |
* copied! |
| 3278 |
*/ |
| 3279 |
if (end >= start) |
| 3280 |
numcols = end - start; |
| 3281 |
else |
| 3282 |
numcols = start - end; |
| 3283 |
if ((start > end) || (start > cols)) |
| 3284 |
{ |
| 3285 |
TIFFError("extractContigSamples8bits", |
| 3286 |
"Invalid start column value %" PRIu32 " ignored", start); |
| 3287 |
start = 0; |
| 3288 |
} |
| 3289 |
if ((end == 0) || (end > cols)) |
| 3290 |
{ |
| 3291 |
TIFFError("extractContigSamples8bits", |
| 3292 |
"Invalid end column value %" PRIu32 " ignored", end); |
| 3293 |
end = cols; |
| 3294 |
} |
| 3295 |
if ((end - start) > numcols) |
| 3296 |
{ |
| 3297 |
end = start + numcols; |
| 3298 |
} |
| 3299 |
|
| 3300 |
ready_bits = 0; |
| 3301 |
maskbits = (uint8_t)-1 >> (8 - bps); |
| 3302 |
buff1 = buff2 = 0; |
| 3303 |
for (col = start; col < end; col++) |
| 3304 |
{ /* Compute src byte(s) and bits within byte(s) */ |
| 3305 |
bit_offset = col * bps * spp; |
| 3306 |
for (sindex = sample; (sindex < spp) && (sindex < (sample + count)); |
| 3307 |
sindex++) |
| 3308 |
{ |
| 3309 |
if (sindex == 0) |
| 3310 |
{ |
| 3311 |
src_byte = bit_offset / 8; |
| 3312 |
src_bit = bit_offset % 8; |
| 3313 |
} |
| 3314 |
else |
| 3315 |
{ |
| 3316 |
src_byte = (bit_offset + (sindex * bps)) / 8; |
| 3317 |
src_bit = (bit_offset + (sindex * bps)) % 8; |
| 3318 |
} |
| 3319 |
|
| 3320 |
src = in + src_byte; |
| 3321 |
matchbits = maskbits << (8 - src_bit - bps); |
| 3322 |
buff1 = ((*src) & matchbits) << (src_bit); |
| 3323 |
|
| 3324 |
/* If we have a full buffer's worth, write it out */ |
| 3325 |
if (ready_bits >= 8) |
| 3326 |
{ |
| 3327 |
*dst++ = buff2; |
| 3328 |
buff2 = buff1; |
| 3329 |
ready_bits -= 8; |
| 3330 |
} |
| 3331 |
else |
| 3332 |
buff2 = (buff2 | (buff1 >> ready_bits)); |
| 3333 |
ready_bits += bps; |
| 3334 |
} |
| 3335 |
} |
| 3336 |
|
| 3337 |
while (ready_bits > 0) |
| 3338 |
{ |
| 3339 |
buff1 = (buff2 & ((unsigned int)255 << (8 - ready_bits))); |
| 3340 |
*dst++ = buff1; |
| 3341 |
ready_bits -= 8; |
| 3342 |
} |
| 3343 |
|
| 3344 |
return (0); |
| 3345 |
} /* end extractContigSamples8bits */ |
| 3346 |
|
| 3347 |
static int extractContigSamples16bits(uint8_t *in, uint8_t *out, uint32_t cols, |
| 3348 |
tsample_t sample, uint16_t spp, |
| 3349 |
uint16_t bps, tsample_t count, |
| 3350 |
uint32_t start, uint32_t end) |
| 3351 |
{ |
| 3352 |
int ready_bits = 0, sindex = 0; |
| 3353 |
uint32_t col, src_byte, src_bit, bit_offset, numcols; |
| 3354 |
uint16_t maskbits = 0, matchbits = 0; |
| 3355 |
uint16_t buff1 = 0, buff2 = 0; |
| 3356 |
uint8_t bytebuff = 0; |
| 3357 |
uint8_t *src = in; |
| 3358 |
uint8_t *dst = out; |
| 3359 |
|
| 3360 |
if ((src == NULL) || (dst == NULL)) |
| 3361 |
{ |
| 3362 |
TIFFError("extractContigSamples16bits", |
| 3363 |
"Invalid input or output buffer"); |
| 3364 |
return (1); |
| 3365 |
} |
| 3366 |
|
| 3367 |
/* Number of extracted columns shall be kept as (end-start + 1). Otherwise |
| 3368 |
* buffer-overflow might occur. 'start' and 'col' count from 0 to (cols-1) |
| 3369 |
* but 'end' is to be set one after the index of the last column to be |
| 3370 |
* copied! |
| 3371 |
*/ |
| 3372 |
if (end >= start) |
| 3373 |
numcols = end - start; |
| 3374 |
else |
| 3375 |
numcols = start - end; |
| 3376 |
if ((start > end) || (start > cols)) |
| 3377 |
{ |
| 3378 |
TIFFError("extractContigSamples16bits", |
| 3379 |
"Invalid start column value %" PRIu32 " ignored", start); |
| 3380 |
start = 0; |
| 3381 |
} |
| 3382 |
if ((end == 0) || (end > cols)) |
| 3383 |
{ |
| 3384 |
TIFFError("extractContigSamples16bits", |
| 3385 |
"Invalid end column value %" PRIu32 " ignored", end); |
| 3386 |
end = cols; |
| 3387 |
} |
| 3388 |
if ((end - start) > numcols) |
| 3389 |
{ |
| 3390 |
end = start + numcols; |
| 3391 |
} |
| 3392 |
|
| 3393 |
ready_bits = 0; |
| 3394 |
maskbits = (uint16_t)-1 >> (16 - bps); |
| 3395 |
|
| 3396 |
for (col = start; col < end; col++) |
| 3397 |
{ /* Compute src byte(s) and bits within byte(s) */ |
| 3398 |
bit_offset = col * bps * spp; |
| 3399 |
for (sindex = sample; (sindex < spp) && (sindex < (sample + count)); |
| 3400 |
sindex++) |
| 3401 |
{ |
| 3402 |
if (sindex == 0) |
| 3403 |
{ |
| 3404 |
src_byte = bit_offset / 8; |
| 3405 |
src_bit = bit_offset % 8; |
| 3406 |
} |
| 3407 |
else |
| 3408 |
{ |
| 3409 |
src_byte = (bit_offset + (sindex * bps)) / 8; |
| 3410 |
src_bit = (bit_offset + (sindex * bps)) % 8; |
| 3411 |
} |
| 3412 |
|
| 3413 |
src = in + src_byte; |
| 3414 |
matchbits = maskbits << (16 - src_bit - bps); |
| 3415 |
|
| 3416 |
if (little_endian) |
| 3417 |
buff1 = (src[0] << 8) | src[1]; |
| 3418 |
else |
| 3419 |
buff1 = (src[1] << 8) | src[0]; |
| 3420 |
|
| 3421 |
buff1 = (buff1 & matchbits) << (src_bit); |
| 3422 |
if (ready_bits < 8) /* add another bps bits to the buffer */ |
| 3423 |
{ |
| 3424 |
buff2 = (buff2 | (buff1 >> ready_bits)); |
| 3425 |
} |
| 3426 |
else /* If we have a full buffer's worth, write it out */ |
| 3427 |
{ |
| 3428 |
bytebuff = (buff2 >> 8); |
| 3429 |
*dst++ = bytebuff; |
| 3430 |
ready_bits -= 8; |
| 3431 |
/* shift in new bits */ |
| 3432 |
buff2 = ((buff2 << 8) | (buff1 >> ready_bits)); |
| 3433 |
} |
| 3434 |
ready_bits += bps; |
| 3435 |
} |
| 3436 |
} |
| 3437 |
|
| 3438 |
/* catch any trailing bits at the end of the line */ |
| 3439 |
while (ready_bits > 0) |
| 3440 |
{ |
| 3441 |
bytebuff = (buff2 >> 8); |
| 3442 |
*dst++ = bytebuff; |
| 3443 |
ready_bits -= 8; |
| 3444 |
} |
| 3445 |
|
| 3446 |
return (0); |
| 3447 |
} /* end extractContigSamples16bits */ |
| 3448 |
|
| 3449 |
static int extractContigSamples24bits(uint8_t *in, uint8_t *out, uint32_t cols, |
| 3450 |
tsample_t sample, uint16_t spp, |
| 3451 |
uint16_t bps, tsample_t count, |
| 3452 |
uint32_t start, uint32_t end) |
| 3453 |
{ |
| 3454 |
int ready_bits = 0, sindex = 0; |
| 3455 |
uint32_t col, src_byte, src_bit, bit_offset, numcols; |
| 3456 |
uint32_t maskbits = 0, matchbits = 0; |
| 3457 |
uint32_t buff1 = 0, buff2 = 0; |
| 3458 |
uint8_t bytebuff1 = 0, bytebuff2 = 0; |
| 3459 |
uint8_t *src = in; |
| 3460 |
uint8_t *dst = out; |
| 3461 |
|
| 3462 |
if ((in == NULL) || (out == NULL)) |
| 3463 |
{ |
| 3464 |
TIFFError("extractContigSamples24bits", |
| 3465 |
"Invalid input or output buffer"); |
| 3466 |
return (1); |
| 3467 |
} |
| 3468 |
|
| 3469 |
/* Number of extracted columns shall be kept as (end-start + 1). Otherwise |
| 3470 |
* buffer-overflow might occur. 'start' and 'col' count from 0 to (cols-1) |
| 3471 |
* but 'end' is to be set one after the index of the last column to be |
| 3472 |
* copied! |
| 3473 |
*/ |
| 3474 |
if (end >= start) |
| 3475 |
numcols = end - start; |
| 3476 |
else |
| 3477 |
numcols = start - end; |
| 3478 |
if ((start > end) || (start > cols)) |
| 3479 |
{ |
| 3480 |
TIFFError("extractContigSamples24bits", |
| 3481 |
"Invalid start column value %" PRIu32 " ignored", start); |
| 3482 |
start = 0; |
| 3483 |
} |
| 3484 |
if ((end == 0) || (end > cols)) |
| 3485 |
{ |
| 3486 |
TIFFError("extractContigSamples24bits", |
| 3487 |
"Invalid end column value %" PRIu32 " ignored", end); |
| 3488 |
end = cols; |
| 3489 |
} |
| 3490 |
if ((end - start) > numcols) |
| 3491 |
{ |
| 3492 |
end = start + numcols; |
| 3493 |
} |
| 3494 |
|
| 3495 |
ready_bits = 0; |
| 3496 |
maskbits = (uint32_t)-1 >> (32 - bps); |
| 3497 |
for (col = start; col < end; col++) |
| 3498 |
{ |
| 3499 |
/* Compute src byte(s) and bits within byte(s) */ |
| 3500 |
bit_offset = col * bps * spp; |
| 3501 |
for (sindex = sample; (sindex < spp) && (sindex < (sample + count)); |
| 3502 |
sindex++) |
| 3503 |
{ |
| 3504 |
if (sindex == 0) |
| 3505 |
{ |
| 3506 |
src_byte = bit_offset / 8; |
| 3507 |
src_bit = bit_offset % 8; |
| 3508 |
} |
| 3509 |
else |
| 3510 |
{ |
| 3511 |
src_byte = (bit_offset + (sindex * bps)) / 8; |
| 3512 |
src_bit = (bit_offset + (sindex * bps)) % 8; |
| 3513 |
} |
| 3514 |
|
| 3515 |
src = in + src_byte; |
| 3516 |
matchbits = maskbits << (32 - src_bit - bps); |
| 3517 |
if (little_endian) |
| 3518 |
{ |
| 3519 |
buff1 = (src[0] << 24); |
| 3520 |
if (matchbits & 0x00ff0000) |
| 3521 |
buff1 |= (src[1] << 16); |
| 3522 |
if (matchbits & 0x0000ff00) |
| 3523 |
buff1 |= (src[2] << 8); |
| 3524 |
if (matchbits & 0x000000ff) |
| 3525 |
buff1 |= src[3]; |
| 3526 |
} |
| 3527 |
else |
| 3528 |
{ |
| 3529 |
buff1 = src[0]; |
| 3530 |
if (matchbits & 0x0000ff00) |
| 3531 |
buff1 |= (src[1] << 8); |
| 3532 |
if (matchbits & 0x00ff0000) |
| 3533 |
buff1 |= (src[2] << 16); |
| 3534 |
if (matchbits & 0xff000000) |
| 3535 |
buff1 |= (src[3] << 24); |
| 3536 |
} |
| 3537 |
buff1 = (buff1 & matchbits) << (src_bit); |
| 3538 |
|
| 3539 |
if (ready_bits < 16) /* add another bps bits to the buffer */ |
| 3540 |
{ |
| 3541 |
buff2 = (buff2 | (buff1 >> ready_bits)); |
| 3542 |
} |
| 3543 |
else /* If we have a full buffer's worth, write it out */ |
| 3544 |
{ |
| 3545 |
bytebuff1 = (buff2 >> 24); |
| 3546 |
*dst++ = bytebuff1; |
| 3547 |
bytebuff2 = (buff2 >> 16); |
| 3548 |
*dst++ = bytebuff2; |
| 3549 |
ready_bits -= 16; |
| 3550 |
|
| 3551 |
/* shift in new bits */ |
| 3552 |
buff2 = ((buff2 << 16) | (buff1 >> ready_bits)); |
| 3553 |
} |
| 3554 |
ready_bits += bps; |
| 3555 |
} |
| 3556 |
} |
| 3557 |
|
| 3558 |
/* catch any trailing bits at the end of the line */ |
| 3559 |
while (ready_bits > 0) |
| 3560 |
{ |
| 3561 |
bytebuff1 = (buff2 >> 24); |
| 3562 |
*dst++ = bytebuff1; |
| 3563 |
|
| 3564 |
buff2 = (buff2 << 8); |
| 3565 |
bytebuff2 = bytebuff1; |
| 3566 |
ready_bits -= 8; |
| 3567 |
} |
| 3568 |
|
| 3569 |
return (0); |
| 3570 |
} /* end extractContigSamples24bits */ |
| 3571 |
|
| 3572 |
static int extractContigSamples32bits(uint8_t *in, uint8_t *out, uint32_t cols, |
| 3573 |
tsample_t sample, uint16_t spp, |
| 3574 |
uint16_t bps, tsample_t count, |
| 3575 |
uint32_t start, uint32_t end) |
| 3576 |
{ |
| 3577 |
int ready_bits = 0, sindex = 0 /*, shift_width = 0 */; |
| 3578 |
uint32_t col, src_byte, src_bit, bit_offset, numcols; |
| 3579 |
uint32_t longbuff1 = 0, longbuff2 = 0; |
| 3580 |
uint64_t maskbits = 0, matchbits = 0; |
| 3581 |
uint64_t buff1 = 0, buff2 = 0, buff3 = 0; |
| 3582 |
uint8_t bytebuff1 = 0, bytebuff2 = 0, bytebuff3 = 0, bytebuff4 = 0; |
| 3583 |
uint8_t *src = in; |
| 3584 |
uint8_t *dst = out; |
| 3585 |
|
| 3586 |
if ((in == NULL) || (out == NULL)) |
| 3587 |
{ |
| 3588 |
TIFFError("extractContigSamples32bits", |
| 3589 |
"Invalid input or output buffer"); |
| 3590 |
return (1); |
| 3591 |
} |
| 3592 |
|
| 3593 |
/* Number of extracted columns shall be kept as (end-start + 1). Otherwise |
| 3594 |
* buffer-overflow might occur. 'start' and 'col' count from 0 to (cols-1) |
| 3595 |
* but 'end' is to be set one after the index of the last column to be |
| 3596 |
* copied! |
| 3597 |
*/ |
| 3598 |
if (end >= start) |
| 3599 |
numcols = end - start; |
| 3600 |
else |
| 3601 |
numcols = start - end; |
| 3602 |
if ((start > end) || (start > cols)) |
| 3603 |
{ |
| 3604 |
TIFFError("extractContigSamples32bits", |
| 3605 |
"Invalid start column value %" PRIu32 " ignored", start); |
| 3606 |
start = 0; |
| 3607 |
} |
| 3608 |
if ((end == 0) || (end > cols)) |
| 3609 |
{ |
| 3610 |
TIFFError("extractContigSamples32bits", |
| 3611 |
"Invalid end column value %" PRIu32 " ignored", end); |
| 3612 |
end = cols; |
| 3613 |
} |
| 3614 |
if ((end - start) > numcols) |
| 3615 |
{ |
| 3616 |
end = start + numcols; |
| 3617 |
} |
| 3618 |
|
| 3619 |
/* shift_width = ((bps + 7) / 8) + 1; */ |
| 3620 |
ready_bits = 0; |
| 3621 |
maskbits = (uint64_t)-1 >> (64 - bps); |
| 3622 |
for (col = start; col < end; col++) |
| 3623 |
{ |
| 3624 |
/* Compute src byte(s) and bits within byte(s) */ |
| 3625 |
bit_offset = col * bps * spp; |
| 3626 |
for (sindex = sample; (sindex < spp) && (sindex < (sample + count)); |
| 3627 |
sindex++) |
| 3628 |
{ |
| 3629 |
if (sindex == 0) |
| 3630 |
{ |
| 3631 |
src_byte = bit_offset / 8; |
| 3632 |
src_bit = bit_offset % 8; |
| 3633 |
} |
| 3634 |
else |
| 3635 |
{ |
| 3636 |
src_byte = (bit_offset + (sindex * bps)) / 8; |
| 3637 |
src_bit = (bit_offset + (sindex * bps)) % 8; |
| 3638 |
} |
| 3639 |
|
| 3640 |
src = in + src_byte; |
| 3641 |
matchbits = maskbits << (64 - src_bit - bps); |
| 3642 |
if (little_endian) |
| 3643 |
{ |
| 3644 |
longbuff1 = |
| 3645 |
(src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3]; |
| 3646 |
longbuff2 = longbuff1; |
| 3647 |
} |
| 3648 |
else |
| 3649 |
{ |
| 3650 |
longbuff1 = |
| 3651 |
(src[3] << 24) | (src[2] << 16) | (src[1] << 8) | src[0]; |
| 3652 |
longbuff2 = longbuff1; |
| 3653 |
} |
| 3654 |
|
| 3655 |
buff3 = ((uint64_t)longbuff1 << 32) | longbuff2; |
| 3656 |
buff1 = (buff3 & matchbits) << (src_bit); |
| 3657 |
|
| 3658 |
/* If we have a full buffer's worth, write it out */ |
| 3659 |
if (ready_bits >= 32) |
| 3660 |
{ |
| 3661 |
bytebuff1 = (uint8_t)(buff2 >> 56); |
| 3662 |
*dst++ = bytebuff1; |
| 3663 |
bytebuff2 = (uint8_t)(buff2 >> 48); |
| 3664 |
*dst++ = bytebuff2; |
| 3665 |
bytebuff3 = (uint8_t)(buff2 >> 40); |
| 3666 |
*dst++ = bytebuff3; |
| 3667 |
bytebuff4 = (uint8_t)(buff2 >> 32); |
| 3668 |
*dst++ = bytebuff4; |
| 3669 |
ready_bits -= 32; |
| 3670 |
|
| 3671 |
/* shift in new bits */ |
| 3672 |
buff2 = ((buff2 << 32) | (buff1 >> ready_bits)); |
| 3673 |
} |
| 3674 |
else |
| 3675 |
{ /* add another bps bits to the buffer */ |
| 3676 |
buff2 = (buff2 | (buff1 >> ready_bits)); |
| 3677 |
} |
| 3678 |
ready_bits += bps; |
| 3679 |
} |
| 3680 |
} |
| 3681 |
while (ready_bits > 0) |
| 3682 |
{ |
| 3683 |
bytebuff1 = (buff2 >> 56); |
| 3684 |
*dst++ = bytebuff1; |
| 3685 |
buff2 = (buff2 << 8); |
| 3686 |
ready_bits -= 8; |
| 3687 |
} |
| 3688 |
|
| 3689 |
return (0); |
| 3690 |
} /* end extractContigSamples32bits */ |
| 3691 |
|
| 3692 |
static int extractContigSamplesShifted8bits(uint8_t *in, uint8_t *out, |
| 3693 |
uint32_t cols, tsample_t sample, |
| 3694 |
uint16_t spp, uint16_t bps, |
| 3695 |
tsample_t count, uint32_t start, |
| 3696 |
uint32_t end, int shift) |
| 3697 |
{ |
| 3698 |
int ready_bits = 0, sindex = 0; |
| 3699 |
uint32_t col, src_byte, src_bit, bit_offset, numcols; |
| 3700 |
uint8_t maskbits = 0, matchbits = 0; |
| 3701 |
uint8_t buff1 = 0, buff2 = 0; |
| 3702 |
uint8_t *src = in; |
| 3703 |
uint8_t *dst = out; |
| 3704 |
|
| 3705 |
if ((src == NULL) || (dst == NULL)) |
| 3706 |
{ |
| 3707 |
TIFFError("extractContigSamplesShifted8bits", |
| 3708 |
"Invalid input or output buffer"); |
| 3709 |
return (1); |
| 3710 |
} |
| 3711 |
|
| 3712 |
/* Number of extracted columns shall be kept as (end-start + 1). Otherwise |
| 3713 |
* buffer-overflow might occur. 'start' and 'col' count from 0 to (cols-1) |
| 3714 |
* but 'end' is to be set one after the index of the last column to be |
| 3715 |
* copied! |
| 3716 |
*/ |
| 3717 |
if (end >= start) |
| 3718 |
numcols = end - start; |
| 3719 |
else |
| 3720 |
numcols = start - end; |
| 3721 |
if ((start > end) || (start > cols)) |
| 3722 |
{ |
| 3723 |
TIFFError("extractContigSamplesShifted8bits", |
| 3724 |
"Invalid start column value %" PRIu32 " ignored", start); |
| 3725 |
start = 0; |
| 3726 |
} |
| 3727 |
if ((end == 0) || (end > cols)) |
| 3728 |
{ |
| 3729 |
TIFFError("extractContigSamplesShifted8bits", |
| 3730 |
"Invalid end column value %" PRIu32 " ignored", end); |
| 3731 |
end = cols; |
| 3732 |
} |
| 3733 |
if ((end - start) > numcols) |
| 3734 |
{ |
| 3735 |
end = start + numcols; |
| 3736 |
} |
| 3737 |
|
| 3738 |
ready_bits = shift; |
| 3739 |
maskbits = (uint8_t)-1 >> (8 - bps); |
| 3740 |
buff1 = buff2 = 0; |
| 3741 |
for (col = start; col < end; col++) |
| 3742 |
{ /* Compute src byte(s) and bits within byte(s) */ |
| 3743 |
bit_offset = col * bps * spp; |
| 3744 |
for (sindex = sample; (sindex < spp) && (sindex < (sample + count)); |
| 3745 |
sindex++) |
| 3746 |
{ |
| 3747 |
if (sindex == 0) |
| 3748 |
{ |
| 3749 |
src_byte = bit_offset / 8; |
| 3750 |
src_bit = bit_offset % 8; |
| 3751 |
} |
| 3752 |
else |
| 3753 |
{ |
| 3754 |
src_byte = (bit_offset + (sindex * bps)) / 8; |
| 3755 |
src_bit = (bit_offset + (sindex * bps)) % 8; |
| 3756 |
} |
| 3757 |
|
| 3758 |
src = in + src_byte; |
| 3759 |
matchbits = maskbits << (8 - src_bit - bps); |
| 3760 |
buff1 = ((*src) & matchbits) << (src_bit); |
| 3761 |
if ((col == start) && (sindex == sample)) |
| 3762 |
buff2 = *src & ((uint8_t)-1) << (shift); |
| 3763 |
|
| 3764 |
/* If we have a full buffer's worth, write it out */ |
| 3765 |
if (ready_bits >= 8) |
| 3766 |
{ |
| 3767 |
*dst++ |= buff2; |
| 3768 |
buff2 = buff1; |
| 3769 |
ready_bits -= 8; |
| 3770 |
} |
| 3771 |
else |
| 3772 |
buff2 = buff2 | (buff1 >> ready_bits); |
| 3773 |
ready_bits += bps; |
| 3774 |
} |
| 3775 |
} |
| 3776 |
|
| 3777 |
while (ready_bits > 0) |
| 3778 |
{ |
| 3779 |
buff1 = (buff2 & ((unsigned int)255 << (8 - ready_bits))); |
| 3780 |
*dst++ = buff1; |
| 3781 |
ready_bits -= 8; |
| 3782 |
} |
| 3783 |
|
| 3784 |
return (0); |
| 3785 |
} /* end extractContigSamplesShifted8bits */ |
| 3786 |
|
| 3787 |
static int extractContigSamplesShifted16bits(uint8_t *in, uint8_t *out, |
| 3788 |
uint32_t cols, tsample_t sample, |
| 3789 |
uint16_t spp, uint16_t bps, |
| 3790 |
tsample_t count, uint32_t start, |
| 3791 |
uint32_t end, int shift) |
| 3792 |
{ |
| 3793 |
int ready_bits = 0, sindex = 0; |
| 3794 |
uint32_t col, src_byte, src_bit, bit_offset, numcols; |
| 3795 |
uint16_t maskbits = 0, matchbits = 0; |
| 3796 |
uint16_t buff1 = 0, buff2 = 0; |
| 3797 |
uint8_t bytebuff = 0; |
| 3798 |
uint8_t *src = in; |
| 3799 |
uint8_t *dst = out; |
| 3800 |
|
| 3801 |
if ((src == NULL) || (dst == NULL)) |
| 3802 |
{ |
| 3803 |
TIFFError("extractContigSamplesShifted16bits", |
| 3804 |
"Invalid input or output buffer"); |
| 3805 |
return (1); |
| 3806 |
} |
| 3807 |
|
| 3808 |
/* Number of extracted columns shall be kept as (end-start + 1). Otherwise |
| 3809 |
* buffer-overflow might occur. 'start' and 'col' count from 0 to (cols-1) |
| 3810 |
* but 'end' is to be set one after the index of the last column to be |
| 3811 |
* copied! |
| 3812 |
*/ |
| 3813 |
if (end >= start) |
| 3814 |
numcols = end - start; |
| 3815 |
else |
| 3816 |
numcols = start - end; |
| 3817 |
if ((start > end) || (start > cols)) |
| 3818 |
{ |
| 3819 |
TIFFError("extractContigSamplesShifted16bits", |
| 3820 |
"Invalid start column value %" PRIu32 " ignored", start); |
| 3821 |
start = 0; |
| 3822 |
} |
| 3823 |
if ((end == 0) || (end > cols)) |
| 3824 |
{ |
| 3825 |
TIFFError("extractContigSamplesShifted16bits", |
| 3826 |
"Invalid end column value %" PRIu32 " ignored", end); |
| 3827 |
end = cols; |
| 3828 |
} |
| 3829 |
if ((end - start) > numcols) |
| 3830 |
{ |
| 3831 |
end = start + numcols; |
| 3832 |
} |
| 3833 |
|
| 3834 |
ready_bits = shift; |
| 3835 |
maskbits = (uint16_t)-1 >> (16 - bps); |
| 3836 |
for (col = start; col < end; col++) |
| 3837 |
{ /* Compute src byte(s) and bits within byte(s) */ |
| 3838 |
bit_offset = col * bps * spp; |
| 3839 |
for (sindex = sample; (sindex < spp) && (sindex < (sample + count)); |
| 3840 |
sindex++) |
| 3841 |
{ |
| 3842 |
if (sindex == 0) |
| 3843 |
{ |
| 3844 |
src_byte = bit_offset / 8; |
| 3845 |
src_bit = bit_offset % 8; |
| 3846 |
} |
| 3847 |
else |
| 3848 |
{ |
| 3849 |
src_byte = (bit_offset + (sindex * bps)) / 8; |
| 3850 |
src_bit = (bit_offset + (sindex * bps)) % 8; |
| 3851 |
} |
| 3852 |
|
| 3853 |
src = in + src_byte; |
| 3854 |
matchbits = maskbits << (16 - src_bit - bps); |
| 3855 |
if (little_endian) |
| 3856 |
buff1 = (src[0] << 8) | src[1]; |
| 3857 |
else |
| 3858 |
buff1 = (src[1] << 8) | src[0]; |
| 3859 |
|
| 3860 |
if ((col == start) && (sindex == sample)) |
| 3861 |
buff2 = buff1 & ((uint16_t)-1) << (8 - shift); |
| 3862 |
|
| 3863 |
buff1 = (buff1 & matchbits) << (src_bit); |
| 3864 |
|
| 3865 |
if (ready_bits < 8) /* add another bps bits to the buffer */ |
| 3866 |
buff2 = buff2 | (buff1 >> ready_bits); |
| 3867 |
else /* If we have a full buffer's worth, write it out */ |
| 3868 |
{ |
| 3869 |
bytebuff = (buff2 >> 8); |
| 3870 |
*dst++ = bytebuff; |
| 3871 |
ready_bits -= 8; |
| 3872 |
/* shift in new bits */ |
| 3873 |
buff2 = ((buff2 << 8) | (buff1 >> ready_bits)); |
| 3874 |
} |
| 3875 |
|
| 3876 |
ready_bits += bps; |
| 3877 |
} |
| 3878 |
} |
| 3879 |
|
| 3880 |
/* catch any trailing bits at the end of the line */ |
| 3881 |
while (ready_bits > 0) |
| 3882 |
{ |
| 3883 |
bytebuff = (buff2 >> 8); |
| 3884 |
*dst++ = bytebuff; |
| 3885 |
ready_bits -= 8; |
| 3886 |
} |
| 3887 |
|
| 3888 |
return (0); |
| 3889 |
} /* end extractContigSamplesShifted16bits */ |
| 3890 |
|
| 3891 |
static int extractContigSamplesShifted24bits(uint8_t *in, uint8_t *out, |
| 3892 |
uint32_t cols, tsample_t sample, |
| 3893 |
uint16_t spp, uint16_t bps, |
| 3894 |
tsample_t count, uint32_t start, |
| 3895 |
uint32_t end, int shift) |
| 3896 |
{ |
| 3897 |
int ready_bits = 0, sindex = 0; |
| 3898 |
uint32_t col, src_byte, src_bit, bit_offset, numcols; |
| 3899 |
uint32_t maskbits = 0, matchbits = 0; |
| 3900 |
uint32_t buff1 = 0, buff2 = 0; |
| 3901 |
uint8_t bytebuff1 = 0, bytebuff2 = 0; |
| 3902 |
uint8_t *src = in; |
| 3903 |
uint8_t *dst = out; |
| 3904 |
|
| 3905 |
if ((in == NULL) || (out == NULL)) |
| 3906 |
{ |
| 3907 |
TIFFError("extractContigSamplesShifted24bits", |
| 3908 |
"Invalid input or output buffer"); |
| 3909 |
return (1); |
| 3910 |
} |
| 3911 |
|
| 3912 |
/* Number of extracted columns shall be kept as (end-start + 1). Otherwise |
| 3913 |
* buffer-overflow might occur. 'start' and 'col' count from 0 to (cols-1) |
| 3914 |
* but 'end' is to be set one after the index of the last column to be |
| 3915 |
* copied! |
| 3916 |
*/ |
| 3917 |
/*--- Remark, which is true for all those functions |
| 3918 |
* extractCongigSamplesXXX() -- The mitigation of the start/end test does |
| 3919 |
* not always make sense, because the function is often called with e.g.: |
| 3920 |
* start = 31; end = 32; cols = 32 to extract the last column in a 32x32 |
| 3921 |
* sample image. If then, a wrong parameter (e.g. cols = 10) is provided, |
| 3922 |
* the mitigated settings would be start=0; end=1. Therefore, an error |
| 3923 |
* message and no copy action might be the better reaction to wrong |
| 3924 |
* parameter configurations. |
| 3925 |
*/ |
| 3926 |
if (end >= start) |
| 3927 |
numcols = end - start; |
| 3928 |
else |
| 3929 |
numcols = start - end; |
| 3930 |
if ((start > end) || (start > cols)) |
| 3931 |
{ |
| 3932 |
TIFFError("extractContigSamplesShifted24bits", |
| 3933 |
"Invalid start column value %" PRIu32 " ignored", start); |
| 3934 |
start = 0; |
| 3935 |
} |
| 3936 |
if ((end == 0) || (end > cols)) |
| 3937 |
{ |
| 3938 |
TIFFError("extractContigSamplesShifted24bits", |
| 3939 |
"Invalid end column value %" PRIu32 " ignored", end); |
| 3940 |
end = cols; |
| 3941 |
} |
| 3942 |
if ((end - start) > numcols) |
| 3943 |
{ |
| 3944 |
end = start + numcols; |
| 3945 |
} |
| 3946 |
|
| 3947 |
ready_bits = shift; |
| 3948 |
maskbits = (uint32_t)-1 >> (32 - bps); |
| 3949 |
for (col = start; col < end; col++) |
| 3950 |
{ |
| 3951 |
/* Compute src byte(s) and bits within byte(s) */ |
| 3952 |
bit_offset = col * bps * spp; |
| 3953 |
for (sindex = sample; (sindex < spp) && (sindex < (sample + count)); |
| 3954 |
sindex++) |
| 3955 |
{ |
| 3956 |
if (sindex == 0) |
| 3957 |
{ |
| 3958 |
src_byte = bit_offset / 8; |
| 3959 |
src_bit = bit_offset % 8; |
| 3960 |
} |
| 3961 |
else |
| 3962 |
{ |
| 3963 |
src_byte = (bit_offset + (sindex * bps)) / 8; |
| 3964 |
src_bit = (bit_offset + (sindex * bps)) % 8; |
| 3965 |
} |
| 3966 |
|
| 3967 |
src = in + src_byte; |
| 3968 |
matchbits = maskbits << (32 - src_bit - bps); |
| 3969 |
if (little_endian) |
| 3970 |
buff1 = |
| 3971 |
(src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3]; |
| 3972 |
else |
| 3973 |
buff1 = |
| 3974 |
(src[3] << 24) | (src[2] << 16) | (src[1] << 8) | src[0]; |
| 3975 |
|
| 3976 |
if ((col == start) && (sindex == sample)) |
| 3977 |
buff2 = buff1 & ((uint32_t)-1) << (16 - shift); |
| 3978 |
|
| 3979 |
buff1 = (buff1 & matchbits) << (src_bit); |
| 3980 |
|
| 3981 |
if (ready_bits < 16) /* add another bps bits to the buffer */ |
| 3982 |
{ |
| 3983 |
buff2 = (buff2 | (buff1 >> ready_bits)); |
| 3984 |
} |
| 3985 |
else /* If we have a full buffer's worth, write it out */ |
| 3986 |
{ |
| 3987 |
bytebuff1 = (buff2 >> 24); |
| 3988 |
*dst++ = bytebuff1; |
| 3989 |
bytebuff2 = (buff2 >> 16); |
| 3990 |
*dst++ = bytebuff2; |
| 3991 |
ready_bits -= 16; |
| 3992 |
|
| 3993 |
/* shift in new bits */ |
| 3994 |
buff2 = ((buff2 << 16) | (buff1 >> ready_bits)); |
| 3995 |
} |
| 3996 |
ready_bits += bps; |
| 3997 |
} |
| 3998 |
} |
| 3999 |
|
| 4000 |
/* catch any trailing bits at the end of the line */ |
| 4001 |
while (ready_bits > 0) |
| 4002 |
{ |
| 4003 |
bytebuff1 = (buff2 >> 24); |
| 4004 |
*dst++ = bytebuff1; |
| 4005 |
|
| 4006 |
buff2 = (buff2 << 8); |
| 4007 |
bytebuff2 = bytebuff1; |
| 4008 |
ready_bits -= 8; |
| 4009 |
} |
| 4010 |
|
| 4011 |
return (0); |
| 4012 |
} /* end extractContigSamplesShifted24bits */ |
| 4013 |
|
| 4014 |
static int extractContigSamplesShifted32bits(uint8_t *in, uint8_t *out, |
| 4015 |
uint32_t cols, tsample_t sample, |
| 4016 |
uint16_t spp, uint16_t bps, |
| 4017 |
tsample_t count, uint32_t start, |
| 4018 |
uint32_t end, int shift) |
| 4019 |
{ |
| 4020 |
int ready_bits = 0, sindex = 0 /*, shift_width = 0 */; |
| 4021 |
uint32_t col, src_byte, src_bit, bit_offset, numcols; |
| 4022 |
uint32_t longbuff1 = 0, longbuff2 = 0; |
| 4023 |
uint64_t maskbits = 0, matchbits = 0; |
| 4024 |
uint64_t buff1 = 0, buff2 = 0, buff3 = 0; |
| 4025 |
uint8_t bytebuff1 = 0, bytebuff2 = 0, bytebuff3 = 0, bytebuff4 = 0; |
| 4026 |
uint8_t *src = in; |
| 4027 |
uint8_t *dst = out; |
| 4028 |
|
| 4029 |
if ((in == NULL) || (out == NULL)) |
| 4030 |
{ |
| 4031 |
TIFFError("extractContigSamplesShifted32bits", |
| 4032 |
"Invalid input or output buffer"); |
| 4033 |
return (1); |
| 4034 |
} |
| 4035 |
|
| 4036 |
/* Number of extracted columns shall be kept as (end-start + 1). Otherwise |
| 4037 |
* buffer-overflow might occur. 'start' and 'col' count from 0 to (cols-1) |
| 4038 |
* but 'end' is to be set one after the index of the last column to be |
| 4039 |
* copied! |
| 4040 |
*/ |
| 4041 |
if (end >= start) |
| 4042 |
numcols = end - start; |
| 4043 |
else |
| 4044 |
numcols = start - end; |
| 4045 |
if ((start > end) || (start > cols)) |
| 4046 |
{ |
| 4047 |
TIFFError("extractContigSamplesShifted32bits", |
| 4048 |
"Invalid start column value %" PRIu32 " ignored", start); |
| 4049 |
start = 0; |
| 4050 |
} |
| 4051 |
if ((end == 0) || (end > cols)) |
| 4052 |
{ |
| 4053 |
TIFFError("extractContigSamplesShifted32bits", |
| 4054 |
"Invalid end column value %" PRIu32 " ignored", end); |
| 4055 |
end = cols; |
| 4056 |
} |
| 4057 |
if ((end - start) > numcols) |
| 4058 |
{ |
| 4059 |
end = start + numcols; |
| 4060 |
} |
| 4061 |
|
| 4062 |
/* shift_width = ((bps + 7) / 8) + 1; */ |
| 4063 |
ready_bits = shift; |
| 4064 |
maskbits = (uint64_t)-1 >> (64 - bps); |
| 4065 |
for (col = start; col < end; col++) |
| 4066 |
{ |
| 4067 |
/* Compute src byte(s) and bits within byte(s) */ |
| 4068 |
bit_offset = col * bps * spp; |
| 4069 |
for (sindex = sample; (sindex < spp) && (sindex < (sample + count)); |
| 4070 |
sindex++) |
| 4071 |
{ |
| 4072 |
if (sindex == 0) |
| 4073 |
{ |
| 4074 |
src_byte = bit_offset / 8; |
| 4075 |
src_bit = bit_offset % 8; |
| 4076 |
} |
| 4077 |
else |
| 4078 |
{ |
| 4079 |
src_byte = (bit_offset + (sindex * bps)) / 8; |
| 4080 |
src_bit = (bit_offset + (sindex * bps)) % 8; |
| 4081 |
} |
| 4082 |
|
| 4083 |
src = in + src_byte; |
| 4084 |
matchbits = maskbits << (64 - src_bit - bps); |
| 4085 |
if (little_endian) |
| 4086 |
{ |
| 4087 |
longbuff1 = |
| 4088 |
(src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3]; |
| 4089 |
longbuff2 = longbuff1; |
| 4090 |
} |
| 4091 |
else |
| 4092 |
{ |
| 4093 |
longbuff1 = |
| 4094 |
(src[3] << 24) | (src[2] << 16) | (src[1] << 8) | src[0]; |
| 4095 |
longbuff2 = longbuff1; |
| 4096 |
} |
| 4097 |
|
| 4098 |
buff3 = ((uint64_t)longbuff1 << 32) | longbuff2; |
| 4099 |
if ((col == start) && (sindex == sample)) |
| 4100 |
buff2 = buff3 & ((uint64_t)-1) << (32 - shift); |
| 4101 |
|
| 4102 |
buff1 = (buff3 & matchbits) << (src_bit); |
| 4103 |
|
| 4104 |
if (ready_bits < 32) |
| 4105 |
{ /* add another bps bits to the buffer */ |
| 4106 |
buff2 = (buff2 | (buff1 >> ready_bits)); |
| 4107 |
} |
| 4108 |
else /* If we have a full buffer's worth, write it out */ |
| 4109 |
{ |
| 4110 |
bytebuff1 = (uint8_t)(buff2 >> 56); |
| 4111 |
*dst++ = bytebuff1; |
| 4112 |
bytebuff2 = (uint8_t)(buff2 >> 48); |
| 4113 |
*dst++ = bytebuff2; |
| 4114 |
bytebuff3 = (uint8_t)(buff2 >> 40); |
| 4115 |
*dst++ = bytebuff3; |
| 4116 |
bytebuff4 = (uint8_t)(buff2 >> 32); |
| 4117 |
*dst++ = bytebuff4; |
| 4118 |
ready_bits -= 32; |
| 4119 |
|
| 4120 |
/* shift in new bits */ |
| 4121 |
buff2 = ((buff2 << 32) | (buff1 >> ready_bits)); |
| 4122 |
} |
| 4123 |
ready_bits += bps; |
| 4124 |
} |
| 4125 |
} |
| 4126 |
while (ready_bits > 0) |
| 4127 |
{ |
| 4128 |
bytebuff1 = (buff2 >> 56); |
| 4129 |
*dst++ = bytebuff1; |
| 4130 |
buff2 = (buff2 << 8); |
| 4131 |
ready_bits -= 8; |
| 4132 |
} |
| 4133 |
|
| 4134 |
return (0); |
| 4135 |
} /* end extractContigSamplesShifted32bits */ |
| 4136 |
|
| 4137 |
static int extractContigSamplesToBuffer(uint8_t *out, uint8_t *in, |
| 4138 |
uint32_t rows, uint32_t cols, |
| 4139 |
tsample_t sample, uint16_t spp, |
| 4140 |
uint16_t bps, struct dump_opts *dump) |
| 4141 |
{ |
| 4142 |
int shift_width, bytes_per_sample, bytes_per_pixel; |
| 4143 |
uint32_t src_rowsize, src_offset, row, first_col = 0; |
| 4144 |
uint32_t dst_rowsize, dst_offset; |
| 4145 |
tsample_t count = 1; |
| 4146 |
uint8_t *src, *dst; |
| 4147 |
|
| 4148 |
bytes_per_sample = (bps + 7) / 8; |
| 4149 |
bytes_per_pixel = ((bps * spp) + 7) / 8; |
| 4150 |
if ((bps % 8) == 0) |
| 4151 |
shift_width = 0; |
| 4152 |
else |
| 4153 |
{ |
| 4154 |
if (bytes_per_pixel < (bytes_per_sample + 1)) |
| 4155 |
shift_width = bytes_per_pixel; |
| 4156 |
else |
| 4157 |
shift_width = bytes_per_sample + 1; |
| 4158 |
} |
| 4159 |
src_rowsize = ((bps * spp * cols) + 7) / 8; |
| 4160 |
dst_rowsize = ((bps * cols) + 7) / 8; |
| 4161 |
|
| 4162 |
if ((dump->outfile != NULL) && (dump->level == 4)) |
| 4163 |
{ |
| 4164 |
dump_info(dump->outfile, dump->format, "extractContigSamplesToBuffer", |
| 4165 |
"Sample %" PRIu32 ", %" PRIu32 " rows", sample + 1u, |
| 4166 |
rows + 1u); |
| 4167 |
} |
| 4168 |
for (row = 0; row < rows; row++) |
| 4169 |
{ |
| 4170 |
src_offset = row * src_rowsize; |
| 4171 |
dst_offset = row * dst_rowsize; |
| 4172 |
src = in + src_offset; |
| 4173 |
dst = out + dst_offset; |
| 4174 |
|
| 4175 |
/* pack the data into the scanline */ |
| 4176 |
switch (shift_width) |
| 4177 |
{ |
| 4178 |
case 0: |
| 4179 |
if (extractContigSamplesBytes(src, dst, cols, sample, spp, bps, |
| 4180 |
count, first_col, cols)) |
| 4181 |
return (1); |
| 4182 |
break; |
| 4183 |
case 1: |
| 4184 |
if (bps == 1) |
| 4185 |
{ |
| 4186 |
if (extractContigSamples8bits(src, dst, cols, sample, spp, |
| 4187 |
bps, count, first_col, cols)) |
| 4188 |
return (1); |
| 4189 |
break; |
| 4190 |
} |
| 4191 |
else if (extractContigSamples16bits(src, dst, cols, sample, spp, |
| 4192 |
bps, count, first_col, |
| 4193 |
cols)) |
| 4194 |
return (1); |
| 4195 |
break; |
| 4196 |
case 2: |
| 4197 |
if (extractContigSamples24bits(src, dst, cols, sample, spp, bps, |
| 4198 |
count, first_col, cols)) |
| 4199 |
return (1); |
| 4200 |
break; |
| 4201 |
case 3: |
| 4202 |
case 4: |
| 4203 |
case 5: |
| 4204 |
if (extractContigSamples32bits(src, dst, cols, sample, spp, bps, |
| 4205 |
count, first_col, cols)) |
| 4206 |
return (1); |
| 4207 |
break; |
| 4208 |
default: |
| 4209 |
TIFFError("extractContigSamplesToBuffer", |
| 4210 |
"Unsupported bit depth: %" PRIu16, bps); |
| 4211 |
return (1); |
| 4212 |
} |
| 4213 |
if ((dump->outfile != NULL) && (dump->level == 4)) |
| 4214 |
dump_buffer(dump->outfile, dump->format, 1, dst_rowsize, row, dst); |
| 4215 |
} |
| 4216 |
|
| 4217 |
return (0); |
| 4218 |
} /* end extractContigSamplesToBuffer */ |
| 4219 |
|
| 4220 |
static int extractContigSamplesToTileBuffer( |
| 4221 |
uint8_t *out, uint8_t *in, uint32_t rows, uint32_t cols, |
| 4222 |
uint32_t imagewidth, uint32_t tilewidth, tsample_t sample, uint16_t count, |
| 4223 |
uint16_t spp, uint16_t bps, struct dump_opts *dump) |
| 4224 |
{ |
| 4225 |
int shift_width, bytes_per_sample, bytes_per_pixel; |
| 4226 |
uint32_t src_rowsize, src_offset, row; |
| 4227 |
uint32_t dst_rowsize, dst_offset; |
| 4228 |
uint8_t *src, *dst; |
| 4229 |
|
| 4230 |
bytes_per_sample = (bps + 7) / 8; |
| 4231 |
bytes_per_pixel = ((bps * spp) + 7) / 8; |
| 4232 |
if ((bps % 8) == 0) |
| 4233 |
shift_width = 0; |
| 4234 |
else |
| 4235 |
{ |
| 4236 |
if (bytes_per_pixel < (bytes_per_sample + 1)) |
| 4237 |
shift_width = bytes_per_pixel; |
| 4238 |
else |
| 4239 |
shift_width = bytes_per_sample + 1; |
| 4240 |
} |
| 4241 |
|
| 4242 |
if ((dump->outfile != NULL) && (dump->level == 4)) |
| 4243 |
{ |
| 4244 |
dump_info( |
| 4245 |
dump->outfile, dump->format, "extractContigSamplesToTileBuffer", |
| 4246 |
"Sample %" PRIu32 ", %" PRIu32 " rows", sample + 1u, rows + 1u); |
| 4247 |
} |
| 4248 |
|
| 4249 |
src_rowsize = ((bps * spp * imagewidth) + 7) / 8; |
| 4250 |
dst_rowsize = ((bps * tilewidth * count) + 7) / 8; |
| 4251 |
|
| 4252 |
for (row = 0; row < rows; row++) |
| 4253 |
{ |
| 4254 |
src_offset = row * src_rowsize; |
| 4255 |
dst_offset = row * dst_rowsize; |
| 4256 |
src = in + src_offset; |
| 4257 |
dst = out + dst_offset; |
| 4258 |
|
| 4259 |
/* pack the data into the scanline */ |
| 4260 |
switch (shift_width) |
| 4261 |
{ |
| 4262 |
case 0: |
| 4263 |
if (extractContigSamplesBytes(src, dst, cols, sample, spp, bps, |
| 4264 |
count, 0, cols)) |
| 4265 |
return (1); |
| 4266 |
break; |
| 4267 |
case 1: |
| 4268 |
if (bps == 1) |
| 4269 |
{ |
| 4270 |
if (extractContigSamples8bits(src, dst, cols, sample, spp, |
| 4271 |
bps, count, 0, cols)) |
| 4272 |
return (1); |
| 4273 |
break; |
| 4274 |
} |
| 4275 |
else if (extractContigSamples16bits(src, dst, cols, sample, spp, |
| 4276 |
bps, count, 0, cols)) |
| 4277 |
return (1); |
| 4278 |
break; |
| 4279 |
case 2: |
| 4280 |
if (extractContigSamples24bits(src, dst, cols, sample, spp, bps, |
| 4281 |
count, 0, cols)) |
| 4282 |
return (1); |
| 4283 |
break; |
| 4284 |
case 3: |
| 4285 |
case 4: |
| 4286 |
case 5: |
| 4287 |
if (extractContigSamples32bits(src, dst, cols, sample, spp, bps, |
| 4288 |
count, 0, cols)) |
| 4289 |
return (1); |
| 4290 |
break; |
| 4291 |
default: |
| 4292 |
TIFFError("extractContigSamplesToTileBuffer", |
| 4293 |
"Unsupported bit depth: %" PRIu16, bps); |
| 4294 |
return (1); |
| 4295 |
} |
| 4296 |
if ((dump->outfile != NULL) && (dump->level == 4)) |
| 4297 |
dump_buffer(dump->outfile, dump->format, 1, dst_rowsize, row, dst); |
| 4298 |
} |
| 4299 |
|
| 4300 |
return (0); |
| 4301 |
} /* end extractContigSamplesToTileBuffer */ |
| 4302 |
|
| 4303 |
static int readContigStripsIntoBuffer(TIFF *in, uint8_t *buf) |
| 4304 |
{ |
| 4305 |
uint8_t *bufp = buf; |
| 4306 |
tmsize_t bytes_read = 0; |
| 4307 |
uint32_t strip, nstrips = TIFFNumberOfStrips(in); |
| 4308 |
tmsize_t stripsize = TIFFStripSize(in); |
| 4309 |
tmsize_t rows = 0; |
| 4310 |
uint32_t rps = TIFFGetFieldDefaulted(in, TIFFTAG_ROWSPERSTRIP, &rps); |
| 4311 |
tsize_t scanline_size = TIFFScanlineSize(in); |
| 4312 |
|
| 4313 |
if (scanline_size == 0) |
| 4314 |
{ |
| 4315 |
TIFFError("", "TIFF scanline size is zero!"); |
| 4316 |
return 0; |
| 4317 |
} |
| 4318 |
|
| 4319 |
for (strip = 0; strip < nstrips; strip++) |
| 4320 |
{ |
| 4321 |
bytes_read = TIFFReadEncodedStrip(in, strip, bufp, -1); |
| 4322 |
rows = bytes_read / scanline_size; |
| 4323 |
if ((strip < (nstrips - 1)) && (bytes_read != (int32_t)stripsize)) |
| 4324 |
TIFFError("", |
| 4325 |
"Strip %" PRIu32 ": read %" PRId64 |
| 4326 |
" bytes, strip size %" PRIu64, |
| 4327 |
strip + 1, (uint64_t)bytes_read, (uint64_t)stripsize); |
| 4328 |
|
| 4329 |
if (bytes_read < 0 && !ignore) |
| 4330 |
{ |
| 4331 |
TIFFError("", |
| 4332 |
"Error reading strip %" PRIu32 " after %" PRIu64 " rows", |
| 4333 |
strip, (uint64_t)rows); |
| 4334 |
return 0; |
| 4335 |
} |
| 4336 |
bufp += stripsize; |
| 4337 |
} |
| 4338 |
|
| 4339 |
return 1; |
| 4340 |
} /* end readContigStripsIntoBuffer */ |
| 4341 |
|
| 4342 |
static int combineSeparateSamplesBytes(unsigned char *srcbuffs[], |
| 4343 |
unsigned char *out, uint32_t cols, |
| 4344 |
uint32_t rows, uint16_t spp, |
| 4345 |
uint16_t bps, FILE *dumpfile, int format, |
| 4346 |
int level) |
| 4347 |
{ |
| 4348 |
int i, bytes_per_sample; |
| 4349 |
uint32_t row, col, col_offset, src_rowsize, dst_rowsize, row_offset; |
| 4350 |
unsigned char *src; |
| 4351 |
unsigned char *dst; |
| 4352 |
tsample_t s; |
| 4353 |
|
| 4354 |
src = srcbuffs[0]; |
| 4355 |
dst = out; |
| 4356 |
if ((src == NULL) || (dst == NULL)) |
| 4357 |
{ |
| 4358 |
TIFFError("combineSeparateSamplesBytes", "Invalid buffer address"); |
| 4359 |
return (1); |
| 4360 |
} |
| 4361 |
|
| 4362 |
bytes_per_sample = (bps + 7) / 8; |
| 4363 |
|
| 4364 |
src_rowsize = ((bps * cols) + 7) / 8; |
| 4365 |
dst_rowsize = ((bps * spp * cols) + 7) / 8; |
| 4366 |
for (row = 0; row < rows; row++) |
| 4367 |
{ |
| 4368 |
if ((dumpfile != NULL) && (level == 2)) |
| 4369 |
{ |
| 4370 |
for (s = 0; s < spp; s++) |
| 4371 |
{ |
| 4372 |
dump_info(dumpfile, format, "combineSeparateSamplesBytes", |
| 4373 |
"Input data, Sample %" PRIu16, s); |
| 4374 |
dump_buffer(dumpfile, format, 1, cols, row, |
| 4375 |
srcbuffs[s] + (row * src_rowsize)); |
| 4376 |
} |
| 4377 |
} |
| 4378 |
dst = out + (row * dst_rowsize); |
| 4379 |
row_offset = row * src_rowsize; |
| 4380 |
for (col = 0; col < cols; col++) |
| 4381 |
{ |
| 4382 |
col_offset = row_offset + (col * (bps / 8)); |
| 4383 |
for (s = 0; (s < spp) && (s < MAX_SAMPLES); s++) |
| 4384 |
{ |
| 4385 |
src = srcbuffs[s] + col_offset; |
| 4386 |
for (i = 0; i < bytes_per_sample; i++) |
| 4387 |
*(dst + i) = *(src + i); |
| 4388 |
dst += bytes_per_sample; |
| 4389 |
} |
| 4390 |
} |
| 4391 |
|
| 4392 |
if ((dumpfile != NULL) && (level == 2)) |
| 4393 |
{ |
| 4394 |
dump_info(dumpfile, format, "combineSeparateSamplesBytes", |
| 4395 |
"Output data, combined samples"); |
| 4396 |
dump_buffer(dumpfile, format, 1, dst_rowsize, row, |
| 4397 |
out + (row * dst_rowsize)); |
| 4398 |
} |
| 4399 |
} |
| 4400 |
|
| 4401 |
return (0); |
| 4402 |
} /* end combineSeparateSamplesBytes */ |
| 4403 |
|
| 4404 |
static int combineSeparateSamples8bits(uint8_t *in[], uint8_t *out, |
| 4405 |
uint32_t cols, uint32_t rows, |
| 4406 |
uint16_t spp, uint16_t bps, |
| 4407 |
FILE *dumpfile, int format, int level) |
| 4408 |
{ |
| 4409 |
int ready_bits = 0; |
| 4410 |
/* int bytes_per_sample = 0; */ |
| 4411 |
uint32_t src_rowsize, dst_rowsize, src_offset; |
| 4412 |
uint32_t bit_offset; |
| 4413 |
uint32_t row, col, src_byte = 0, src_bit = 0; |
| 4414 |
uint8_t maskbits = 0, matchbits = 0; |
| 4415 |
uint8_t buff1 = 0, buff2 = 0; |
| 4416 |
tsample_t s; |
| 4417 |
unsigned char *src = in[0]; |
| 4418 |
unsigned char *dst = out; |
| 4419 |
char action[32]; |
| 4420 |
|
| 4421 |
if ((src == NULL) || (dst == NULL)) |
| 4422 |
{ |
| 4423 |
TIFFError("combineSeparateSamples8bits", |
| 4424 |
"Invalid input or output buffer"); |
| 4425 |
return (1); |
| 4426 |
} |
| 4427 |
|
| 4428 |
/* bytes_per_sample = (bps + 7) / 8; */ |
| 4429 |
src_rowsize = ((bps * cols) + 7) / 8; |
| 4430 |
dst_rowsize = ((bps * cols * spp) + 7) / 8; |
| 4431 |
maskbits = (uint8_t)-1 >> (8 - bps); |
| 4432 |
|
| 4433 |
for (row = 0; row < rows; row++) |
| 4434 |
{ |
| 4435 |
ready_bits = 0; |
| 4436 |
buff1 = buff2 = 0; |
| 4437 |
dst = out + (row * dst_rowsize); |
| 4438 |
src_offset = row * src_rowsize; |
| 4439 |
for (col = 0; col < cols; col++) |
| 4440 |
{ |
| 4441 |
/* Compute src byte(s) and bits within byte(s) */ |
| 4442 |
bit_offset = col * bps; |
| 4443 |
src_byte = bit_offset / 8; |
| 4444 |
src_bit = bit_offset % 8; |
| 4445 |
|
| 4446 |
matchbits = maskbits << (8 - src_bit - bps); |
| 4447 |
/* load up next sample from each plane */ |
| 4448 |
for (s = 0; (s < spp) && (s < MAX_SAMPLES); s++) |
| 4449 |
{ |
| 4450 |
src = in[s] + src_offset + src_byte; |
| 4451 |
buff1 = ((*src) & matchbits) << (src_bit); |
| 4452 |
|
| 4453 |
/* If we have a full buffer's worth, write it out */ |
| 4454 |
if (ready_bits >= 8) |
| 4455 |
{ |
| 4456 |
*dst++ = buff2; |
| 4457 |
buff2 = buff1; |
| 4458 |
ready_bits -= 8; |
| 4459 |
strcpy(action, "Flush"); |
| 4460 |
} |
| 4461 |
else |
| 4462 |
{ |
| 4463 |
buff2 = (buff2 | (buff1 >> ready_bits)); |
| 4464 |
strcpy(action, "Update"); |
| 4465 |
} |
| 4466 |
ready_bits += bps; |
| 4467 |
|
| 4468 |
if ((dumpfile != NULL) && (level == 3)) |
| 4469 |
{ |
| 4470 |
dump_info(dumpfile, format, "", |
| 4471 |
"Row %3" PRIu32 ", Col %3" PRIu32 |
| 4472 |
", Samples %" PRIu16 ", Src byte offset %3" PRIu32 |
| 4473 |
" bit offset %2" PRIu32 " Dst offset %3td", |
| 4474 |
row + 1u, col + 1u, s, src_byte, src_bit, |
| 4475 |
dst - out); |
| 4476 |
dump_byte(dumpfile, format, "Match bits", matchbits); |
| 4477 |
dump_byte(dumpfile, format, "Src bits", *src); |
| 4478 |
dump_byte(dumpfile, format, "Buff1 bits", buff1); |
| 4479 |
dump_byte(dumpfile, format, "Buff2 bits", buff2); |
| 4480 |
dump_info(dumpfile, format, "", "%s", action); |
| 4481 |
} |
| 4482 |
} |
| 4483 |
} |
| 4484 |
|
| 4485 |
if (ready_bits > 0) |
| 4486 |
{ |
| 4487 |
buff1 = (buff2 & ((unsigned int)255 << (8 - ready_bits))); |
| 4488 |
*dst++ = buff1; |
| 4489 |
if ((dumpfile != NULL) && (level == 3)) |
| 4490 |
{ |
| 4491 |
dump_info(dumpfile, format, "", |
| 4492 |
"Row %3" PRIu32 ", Col %3" PRIu32 |
| 4493 |
", Src byte offset %3" PRIu32 " bit offset %2" PRIu32 |
| 4494 |
" Dst offset %3td", |
| 4495 |
row + 1u, col + 1u, src_byte, src_bit, dst - out); |
| 4496 |
dump_byte(dumpfile, format, "Final bits", buff1); |
| 4497 |
} |
| 4498 |
} |
| 4499 |
|
| 4500 |
if ((dumpfile != NULL) && (level >= 2)) |
| 4501 |
{ |
| 4502 |
dump_info(dumpfile, format, "combineSeparateSamples8bits", |
| 4503 |
"Output data"); |
| 4504 |
dump_buffer(dumpfile, format, 1, dst_rowsize, row, |
| 4505 |
out + (row * dst_rowsize)); |
| 4506 |
} |
| 4507 |
} |
| 4508 |
|
| 4509 |
return (0); |
| 4510 |
} /* end combineSeparateSamples8bits */ |
| 4511 |
|
| 4512 |
static int combineSeparateSamples16bits(uint8_t *in[], uint8_t *out, |
| 4513 |
uint32_t cols, uint32_t rows, |
| 4514 |
uint16_t spp, uint16_t bps, |
| 4515 |
FILE *dumpfile, int format, int level) |
| 4516 |
{ |
| 4517 |
int ready_bits = 0 /*, bytes_per_sample = 0 */; |
| 4518 |
uint32_t src_rowsize, dst_rowsize; |
| 4519 |
uint32_t bit_offset, src_offset; |
| 4520 |
uint32_t row, col, src_byte = 0, src_bit = 0; |
| 4521 |
uint16_t maskbits = 0, matchbits = 0; |
| 4522 |
uint16_t buff1 = 0, buff2 = 0; |
| 4523 |
uint8_t bytebuff = 0; |
| 4524 |
tsample_t s; |
| 4525 |
unsigned char *src = in[0]; |
| 4526 |
unsigned char *dst = out; |
| 4527 |
char action[8]; |
| 4528 |
|
| 4529 |
if ((src == NULL) || (dst == NULL)) |
| 4530 |
{ |
| 4531 |
TIFFError("combineSeparateSamples16bits", |
| 4532 |
"Invalid input or output buffer"); |
| 4533 |
return (1); |
| 4534 |
} |
| 4535 |
|
| 4536 |
/* bytes_per_sample = (bps + 7) / 8; */ |
| 4537 |
src_rowsize = ((bps * cols) + 7) / 8; |
| 4538 |
dst_rowsize = ((bps * cols * spp) + 7) / 8; |
| 4539 |
maskbits = (uint16_t)-1 >> (16 - bps); |
| 4540 |
|
| 4541 |
for (row = 0; row < rows; row++) |
| 4542 |
{ |
| 4543 |
ready_bits = 0; |
| 4544 |
buff1 = buff2 = 0; |
| 4545 |
dst = out + (row * dst_rowsize); |
| 4546 |
src_offset = row * src_rowsize; |
| 4547 |
for (col = 0; col < cols; col++) |
| 4548 |
{ |
| 4549 |
/* Compute src byte(s) and bits within byte(s) */ |
| 4550 |
bit_offset = col * bps; |
| 4551 |
src_byte = bit_offset / 8; |
| 4552 |
src_bit = bit_offset % 8; |
| 4553 |
|
| 4554 |
matchbits = maskbits << (16 - src_bit - bps); |
| 4555 |
for (s = 0; (s < spp) && (s < MAX_SAMPLES); s++) |
| 4556 |
{ |
| 4557 |
src = in[s] + src_offset + src_byte; |
| 4558 |
if (little_endian) |
| 4559 |
buff1 = (src[0] << 8) | src[1]; |
| 4560 |
else |
| 4561 |
buff1 = (src[1] << 8) | src[0]; |
| 4562 |
|
| 4563 |
buff1 = (buff1 & matchbits) << (src_bit); |
| 4564 |
|
| 4565 |
/* If we have a full buffer's worth, write it out */ |
| 4566 |
if (ready_bits >= 8) |
| 4567 |
{ |
| 4568 |
bytebuff = (buff2 >> 8); |
| 4569 |
*dst++ = bytebuff; |
| 4570 |
ready_bits -= 8; |
| 4571 |
/* shift in new bits */ |
| 4572 |
buff2 = ((buff2 << 8) | (buff1 >> ready_bits)); |
| 4573 |
strcpy(action, "Flush"); |
| 4574 |
} |
| 4575 |
else |
| 4576 |
{ /* add another bps bits to the buffer */ |
| 4577 |
bytebuff = 0; |
| 4578 |
buff2 = (buff2 | (buff1 >> ready_bits)); |
| 4579 |
strcpy(action, "Update"); |
| 4580 |
} |
| 4581 |
ready_bits += bps; |
| 4582 |
|
| 4583 |
if ((dumpfile != NULL) && (level == 3)) |
| 4584 |
{ |
| 4585 |
dump_info(dumpfile, format, "", |
| 4586 |
"Row %3" PRIu32 ", Col %3" PRIu32 |
| 4587 |
", Samples %" PRIu16 ", Src byte offset %3" PRIu32 |
| 4588 |
" bit offset %2" PRIu32 " Dst offset %3td", |
| 4589 |
row + 1u, col + 1u, s, src_byte, src_bit, |
| 4590 |
dst - out); |
| 4591 |
|
| 4592 |
dump_short(dumpfile, format, "Match bits", matchbits); |
| 4593 |
dump_data(dumpfile, format, "Src bits", src, 2); |
| 4594 |
dump_short(dumpfile, format, "Buff1 bits", buff1); |
| 4595 |
dump_short(dumpfile, format, "Buff2 bits", buff2); |
| 4596 |
dump_byte(dumpfile, format, "Write byte", bytebuff); |
| 4597 |
dump_info(dumpfile, format, "", "Ready bits: %d, %s", |
| 4598 |
ready_bits, action); |
| 4599 |
} |
| 4600 |
} |
| 4601 |
} |
| 4602 |
|
| 4603 |
/* catch any trailing bits at the end of the line */ |
| 4604 |
if (ready_bits > 0) |
| 4605 |
{ |
| 4606 |
bytebuff = (buff2 >> 8); |
| 4607 |
*dst++ = bytebuff; |
| 4608 |
if ((dumpfile != NULL) && (level == 3)) |
| 4609 |
{ |
| 4610 |
dump_info(dumpfile, format, "", |
| 4611 |
"Row %3d, Col %3d, Src byte offset %3d bit offset " |
| 4612 |
"%2d Dst offset %3zd", |
| 4613 |
row + 1, col + 1, src_byte, src_bit, dst - out); |
| 4614 |
dump_byte(dumpfile, format, "Final bits", bytebuff); |
| 4615 |
} |
| 4616 |
} |
| 4617 |
|
| 4618 |
if ((dumpfile != NULL) && (level == 2)) |
| 4619 |
{ |
| 4620 |
dump_info(dumpfile, format, "combineSeparateSamples16bits", |
| 4621 |
"Output data"); |
| 4622 |
dump_buffer(dumpfile, format, 1, dst_rowsize, row, |
| 4623 |
out + (row * dst_rowsize)); |
| 4624 |
} |
| 4625 |
} |
| 4626 |
|
| 4627 |
return (0); |
| 4628 |
} /* end combineSeparateSamples16bits */ |
| 4629 |
|
| 4630 |
static int combineSeparateSamples24bits(uint8_t *in[], uint8_t *out, |
| 4631 |
uint32_t cols, uint32_t rows, |
| 4632 |
uint16_t spp, uint16_t bps, |
| 4633 |
FILE *dumpfile, int format, int level) |
| 4634 |
{ |
| 4635 |
int ready_bits = 0 /*, bytes_per_sample = 0 */; |
| 4636 |
uint32_t src_rowsize, dst_rowsize; |
| 4637 |
uint32_t bit_offset, src_offset; |
| 4638 |
uint32_t row, col, src_byte = 0, src_bit = 0; |
| 4639 |
uint32_t maskbits = 0, matchbits = 0; |
| 4640 |
uint32_t buff1 = 0, buff2 = 0; |
| 4641 |
uint8_t bytebuff1 = 0, bytebuff2 = 0; |
| 4642 |
tsample_t s; |
| 4643 |
unsigned char *src = in[0]; |
| 4644 |
unsigned char *dst = out; |
| 4645 |
char action[8]; |
| 4646 |
|
| 4647 |
if ((src == NULL) || (dst == NULL)) |
| 4648 |
{ |
| 4649 |
TIFFError("combineSeparateSamples24bits", |
| 4650 |
"Invalid input or output buffer"); |
| 4651 |
return (1); |
| 4652 |
} |
| 4653 |
|
| 4654 |
/* bytes_per_sample = (bps + 7) / 8; */ |
| 4655 |
src_rowsize = ((bps * cols) + 7) / 8; |
| 4656 |
dst_rowsize = ((bps * cols * spp) + 7) / 8; |
| 4657 |
maskbits = (uint32_t)-1 >> (32 - bps); |
| 4658 |
|
| 4659 |
for (row = 0; row < rows; row++) |
| 4660 |
{ |
| 4661 |
ready_bits = 0; |
| 4662 |
buff1 = buff2 = 0; |
| 4663 |
dst = out + (row * dst_rowsize); |
| 4664 |
src_offset = row * src_rowsize; |
| 4665 |
for (col = 0; col < cols; col++) |
| 4666 |
{ |
| 4667 |
/* Compute src byte(s) and bits within byte(s) */ |
| 4668 |
bit_offset = col * bps; |
| 4669 |
src_byte = bit_offset / 8; |
| 4670 |
src_bit = bit_offset % 8; |
| 4671 |
|
| 4672 |
matchbits = maskbits << (32 - src_bit - bps); |
| 4673 |
for (s = 0; (s < spp) && (s < MAX_SAMPLES); s++) |
| 4674 |
{ |
| 4675 |
src = in[s] + src_offset + src_byte; |
| 4676 |
if (little_endian) |
| 4677 |
buff1 = ((uint32_t)src[0] << 24) | |
| 4678 |
((uint32_t)src[1] << 16) | ((uint32_t)src[2] << 8) | |
| 4679 |
(uint32_t)src[3]; |
| 4680 |
else |
| 4681 |
buff1 = ((uint32_t)src[3] << 24) | |
| 4682 |
((uint32_t)src[2] << 16) | ((uint32_t)src[1] << 8) | |
| 4683 |
(uint32_t)src[0]; |
| 4684 |
buff1 = (buff1 & matchbits) << (src_bit); |
| 4685 |
|
| 4686 |
/* If we have a full buffer's worth, write it out */ |
| 4687 |
if (ready_bits >= 16) |
| 4688 |
{ |
| 4689 |
bytebuff1 = (buff2 >> 24); |
| 4690 |
*dst++ = bytebuff1; |
| 4691 |
bytebuff2 = (buff2 >> 16); |
| 4692 |
*dst++ = bytebuff2; |
| 4693 |
ready_bits -= 16; |
| 4694 |
|
| 4695 |
/* shift in new bits */ |
| 4696 |
buff2 = ((buff2 << 16) | (buff1 >> ready_bits)); |
| 4697 |
strcpy(action, "Flush"); |
| 4698 |
} |
| 4699 |
else |
| 4700 |
{ /* add another bps bits to the buffer */ |
| 4701 |
bytebuff1 = bytebuff2 = 0; |
| 4702 |
buff2 = (buff2 | (buff1 >> ready_bits)); |
| 4703 |
strcpy(action, "Update"); |
| 4704 |
} |
| 4705 |
ready_bits += bps; |
| 4706 |
|
| 4707 |
if ((dumpfile != NULL) && (level == 3)) |
| 4708 |
{ |
| 4709 |
dump_info(dumpfile, format, "", |
| 4710 |
"Row %3" PRIu32 ", Col %3" PRIu32 |
| 4711 |
", Samples %" PRIu16 ", Src byte offset %3" PRIu32 |
| 4712 |
" bit offset %2" PRIu32 " Dst offset %3td", |
| 4713 |
row + 1u, col + 1u, s, src_byte, src_bit, |
| 4714 |
dst - out); |
| 4715 |
dump_long(dumpfile, format, "Match bits ", matchbits); |
| 4716 |
dump_data(dumpfile, format, "Src bits ", src, 4); |
| 4717 |
dump_long(dumpfile, format, "Buff1 bits ", buff1); |
| 4718 |
dump_long(dumpfile, format, "Buff2 bits ", buff2); |
| 4719 |
dump_byte(dumpfile, format, "Write bits1", bytebuff1); |
| 4720 |
dump_byte(dumpfile, format, "Write bits2", bytebuff2); |
| 4721 |
dump_info(dumpfile, format, "", "Ready bits: %d, %s", |
| 4722 |
ready_bits, action); |
| 4723 |
} |
| 4724 |
} |
| 4725 |
} |
| 4726 |
|
| 4727 |
/* catch any trailing bits at the end of the line */ |
| 4728 |
while (ready_bits > 0) |
| 4729 |
{ |
| 4730 |
bytebuff1 = (buff2 >> 24); |
| 4731 |
*dst++ = bytebuff1; |
| 4732 |
|
| 4733 |
buff2 = (buff2 << 8); |
| 4734 |
bytebuff2 = bytebuff1; |
| 4735 |
ready_bits -= 8; |
| 4736 |
} |
| 4737 |
|
| 4738 |
if ((dumpfile != NULL) && (level == 3)) |
| 4739 |
{ |
| 4740 |
dump_info(dumpfile, format, "", |
| 4741 |
"Row %3d, Col %3d, Src byte offset %3d bit offset %2d " |
| 4742 |
"Dst offset %3zd", |
| 4743 |
row + 1, col + 1, src_byte, src_bit, dst - out); |
| 4744 |
|
| 4745 |
dump_long(dumpfile, format, "Match bits ", matchbits); |
| 4746 |
dump_data(dumpfile, format, "Src bits ", src, 4); |
| 4747 |
dump_long(dumpfile, format, "Buff1 bits ", buff1); |
| 4748 |
dump_long(dumpfile, format, "Buff2 bits ", buff2); |
| 4749 |
dump_byte(dumpfile, format, "Write bits1", bytebuff1); |
| 4750 |
dump_byte(dumpfile, format, "Write bits2", bytebuff2); |
| 4751 |
dump_info(dumpfile, format, "", "Ready bits: %2d", ready_bits); |
| 4752 |
} |
| 4753 |
|
| 4754 |
if ((dumpfile != NULL) && (level == 2)) |
| 4755 |
{ |
| 4756 |
dump_info(dumpfile, format, "combineSeparateSamples24bits", |
| 4757 |
"Output data"); |
| 4758 |
dump_buffer(dumpfile, format, 1, dst_rowsize, row, |
| 4759 |
out + (row * dst_rowsize)); |
| 4760 |
} |
| 4761 |
} |
| 4762 |
|
| 4763 |
return (0); |
| 4764 |
} /* end combineSeparateSamples24bits */ |
| 4765 |
|
| 4766 |
static int combineSeparateSamples32bits(uint8_t *in[], uint8_t *out, |
| 4767 |
uint32_t cols, uint32_t rows, |
| 4768 |
uint16_t spp, uint16_t bps, |
| 4769 |
FILE *dumpfile, int format, int level) |
| 4770 |
{ |
| 4771 |
int ready_bits = 0 /*, bytes_per_sample = 0, shift_width = 0 */; |
| 4772 |
uint32_t src_rowsize, dst_rowsize, bit_offset, src_offset; |
| 4773 |
uint32_t src_byte = 0, src_bit = 0; |
| 4774 |
uint32_t row, col; |
| 4775 |
uint32_t longbuff1 = 0, longbuff2 = 0; |
| 4776 |
uint64_t maskbits = 0, matchbits = 0; |
| 4777 |
uint64_t buff1 = 0, buff2 = 0, buff3 = 0; |
| 4778 |
uint8_t bytebuff1 = 0, bytebuff2 = 0, bytebuff3 = 0, bytebuff4 = 0; |
| 4779 |
tsample_t s; |
| 4780 |
unsigned char *src = in[0]; |
| 4781 |
unsigned char *dst = out; |
| 4782 |
char action[8]; |
| 4783 |
|
| 4784 |
if ((src == NULL) || (dst == NULL)) |
| 4785 |
{ |
| 4786 |
TIFFError("combineSeparateSamples32bits", |
| 4787 |
"Invalid input or output buffer"); |
| 4788 |
return (1); |
| 4789 |
} |
| 4790 |
|
| 4791 |
/* bytes_per_sample = (bps + 7) / 8; */ |
| 4792 |
src_rowsize = ((bps * cols) + 7) / 8; |
| 4793 |
dst_rowsize = ((bps * cols * spp) + 7) / 8; |
| 4794 |
maskbits = (uint64_t)-1 >> (64 - bps); |
| 4795 |
/* shift_width = ((bps + 7) / 8) + 1; */ |
| 4796 |
|
| 4797 |
for (row = 0; row < rows; row++) |
| 4798 |
{ |
| 4799 |
ready_bits = 0; |
| 4800 |
buff1 = buff2 = 0; |
| 4801 |
dst = out + (row * dst_rowsize); |
| 4802 |
src_offset = row * src_rowsize; |
| 4803 |
for (col = 0; col < cols; col++) |
| 4804 |
{ |
| 4805 |
/* Compute src byte(s) and bits within byte(s) */ |
| 4806 |
bit_offset = col * bps; |
| 4807 |
src_byte = bit_offset / 8; |
| 4808 |
src_bit = bit_offset % 8; |
| 4809 |
|
| 4810 |
matchbits = maskbits << (64 - src_bit - bps); |
| 4811 |
for (s = 0; (s < spp) && (s < MAX_SAMPLES); s++) |
| 4812 |
{ |
| 4813 |
src = in[s] + src_offset + src_byte; |
| 4814 |
if (little_endian) |
| 4815 |
{ |
| 4816 |
longbuff1 = (src[0] << 24) | (src[1] << 16) | |
| 4817 |
(src[2] << 8) | src[3]; |
| 4818 |
longbuff2 = longbuff1; |
| 4819 |
} |
| 4820 |
else |
| 4821 |
{ |
| 4822 |
longbuff1 = (src[3] << 24) | (src[2] << 16) | |
| 4823 |
(src[1] << 8) | src[0]; |
| 4824 |
longbuff2 = longbuff1; |
| 4825 |
} |
| 4826 |
buff3 = ((uint64_t)longbuff1 << 32) | longbuff2; |
| 4827 |
buff1 = (buff3 & matchbits) << (src_bit); |
| 4828 |
|
| 4829 |
/* If we have a full buffer's worth, write it out */ |
| 4830 |
if (ready_bits >= 32) |
| 4831 |
{ |
| 4832 |
bytebuff1 = (uint8_t)(buff2 >> 56); |
| 4833 |
*dst++ = bytebuff1; |
| 4834 |
bytebuff2 = (uint8_t)(buff2 >> 48); |
| 4835 |
*dst++ = bytebuff2; |
| 4836 |
bytebuff3 = (uint8_t)(buff2 >> 40); |
| 4837 |
*dst++ = bytebuff3; |
| 4838 |
bytebuff4 = (uint8_t)(buff2 >> 32); |
| 4839 |
*dst++ = bytebuff4; |
| 4840 |
ready_bits -= 32; |
| 4841 |
|
| 4842 |
/* shift in new bits */ |
| 4843 |
buff2 = ((buff2 << 32) | (buff1 >> ready_bits)); |
| 4844 |
strcpy(action, "Flush"); |
| 4845 |
} |
| 4846 |
else |
| 4847 |
{ /* add another bps bits to the buffer */ |
| 4848 |
bytebuff1 = bytebuff2 = bytebuff3 = bytebuff4 = 0; |
| 4849 |
buff2 = (buff2 | (buff1 >> ready_bits)); |
| 4850 |
strcpy(action, "Update"); |
| 4851 |
} |
| 4852 |
ready_bits += bps; |
| 4853 |
|
| 4854 |
if ((dumpfile != NULL) && (level == 3)) |
| 4855 |
{ |
| 4856 |
dump_info(dumpfile, format, "", |
| 4857 |
"Row %3" PRIu32 ", Col %3" PRIu32 |
| 4858 |
", Sample %" PRIu16 ", Src byte offset %3" PRIu32 |
| 4859 |
" bit offset %2" PRIu32 " Dst offset %3td", |
| 4860 |
row + 1u, col + 1u, s, src_byte, src_bit, |
| 4861 |
dst - out); |
| 4862 |
dump_wide(dumpfile, format, "Match bits ", matchbits); |
| 4863 |
dump_data(dumpfile, format, "Src bits ", src, 8); |
| 4864 |
dump_wide(dumpfile, format, "Buff1 bits ", buff1); |
| 4865 |
dump_wide(dumpfile, format, "Buff2 bits ", buff2); |
| 4866 |
dump_info(dumpfile, format, "", "Ready bits: %d, %s", |
| 4867 |
ready_bits, action); |
| 4868 |
} |
| 4869 |
} |
| 4870 |
} |
| 4871 |
while (ready_bits > 0) |
| 4872 |
{ |
| 4873 |
bytebuff1 = (buff2 >> 56); |
| 4874 |
*dst++ = bytebuff1; |
| 4875 |
buff2 = (buff2 << 8); |
| 4876 |
ready_bits -= 8; |
| 4877 |
} |
| 4878 |
|
| 4879 |
if ((dumpfile != NULL) && (level == 3)) |
| 4880 |
{ |
| 4881 |
dump_info(dumpfile, format, "", |
| 4882 |
"Row %3d, Col %3d, Src byte offset %3d bit offset %2d " |
| 4883 |
"Dst offset %3zd", |
| 4884 |
row + 1, col + 1, src_byte, src_bit, dst - out); |
| 4885 |
|
| 4886 |
dump_wide(dumpfile, format, "Match bits ", matchbits); |
| 4887 |
dump_data(dumpfile, format, "Src bits ", src, 4); |
| 4888 |
dump_wide(dumpfile, format, "Buff1 bits ", buff1); |
| 4889 |
dump_wide(dumpfile, format, "Buff2 bits ", buff2); |
| 4890 |
dump_byte(dumpfile, format, "Write bits1", bytebuff1); |
| 4891 |
dump_byte(dumpfile, format, "Write bits2", bytebuff2); |
| 4892 |
dump_info(dumpfile, format, "", "Ready bits: %2d", ready_bits); |
| 4893 |
} |
| 4894 |
|
| 4895 |
if ((dumpfile != NULL) && (level == 2)) |
| 4896 |
{ |
| 4897 |
dump_info(dumpfile, format, "combineSeparateSamples32bits", |
| 4898 |
"Output data"); |
| 4899 |
dump_buffer(dumpfile, format, 1, dst_rowsize, row, out); |
| 4900 |
} |
| 4901 |
} |
| 4902 |
|
| 4903 |
return (0); |
| 4904 |
} /* end combineSeparateSamples32bits */ |
| 4905 |
|
| 4906 |
static int combineSeparateTileSamplesBytes(unsigned char *srcbuffs[], |
| 4907 |
unsigned char *out, uint32_t cols, |
| 4908 |
uint32_t rows, uint32_t imagewidth, |
| 4909 |
uint32_t tw, uint16_t spp, |
| 4910 |
uint16_t bps, FILE *dumpfile, |
| 4911 |
int format, int level) |
| 4912 |
{ |
| 4913 |
int i, bytes_per_sample; |
| 4914 |
uint32_t row, col, col_offset, src_rowsize, dst_rowsize, src_offset; |
| 4915 |
unsigned char *src; |
| 4916 |
unsigned char *dst; |
| 4917 |
tsample_t s; |
| 4918 |
|
| 4919 |
src = srcbuffs[0]; |
| 4920 |
dst = out; |
| 4921 |
if ((src == NULL) || (dst == NULL)) |
| 4922 |
{ |
| 4923 |
TIFFError("combineSeparateTileSamplesBytes", "Invalid buffer address"); |
| 4924 |
return (1); |
| 4925 |
} |
| 4926 |
|
| 4927 |
bytes_per_sample = (bps + 7) / 8; |
| 4928 |
src_rowsize = ((bps * tw) + 7) / 8; |
| 4929 |
dst_rowsize = imagewidth * bytes_per_sample * spp; |
| 4930 |
for (row = 0; row < rows; row++) |
| 4931 |
{ |
| 4932 |
if ((dumpfile != NULL) && (level == 2)) |
| 4933 |
{ |
| 4934 |
for (s = 0; s < spp; s++) |
| 4935 |
{ |
| 4936 |
dump_info(dumpfile, format, "combineSeparateTileSamplesBytes", |
| 4937 |
"Input data, Sample %" PRIu16, s); |
| 4938 |
dump_buffer(dumpfile, format, 1, cols, row, |
| 4939 |
srcbuffs[s] + (row * src_rowsize)); |
| 4940 |
} |
| 4941 |
} |
| 4942 |
dst = out + (row * dst_rowsize); |
| 4943 |
src_offset = row * src_rowsize; |
| 4944 |
#ifdef DEVELMODE |
| 4945 |
TIFFError("", "Tile row %4d, Src offset %6d Dst offset %6zd", row, |
| 4946 |
src_offset, dst - out); |
| 4947 |
#endif |
| 4948 |
for (col = 0; col < cols; col++) |
| 4949 |
{ |
| 4950 |
col_offset = src_offset + (col * (bps / 8)); |
| 4951 |
for (s = 0; (s < spp) && (s < MAX_SAMPLES); s++) |
| 4952 |
{ |
| 4953 |
src = srcbuffs[s] + col_offset; |
| 4954 |
for (i = 0; i < bytes_per_sample; i++) |
| 4955 |
*(dst + i) = *(src + i); |
| 4956 |
dst += bytes_per_sample; |
| 4957 |
} |
| 4958 |
} |
| 4959 |
|
| 4960 |
if ((dumpfile != NULL) && (level == 2)) |
| 4961 |
{ |
| 4962 |
dump_info(dumpfile, format, "combineSeparateTileSamplesBytes", |
| 4963 |
"Output data, combined samples"); |
| 4964 |
dump_buffer(dumpfile, format, 1, dst_rowsize, row, |
| 4965 |
out + (row * dst_rowsize)); |
| 4966 |
} |
| 4967 |
} |
| 4968 |
|
| 4969 |
return (0); |
| 4970 |
} /* end combineSeparateTileSamplesBytes */ |
| 4971 |
|
| 4972 |
static int combineSeparateTileSamples8bits(uint8_t *in[], uint8_t *out, |
| 4973 |
uint32_t cols, uint32_t rows, |
| 4974 |
uint32_t imagewidth, uint32_t tw, |
| 4975 |
uint16_t spp, uint16_t bps, |
| 4976 |
FILE *dumpfile, int format, |
| 4977 |
int level) |
| 4978 |
{ |
| 4979 |
int ready_bits = 0; |
| 4980 |
uint32_t src_rowsize, dst_rowsize, src_offset; |
| 4981 |
uint32_t bit_offset; |
| 4982 |
uint32_t row, col, src_byte = 0, src_bit = 0; |
| 4983 |
uint8_t maskbits = 0, matchbits = 0; |
| 4984 |
uint8_t buff1 = 0, buff2 = 0; |
| 4985 |
tsample_t s; |
| 4986 |
unsigned char *src = in[0]; |
| 4987 |
unsigned char *dst = out; |
| 4988 |
char action[32]; |
| 4989 |
|
| 4990 |
if ((src == NULL) || (dst == NULL)) |
| 4991 |
{ |
| 4992 |
TIFFError("combineSeparateTileSamples8bits", |
| 4993 |
"Invalid input or output buffer"); |
| 4994 |
return (1); |
| 4995 |
} |
| 4996 |
|
| 4997 |
src_rowsize = ((bps * tw) + 7) / 8; |
| 4998 |
dst_rowsize = ((imagewidth * bps * spp) + 7) / 8; |
| 4999 |
maskbits = (uint8_t)-1 >> (8 - bps); |
| 5000 |
|
| 5001 |
for (row = 0; row < rows; row++) |
| 5002 |
{ |
| 5003 |
ready_bits = 0; |
| 5004 |
buff1 = buff2 = 0; |
| 5005 |
dst = out + (row * dst_rowsize); |
| 5006 |
src_offset = row * src_rowsize; |
| 5007 |
for (col = 0; col < cols; col++) |
| 5008 |
{ |
| 5009 |
/* Compute src byte(s) and bits within byte(s) */ |
| 5010 |
bit_offset = col * bps; |
| 5011 |
src_byte = bit_offset / 8; |
| 5012 |
src_bit = bit_offset % 8; |
| 5013 |
|
| 5014 |
matchbits = maskbits << (8 - src_bit - bps); |
| 5015 |
/* load up next sample from each plane */ |
| 5016 |
for (s = 0; (s < spp) && (s < MAX_SAMPLES); s++) |
| 5017 |
{ |
| 5018 |
src = in[s] + src_offset + src_byte; |
| 5019 |
buff1 = ((*src) & matchbits) << (src_bit); |
| 5020 |
|
| 5021 |
/* If we have a full buffer's worth, write it out */ |
| 5022 |
if (ready_bits >= 8) |
| 5023 |
{ |
| 5024 |
*dst++ = buff2; |
| 5025 |
buff2 = buff1; |
| 5026 |
ready_bits -= 8; |
| 5027 |
strcpy(action, "Flush"); |
| 5028 |
} |
| 5029 |
else |
| 5030 |
{ |
| 5031 |
buff2 = (buff2 | (buff1 >> ready_bits)); |
| 5032 |
strcpy(action, "Update"); |
| 5033 |
} |
| 5034 |
ready_bits += bps; |
| 5035 |
|
| 5036 |
if ((dumpfile != NULL) && (level == 3)) |
| 5037 |
{ |
| 5038 |
dump_info(dumpfile, format, "", |
| 5039 |
"Row %3" PRIu32 ", Col %3" PRIu32 |
| 5040 |
", Samples %" PRIu16 ", Src byte offset %3" PRIu32 |
| 5041 |
" bit offset %2" PRIu32 " Dst offset %3td", |
| 5042 |
row + 1u, col + 1u, s, src_byte, src_bit, |
| 5043 |
dst - out); |
| 5044 |
dump_byte(dumpfile, format, "Match bits", matchbits); |
| 5045 |
dump_byte(dumpfile, format, "Src bits", *src); |
| 5046 |
dump_byte(dumpfile, format, "Buff1 bits", buff1); |
| 5047 |
dump_byte(dumpfile, format, "Buff2 bits", buff2); |
| 5048 |
dump_info(dumpfile, format, "", "%s", action); |
| 5049 |
} |
| 5050 |
} |
| 5051 |
} |
| 5052 |
|
| 5053 |
if (ready_bits > 0) |
| 5054 |
{ |
| 5055 |
buff1 = (buff2 & ((unsigned int)255 << (8 - ready_bits))); |
| 5056 |
*dst++ = buff1; |
| 5057 |
if ((dumpfile != NULL) && (level == 3)) |
| 5058 |
{ |
| 5059 |
dump_info(dumpfile, format, "", |
| 5060 |
"Row %3d, Col %3d, Src byte offset %3d bit offset " |
| 5061 |
"%2d Dst offset %3zd", |
| 5062 |
row + 1, col + 1, src_byte, src_bit, dst - out); |
| 5063 |
dump_byte(dumpfile, format, "Final bits", buff1); |
| 5064 |
} |
| 5065 |
} |
| 5066 |
|
| 5067 |
if ((dumpfile != NULL) && (level >= 2)) |
| 5068 |
{ |
| 5069 |
dump_info(dumpfile, format, "combineSeparateTileSamples8bits", |
| 5070 |
"Output data"); |
| 5071 |
dump_buffer(dumpfile, format, 1, dst_rowsize, row, |
| 5072 |
out + (row * dst_rowsize)); |
| 5073 |
} |
| 5074 |
} |
| 5075 |
|
| 5076 |
return (0); |
| 5077 |
} /* end combineSeparateTileSamples8bits */ |
| 5078 |
|
| 5079 |
static int combineSeparateTileSamples16bits(uint8_t *in[], uint8_t *out, |
| 5080 |
uint32_t cols, uint32_t rows, |
| 5081 |
uint32_t imagewidth, uint32_t tw, |
| 5082 |
uint16_t spp, uint16_t bps, |
| 5083 |
FILE *dumpfile, int format, |
| 5084 |
int level) |
| 5085 |
{ |
| 5086 |
int ready_bits = 0; |
| 5087 |
uint32_t src_rowsize, dst_rowsize; |
| 5088 |
uint32_t bit_offset, src_offset; |
| 5089 |
uint32_t row, col, src_byte = 0, src_bit = 0; |
| 5090 |
uint16_t maskbits = 0, matchbits = 0; |
| 5091 |
uint16_t buff1 = 0, buff2 = 0; |
| 5092 |
uint8_t bytebuff = 0; |
| 5093 |
tsample_t s; |
| 5094 |
unsigned char *src = in[0]; |
| 5095 |
unsigned char *dst = out; |
| 5096 |
char action[8]; |
| 5097 |
|
| 5098 |
if ((src == NULL) || (dst == NULL)) |
| 5099 |
{ |
| 5100 |
TIFFError("combineSeparateTileSamples16bits", |
| 5101 |
"Invalid input or output buffer"); |
| 5102 |
return (1); |
| 5103 |
} |
| 5104 |
|
| 5105 |
src_rowsize = ((bps * tw) + 7) / 8; |
| 5106 |
dst_rowsize = ((imagewidth * bps * spp) + 7) / 8; |
| 5107 |
maskbits = (uint16_t)-1 >> (16 - bps); |
| 5108 |
|
| 5109 |
for (row = 0; row < rows; row++) |
| 5110 |
{ |
| 5111 |
ready_bits = 0; |
| 5112 |
buff1 = buff2 = 0; |
| 5113 |
dst = out + (row * dst_rowsize); |
| 5114 |
src_offset = row * src_rowsize; |
| 5115 |
for (col = 0; col < cols; col++) |
| 5116 |
{ |
| 5117 |
/* Compute src byte(s) and bits within byte(s) */ |
| 5118 |
bit_offset = col * bps; |
| 5119 |
src_byte = bit_offset / 8; |
| 5120 |
src_bit = bit_offset % 8; |
| 5121 |
|
| 5122 |
matchbits = maskbits << (16 - src_bit - bps); |
| 5123 |
for (s = 0; (s < spp) && (s < MAX_SAMPLES); s++) |
| 5124 |
{ |
| 5125 |
src = in[s] + src_offset + src_byte; |
| 5126 |
if (little_endian) |
| 5127 |
buff1 = (src[0] << 8) | src[1]; |
| 5128 |
else |
| 5129 |
buff1 = (src[1] << 8) | src[0]; |
| 5130 |
buff1 = (buff1 & matchbits) << (src_bit); |
| 5131 |
|
| 5132 |
/* If we have a full buffer's worth, write it out */ |
| 5133 |
if (ready_bits >= 8) |
| 5134 |
{ |
| 5135 |
bytebuff = (buff2 >> 8); |
| 5136 |
*dst++ = bytebuff; |
| 5137 |
ready_bits -= 8; |
| 5138 |
/* shift in new bits */ |
| 5139 |
buff2 = ((buff2 << 8) | (buff1 >> ready_bits)); |
| 5140 |
strcpy(action, "Flush"); |
| 5141 |
} |
| 5142 |
else |
| 5143 |
{ /* add another bps bits to the buffer */ |
| 5144 |
bytebuff = 0; |
| 5145 |
buff2 = (buff2 | (buff1 >> ready_bits)); |
| 5146 |
strcpy(action, "Update"); |
| 5147 |
} |
| 5148 |
ready_bits += bps; |
| 5149 |
|
| 5150 |
if ((dumpfile != NULL) && (level == 3)) |
| 5151 |
{ |
| 5152 |
dump_info(dumpfile, format, "", |
| 5153 |
"Row %3" PRIu32 ", Col %3" PRIu32 |
| 5154 |
", Samples %" PRIu16 ", Src byte offset %3" PRIu32 |
| 5155 |
" bit offset %2" PRIu32 " Dst offset %3td", |
| 5156 |
row + 1u, col + 1u, s, src_byte, src_bit, |
| 5157 |
dst - out); |
| 5158 |
|
| 5159 |
dump_short(dumpfile, format, "Match bits", matchbits); |
| 5160 |
dump_data(dumpfile, format, "Src bits", src, 2); |
| 5161 |
dump_short(dumpfile, format, "Buff1 bits", buff1); |
| 5162 |
dump_short(dumpfile, format, "Buff2 bits", buff2); |
| 5163 |
dump_byte(dumpfile, format, "Write byte", bytebuff); |
| 5164 |
dump_info(dumpfile, format, "", "Ready bits: %d, %s", |
| 5165 |
ready_bits, action); |
| 5166 |
} |
| 5167 |
} |
| 5168 |
} |
| 5169 |
|
| 5170 |
/* catch any trailing bits at the end of the line */ |
| 5171 |
if (ready_bits > 0) |
| 5172 |
{ |
| 5173 |
bytebuff = (buff2 >> 8); |
| 5174 |
*dst++ = bytebuff; |
| 5175 |
if ((dumpfile != NULL) && (level == 3)) |
| 5176 |
{ |
| 5177 |
dump_info(dumpfile, format, "", |
| 5178 |
"Row %3d, Col %3d, Src byte offset %3d bit offset " |
| 5179 |
"%2d Dst offset %3zd", |
| 5180 |
row + 1, col + 1, src_byte, src_bit, dst - out); |
| 5181 |
dump_byte(dumpfile, format, "Final bits", bytebuff); |
| 5182 |
} |
| 5183 |
} |
| 5184 |
|
| 5185 |
if ((dumpfile != NULL) && (level == 2)) |
| 5186 |
{ |
| 5187 |
dump_info(dumpfile, format, "combineSeparateTileSamples16bits", |
| 5188 |
"Output data"); |
| 5189 |
dump_buffer(dumpfile, format, 1, dst_rowsize, row, |
| 5190 |
out + (row * dst_rowsize)); |
| 5191 |
} |
| 5192 |
} |
| 5193 |
|
| 5194 |
return (0); |
| 5195 |
} /* end combineSeparateTileSamples16bits */ |
| 5196 |
|
| 5197 |
static int combineSeparateTileSamples24bits(uint8_t *in[], uint8_t *out, |
| 5198 |
uint32_t cols, uint32_t rows, |
| 5199 |
uint32_t imagewidth, uint32_t tw, |
| 5200 |
uint16_t spp, uint16_t bps, |
| 5201 |
FILE *dumpfile, int format, |
| 5202 |
int level) |
| 5203 |
{ |
| 5204 |
int ready_bits = 0; |
| 5205 |
uint32_t src_rowsize, dst_rowsize; |
| 5206 |
uint32_t bit_offset, src_offset; |
| 5207 |
uint32_t row, col, src_byte = 0, src_bit = 0; |
| 5208 |
uint32_t maskbits = 0, matchbits = 0; |
| 5209 |
uint32_t buff1 = 0, buff2 = 0; |
| 5210 |
uint8_t bytebuff1 = 0, bytebuff2 = 0; |
| 5211 |
tsample_t s; |
| 5212 |
unsigned char *src = in[0]; |
| 5213 |
unsigned char *dst = out; |
| 5214 |
char action[8]; |
| 5215 |
|
| 5216 |
if ((src == NULL) || (dst == NULL)) |
| 5217 |
{ |
| 5218 |
TIFFError("combineSeparateTileSamples24bits", |
| 5219 |
"Invalid input or output buffer"); |
| 5220 |
return (1); |
| 5221 |
} |
| 5222 |
|
| 5223 |
src_rowsize = ((bps * tw) + 7) / 8; |
| 5224 |
dst_rowsize = ((imagewidth * bps * spp) + 7) / 8; |
| 5225 |
maskbits = (uint32_t)-1 >> (32 - bps); |
| 5226 |
|
| 5227 |
for (row = 0; row < rows; row++) |
| 5228 |
{ |
| 5229 |
ready_bits = 0; |
| 5230 |
buff1 = buff2 = 0; |
| 5231 |
dst = out + (row * dst_rowsize); |
| 5232 |
src_offset = row * src_rowsize; |
| 5233 |
for (col = 0; col < cols; col++) |
| 5234 |
{ |
| 5235 |
/* Compute src byte(s) and bits within byte(s) */ |
| 5236 |
bit_offset = col * bps; |
| 5237 |
src_byte = bit_offset / 8; |
| 5238 |
src_bit = bit_offset % 8; |
| 5239 |
|
| 5240 |
matchbits = maskbits << (32 - src_bit - bps); |
| 5241 |
for (s = 0; (s < spp) && (s < MAX_SAMPLES); s++) |
| 5242 |
{ |
| 5243 |
src = in[s] + src_offset + src_byte; |
| 5244 |
if (little_endian) |
| 5245 |
buff1 = (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | |
| 5246 |
src[3]; |
| 5247 |
else |
| 5248 |
buff1 = (src[3] << 24) | (src[2] << 16) | (src[1] << 8) | |
| 5249 |
src[0]; |
| 5250 |
buff1 = (buff1 & matchbits) << (src_bit); |
| 5251 |
|
| 5252 |
/* If we have a full buffer's worth, write it out */ |
| 5253 |
if (ready_bits >= 16) |
| 5254 |
{ |
| 5255 |
bytebuff1 = (buff2 >> 24); |
| 5256 |
*dst++ = bytebuff1; |
| 5257 |
bytebuff2 = (buff2 >> 16); |
| 5258 |
*dst++ = bytebuff2; |
| 5259 |
ready_bits -= 16; |
| 5260 |
|
| 5261 |
/* shift in new bits */ |
| 5262 |
buff2 = ((buff2 << 16) | (buff1 >> ready_bits)); |
| 5263 |
strcpy(action, "Flush"); |
| 5264 |
} |
| 5265 |
else |
| 5266 |
{ /* add another bps bits to the buffer */ |
| 5267 |
bytebuff1 = bytebuff2 = 0; |
| 5268 |
buff2 = (buff2 | (buff1 >> ready_bits)); |
| 5269 |
strcpy(action, "Update"); |
| 5270 |
} |
| 5271 |
ready_bits += bps; |
| 5272 |
|
| 5273 |
if ((dumpfile != NULL) && (level == 3)) |
| 5274 |
{ |
| 5275 |
dump_info(dumpfile, format, "", |
| 5276 |
"Row %3" PRIu32 ", Col %3" PRIu32 |
| 5277 |
", Samples %" PRIu16 ", Src byte offset %3" PRIu32 |
| 5278 |
" bit offset %2" PRIu32 " Dst offset %3td", |
| 5279 |
row + 1u, col + 1u, s, src_byte, src_bit, |
| 5280 |
dst - out); |
| 5281 |
dump_long(dumpfile, format, "Match bits ", matchbits); |
| 5282 |
dump_data(dumpfile, format, "Src bits ", src, 4); |
| 5283 |
dump_long(dumpfile, format, "Buff1 bits ", buff1); |
| 5284 |
dump_long(dumpfile, format, "Buff2 bits ", buff2); |
| 5285 |
dump_byte(dumpfile, format, "Write bits1", bytebuff1); |
| 5286 |
dump_byte(dumpfile, format, "Write bits2", bytebuff2); |
| 5287 |
dump_info(dumpfile, format, "", "Ready bits: %d, %s", |
| 5288 |
ready_bits, action); |
| 5289 |
} |
| 5290 |
} |
| 5291 |
} |
| 5292 |
|
| 5293 |
/* catch any trailing bits at the end of the line */ |
| 5294 |
while (ready_bits > 0) |
| 5295 |
{ |
| 5296 |
bytebuff1 = (buff2 >> 24); |
| 5297 |
*dst++ = bytebuff1; |
| 5298 |
|
| 5299 |
buff2 = (buff2 << 8); |
| 5300 |
bytebuff2 = bytebuff1; |
| 5301 |
ready_bits -= 8; |
| 5302 |
} |
| 5303 |
|
| 5304 |
if ((dumpfile != NULL) && (level == 3)) |
| 5305 |
{ |
| 5306 |
dump_info(dumpfile, format, "", |
| 5307 |
"Row %3d, Col %3d, Src byte offset %3d bit offset %2d " |
| 5308 |
"Dst offset %3zd", |
| 5309 |
row + 1, col + 1, src_byte, src_bit, dst - out); |
| 5310 |
|
| 5311 |
dump_long(dumpfile, format, "Match bits ", matchbits); |
| 5312 |
dump_data(dumpfile, format, "Src bits ", src, 4); |
| 5313 |
dump_long(dumpfile, format, "Buff1 bits ", buff1); |
| 5314 |
dump_long(dumpfile, format, "Buff2 bits ", buff2); |
| 5315 |
dump_byte(dumpfile, format, "Write bits1", bytebuff1); |
| 5316 |
dump_byte(dumpfile, format, "Write bits2", bytebuff2); |
| 5317 |
dump_info(dumpfile, format, "", "Ready bits: %2d", ready_bits); |
| 5318 |
} |
| 5319 |
|
| 5320 |
if ((dumpfile != NULL) && (level == 2)) |
| 5321 |
{ |
| 5322 |
dump_info(dumpfile, format, "combineSeparateTileSamples24bits", |
| 5323 |
"Output data"); |
| 5324 |
dump_buffer(dumpfile, format, 1, dst_rowsize, row, |
| 5325 |
out + (row * dst_rowsize)); |
| 5326 |
} |
| 5327 |
} |
| 5328 |
|
| 5329 |
return (0); |
| 5330 |
} /* end combineSeparateTileSamples24bits */ |
| 5331 |
|
| 5332 |
static int combineSeparateTileSamples32bits(uint8_t *in[], uint8_t *out, |
| 5333 |
uint32_t cols, uint32_t rows, |
| 5334 |
uint32_t imagewidth, uint32_t tw, |
| 5335 |
uint16_t spp, uint16_t bps, |
| 5336 |
FILE *dumpfile, int format, |
| 5337 |
int level) |
| 5338 |
{ |
| 5339 |
int ready_bits = 0 /*, shift_width = 0 */; |
| 5340 |
uint32_t src_rowsize, dst_rowsize, bit_offset, src_offset; |
| 5341 |
uint32_t src_byte = 0, src_bit = 0; |
| 5342 |
uint32_t row, col; |
| 5343 |
uint32_t longbuff1 = 0, longbuff2 = 0; |
| 5344 |
uint64_t maskbits = 0, matchbits = 0; |
| 5345 |
uint64_t buff1 = 0, buff2 = 0, buff3 = 0; |
| 5346 |
uint8_t bytebuff1 = 0, bytebuff2 = 0, bytebuff3 = 0, bytebuff4 = 0; |
| 5347 |
tsample_t s; |
| 5348 |
unsigned char *src = in[0]; |
| 5349 |
unsigned char *dst = out; |
| 5350 |
char action[8]; |
| 5351 |
|
| 5352 |
if ((src == NULL) || (dst == NULL)) |
| 5353 |
{ |
| 5354 |
TIFFError("combineSeparateTileSamples32bits", |
| 5355 |
"Invalid input or output buffer"); |
| 5356 |
return (1); |
| 5357 |
} |
| 5358 |
|
| 5359 |
src_rowsize = ((bps * tw) + 7) / 8; |
| 5360 |
dst_rowsize = ((imagewidth * bps * spp) + 7) / 8; |
| 5361 |
maskbits = (uint64_t)-1 >> (64 - bps); |
| 5362 |
/* shift_width = ((bps + 7) / 8) + 1; */ |
| 5363 |
|
| 5364 |
for (row = 0; row < rows; row++) |
| 5365 |
{ |
| 5366 |
ready_bits = 0; |
| 5367 |
buff1 = buff2 = 0; |
| 5368 |
dst = out + (row * dst_rowsize); |
| 5369 |
src_offset = row * src_rowsize; |
| 5370 |
for (col = 0; col < cols; col++) |
| 5371 |
{ |
| 5372 |
/* Compute src byte(s) and bits within byte(s) */ |
| 5373 |
bit_offset = col * bps; |
| 5374 |
src_byte = bit_offset / 8; |
| 5375 |
src_bit = bit_offset % 8; |
| 5376 |
|
| 5377 |
matchbits = maskbits << (64 - src_bit - bps); |
| 5378 |
for (s = 0; (s < spp) && (s < MAX_SAMPLES); s++) |
| 5379 |
{ |
| 5380 |
src = in[s] + src_offset + src_byte; |
| 5381 |
if (little_endian) |
| 5382 |
{ |
| 5383 |
longbuff1 = (src[0] << 24) | (src[1] << 16) | |
| 5384 |
(src[2] << 8) | src[3]; |
| 5385 |
longbuff2 = longbuff1; |
| 5386 |
} |
| 5387 |
else |
| 5388 |
{ |
| 5389 |
longbuff1 = (src[3] << 24) | (src[2] << 16) | |
| 5390 |
(src[1] << 8) | src[0]; |
| 5391 |
longbuff2 = longbuff1; |
| 5392 |
} |
| 5393 |
|
| 5394 |
buff3 = ((uint64_t)longbuff1 << 32) | longbuff2; |
| 5395 |
buff1 = (buff3 & matchbits) << (src_bit); |
| 5396 |
|
| 5397 |
/* If we have a full buffer's worth, write it out */ |
| 5398 |
if (ready_bits >= 32) |
| 5399 |
{ |
| 5400 |
bytebuff1 = (uint8_t)(buff2 >> 56); |
| 5401 |
*dst++ = bytebuff1; |
| 5402 |
bytebuff2 = (uint8_t)(buff2 >> 48); |
| 5403 |
*dst++ = bytebuff2; |
| 5404 |
bytebuff3 = (uint8_t)(buff2 >> 40); |
| 5405 |
*dst++ = bytebuff3; |
| 5406 |
bytebuff4 = (uint8_t)(buff2 >> 32); |
| 5407 |
*dst++ = bytebuff4; |
| 5408 |
ready_bits -= 32; |
| 5409 |
|
| 5410 |
/* shift in new bits */ |
| 5411 |
buff2 = ((buff2 << 32) | (buff1 >> ready_bits)); |
| 5412 |
strcpy(action, "Flush"); |
| 5413 |
} |
| 5414 |
else |
| 5415 |
{ /* add another bps bits to the buffer */ |
| 5416 |
bytebuff1 = bytebuff2 = bytebuff3 = bytebuff4 = 0; |
| 5417 |
buff2 = (buff2 | (buff1 >> ready_bits)); |
| 5418 |
strcpy(action, "Update"); |
| 5419 |
} |
| 5420 |
ready_bits += bps; |
| 5421 |
|
| 5422 |
if ((dumpfile != NULL) && (level == 3)) |
| 5423 |
{ |
| 5424 |
dump_info(dumpfile, format, "", |
| 5425 |
"Row %3" PRIu32 ", Col %3" PRIu32 |
| 5426 |
", Sample %" PRIu16 ", Src byte offset %3" PRIu32 |
| 5427 |
" bit offset %2" PRIu32 " Dst offset %3td", |
| 5428 |
row + 1u, col + 1u, s, src_byte, src_bit, |
| 5429 |
dst - out); |
| 5430 |
dump_wide(dumpfile, format, "Match bits ", matchbits); |
| 5431 |
dump_data(dumpfile, format, "Src bits ", src, 8); |
| 5432 |
dump_wide(dumpfile, format, "Buff1 bits ", buff1); |
| 5433 |
dump_wide(dumpfile, format, "Buff2 bits ", buff2); |
| 5434 |
dump_info(dumpfile, format, "", "Ready bits: %d, %s", |
| 5435 |
ready_bits, action); |
| 5436 |
} |
| 5437 |
} |
| 5438 |
} |
| 5439 |
while (ready_bits > 0) |
| 5440 |
{ |
| 5441 |
bytebuff1 = (buff2 >> 56); |
| 5442 |
*dst++ = bytebuff1; |
| 5443 |
buff2 = (buff2 << 8); |
| 5444 |
ready_bits -= 8; |
| 5445 |
} |
| 5446 |
|
| 5447 |
if ((dumpfile != NULL) && (level == 3)) |
| 5448 |
{ |
| 5449 |
dump_info(dumpfile, format, "", |
| 5450 |
"Row %3d, Col %3d, Src byte offset %3d bit offset %2d " |
| 5451 |
"Dst offset %3zd", |
| 5452 |
row + 1, col + 1, src_byte, src_bit, dst - out); |
| 5453 |
|
| 5454 |
dump_wide(dumpfile, format, "Match bits ", matchbits); |
| 5455 |
dump_data(dumpfile, format, "Src bits ", src, 4); |
| 5456 |
dump_wide(dumpfile, format, "Buff1 bits ", buff1); |
| 5457 |
dump_wide(dumpfile, format, "Buff2 bits ", buff2); |
| 5458 |
dump_byte(dumpfile, format, "Write bits1", bytebuff1); |
| 5459 |
dump_byte(dumpfile, format, "Write bits2", bytebuff2); |
| 5460 |
dump_info(dumpfile, format, "", "Ready bits: %2d", ready_bits); |
| 5461 |
} |
| 5462 |
|
| 5463 |
if ((dumpfile != NULL) && (level == 2)) |
| 5464 |
{ |
| 5465 |
dump_info(dumpfile, format, "combineSeparateTileSamples32bits", |
| 5466 |
"Output data"); |
| 5467 |
dump_buffer(dumpfile, format, 1, dst_rowsize, row, out); |
| 5468 |
} |
| 5469 |
} |
| 5470 |
|
| 5471 |
return (0); |
| 5472 |
} /* end combineSeparateTileSamples32bits */ |
| 5473 |
|
| 5474 |
static int readSeparateStripsIntoBuffer(TIFF *in, uint8_t *obuf, |
| 5475 |
uint32_t length, uint32_t width, |
| 5476 |
uint16_t spp, struct dump_opts *dump) |
| 5477 |
{ |
| 5478 |
int i, bytes_per_sample, bytes_per_pixel, shift_width, result = 1; |
| 5479 |
uint32_t j; |
| 5480 |
tmsize_t bytes_read = 0; |
| 5481 |
uint16_t bps = 0, planar; |
| 5482 |
uint32_t nstrips; |
| 5483 |
uint32_t strips_per_sample; |
| 5484 |
uint32_t src_rowsize, dst_rowsize, rows_processed, rps; |
| 5485 |
uint32_t rows_this_strip = 0; |
| 5486 |
tsample_t s; |
| 5487 |
tstrip_t strip; |
| 5488 |
tsize_t scanlinesize = TIFFScanlineSize(in); |
| 5489 |
tsize_t stripsize = TIFFStripSize(in); |
| 5490 |
unsigned char *srcbuffs[MAX_SAMPLES]; |
| 5491 |
unsigned char *buff = NULL; |
| 5492 |
unsigned char *dst = NULL; |
| 5493 |
|
| 5494 |
if (obuf == NULL) |
| 5495 |
{ |
| 5496 |
TIFFError("readSeparateStripsIntoBuffer", "Invalid buffer argument"); |
| 5497 |
return (0); |
| 5498 |
} |
| 5499 |
|
| 5500 |
memset(srcbuffs, '\0', sizeof(srcbuffs)); |
| 5501 |
TIFFGetFieldDefaulted(in, TIFFTAG_BITSPERSAMPLE, &bps); |
| 5502 |
TIFFGetFieldDefaulted(in, TIFFTAG_PLANARCONFIG, &planar); |
| 5503 |
TIFFGetFieldDefaulted(in, TIFFTAG_ROWSPERSTRIP, &rps); |
| 5504 |
if (rps > length) |
| 5505 |
rps = length; |
| 5506 |
|
| 5507 |
bytes_per_sample = (bps + 7) / 8; |
| 5508 |
bytes_per_pixel = ((bps * spp) + 7) / 8; |
| 5509 |
if (bytes_per_pixel < (bytes_per_sample + 1)) |
| 5510 |
shift_width = bytes_per_pixel; |
| 5511 |
else |
| 5512 |
shift_width = bytes_per_sample + 1; |
| 5513 |
|
| 5514 |
src_rowsize = ((bps * width) + 7) / 8; |
| 5515 |
dst_rowsize = ((bps * width * spp) + 7) / 8; |
| 5516 |
dst = obuf; |
| 5517 |
|
| 5518 |
if ((dump->infile != NULL) && (dump->level == 3)) |
| 5519 |
{ |
| 5520 |
dump_info(dump->infile, dump->format, "", |
| 5521 |
"Image width %" PRIu32 ", length %" PRIu32 |
| 5522 |
", Scanline size, %4" PRId64 " bytes", |
| 5523 |
width, length, scanlinesize); |
| 5524 |
dump_info(dump->infile, dump->format, "", |
| 5525 |
"Bits per sample %" PRIu16 ", Samples per pixel %" PRIu16 |
| 5526 |
", Shift width %d", |
| 5527 |
bps, spp, shift_width); |
| 5528 |
} |
| 5529 |
|
| 5530 |
/* Libtiff seems to assume/require that data for separate planes are |
| 5531 |
* written one complete plane after another and not interleaved in any way. |
| 5532 |
* Multiple scanlines and possibly strips of the same plane must be |
| 5533 |
* written before data for any other plane. |
| 5534 |
*/ |
| 5535 |
nstrips = TIFFNumberOfStrips(in); |
| 5536 |
strips_per_sample = nstrips / spp; |
| 5537 |
|
| 5538 |
/* Add 3 padding bytes for combineSeparateSamples32bits */ |
| 5539 |
if ((size_t)stripsize > TIFF_TMSIZE_T_MAX - NUM_BUFF_OVERSIZE_BYTES) |
| 5540 |
{ |
| 5541 |
TIFFError("readSeparateStripsIntoBuffer", |
| 5542 |
"Integer overflow when calculating buffer size."); |
| 5543 |
exit(EXIT_FAILURE); |
| 5544 |
} |
| 5545 |
|
| 5546 |
for (s = 0; (s < spp) && (s < MAX_SAMPLES); s++) |
| 5547 |
{ |
| 5548 |
srcbuffs[s] = NULL; |
| 5549 |
buff = limitMalloc(stripsize + NUM_BUFF_OVERSIZE_BYTES); |
| 5550 |
if (!buff) |
| 5551 |
{ |
| 5552 |
TIFFError( |
| 5553 |
"readSeparateStripsIntoBuffer", |
| 5554 |
"Unable to allocate strip read buffer for sample %" PRIu16, s); |
| 5555 |
for (i = 0; i < s; i++) |
| 5556 |
_TIFFfree(srcbuffs[i]); |
| 5557 |
return 0; |
| 5558 |
} |
| 5559 |
buff[stripsize] = 0; |
| 5560 |
buff[stripsize + 1] = 0; |
| 5561 |
buff[stripsize + 2] = 0; |
| 5562 |
srcbuffs[s] = buff; |
| 5563 |
} |
| 5564 |
|
| 5565 |
rows_processed = 0; |
| 5566 |
for (j = 0; (j < strips_per_sample) && (result == 1); j++) |
| 5567 |
{ |
| 5568 |
for (s = 0; (s < spp) && (s < MAX_SAMPLES); s++) |
| 5569 |
{ |
| 5570 |
buff = srcbuffs[s]; |
| 5571 |
strip = (s * strips_per_sample) + j; |
| 5572 |
bytes_read = TIFFReadEncodedStrip(in, strip, buff, stripsize); |
| 5573 |
if (bytes_read < 0) |
| 5574 |
{ |
| 5575 |
rows_this_strip = 0; |
| 5576 |
} |
| 5577 |
else |
| 5578 |
{ |
| 5579 |
rows_this_strip = (uint32_t)(bytes_read / src_rowsize); |
| 5580 |
} |
| 5581 |
if (bytes_read < 0 && !ignore) |
| 5582 |
{ |
| 5583 |
TIFFError(TIFFFileName(in), |
| 5584 |
"Error, can't read strip %" PRIu32 |
| 5585 |
" for sample %" PRIu32, |
| 5586 |
strip, s + 1u); |
| 5587 |
result = 0; |
| 5588 |
break; |
| 5589 |
} |
| 5590 |
#ifdef DEVELMODE |
| 5591 |
TIFFError("", |
| 5592 |
"Strip %2" PRIu32 ", read %5zd" |
| 5593 |
" bytes for %4" PRIu32 " scanlines, shift width %d", |
| 5594 |
strip, bytes_read, rows_this_strip, shift_width); |
| 5595 |
#endif |
| 5596 |
} |
| 5597 |
|
| 5598 |
if (rps > rows_this_strip) |
| 5599 |
rps = rows_this_strip; |
| 5600 |
dst = obuf + (dst_rowsize * rows_processed); |
| 5601 |
if ((bps % 8) == 0) |
| 5602 |
{ |
| 5603 |
if (combineSeparateSamplesBytes(srcbuffs, dst, width, rps, spp, bps, |
| 5604 |
dump->infile, dump->format, |
| 5605 |
dump->level)) |
| 5606 |
{ |
| 5607 |
result = 0; |
| 5608 |
break; |
| 5609 |
} |
| 5610 |
} |
| 5611 |
else |
| 5612 |
{ |
| 5613 |
switch (shift_width) |
| 5614 |
{ |
| 5615 |
case 1: |
| 5616 |
if (combineSeparateSamples8bits(srcbuffs, dst, width, rps, |
| 5617 |
spp, bps, dump->infile, |
| 5618 |
dump->format, dump->level)) |
| 5619 |
{ |
| 5620 |
result = 0; |
| 5621 |
break; |
| 5622 |
} |
| 5623 |
break; |
| 5624 |
case 2: |
| 5625 |
if (combineSeparateSamples16bits(srcbuffs, dst, width, rps, |
| 5626 |
spp, bps, dump->infile, |
| 5627 |
dump->format, dump->level)) |
| 5628 |
{ |
| 5629 |
result = 0; |
| 5630 |
break; |
| 5631 |
} |
| 5632 |
break; |
| 5633 |
case 3: |
| 5634 |
if (combineSeparateSamples24bits(srcbuffs, dst, width, rps, |
| 5635 |
spp, bps, dump->infile, |
| 5636 |
dump->format, dump->level)) |
| 5637 |
{ |
| 5638 |
result = 0; |
| 5639 |
break; |
| 5640 |
} |
| 5641 |
break; |
| 5642 |
case 4: |
| 5643 |
case 5: |
| 5644 |
case 6: |
| 5645 |
case 7: |
| 5646 |
case 8: |
| 5647 |
if (combineSeparateSamples32bits(srcbuffs, dst, width, rps, |
| 5648 |
spp, bps, dump->infile, |
| 5649 |
dump->format, dump->level)) |
| 5650 |
{ |
| 5651 |
result = 0; |
| 5652 |
break; |
| 5653 |
} |
| 5654 |
break; |
| 5655 |
default: |
| 5656 |
TIFFError("readSeparateStripsIntoBuffer", |
| 5657 |
"Unsupported bit depth: %" PRIu16, bps); |
| 5658 |
result = 0; |
| 5659 |
break; |
| 5660 |
} |
| 5661 |
} |
| 5662 |
|
| 5663 |
if ((rows_processed + rps) > length) |
| 5664 |
{ |
| 5665 |
rows_processed = length; |
| 5666 |
rps = length - rows_processed; |
| 5667 |
} |
| 5668 |
else |
| 5669 |
rows_processed += rps; |
| 5670 |
} |
| 5671 |
|
| 5672 |
/* free any buffers allocated for each plane or scanline and |
| 5673 |
* any temporary buffers |
| 5674 |
*/ |
| 5675 |
for (s = 0; (s < spp) && (s < MAX_SAMPLES); s++) |
| 5676 |
{ |
| 5677 |
buff = srcbuffs[s]; |
| 5678 |
if (buff != NULL) |
| 5679 |
_TIFFfree(buff); |
| 5680 |
} |
| 5681 |
|
| 5682 |
return (result); |
| 5683 |
} /* end readSeparateStripsIntoBuffer */ |
| 5684 |
|
| 5685 |
static int get_page_geometry(char *name, struct pagedef *page) |
| 5686 |
{ |
| 5687 |
char *ptr; |
| 5688 |
unsigned int n; |
| 5689 |
|
| 5690 |
for (ptr = name; *ptr; ptr++) |
| 5691 |
*ptr = (char)tolower((int)*ptr); |
| 5692 |
|
| 5693 |
for (n = 0; n < MAX_PAPERNAMES; n++) |
| 5694 |
{ |
| 5695 |
if (strcmp(name, PaperTable[n].name) == 0) |
| 5696 |
{ |
| 5697 |
page->width = PaperTable[n].width; |
| 5698 |
page->length = PaperTable[n].length; |
| 5699 |
strncpy(page->name, PaperTable[n].name, 15); |
| 5700 |
page->name[15] = '\0'; |
| 5701 |
return (0); |
| 5702 |
} |
| 5703 |
} |
| 5704 |
|
| 5705 |
return (1); |
| 5706 |
} |
| 5707 |
|
| 5708 |
static void initPageSetup(struct pagedef *page, struct pageseg *pagelist, |
| 5709 |
struct buffinfo seg_buffs[]) |
| 5710 |
{ |
| 5711 |
int i; |
| 5712 |
|
| 5713 |
strcpy(page->name, ""); |
| 5714 |
page->mode = PAGE_MODE_NONE; |
| 5715 |
page->res_unit = RESUNIT_NONE; |
| 5716 |
page->hres = 0.0; |
| 5717 |
page->vres = 0.0; |
| 5718 |
page->width = 0.0; |
| 5719 |
page->length = 0.0; |
| 5720 |
page->hmargin = 0.0; |
| 5721 |
page->vmargin = 0.0; |
| 5722 |
page->rows = 0; |
| 5723 |
page->cols = 0; |
| 5724 |
page->orient = ORIENTATION_NONE; |
| 5725 |
|
| 5726 |
for (i = 0; i < MAX_SECTIONS; i++) |
| 5727 |
{ |
| 5728 |
pagelist[i].x1 = (uint32_t)0; |
| 5729 |
pagelist[i].x2 = (uint32_t)0; |
| 5730 |
pagelist[i].y1 = (uint32_t)0; |
| 5731 |
pagelist[i].y2 = (uint32_t)0; |
| 5732 |
pagelist[i].buffsize = (uint32_t)0; |
| 5733 |
pagelist[i].position = 0; |
| 5734 |
pagelist[i].total = 0; |
| 5735 |
} |
| 5736 |
|
| 5737 |
for (i = 0; i < MAX_OUTBUFFS; i++) |
| 5738 |
{ |
| 5739 |
seg_buffs[i].size = 0; |
| 5740 |
seg_buffs[i].buffer = NULL; |
| 5741 |
} |
| 5742 |
} |
| 5743 |
|
| 5744 |
static void initImageData(struct image_data *image) |
| 5745 |
{ |
| 5746 |
image->xres = 0.0; |
| 5747 |
image->yres = 0.0; |
| 5748 |
image->width = 0; |
| 5749 |
image->length = 0; |
| 5750 |
image->res_unit = RESUNIT_NONE; |
| 5751 |
image->bps = 0; |
| 5752 |
image->spp = 0; |
| 5753 |
image->planar = 0; |
| 5754 |
image->photometric = 0; |
| 5755 |
image->orientation = 0; |
| 5756 |
image->compression = COMPRESSION_NONE; |
| 5757 |
image->adjustments = 0; |
| 5758 |
} |
| 5759 |
|
| 5760 |
static void initCropMasks(struct crop_mask *cps) |
| 5761 |
{ |
| 5762 |
int i; |
| 5763 |
|
| 5764 |
cps->crop_mode = CROP_NONE; |
| 5765 |
cps->res_unit = RESUNIT_NONE; |
| 5766 |
cps->edge_ref = EDGE_TOP; |
| 5767 |
cps->width = 0; |
| 5768 |
cps->length = 0; |
| 5769 |
for (i = 0; i < 4; i++) |
| 5770 |
cps->margins[i] = 0.0; |
| 5771 |
cps->bufftotal = (uint32_t)0; |
| 5772 |
cps->combined_width = (uint32_t)0; |
| 5773 |
cps->combined_length = (uint32_t)0; |
| 5774 |
cps->rotation = (uint16_t)0; |
| 5775 |
cps->photometric = INVERT_DATA_AND_TAG; |
| 5776 |
cps->mirror = (uint16_t)0; |
| 5777 |
cps->invert = (uint16_t)0; |
| 5778 |
cps->zones = (uint32_t)0; |
| 5779 |
cps->regions = (uint32_t)0; |
| 5780 |
for (i = 0; i < MAX_REGIONS; i++) |
| 5781 |
{ |
| 5782 |
cps->corners[i].X1 = 0.0; |
| 5783 |
cps->corners[i].X2 = 0.0; |
| 5784 |
cps->corners[i].Y1 = 0.0; |
| 5785 |
cps->corners[i].Y2 = 0.0; |
| 5786 |
cps->regionlist[i].x1 = 0; |
| 5787 |
cps->regionlist[i].x2 = 0; |
| 5788 |
cps->regionlist[i].y1 = 0; |
| 5789 |
cps->regionlist[i].y2 = 0; |
| 5790 |
cps->regionlist[i].width = 0; |
| 5791 |
cps->regionlist[i].length = 0; |
| 5792 |
cps->regionlist[i].buffsize = 0; |
| 5793 |
cps->zonelist[i].position = 0; |
| 5794 |
cps->zonelist[i].total = 0; |
| 5795 |
} |
| 5796 |
cps->exp_mode = ONE_FILE_COMPOSITE; |
| 5797 |
cps->img_mode = COMPOSITE_IMAGES; |
| 5798 |
} |
| 5799 |
|
| 5800 |
static void initDumpOptions(struct dump_opts *dump) |
| 5801 |
{ |
| 5802 |
dump->debug = 0; |
| 5803 |
dump->format = DUMP_NONE; |
| 5804 |
dump->level = 1; |
| 5805 |
sprintf(dump->mode, "w"); |
| 5806 |
memset(dump->infilename, '\0', PATH_MAX + 1); |
| 5807 |
memset(dump->outfilename, '\0', PATH_MAX + 1); |
| 5808 |
dump->infile = NULL; |
| 5809 |
dump->outfile = NULL; |
| 5810 |
} |
| 5811 |
|
| 5812 |
/* Compute pixel offsets into the image for margins and fixed regions */ |
| 5813 |
static int computeInputPixelOffsets(struct crop_mask *crop, |
| 5814 |
struct image_data *image, |
| 5815 |
struct offset *off) |
| 5816 |
{ |
| 5817 |
double scale; |
| 5818 |
float xres, yres; |
| 5819 |
/* Values for these offsets are in pixels from start of image, not bytes, |
| 5820 |
* and are indexed from zero to width - 1 or length - 1 */ |
| 5821 |
uint32_t tmargin, bmargin, lmargin, rmargin; |
| 5822 |
uint32_t startx, endx; /* offsets of first and last columns to extract */ |
| 5823 |
uint32_t starty, endy; /* offsets of first and last row to extract */ |
| 5824 |
uint32_t width, length, crop_width, crop_length; |
| 5825 |
uint32_t i, max_width, max_length, zwidth, zlength, buffsize; |
| 5826 |
uint32_t x1, x2, y1, y2; |
| 5827 |
|
| 5828 |
if (image->res_unit != RESUNIT_INCH && |
| 5829 |
image->res_unit != RESUNIT_CENTIMETER) |
| 5830 |
{ |
| 5831 |
xres = 1.0; |
| 5832 |
yres = 1.0; |
| 5833 |
} |
| 5834 |
else |
| 5835 |
{ |
| 5836 |
if (((image->xres == 0) || (image->yres == 0)) && |
| 5837 |
(crop->res_unit != RESUNIT_NONE) && |
| 5838 |
((crop->crop_mode & CROP_REGIONS) || |
| 5839 |
(crop->crop_mode & CROP_MARGINS) || |
| 5840 |
(crop->crop_mode & CROP_LENGTH) || (crop->crop_mode & CROP_WIDTH))) |
| 5841 |
{ |
| 5842 |
TIFFError("computeInputPixelOffsets", |
| 5843 |
"Cannot compute margins or fixed size sections without " |
| 5844 |
"image resolution"); |
| 5845 |
TIFFError("computeInputPixelOffsets", |
| 5846 |
"Specify units in pixels and try again"); |
| 5847 |
return (-1); |
| 5848 |
} |
| 5849 |
xres = image->xres; |
| 5850 |
yres = image->yres; |
| 5851 |
} |
| 5852 |
|
| 5853 |
/* Translate user units to image units */ |
| 5854 |
scale = 1.0; |
| 5855 |
switch (crop->res_unit) |
| 5856 |
{ |
| 5857 |
case RESUNIT_CENTIMETER: |
| 5858 |
if (image->res_unit == RESUNIT_INCH) |
| 5859 |
scale = 1.0 / 2.54; |
| 5860 |
break; |
| 5861 |
case RESUNIT_INCH: |
| 5862 |
if (image->res_unit == RESUNIT_CENTIMETER) |
| 5863 |
scale = 2.54; |
| 5864 |
break; |
| 5865 |
case RESUNIT_NONE: /* Dimensions in pixels */ |
| 5866 |
default: |
| 5867 |
break; |
| 5868 |
} |
| 5869 |
|
| 5870 |
if (crop->crop_mode & CROP_REGIONS) |
| 5871 |
{ |
| 5872 |
max_width = max_length = 0; |
| 5873 |
for (i = 0; i < crop->regions; i++) |
| 5874 |
{ |
| 5875 |
if ((crop->res_unit == RESUNIT_INCH) || |
| 5876 |
(crop->res_unit == RESUNIT_CENTIMETER)) |
| 5877 |
{ |
| 5878 |
x1 = _TIFFClampDoubleToUInt32(crop->corners[i].X1 * scale * |
| 5879 |
xres); |
| 5880 |
x2 = _TIFFClampDoubleToUInt32(crop->corners[i].X2 * scale * |
| 5881 |
xres); |
| 5882 |
y1 = _TIFFClampDoubleToUInt32(crop->corners[i].Y1 * scale * |
| 5883 |
yres); |
| 5884 |
y2 = _TIFFClampDoubleToUInt32(crop->corners[i].Y2 * scale * |
| 5885 |
yres); |
| 5886 |
} |
| 5887 |
else |
| 5888 |
{ |
| 5889 |
x1 = _TIFFClampDoubleToUInt32(crop->corners[i].X1); |
| 5890 |
x2 = _TIFFClampDoubleToUInt32(crop->corners[i].X2); |
| 5891 |
y1 = _TIFFClampDoubleToUInt32(crop->corners[i].Y1); |
| 5892 |
y2 = _TIFFClampDoubleToUInt32(crop->corners[i].Y2); |
| 5893 |
} |
| 5894 |
/* a) Region needs to be within image sizes 0.. width-1; 0..length-1 |
| 5895 |
* b) Corners are expected to be submitted as top-left to |
| 5896 |
* bottom-right. Therefore, check that and reorder input. (be aware |
| 5897 |
* x,y are already casted to (uint32_t) and avoid (0 - 1) ) |
| 5898 |
*/ |
| 5899 |
uint32_t aux; |
| 5900 |
if (x1 > x2) |
| 5901 |
{ |
| 5902 |
aux = x1; |
| 5903 |
x1 = x2; |
| 5904 |
x2 = aux; |
| 5905 |
} |
| 5906 |
if (y1 > y2) |
| 5907 |
{ |
| 5908 |
aux = y1; |
| 5909 |
y1 = y2; |
| 5910 |
y2 = aux; |
| 5911 |
} |
| 5912 |
if (x1 > image->width - 1) |
| 5913 |
crop->regionlist[i].x1 = image->width - 1; |
| 5914 |
else if (x1 > 0) |
| 5915 |
crop->regionlist[i].x1 = (uint32_t)(x1 - 1); |
| 5916 |
|
| 5917 |
if (x2 > image->width - 1) |
| 5918 |
crop->regionlist[i].x2 = image->width - 1; |
| 5919 |
else if (x2 > 0) |
| 5920 |
crop->regionlist[i].x2 = (uint32_t)(x2 - 1); |
| 5921 |
|
| 5922 |
zwidth = crop->regionlist[i].x2 - crop->regionlist[i].x1 + 1; |
| 5923 |
|
| 5924 |
if (y1 > image->length - 1) |
| 5925 |
crop->regionlist[i].y1 = image->length - 1; |
| 5926 |
else if (y1 > 0) |
| 5927 |
crop->regionlist[i].y1 = (uint32_t)(y1 - 1); |
| 5928 |
|
| 5929 |
if (y2 > image->length - 1) |
| 5930 |
crop->regionlist[i].y2 = image->length - 1; |
| 5931 |
else if (y2 > 0) |
| 5932 |
crop->regionlist[i].y2 = (uint32_t)(y2 - 1); |
| 5933 |
|
| 5934 |
zlength = crop->regionlist[i].y2 - crop->regionlist[i].y1 + 1; |
| 5935 |
if (zwidth > max_width) |
| 5936 |
max_width = zwidth; |
| 5937 |
if (zlength > max_length) |
| 5938 |
max_length = zlength; |
| 5939 |
|
| 5940 |
buffsize = (uint32_t)(((zwidth * image->bps * image->spp + 7) / 8) * |
| 5941 |
(zlength + 1)); |
| 5942 |
|
| 5943 |
crop->regionlist[i].buffsize = buffsize; |
| 5944 |
crop->bufftotal += buffsize; |
| 5945 |
|
| 5946 |
/* For composite images with more than one region, the |
| 5947 |
* combined_length or combined_width always needs to be equal, |
| 5948 |
* respectively. |
| 5949 |
* Otherwise, even the first section/region copy |
| 5950 |
* action might cause buffer overrun. */ |
| 5951 |
if (crop->img_mode == COMPOSITE_IMAGES) |
| 5952 |
{ |
| 5953 |
switch (crop->edge_ref) |
| 5954 |
{ |
| 5955 |
case EDGE_LEFT: |
| 5956 |
case EDGE_RIGHT: |
| 5957 |
if (i > 0 && zlength != crop->combined_length) |
| 5958 |
{ |
| 5959 |
TIFFError( |
| 5960 |
"computeInputPixelOffsets", |
| 5961 |
"Only equal length regions can be combined for " |
| 5962 |
"-E left or right"); |
| 5963 |
return (-1); |
| 5964 |
} |
| 5965 |
crop->combined_length = zlength; |
| 5966 |
crop->combined_width += zwidth; |
| 5967 |
break; |
| 5968 |
case EDGE_BOTTOM: |
| 5969 |
case EDGE_TOP: /* width from left, length from top */ |
| 5970 |
default: |
| 5971 |
if (i > 0 && zwidth != crop->combined_width) |
| 5972 |
{ |
| 5973 |
TIFFError("computeInputPixelOffsets", |
| 5974 |
"Only equal width regions can be " |
| 5975 |
"combined for -E " |
| 5976 |
"top or bottom"); |
| 5977 |
return (-1); |
| 5978 |
} |
| 5979 |
crop->combined_width = zwidth; |
| 5980 |
crop->combined_length += zlength; |
| 5981 |
break; |
| 5982 |
} |
| 5983 |
} |
| 5984 |
} |
| 5985 |
return (0); |
| 5986 |
} /* crop_mode == CROP_REGIONS */ |
| 5987 |
|
| 5988 |
/* Convert crop margins into offsets into image |
| 5989 |
* Margins are expressed as pixel rows and columns, not bytes |
| 5990 |
*/ |
| 5991 |
if (crop->crop_mode & CROP_MARGINS) |
| 5992 |
{ |
| 5993 |
if (crop->res_unit != RESUNIT_INCH && |
| 5994 |
crop->res_unit != RESUNIT_CENTIMETER) |
| 5995 |
{ /* User has specified pixels as reference unit */ |
| 5996 |
tmargin = _TIFFClampDoubleToUInt32(crop->margins[0]); |
| 5997 |
lmargin = _TIFFClampDoubleToUInt32(crop->margins[1]); |
| 5998 |
bmargin = _TIFFClampDoubleToUInt32(crop->margins[2]); |
| 5999 |
rmargin = _TIFFClampDoubleToUInt32(crop->margins[3]); |
| 6000 |
} |
| 6001 |
else |
| 6002 |
{ /* inches or centimeters specified */ |
| 6003 |
tmargin = _TIFFClampDoubleToUInt32(crop->margins[0] * scale * yres); |
| 6004 |
lmargin = _TIFFClampDoubleToUInt32(crop->margins[1] * scale * xres); |
| 6005 |
bmargin = _TIFFClampDoubleToUInt32(crop->margins[2] * scale * yres); |
| 6006 |
rmargin = _TIFFClampDoubleToUInt32(crop->margins[3] * scale * xres); |
| 6007 |
} |
| 6008 |
|
| 6009 |
if (lmargin == 0xFFFFFFFFU || rmargin == 0xFFFFFFFFU || |
| 6010 |
(lmargin + rmargin) > image->width) |
| 6011 |
{ |
| 6012 |
TIFFError("computeInputPixelOffsets", |
| 6013 |
"Combined left and right margins exceed image width"); |
| 6014 |
lmargin = (uint32_t)0; |
| 6015 |
rmargin = (uint32_t)0; |
| 6016 |
return (-1); |
| 6017 |
} |
| 6018 |
if (tmargin == 0xFFFFFFFFU || bmargin == 0xFFFFFFFFU || |
| 6019 |
(tmargin + bmargin) > image->length) |
| 6020 |
{ |
| 6021 |
TIFFError("computeInputPixelOffsets", |
| 6022 |
"Combined top and bottom margins exceed image length"); |
| 6023 |
tmargin = (uint32_t)0; |
| 6024 |
bmargin = (uint32_t)0; |
| 6025 |
return (-1); |
| 6026 |
} |
| 6027 |
} /* crop_mode == CROP_MARGINS */ |
| 6028 |
else |
| 6029 |
{ /* no margins requested */ |
| 6030 |
tmargin = (uint32_t)0; |
| 6031 |
lmargin = (uint32_t)0; |
| 6032 |
bmargin = (uint32_t)0; |
| 6033 |
rmargin = (uint32_t)0; |
| 6034 |
} |
| 6035 |
|
| 6036 |
/* Width, height, and margins are expressed as pixel offsets into image */ |
| 6037 |
if (crop->res_unit != RESUNIT_INCH && crop->res_unit != RESUNIT_CENTIMETER) |
| 6038 |
{ |
| 6039 |
if (crop->crop_mode & CROP_WIDTH) |
| 6040 |
width = _TIFFClampDoubleToUInt32(crop->width); |
| 6041 |
else |
| 6042 |
width = image->width - lmargin - rmargin; |
| 6043 |
|
| 6044 |
if (crop->crop_mode & CROP_LENGTH) |
| 6045 |
length = _TIFFClampDoubleToUInt32(crop->length); |
| 6046 |
else |
| 6047 |
length = image->length - tmargin - bmargin; |
| 6048 |
} |
| 6049 |
else |
| 6050 |
{ |
| 6051 |
if (crop->crop_mode & CROP_WIDTH) |
| 6052 |
width = _TIFFClampDoubleToUInt32(crop->width * scale * image->xres); |
| 6053 |
else |
| 6054 |
width = image->width - lmargin - rmargin; |
| 6055 |
|
| 6056 |
if (crop->crop_mode & CROP_LENGTH) |
| 6057 |
length = |
| 6058 |
_TIFFClampDoubleToUInt32(crop->length * scale * image->yres); |
| 6059 |
else |
| 6060 |
length = image->length - tmargin - bmargin; |
| 6061 |
} |
| 6062 |
|
| 6063 |
off->tmargin = tmargin; |
| 6064 |
off->bmargin = bmargin; |
| 6065 |
off->lmargin = lmargin; |
| 6066 |
off->rmargin = rmargin; |
| 6067 |
|
| 6068 |
/* Calculate regions defined by margins, width, and length. |
| 6069 |
* Coordinates expressed as 0 to imagewidth - 1, imagelength - 1, |
| 6070 |
* since they are used to compute offsets into buffers */ |
| 6071 |
switch (crop->edge_ref) |
| 6072 |
{ |
| 6073 |
case EDGE_BOTTOM: |
| 6074 |
startx = lmargin; |
| 6075 |
if ((startx + width) >= (image->width - rmargin)) |
| 6076 |
endx = image->width - rmargin - 1; |
| 6077 |
else |
| 6078 |
endx = startx + width - 1; |
| 6079 |
|
| 6080 |
endy = image->length - bmargin - 1; |
| 6081 |
if ((endy - length) <= tmargin) |
| 6082 |
starty = tmargin; |
| 6083 |
else |
| 6084 |
starty = endy - length + 1; |
| 6085 |
break; |
| 6086 |
case EDGE_RIGHT: |
| 6087 |
endx = image->width - rmargin - 1; |
| 6088 |
if ((endx - width) <= lmargin) |
| 6089 |
startx = lmargin; |
| 6090 |
else |
| 6091 |
startx = endx - width + 1; |
| 6092 |
|
| 6093 |
starty = tmargin; |
| 6094 |
if ((starty + length) >= (image->length - bmargin)) |
| 6095 |
endy = image->length - bmargin - 1; |
| 6096 |
else |
| 6097 |
endy = starty + length - 1; |
| 6098 |
break; |
| 6099 |
case EDGE_TOP: /* width from left, length from top */ |
| 6100 |
case EDGE_LEFT: |
| 6101 |
default: |
| 6102 |
startx = lmargin; |
| 6103 |
if ((startx + width) >= (image->width - rmargin)) |
| 6104 |
endx = image->width - rmargin - 1; |
| 6105 |
else |
| 6106 |
endx = startx + width - 1; |
| 6107 |
|
| 6108 |
starty = tmargin; |
| 6109 |
if ((starty + length) >= (image->length - bmargin)) |
| 6110 |
endy = image->length - bmargin - 1; |
| 6111 |
else |
| 6112 |
endy = starty + length - 1; |
| 6113 |
break; |
| 6114 |
} |
| 6115 |
off->startx = startx; |
| 6116 |
off->starty = starty; |
| 6117 |
off->endx = endx; |
| 6118 |
off->endy = endy; |
| 6119 |
|
| 6120 |
if (endx + 1 <= startx) |
| 6121 |
{ |
| 6122 |
TIFFError( |
| 6123 |
"computeInputPixelOffsets", |
| 6124 |
"Invalid left/right margins and /or image crop width requested"); |
| 6125 |
return (-1); |
| 6126 |
} |
| 6127 |
crop_width = endx - startx + 1; |
| 6128 |
if (crop_width > image->width) |
| 6129 |
crop_width = image->width; |
| 6130 |
|
| 6131 |
if (endy + 1 <= starty) |
| 6132 |
{ |
| 6133 |
TIFFError( |
| 6134 |
"computeInputPixelOffsets", |
| 6135 |
"Invalid top/bottom margins and /or image crop length requested"); |
| 6136 |
return (-1); |
| 6137 |
} |
| 6138 |
crop_length = endy - starty + 1; |
| 6139 |
if (crop_length > image->length) |
| 6140 |
crop_length = image->length; |
| 6141 |
|
| 6142 |
off->crop_width = crop_width; |
| 6143 |
off->crop_length = crop_length; |
| 6144 |
|
| 6145 |
return (0); |
| 6146 |
} /* end computeInputPixelOffsets */ |
| 6147 |
|
| 6148 |
/* |
| 6149 |
* Translate crop options into pixel offsets for one or more regions of the |
| 6150 |
* image. Options are applied in this order: margins, specific width and length, |
| 6151 |
* zones, but all are optional. Margins are relative to each edge. Width, length |
| 6152 |
* and zones are relative to the specified reference edge. Zones are expressed |
| 6153 |
* as X:Y where X is the ordinal value in a set of Y equal sized portions. eg. |
| 6154 |
* 2:3 would indicate the middle third of the region qualified by margins and |
| 6155 |
* any explicit width and length specified. Regions are specified by coordinates |
| 6156 |
* of the top left and lower right corners with range 1 to width or height. |
| 6157 |
*/ |
| 6158 |
|
| 6159 |
static int getCropOffsets(struct image_data *image, struct crop_mask *crop, |
| 6160 |
struct dump_opts *dump) |
| 6161 |
{ |
| 6162 |
struct offset offsets; |
| 6163 |
int i; |
| 6164 |
uint32_t uaux; |
| 6165 |
uint32_t seg, total, need_buff = 0; |
| 6166 |
uint32_t buffsize; |
| 6167 |
uint32_t zwidth, zlength; |
| 6168 |
|
| 6169 |
memset(&offsets, '\0', sizeof(struct offset)); |
| 6170 |
crop->bufftotal = 0; |
| 6171 |
crop->combined_width = (uint32_t)0; |
| 6172 |
crop->combined_length = (uint32_t)0; |
| 6173 |
crop->selections = 0; |
| 6174 |
|
| 6175 |
/* Compute pixel offsets if margins or fixed width or length specified */ |
| 6176 |
if ((crop->crop_mode & CROP_MARGINS) || (crop->crop_mode & CROP_REGIONS) || |
| 6177 |
(crop->crop_mode & CROP_LENGTH) || (crop->crop_mode & CROP_WIDTH)) |
| 6178 |
{ |
| 6179 |
if (computeInputPixelOffsets(crop, image, &offsets)) |
| 6180 |
{ |
| 6181 |
TIFFError("getCropOffsets", "Unable to compute crop margins"); |
| 6182 |
return (-1); |
| 6183 |
} |
| 6184 |
need_buff = TRUE; |
| 6185 |
crop->selections = crop->regions; |
| 6186 |
/* Regions are only calculated from top and left edges with no margins |
| 6187 |
*/ |
| 6188 |
if (crop->crop_mode & CROP_REGIONS) |
| 6189 |
return (0); |
| 6190 |
} |
| 6191 |
else |
| 6192 |
{ /* cropped area is the full image */ |
| 6193 |
offsets.tmargin = 0; |
| 6194 |
offsets.lmargin = 0; |
| 6195 |
offsets.bmargin = 0; |
| 6196 |
offsets.rmargin = 0; |
| 6197 |
offsets.crop_width = image->width; |
| 6198 |
offsets.crop_length = image->length; |
| 6199 |
offsets.startx = 0; |
| 6200 |
offsets.endx = image->width - 1; |
| 6201 |
offsets.starty = 0; |
| 6202 |
offsets.endy = image->length - 1; |
| 6203 |
need_buff = FALSE; |
| 6204 |
} |
| 6205 |
|
| 6206 |
if (dump->outfile != NULL) |
| 6207 |
{ |
| 6208 |
dump_info(dump->outfile, dump->format, "", |
| 6209 |
"Margins: Top: %" PRIu32 " Left: %" PRIu32 |
| 6210 |
" Bottom: %" PRIu32 " Right: %" PRIu32, |
| 6211 |
offsets.tmargin, offsets.lmargin, offsets.bmargin, |
| 6212 |
offsets.rmargin); |
| 6213 |
dump_info(dump->outfile, dump->format, "", |
| 6214 |
"Crop region within margins: Adjusted Width: %6" PRIu32 |
| 6215 |
" Length: %6" PRIu32, |
| 6216 |
offsets.crop_width, offsets.crop_length); |
| 6217 |
} |
| 6218 |
|
| 6219 |
if (!(crop->crop_mode & CROP_ZONES)) /* no crop zones requested */ |
| 6220 |
{ |
| 6221 |
if (need_buff == FALSE) /* No margins or fixed width or length areas */ |
| 6222 |
{ |
| 6223 |
crop->selections = 0; |
| 6224 |
crop->combined_width = image->width; |
| 6225 |
crop->combined_length = image->length; |
| 6226 |
return (0); |
| 6227 |
} |
| 6228 |
else |
| 6229 |
{ |
| 6230 |
/* Use one region for margins and fixed width or length areas |
| 6231 |
* even though it was not formally declared as a region. |
| 6232 |
*/ |
| 6233 |
crop->selections = 1; |
| 6234 |
crop->zones = 1; |
| 6235 |
crop->zonelist[0].total = 1; |
| 6236 |
crop->zonelist[0].position = 1; |
| 6237 |
} |
| 6238 |
} |
| 6239 |
else |
| 6240 |
crop->selections = crop->zones; |
| 6241 |
|
| 6242 |
/* Initialize regions iterator i */ |
| 6243 |
i = 0; |
| 6244 |
for (int j = 0; j < crop->zones; j++) |
| 6245 |
{ |
| 6246 |
seg = crop->zonelist[j].position; |
| 6247 |
total = crop->zonelist[j].total; |
| 6248 |
|
| 6249 |
/* check for not allowed zone cases like 0:0; 4:3; or negative ones etc. |
| 6250 |
* and skip that input */ |
| 6251 |
if (crop->zonelist[j].position < 0 || crop->zonelist[j].total < 0) |
| 6252 |
{ |
| 6253 |
TIFFError("getCropOffsets", |
| 6254 |
"Negative crop zone values %d:%d are not allowed, thus " |
| 6255 |
"skipped.", |
| 6256 |
crop->zonelist[j].position, crop->zonelist[j].total); |
| 6257 |
continue; |
| 6258 |
} |
| 6259 |
if (seg == 0 || total == 0 || seg > total) |
| 6260 |
{ |
| 6261 |
TIFFError("getCropOffsets", |
| 6262 |
"Crop zone %d:%d is out of specification, thus skipped.", |
| 6263 |
seg, total); |
| 6264 |
continue; |
| 6265 |
} |
| 6266 |
|
| 6267 |
switch (crop->edge_ref) |
| 6268 |
{ |
| 6269 |
case EDGE_LEFT: /* zones from left to right, length from top */ |
| 6270 |
zlength = offsets.crop_length; |
| 6271 |
crop->regionlist[i].y1 = offsets.starty; |
| 6272 |
crop->regionlist[i].y2 = offsets.endy; |
| 6273 |
|
| 6274 |
crop->regionlist[i].x1 = |
| 6275 |
offsets.startx + |
| 6276 |
(uint32_t)(offsets.crop_width * 1.0 * (seg - 1) / total); |
| 6277 |
/* FAULT: IMHO in the old code here, the calculation of x2 was |
| 6278 |
* based on wrong assumptions. The whole image was assumed and |
| 6279 |
* 'endy' and 'starty' are not respected anymore!*/ |
| 6280 |
/* NEW PROPOSED Code: Assumption: offsets are within image with |
| 6281 |
* top left corner as origin (0,0) and 'start' <= 'end'. */ |
| 6282 |
if (crop->regionlist[i].x1 > offsets.endx) |
| 6283 |
{ |
| 6284 |
crop->regionlist[i].x1 = offsets.endx; |
| 6285 |
} |
| 6286 |
else if (crop->regionlist[i].x1 >= image->width) |
| 6287 |
{ |
| 6288 |
crop->regionlist[i].x1 = image->width - 1; |
| 6289 |
} |
| 6290 |
|
| 6291 |
crop->regionlist[i].x2 = |
| 6292 |
offsets.startx + |
| 6293 |
(uint32_t)(offsets.crop_width * 1.0 * seg / total); |
| 6294 |
if (crop->regionlist[i].x2 > 0) |
| 6295 |
crop->regionlist[i].x2 = crop->regionlist[i].x2 - 1; |
| 6296 |
if (crop->regionlist[i].x2 < crop->regionlist[i].x1) |
| 6297 |
{ |
| 6298 |
crop->regionlist[i].x2 = crop->regionlist[i].x1; |
| 6299 |
} |
| 6300 |
else if (crop->regionlist[i].x2 > offsets.endx) |
| 6301 |
{ |
| 6302 |
crop->regionlist[i].x2 = offsets.endx; |
| 6303 |
} |
| 6304 |
else if (crop->regionlist[i].x2 >= image->width) |
| 6305 |
{ |
| 6306 |
crop->regionlist[i].x2 = image->width - 1; |
| 6307 |
} |
| 6308 |
zwidth = crop->regionlist[i].x2 - crop->regionlist[i].x1 + 1; |
| 6309 |
|
| 6310 |
/* This is passed to extractCropZone or extractCompositeZones */ |
| 6311 |
crop->combined_length = (uint32_t)zlength; |
| 6312 |
if (crop->exp_mode == COMPOSITE_IMAGES) |
| 6313 |
crop->combined_width += (uint32_t)zwidth; |
| 6314 |
else |
| 6315 |
crop->combined_width = (uint32_t)zwidth; |
| 6316 |
|
| 6317 |
/* When the degrees clockwise rotation is 90 or 270, check the |
| 6318 |
* boundary */ |
| 6319 |
if (((crop->rotation == 90) || (crop->rotation == 270)) && |
| 6320 |
((crop->combined_length > image->width) || |
| 6321 |
(crop->combined_width > image->length))) |
| 6322 |
{ |
| 6323 |
TIFFError("getCropOffsets", |
| 6324 |
"The crop size exceeds the image boundary size"); |
| 6325 |
return -1; |
| 6326 |
} |
| 6327 |
|
| 6328 |
break; |
| 6329 |
case EDGE_BOTTOM: /* width from left, zones from bottom to top */ |
| 6330 |
zwidth = offsets.crop_width; |
| 6331 |
crop->regionlist[i].x1 = offsets.startx; |
| 6332 |
crop->regionlist[i].x2 = offsets.endx; |
| 6333 |
|
| 6334 |
/* FAULT: IMHO in the old code here, the calculation of y1/y2 |
| 6335 |
* was based on wrong assumptions. The whole image was assumed |
| 6336 |
* and 'endy' and 'starty' are not respected anymore!*/ |
| 6337 |
/* NEW PROPOSED Code: Assumption: offsets are within image with |
| 6338 |
* top left corner as origin (0,0) and 'start' <= 'end'. */ |
| 6339 |
uaux = (uint32_t)(offsets.crop_length * 1.0 * seg / total); |
| 6340 |
if (uaux <= offsets.endy + 1) |
| 6341 |
{ |
| 6342 |
crop->regionlist[i].y1 = offsets.endy - uaux + 1; |
| 6343 |
} |
| 6344 |
else |
| 6345 |
{ |
| 6346 |
crop->regionlist[i].y1 = 0; |
| 6347 |
} |
| 6348 |
if (crop->regionlist[i].y1 < offsets.starty) |
| 6349 |
{ |
| 6350 |
crop->regionlist[i].y1 = offsets.starty; |
| 6351 |
} |
| 6352 |
|
| 6353 |
uaux = |
| 6354 |
(uint32_t)(offsets.crop_length * 1.0 * (seg - 1) / total); |
| 6355 |
if (uaux <= offsets.endy) |
| 6356 |
{ |
| 6357 |
crop->regionlist[i].y2 = offsets.endy - uaux; |
| 6358 |
} |
| 6359 |
else |
| 6360 |
{ |
| 6361 |
crop->regionlist[i].y2 = 0; |
| 6362 |
} |
| 6363 |
if (crop->regionlist[i].y2 < offsets.starty) |
| 6364 |
{ |
| 6365 |
crop->regionlist[i].y2 = offsets.starty; |
| 6366 |
} |
| 6367 |
zlength = crop->regionlist[i].y2 - crop->regionlist[i].y1 + 1; |
| 6368 |
|
| 6369 |
/* This is passed to extractCropZone or extractCompositeZones */ |
| 6370 |
if (crop->exp_mode == COMPOSITE_IMAGES) |
| 6371 |
crop->combined_length += (uint32_t)zlength; |
| 6372 |
else |
| 6373 |
crop->combined_length = (uint32_t)zlength; |
| 6374 |
crop->combined_width = (uint32_t)zwidth; |
| 6375 |
|
| 6376 |
/* When the degrees clockwise rotation is 90 or 270, check the |
| 6377 |
* boundary */ |
| 6378 |
if (((crop->rotation == 90) || (crop->rotation == 270)) && |
| 6379 |
((crop->combined_length > image->width) || |
| 6380 |
(crop->combined_width > image->length))) |
| 6381 |
{ |
| 6382 |
TIFFError("getCropOffsets", |
| 6383 |
"The crop size exceeds the image boundary size"); |
| 6384 |
return -1; |
| 6385 |
} |
| 6386 |
|
| 6387 |
break; |
| 6388 |
case EDGE_RIGHT: /* zones from right to left, length from top */ |
| 6389 |
zlength = offsets.crop_length; |
| 6390 |
crop->regionlist[i].y1 = offsets.starty; |
| 6391 |
crop->regionlist[i].y2 = offsets.endy; |
| 6392 |
|
| 6393 |
crop->regionlist[i].x1 = |
| 6394 |
offsets.startx + (uint32_t)(offsets.crop_width * |
| 6395 |
(total - seg) * 1.0 / total); |
| 6396 |
/* FAULT: IMHO from here on, the calculation of y2 are based on |
| 6397 |
* wrong assumptions. The whole image is assumed and 'endy' and |
| 6398 |
* 'starty' are not respected anymore!*/ |
| 6399 |
/* NEW PROPOSED Code: Assumption: offsets are within image with |
| 6400 |
* top left corner as origin (0,0) and 'start' <= 'end'. */ |
| 6401 |
uaux = (uint32_t)(offsets.crop_width * 1.0 * seg / total); |
| 6402 |
if (uaux <= offsets.endx + 1) |
| 6403 |
{ |
| 6404 |
crop->regionlist[i].x1 = offsets.endx - uaux + 1; |
| 6405 |
} |
| 6406 |
else |
| 6407 |
{ |
| 6408 |
crop->regionlist[i].x1 = 0; |
| 6409 |
} |
| 6410 |
if (crop->regionlist[i].x1 < offsets.startx) |
| 6411 |
{ |
| 6412 |
crop->regionlist[i].x1 = offsets.startx; |
| 6413 |
} |
| 6414 |
|
| 6415 |
uaux = (uint32_t)(offsets.crop_width * 1.0 * (seg - 1) / total); |
| 6416 |
if (uaux <= offsets.endx) |
| 6417 |
{ |
| 6418 |
crop->regionlist[i].x2 = offsets.endx - uaux; |
| 6419 |
} |
| 6420 |
else |
| 6421 |
{ |
| 6422 |
crop->regionlist[i].x2 = 0; |
| 6423 |
} |
| 6424 |
if (crop->regionlist[i].x2 < offsets.startx) |
| 6425 |
{ |
| 6426 |
crop->regionlist[i].x2 = offsets.startx; |
| 6427 |
} |
| 6428 |
zwidth = crop->regionlist[i].x2 - crop->regionlist[i].x1 + 1; |
| 6429 |
|
| 6430 |
/* This is passed to extractCropZone or extractCompositeZones */ |
| 6431 |
crop->combined_length = (uint32_t)zlength; |
| 6432 |
if (crop->exp_mode == COMPOSITE_IMAGES) |
| 6433 |
crop->combined_width += (uint32_t)zwidth; |
| 6434 |
else |
| 6435 |
crop->combined_width = (uint32_t)zwidth; |
| 6436 |
|
| 6437 |
/* When the degrees clockwise rotation is 90 or 270, check the |
| 6438 |
* boundary */ |
| 6439 |
if (((crop->rotation == 90) || (crop->rotation == 270)) && |
| 6440 |
((crop->combined_length > image->width) || |
| 6441 |
(crop->combined_width > image->length))) |
| 6442 |
{ |
| 6443 |
TIFFError("getCropOffsets", |
| 6444 |
"The crop size exceeds the image boundary size"); |
| 6445 |
return -1; |
| 6446 |
} |
| 6447 |
|
| 6448 |
break; |
| 6449 |
case EDGE_TOP: /* width from left, zones from top to bottom */ |
| 6450 |
default: |
| 6451 |
zwidth = offsets.crop_width; |
| 6452 |
crop->regionlist[i].x1 = offsets.startx; |
| 6453 |
crop->regionlist[i].x2 = offsets.endx; |
| 6454 |
|
| 6455 |
crop->regionlist[i].y1 = |
| 6456 |
offsets.starty + |
| 6457 |
(uint32_t)(offsets.crop_length * 1.0 * (seg - 1) / total); |
| 6458 |
if (crop->regionlist[i].y1 > offsets.endy) |
| 6459 |
{ |
| 6460 |
crop->regionlist[i].y1 = offsets.endy; |
| 6461 |
} |
| 6462 |
else if (crop->regionlist[i].y1 >= image->length) |
| 6463 |
{ |
| 6464 |
crop->regionlist[i].y1 = image->length - 1; |
| 6465 |
} |
| 6466 |
|
| 6467 |
/* FAULT: IMHO from here on, the calculation of y2 are based on |
| 6468 |
* wrong assumptions. The whole image is assumed and 'endy' and |
| 6469 |
* 'starty' are not respected anymore!*/ |
| 6470 |
/* OLD Code: |
| 6471 |
test = offsets.starty + (uint32_t)(offsets.crop_length * 1.0 * |
| 6472 |
seg / total); if (test < 1 ) crop->regionlist[i].y2 = 0; else |
| 6473 |
{ |
| 6474 |
if (test > (int32_t)(image->length - 1)) |
| 6475 |
crop->regionlist[i].y2 = image->length - 1; |
| 6476 |
else |
| 6477 |
crop->regionlist[i].y2 = test - 1; |
| 6478 |
} |
| 6479 |
*/ |
| 6480 |
/* NEW PROPOSED Code: Assumption: offsets are within image with |
| 6481 |
* top left corner as origin (0,0) and 'start' <= 'end'. */ |
| 6482 |
crop->regionlist[i].y2 = |
| 6483 |
offsets.starty + |
| 6484 |
(uint32_t)(offsets.crop_length * 1.0 * seg / total); |
| 6485 |
if (crop->regionlist[i].y2 > 0) |
| 6486 |
crop->regionlist[i].y2 = crop->regionlist[i].y2 - 1; |
| 6487 |
if (crop->regionlist[i].y2 < crop->regionlist[i].y1) |
| 6488 |
{ |
| 6489 |
crop->regionlist[i].y2 = crop->regionlist[i].y1; |
| 6490 |
} |
| 6491 |
else if (crop->regionlist[i].y2 > offsets.endy) |
| 6492 |
{ |
| 6493 |
crop->regionlist[i].y2 = offsets.endy; |
| 6494 |
} |
| 6495 |
else if (crop->regionlist[i].y2 >= image->length) |
| 6496 |
{ |
| 6497 |
crop->regionlist[i].y2 = image->length - 1; |
| 6498 |
} |
| 6499 |
|
| 6500 |
zlength = crop->regionlist[i].y2 - crop->regionlist[i].y1 + 1; |
| 6501 |
|
| 6502 |
/* This is passed to extractCropZone or extractCompositeZones */ |
| 6503 |
if (crop->exp_mode == COMPOSITE_IMAGES) |
| 6504 |
crop->combined_length += (uint32_t)zlength; |
| 6505 |
else |
| 6506 |
crop->combined_length = (uint32_t)zlength; |
| 6507 |
crop->combined_width = (uint32_t)zwidth; |
| 6508 |
|
| 6509 |
/* When the degrees clockwise rotation is 90 or 270, check the |
| 6510 |
* boundary */ |
| 6511 |
if (((crop->rotation == 90) || (crop->rotation == 270)) && |
| 6512 |
((crop->combined_length > image->width) || |
| 6513 |
(crop->combined_width > image->length))) |
| 6514 |
{ |
| 6515 |
TIFFError("getCropOffsets", |
| 6516 |
"The crop size exceeds the image boundary size"); |
| 6517 |
return -1; |
| 6518 |
} |
| 6519 |
|
| 6520 |
break; |
| 6521 |
} /* end switch statement */ |
| 6522 |
|
| 6523 |
buffsize = (uint32_t)((((zwidth * image->bps * image->spp) + 7) / 8) * |
| 6524 |
(zlength + 1)); |
| 6525 |
crop->regionlist[i].width = (uint32_t)zwidth; |
| 6526 |
crop->regionlist[i].length = (uint32_t)zlength; |
| 6527 |
crop->regionlist[i].buffsize = buffsize; |
| 6528 |
crop->bufftotal += buffsize; |
| 6529 |
|
| 6530 |
if (dump->outfile != NULL) |
| 6531 |
dump_info(dump->outfile, dump->format, "", |
| 6532 |
"Zone %d, width: %4" PRIu32 ", length: %4" PRIu32 |
| 6533 |
", x1: %4" PRIu32 " x2: %4" PRIu32 " y1: %4" PRIu32 |
| 6534 |
" y2: %4" PRIu32, |
| 6535 |
i + 1, zwidth, zlength, crop->regionlist[i].x1, |
| 6536 |
crop->regionlist[i].x2, crop->regionlist[i].y1, |
| 6537 |
crop->regionlist[i].y2); |
| 6538 |
/* increment regions iterator */ |
| 6539 |
i++; |
| 6540 |
} |
| 6541 |
/* set number of generated regions out of given zones */ |
| 6542 |
crop->selections = i; |
| 6543 |
return (0); |
| 6544 |
} /* end getCropOffsets */ |
| 6545 |
|
| 6546 |
static int computeOutputPixelOffsets(struct crop_mask *crop, |
| 6547 |
struct image_data *image, |
| 6548 |
struct pagedef *page, |
| 6549 |
struct pageseg *sections, |
| 6550 |
struct dump_opts *dump) |
| 6551 |
{ |
| 6552 |
double scale; |
| 6553 |
double pwidth, plength; /* Output page width and length in user units*/ |
| 6554 |
uint32_t iwidth, ilength; /* Input image width and length in pixels*/ |
| 6555 |
uint32_t owidth, olength; /* Output image width and length in pixels*/ |
| 6556 |
uint32_t orows, ocols; /* rows and cols for output */ |
| 6557 |
uint32_t hmargin, vmargin; /* Horizontal and vertical margins */ |
| 6558 |
uint32_t x1, x2, y1, y2, line_bytes; |
| 6559 |
/* unsigned int orientation; */ |
| 6560 |
uint32_t i, j, k; |
| 6561 |
|
| 6562 |
scale = 1.0; |
| 6563 |
if (page->res_unit == RESUNIT_NONE) |
| 6564 |
page->res_unit = image->res_unit; |
| 6565 |
|
| 6566 |
switch (image->res_unit) |
| 6567 |
{ |
| 6568 |
case RESUNIT_CENTIMETER: |
| 6569 |
if (page->res_unit == RESUNIT_INCH) |
| 6570 |
scale = 1.0 / 2.54; |
| 6571 |
break; |
| 6572 |
case RESUNIT_INCH: |
| 6573 |
if (page->res_unit == RESUNIT_CENTIMETER) |
| 6574 |
scale = 2.54; |
| 6575 |
break; |
| 6576 |
case RESUNIT_NONE: /* Dimensions in pixels */ |
| 6577 |
default: |
| 6578 |
break; |
| 6579 |
} |
| 6580 |
|
| 6581 |
/* get width, height, resolutions of input image selection */ |
| 6582 |
if (crop->combined_width > 0) |
| 6583 |
iwidth = crop->combined_width; |
| 6584 |
else |
| 6585 |
iwidth = image->width; |
| 6586 |
if (crop->combined_length > 0) |
| 6587 |
ilength = crop->combined_length; |
| 6588 |
else |
| 6589 |
ilength = image->length; |
| 6590 |
|
| 6591 |
if (page->hres <= 1.0) |
| 6592 |
page->hres = image->xres; |
| 6593 |
if (page->vres <= 1.0) |
| 6594 |
page->vres = image->yres; |
| 6595 |
|
| 6596 |
if ((page->hres < 1.0) || (page->vres < 1.0)) |
| 6597 |
{ |
| 6598 |
TIFFError("computeOutputPixelOffsets", |
| 6599 |
"Invalid horizontal or vertical resolution specified or read " |
| 6600 |
"from input image"); |
| 6601 |
return (1); |
| 6602 |
} |
| 6603 |
|
| 6604 |
/* If no page sizes are being specified, we just use the input image size to |
| 6605 |
* calculate maximum margins that can be taken from image. |
| 6606 |
*/ |
| 6607 |
if (page->width <= 0) |
| 6608 |
pwidth = iwidth; |
| 6609 |
else |
| 6610 |
pwidth = page->width; |
| 6611 |
|
| 6612 |
if (page->length <= 0) |
| 6613 |
plength = ilength; |
| 6614 |
else |
| 6615 |
plength = page->length; |
| 6616 |
|
| 6617 |
if (dump->debug) |
| 6618 |
{ |
| 6619 |
TIFFError("", |
| 6620 |
"Page size: %s, Vres: %3.2f, Hres: %3.2f, " |
| 6621 |
"Hmargin: %3.2f, Vmargin: %3.2f", |
| 6622 |
page->name, page->vres, page->hres, page->hmargin, |
| 6623 |
page->vmargin); |
| 6624 |
TIFFError("", |
| 6625 |
"Res_unit: %" PRIu16 |
| 6626 |
", Scale: %3.2f, Page width: %3.2f, length: %3.2f", |
| 6627 |
page->res_unit, scale, pwidth, plength); |
| 6628 |
} |
| 6629 |
|
| 6630 |
/* compute margins at specified unit and resolution */ |
| 6631 |
if (page->mode & PAGE_MODE_MARGINS) |
| 6632 |
{ |
| 6633 |
if (page->res_unit == RESUNIT_INCH || |
| 6634 |
page->res_unit == RESUNIT_CENTIMETER) |
| 6635 |
{ /* inches or centimeters specified */ |
| 6636 |
hmargin = _TIFFClampDoubleToUInt32( |
| 6637 |
page->hmargin * scale * page->hres * ((image->bps + 7) / 8)); |
| 6638 |
vmargin = _TIFFClampDoubleToUInt32( |
| 6639 |
page->vmargin * scale * page->vres * ((image->bps + 7) / 8)); |
| 6640 |
} |
| 6641 |
else |
| 6642 |
{ /* Otherwise user has specified pixels as reference unit */ |
| 6643 |
hmargin = _TIFFClampDoubleToUInt32(page->hmargin * scale * |
| 6644 |
((image->bps + 7) / 8)); |
| 6645 |
vmargin = _TIFFClampDoubleToUInt32(page->vmargin * scale * |
| 6646 |
((image->bps + 7) / 8)); |
| 6647 |
} |
| 6648 |
|
| 6649 |
if (hmargin == 0xFFFFFFFFU || (hmargin * 2.0) > (pwidth * page->hres)) |
| 6650 |
{ |
| 6651 |
TIFFError("computeOutputPixelOffsets", |
| 6652 |
"Combined left and right margins exceed page width"); |
| 6653 |
hmargin = (uint32_t)0; |
| 6654 |
return (-1); |
| 6655 |
} |
| 6656 |
if (vmargin == 0xFFFFFFFFU || (vmargin * 2.0) > (plength * page->vres)) |
| 6657 |
{ |
| 6658 |
TIFFError("computeOutputPixelOffsets", |
| 6659 |
"Combined top and bottom margins exceed page length"); |
| 6660 |
vmargin = (uint32_t)0; |
| 6661 |
return (-1); |
| 6662 |
} |
| 6663 |
} |
| 6664 |
else |
| 6665 |
{ |
| 6666 |
hmargin = 0; |
| 6667 |
vmargin = 0; |
| 6668 |
} |
| 6669 |
|
| 6670 |
if (page->mode & PAGE_MODE_ROWSCOLS) |
| 6671 |
{ |
| 6672 |
/* Maybe someday but not for now */ |
| 6673 |
if (page->mode & PAGE_MODE_MARGINS) |
| 6674 |
TIFFError( |
| 6675 |
"computeOutputPixelOffsets", |
| 6676 |
"Output margins cannot be specified with rows and columns"); |
| 6677 |
|
| 6678 |
owidth = TIFFhowmany(iwidth, page->cols); |
| 6679 |
olength = TIFFhowmany(ilength, page->rows); |
| 6680 |
} |
| 6681 |
else |
| 6682 |
{ |
| 6683 |
if (page->mode & PAGE_MODE_PAPERSIZE) |
| 6684 |
{ |
| 6685 |
owidth = |
| 6686 |
_TIFFClampDoubleToUInt32((pwidth * page->hres) - (hmargin * 2)); |
| 6687 |
olength = _TIFFClampDoubleToUInt32((plength * page->vres) - |
| 6688 |
(vmargin * 2)); |
| 6689 |
} |
| 6690 |
else |
| 6691 |
{ |
| 6692 |
owidth = |
| 6693 |
_TIFFClampDoubleToUInt32(iwidth - (hmargin * 2 * page->hres)); |
| 6694 |
olength = |
| 6695 |
_TIFFClampDoubleToUInt32(ilength - (vmargin * 2 * page->vres)); |
| 6696 |
} |
| 6697 |
} |
| 6698 |
|
| 6699 |
if (owidth > iwidth) |
| 6700 |
owidth = iwidth; |
| 6701 |
if (olength > ilength) |
| 6702 |
olength = ilength; |
| 6703 |
|
| 6704 |
if (owidth == 0 || olength == 0) |
| 6705 |
{ |
| 6706 |
TIFFError("computeOutputPixelOffsets", |
| 6707 |
"Integer overflow when calculating the number of pages"); |
| 6708 |
exit(EXIT_FAILURE); |
| 6709 |
} |
| 6710 |
|
| 6711 |
/* Compute the number of pages required for Portrait or Landscape */ |
| 6712 |
switch (page->orient) |
| 6713 |
{ |
| 6714 |
case ORIENTATION_NONE: |
| 6715 |
case ORIENTATION_PORTRAIT: |
| 6716 |
ocols = TIFFhowmany(iwidth, owidth); |
| 6717 |
orows = TIFFhowmany(ilength, olength); |
| 6718 |
/* orientation = ORIENTATION_PORTRAIT; */ |
| 6719 |
break; |
| 6720 |
|
| 6721 |
case ORIENTATION_LANDSCAPE: |
| 6722 |
ocols = TIFFhowmany(iwidth, olength); |
| 6723 |
orows = TIFFhowmany(ilength, owidth); |
| 6724 |
x1 = olength; |
| 6725 |
olength = owidth; |
| 6726 |
owidth = x1; |
| 6727 |
/* orientation = ORIENTATION_LANDSCAPE; */ |
| 6728 |
break; |
| 6729 |
|
| 6730 |
case ORIENTATION_AUTO: |
| 6731 |
default: |
| 6732 |
x1 = TIFFhowmany(iwidth, owidth); |
| 6733 |
x2 = TIFFhowmany(ilength, olength); |
| 6734 |
y1 = TIFFhowmany(iwidth, olength); |
| 6735 |
y2 = TIFFhowmany(ilength, owidth); |
| 6736 |
|
| 6737 |
if ((x1 * x2) < (y1 * y2)) |
| 6738 |
{ /* Portrait */ |
| 6739 |
ocols = x1; |
| 6740 |
orows = x2; |
| 6741 |
/* orientation = ORIENTATION_PORTRAIT; */ |
| 6742 |
} |
| 6743 |
else |
| 6744 |
{ /* Landscape */ |
| 6745 |
ocols = y1; |
| 6746 |
orows = y2; |
| 6747 |
x1 = olength; |
| 6748 |
olength = owidth; |
| 6749 |
owidth = x1; |
| 6750 |
/* orientation = ORIENTATION_LANDSCAPE; */ |
| 6751 |
} |
| 6752 |
} |
| 6753 |
|
| 6754 |
if (ocols < 1) |
| 6755 |
ocols = 1; |
| 6756 |
if (orows < 1) |
| 6757 |
orows = 1; |
| 6758 |
|
| 6759 |
/* Always return rows and cols from calcuation above. |
| 6760 |
* (correct values needed external to this function) |
| 6761 |
* Warn, if user input settings has been changed. |
| 6762 |
*/ |
| 6763 |
|
| 6764 |
if ((page->rows > 0) && (page->rows != orows)) |
| 6765 |
{ |
| 6766 |
TIFFError("computeOutputPixelOffsets", |
| 6767 |
"Number of user input section rows down (%" PRIu32 |
| 6768 |
") was changed to (%" PRIu32 ")", |
| 6769 |
page->rows, orows); |
| 6770 |
} |
| 6771 |
page->rows = orows; |
| 6772 |
if ((page->cols > 0) && (page->cols != ocols)) |
| 6773 |
{ |
| 6774 |
TIFFError("computeOutputPixelOffsets", |
| 6775 |
"Number of user input section cols across (%" PRIu32 |
| 6776 |
") was changed to (%" PRIu32 ")", |
| 6777 |
page->cols, ocols); |
| 6778 |
} |
| 6779 |
page->cols = ocols; |
| 6780 |
|
| 6781 |
line_bytes = TIFFhowmany8(owidth * image->spp * image->bps); |
| 6782 |
|
| 6783 |
if ((orows * ocols) > MAX_SECTIONS) |
| 6784 |
{ |
| 6785 |
TIFFError("computeOutputPixelOffsets", |
| 6786 |
"Rows and Columns exceed maximum sections\nIncrease " |
| 6787 |
"resolution or reduce sections"); |
| 6788 |
return (-1); |
| 6789 |
} |
| 6790 |
|
| 6791 |
/* build the list of offsets for each output section */ |
| 6792 |
for (k = 0, i = 0; i < orows; i++) |
| 6793 |
{ |
| 6794 |
y1 = (uint32_t)(olength * i); |
| 6795 |
y2 = (uint32_t)(olength * (i + 1) - 1); |
| 6796 |
if (y2 >= ilength) |
| 6797 |
y2 = ilength - 1; |
| 6798 |
for (j = 0; (j < ocols) && (k < MAX_SECTIONS); j++, k++) |
| 6799 |
{ |
| 6800 |
x1 = (uint32_t)(owidth * j); |
| 6801 |
x2 = (uint32_t)(owidth * (j + 1) - 1); |
| 6802 |
if (x2 >= iwidth) |
| 6803 |
x2 = iwidth - 1; |
| 6804 |
sections[k].x1 = x1; |
| 6805 |
sections[k].x2 = x2; |
| 6806 |
sections[k].y1 = y1; |
| 6807 |
sections[k].y2 = y2; |
| 6808 |
sections[k].buffsize = line_bytes * olength; |
| 6809 |
sections[k].position = k + 1; |
| 6810 |
sections[k].total = orows * ocols; |
| 6811 |
} |
| 6812 |
} |
| 6813 |
return (0); |
| 6814 |
} /* end computeOutputPixelOffsets */ |
| 6815 |
|
| 6816 |
static int loadImage(TIFF *in, struct image_data *image, struct dump_opts *dump, |
| 6817 |
unsigned char **read_ptr) |
| 6818 |
{ |
| 6819 |
uint32_t i; |
| 6820 |
float xres = 0.0, yres = 0.0; |
| 6821 |
uint32_t nstrips = 0, ntiles = 0; |
| 6822 |
uint16_t planar = 0; |
| 6823 |
uint16_t bps = 0, spp = 0, res_unit = 0; |
| 6824 |
uint16_t orientation = 0; |
| 6825 |
uint16_t input_compression = 0, input_photometric = 0; |
| 6826 |
uint16_t subsampling_horiz, subsampling_vert; |
| 6827 |
uint32_t width = 0, length = 0; |
| 6828 |
tmsize_t stsize = 0, tlsize = 0, buffsize = 0; |
| 6829 |
tmsize_t scanlinesize = 0; |
| 6830 |
uint32_t tw = 0, tl = 0; /* Tile width and length */ |
| 6831 |
tmsize_t tile_rowsize = 0; |
| 6832 |
unsigned char *read_buff = NULL; |
| 6833 |
int readunit = 0; |
| 6834 |
|
| 6835 |
TIFFGetFieldDefaulted(in, TIFFTAG_BITSPERSAMPLE, &bps); |
| 6836 |
TIFFGetFieldDefaulted(in, TIFFTAG_SAMPLESPERPIXEL, &spp); |
| 6837 |
TIFFGetFieldDefaulted(in, TIFFTAG_PLANARCONFIG, &planar); |
| 6838 |
TIFFGetFieldDefaulted(in, TIFFTAG_ORIENTATION, &orientation); |
| 6839 |
if (!TIFFGetFieldDefaulted(in, TIFFTAG_PHOTOMETRIC, &input_photometric)) |
| 6840 |
TIFFError("loadImage", "Image lacks Photometric interpretation tag"); |
| 6841 |
if (!TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width)) |
| 6842 |
TIFFError("loadimage", "Image lacks image width tag"); |
| 6843 |
if (!TIFFGetField(in, TIFFTAG_IMAGELENGTH, &length)) |
| 6844 |
TIFFError("loadimage", "Image lacks image length tag"); |
| 6845 |
TIFFGetFieldDefaulted(in, TIFFTAG_XRESOLUTION, &xres); |
| 6846 |
TIFFGetFieldDefaulted(in, TIFFTAG_YRESOLUTION, &yres); |
| 6847 |
if (!TIFFGetFieldDefaulted(in, TIFFTAG_RESOLUTIONUNIT, &res_unit)) |
| 6848 |
res_unit = RESUNIT_INCH; |
| 6849 |
if (!TIFFGetField(in, TIFFTAG_COMPRESSION, &input_compression)) |
| 6850 |
input_compression = COMPRESSION_NONE; |
| 6851 |
|
| 6852 |
#ifdef DEBUG2 |
| 6853 |
char compressionid[16]; |
| 6854 |
|
| 6855 |
switch (input_compression) |
| 6856 |
{ |
| 6857 |
case COMPRESSION_NONE: /* 1 dump mode */ |
| 6858 |
strcpy(compressionid, "None/dump"); |
| 6859 |
break; |
| 6860 |
case COMPRESSION_CCITTRLE: /* 2 CCITT modified Huffman RLE */ |
| 6861 |
strcpy(compressionid, "Huffman RLE"); |
| 6862 |
break; |
| 6863 |
case COMPRESSION_CCITTFAX3: /* 3 CCITT Group 3 fax encoding */ |
| 6864 |
strcpy(compressionid, "Group3 Fax"); |
| 6865 |
break; |
| 6866 |
case COMPRESSION_CCITTFAX4: /* 4 CCITT Group 4 fax encoding */ |
| 6867 |
strcpy(compressionid, "Group4 Fax"); |
| 6868 |
break; |
| 6869 |
case COMPRESSION_LZW: /* 5 Lempel-Ziv & Welch */ |
| 6870 |
strcpy(compressionid, "LZW"); |
| 6871 |
break; |
| 6872 |
case COMPRESSION_OJPEG: /* 6 !6.0 JPEG */ |
| 6873 |
strcpy(compressionid, "Old Jpeg"); |
| 6874 |
break; |
| 6875 |
case COMPRESSION_JPEG: /* 7 %JPEG DCT compression */ |
| 6876 |
strcpy(compressionid, "New Jpeg"); |
| 6877 |
break; |
| 6878 |
case COMPRESSION_NEXT: /* 32766 NeXT 2-bit RLE */ |
| 6879 |
strcpy(compressionid, "Next RLE"); |
| 6880 |
break; |
| 6881 |
case COMPRESSION_CCITTRLEW: /* 32771 #1 w/ word alignment */ |
| 6882 |
strcpy(compressionid, "CITTRLEW"); |
| 6883 |
break; |
| 6884 |
case COMPRESSION_PACKBITS: /* 32773 Macintosh RLE */ |
| 6885 |
strcpy(compressionid, "Mac Packbits"); |
| 6886 |
break; |
| 6887 |
case COMPRESSION_THUNDERSCAN: /* 32809 ThunderScan RLE */ |
| 6888 |
strcpy(compressionid, "Thunderscan"); |
| 6889 |
break; |
| 6890 |
case COMPRESSION_IT8CTPAD: /* 32895 IT8 CT w/padding */ |
| 6891 |
strcpy(compressionid, "IT8 padded"); |
| 6892 |
break; |
| 6893 |
case COMPRESSION_IT8LW: /* 32896 IT8 Linework RLE */ |
| 6894 |
strcpy(compressionid, "IT8 RLE"); |
| 6895 |
break; |
| 6896 |
case COMPRESSION_IT8MP: /* 32897 IT8 Monochrome picture */ |
| 6897 |
strcpy(compressionid, "IT8 mono"); |
| 6898 |
break; |
| 6899 |
case COMPRESSION_IT8BL: /* 32898 IT8 Binary line art */ |
| 6900 |
strcpy(compressionid, "IT8 lineart"); |
| 6901 |
break; |
| 6902 |
case COMPRESSION_PIXARFILM: /* 32908 Pixar companded 10bit LZW */ |
| 6903 |
strcpy(compressionid, "Pixar 10 bit"); |
| 6904 |
break; |
| 6905 |
case COMPRESSION_PIXARLOG: /* 32909 Pixar companded 11bit ZIP */ |
| 6906 |
strcpy(compressionid, "Pixar 11bit"); |
| 6907 |
break; |
| 6908 |
case COMPRESSION_DEFLATE: /* 32946 Deflate compression */ |
| 6909 |
strcpy(compressionid, "Deflate"); |
| 6910 |
break; |
| 6911 |
case COMPRESSION_ADOBE_DEFLATE: /* 8 Deflate compression */ |
| 6912 |
strcpy(compressionid, "Adobe deflate"); |
| 6913 |
break; |
| 6914 |
default: |
| 6915 |
strcpy(compressionid, "None/unknown"); |
| 6916 |
break; |
| 6917 |
} |
| 6918 |
TIFFError("loadImage", "Input compression %s", compressionid); |
| 6919 |
#endif |
| 6920 |
|
| 6921 |
scanlinesize = TIFFScanlineSize(in); |
| 6922 |
image->bps = bps; |
| 6923 |
image->spp = spp; |
| 6924 |
image->planar = planar; |
| 6925 |
image->width = width; |
| 6926 |
image->length = length; |
| 6927 |
image->xres = xres; |
| 6928 |
image->yres = yres; |
| 6929 |
image->res_unit = res_unit; |
| 6930 |
image->compression = input_compression; |
| 6931 |
image->photometric = input_photometric; |
| 6932 |
#ifdef DEBUG2 |
| 6933 |
char photometricid[12]; |
| 6934 |
|
| 6935 |
switch (input_photometric) |
| 6936 |
{ |
| 6937 |
case PHOTOMETRIC_MINISWHITE: |
| 6938 |
strcpy(photometricid, "MinIsWhite"); |
| 6939 |
break; |
| 6940 |
case PHOTOMETRIC_MINISBLACK: |
| 6941 |
strcpy(photometricid, "MinIsBlack"); |
| 6942 |
break; |
| 6943 |
case PHOTOMETRIC_RGB: |
| 6944 |
strcpy(photometricid, "RGB"); |
| 6945 |
break; |
| 6946 |
case PHOTOMETRIC_PALETTE: |
| 6947 |
strcpy(photometricid, "Palette"); |
| 6948 |
break; |
| 6949 |
case PHOTOMETRIC_MASK: |
| 6950 |
strcpy(photometricid, "Mask"); |
| 6951 |
break; |
| 6952 |
case PHOTOMETRIC_SEPARATED: |
| 6953 |
strcpy(photometricid, "Separated"); |
| 6954 |
break; |
| 6955 |
case PHOTOMETRIC_YCBCR: |
| 6956 |
strcpy(photometricid, "YCBCR"); |
| 6957 |
break; |
| 6958 |
case PHOTOMETRIC_CIELAB: |
| 6959 |
strcpy(photometricid, "CIELab"); |
| 6960 |
break; |
| 6961 |
case PHOTOMETRIC_ICCLAB: |
| 6962 |
strcpy(photometricid, "ICCLab"); |
| 6963 |
break; |
| 6964 |
case PHOTOMETRIC_ITULAB: |
| 6965 |
strcpy(photometricid, "ITULab"); |
| 6966 |
break; |
| 6967 |
case PHOTOMETRIC_LOGL: |
| 6968 |
strcpy(photometricid, "LogL"); |
| 6969 |
break; |
| 6970 |
case PHOTOMETRIC_LOGLUV: |
| 6971 |
strcpy(photometricid, "LOGLuv"); |
| 6972 |
break; |
| 6973 |
default: |
| 6974 |
strcpy(photometricid, "Unknown"); |
| 6975 |
break; |
| 6976 |
} |
| 6977 |
TIFFError("loadImage", "Input photometric interpretation %s", |
| 6978 |
photometricid); |
| 6979 |
|
| 6980 |
#endif |
| 6981 |
image->orientation = orientation; |
| 6982 |
switch (orientation) |
| 6983 |
{ |
| 6984 |
case 0: |
| 6985 |
case ORIENTATION_TOPLEFT: |
| 6986 |
image->adjustments = 0; |
| 6987 |
break; |
| 6988 |
case ORIENTATION_TOPRIGHT: |
| 6989 |
image->adjustments = MIRROR_HORIZ; |
| 6990 |
break; |
| 6991 |
case ORIENTATION_BOTRIGHT: |
| 6992 |
image->adjustments = ROTATECW_180; |
| 6993 |
break; |
| 6994 |
case ORIENTATION_BOTLEFT: |
| 6995 |
image->adjustments = MIRROR_VERT; |
| 6996 |
break; |
| 6997 |
case ORIENTATION_LEFTTOP: |
| 6998 |
image->adjustments = MIRROR_VERT | ROTATECW_90; |
| 6999 |
break; |
| 7000 |
case ORIENTATION_RIGHTTOP: |
| 7001 |
image->adjustments = ROTATECW_90; |
| 7002 |
break; |
| 7003 |
case ORIENTATION_RIGHTBOT: |
| 7004 |
image->adjustments = MIRROR_VERT | ROTATECW_270; |
| 7005 |
break; |
| 7006 |
case ORIENTATION_LEFTBOT: |
| 7007 |
image->adjustments = ROTATECW_270; |
| 7008 |
break; |
| 7009 |
default: |
| 7010 |
image->adjustments = 0; |
| 7011 |
image->orientation = ORIENTATION_TOPLEFT; |
| 7012 |
} |
| 7013 |
|
| 7014 |
if ((bps == 0) || (spp == 0)) |
| 7015 |
{ |
| 7016 |
TIFFError("loadImage", |
| 7017 |
"Invalid samples per pixel (%" PRIu16 |
| 7018 |
") or bits per sample (%" PRIu16 ")", |
| 7019 |
spp, bps); |
| 7020 |
return (-1); |
| 7021 |
} |
| 7022 |
|
| 7023 |
if (TIFFIsTiled(in)) |
| 7024 |
{ |
| 7025 |
readunit = TILE; |
| 7026 |
tlsize = TIFFTileSize(in); |
| 7027 |
ntiles = TIFFNumberOfTiles(in); |
| 7028 |
TIFFGetField(in, TIFFTAG_TILEWIDTH, &tw); |
| 7029 |
TIFFGetField(in, TIFFTAG_TILELENGTH, &tl); |
| 7030 |
|
| 7031 |
tile_rowsize = TIFFTileRowSize(in); |
| 7032 |
if (ntiles == 0 || tlsize == 0 || tile_rowsize == 0) |
| 7033 |
{ |
| 7034 |
TIFFError("loadImage", |
| 7035 |
"File appears to be tiled, but the number of tiles, tile " |
| 7036 |
"size, or tile rowsize is zero."); |
| 7037 |
exit(EXIT_FAILURE); |
| 7038 |
} |
| 7039 |
if (ntiles != 0 && tlsize > (tmsize_t)(TIFF_TMSIZE_T_MAX / ntiles)) |
| 7040 |
{ |
| 7041 |
TIFFError("loadImage", |
| 7042 |
"Integer overflow when calculating buffer size"); |
| 7043 |
exit(EXIT_FAILURE); |
| 7044 |
} |
| 7045 |
buffsize = tlsize * ntiles; |
| 7046 |
|
| 7047 |
if (tl != 0 && ntiles != 0 && |
| 7048 |
tile_rowsize > (tmsize_t)(TIFF_TMSIZE_T_MAX / tl / ntiles)) |
| 7049 |
{ |
| 7050 |
TIFFError("loadImage", |
| 7051 |
"Integer overflow when calculating buffer size"); |
| 7052 |
exit(EXIT_FAILURE); |
| 7053 |
} |
| 7054 |
if (buffsize < (tmsize_t)(ntiles * tl * tile_rowsize)) |
| 7055 |
{ |
| 7056 |
buffsize = ntiles * tl * tile_rowsize; |
| 7057 |
|
| 7058 |
#ifdef DEBUG2 |
| 7059 |
TIFFError("loadImage", |
| 7060 |
"Tilesize %" PRIu32 " is too small, using ntiles * " |
| 7061 |
"tilelength * tilerowsize %" PRIu32, |
| 7062 |
tlsize, buffsize); |
| 7063 |
#endif |
| 7064 |
} |
| 7065 |
|
| 7066 |
if (dump->infile != NULL) |
| 7067 |
dump_info(dump->infile, dump->format, "", |
| 7068 |
"Tilesize: %" TIFF_SSIZE_FORMAT |
| 7069 |
", Number of Tiles: %" PRIu32 |
| 7070 |
", Tile row size: %" TIFF_SSIZE_FORMAT, |
| 7071 |
tlsize, ntiles, tile_rowsize); |
| 7072 |
} |
| 7073 |
else |
| 7074 |
{ |
| 7075 |
tmsize_t buffsize_check; |
| 7076 |
readunit = STRIP; |
| 7077 |
TIFFGetFieldDefaulted(in, TIFFTAG_ROWSPERSTRIP, &rowsperstrip); |
| 7078 |
stsize = TIFFStripSize(in); |
| 7079 |
nstrips = TIFFNumberOfStrips(in); |
| 7080 |
if (nstrips == 0 || stsize == 0) |
| 7081 |
{ |
| 7082 |
TIFFError("loadImage", "File appears to be striped, but the number " |
| 7083 |
"of stipes or stripe size is zero."); |
| 7084 |
exit(EXIT_FAILURE); |
| 7085 |
} |
| 7086 |
|
| 7087 |
if (nstrips != 0 && stsize > (tmsize_t)(TIFF_TMSIZE_T_MAX / nstrips)) |
| 7088 |
{ |
| 7089 |
TIFFError("loadImage", |
| 7090 |
"Integer overflow when calculating buffer size"); |
| 7091 |
exit(EXIT_FAILURE); |
| 7092 |
} |
| 7093 |
buffsize = stsize * nstrips; |
| 7094 |
/* The buffsize_check and the possible adaptation of buffsize |
| 7095 |
* has to account also for padding of each line to a byte boundary. |
| 7096 |
* This is assumed by mirrorImage() and rotateImage(). |
| 7097 |
* Furthermore, functions like extractContigSamplesShifted32bits() |
| 7098 |
* need a buffer, which is at least 3 bytes larger than the actual |
| 7099 |
* image. Otherwise buffer-overflow might occur there. |
| 7100 |
*/ |
| 7101 |
if ((spp != 0 && bps != 0 && |
| 7102 |
width > (uint32_t)((UINT32_MAX - 7) / spp / bps)) || |
| 7103 |
(width != 0 && spp != 0 && bps != 0 && |
| 7104 |
length > (tmsize_t)(TIFF_TMSIZE_T_MAX / |
| 7105 |
(uint32_t)(((width * spp * bps) + 7) / 8)))) |
| 7106 |
{ |
| 7107 |
TIFFError("loadImage", "Integer overflow detected."); |
| 7108 |
exit(EXIT_FAILURE); |
| 7109 |
} |
| 7110 |
buffsize_check = |
| 7111 |
(tmsize_t)length * (uint32_t)(((width * spp * bps) + 7) / 8); |
| 7112 |
if (buffsize < buffsize_check) |
| 7113 |
{ |
| 7114 |
buffsize = buffsize_check; |
| 7115 |
#ifdef DEBUG2 |
| 7116 |
TIFFError("loadImage", |
| 7117 |
"Stripsize %" PRIu32 " is too small, using imagelength * " |
| 7118 |
"width * spp * bps / 8 = %" PRIu32, |
| 7119 |
stsize, (unsigned long)buffsize); |
| 7120 |
#endif |
| 7121 |
} |
| 7122 |
|
| 7123 |
if (dump->infile != NULL) |
| 7124 |
dump_info(dump->infile, dump->format, "", |
| 7125 |
"Stripsize: %" TIFF_SSIZE_FORMAT |
| 7126 |
", Number of Strips: %" PRIu32 |
| 7127 |
", Rows per Strip: %" PRIu32 |
| 7128 |
", Scanline size: %" TIFF_SSIZE_FORMAT, |
| 7129 |
stsize, nstrips, rowsperstrip, scanlinesize); |
| 7130 |
} |
| 7131 |
|
| 7132 |
if (input_compression == COMPRESSION_JPEG) |
| 7133 |
{ /* Force conversion to RGB */ |
| 7134 |
TIFFSetField(in, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB); |
| 7135 |
} |
| 7136 |
/* The clause up to the read statement is taken from Tom Lane's tiffcp patch |
| 7137 |
*/ |
| 7138 |
else |
| 7139 |
{ /* Otherwise, can't handle subsampled input */ |
| 7140 |
if (input_photometric == PHOTOMETRIC_YCBCR) |
| 7141 |
{ |
| 7142 |
TIFFGetFieldDefaulted(in, TIFFTAG_YCBCRSUBSAMPLING, |
| 7143 |
&subsampling_horiz, &subsampling_vert); |
| 7144 |
if (subsampling_horiz != 1 || subsampling_vert != 1) |
| 7145 |
{ |
| 7146 |
TIFFError("loadImage", |
| 7147 |
"Can't copy/convert subsampled image with " |
| 7148 |
"subsampling %" PRIu16 " horiz %" PRIu16 " vert", |
| 7149 |
subsampling_horiz, subsampling_vert); |
| 7150 |
return (-1); |
| 7151 |
} |
| 7152 |
} |
| 7153 |
} |
| 7154 |
|
| 7155 |
read_buff = *read_ptr; |
| 7156 |
/* +3 : add a few guard bytes since reverseSamples16bits() can read a bit |
| 7157 |
* outside buffer */ |
| 7158 |
/* Reuse of read_buff from previous image is quite unsafe, because other |
| 7159 |
* functions (like rotateImage() etc.) reallocate that buffer with different |
| 7160 |
* size without updating the local prev_readsize value. */ |
| 7161 |
if (read_buff) |
| 7162 |
{ |
| 7163 |
_TIFFfree(read_buff); |
| 7164 |
} |
| 7165 |
if (buffsize > 0xFFFFFFFFU - 3) |
| 7166 |
{ |
| 7167 |
TIFFError("loadImage", "Required read buffer size too large"); |
| 7168 |
return (-1); |
| 7169 |
} |
| 7170 |
read_buff = |
| 7171 |
(unsigned char *)limitMalloc(buffsize + NUM_BUFF_OVERSIZE_BYTES); |
| 7172 |
if (!read_buff) |
| 7173 |
{ |
| 7174 |
TIFFError("loadImage", "Unable to allocate read buffer"); |
| 7175 |
return (-1); |
| 7176 |
} |
| 7177 |
check_buffsize = buffsize + NUM_BUFF_OVERSIZE_BYTES; |
| 7178 |
|
| 7179 |
read_buff[buffsize] = 0; |
| 7180 |
read_buff[buffsize + 1] = 0; |
| 7181 |
read_buff[buffsize + 2] = 0; |
| 7182 |
|
| 7183 |
*read_ptr = read_buff; |
| 7184 |
|
| 7185 |
/* N.B. The read functions used copy separate plane data into a buffer as |
| 7186 |
* interleaved samples rather than separate planes so the same logic works |
| 7187 |
* to extract regions regardless of the way the data are organized in the |
| 7188 |
* input file. |
| 7189 |
*/ |
| 7190 |
switch (readunit) |
| 7191 |
{ |
| 7192 |
case STRIP: |
| 7193 |
if (planar == PLANARCONFIG_CONTIG) |
| 7194 |
{ |
| 7195 |
if (!(readContigStripsIntoBuffer(in, read_buff))) |
| 7196 |
{ |
| 7197 |
TIFFError("loadImage", |
| 7198 |
"Unable to read contiguous strips into buffer"); |
| 7199 |
return (-1); |
| 7200 |
} |
| 7201 |
} |
| 7202 |
else |
| 7203 |
{ |
| 7204 |
if (!(readSeparateStripsIntoBuffer(in, read_buff, length, width, |
| 7205 |
spp, dump))) |
| 7206 |
{ |
| 7207 |
TIFFError("loadImage", |
| 7208 |
"Unable to read separate strips into buffer"); |
| 7209 |
return (-1); |
| 7210 |
} |
| 7211 |
} |
| 7212 |
break; |
| 7213 |
|
| 7214 |
case TILE: |
| 7215 |
if (planar == PLANARCONFIG_CONTIG) |
| 7216 |
{ |
| 7217 |
if (!(readContigTilesIntoBuffer(in, read_buff, length, width, |
| 7218 |
tw, tl, spp, bps))) |
| 7219 |
{ |
| 7220 |
TIFFError("loadImage", |
| 7221 |
"Unable to read contiguous tiles into buffer"); |
| 7222 |
return (-1); |
| 7223 |
} |
| 7224 |
} |
| 7225 |
else |
| 7226 |
{ |
| 7227 |
if (!(readSeparateTilesIntoBuffer(in, read_buff, length, width, |
| 7228 |
tw, tl, spp, bps))) |
| 7229 |
{ |
| 7230 |
TIFFError("loadImage", |
| 7231 |
"Unable to read separate tiles into buffer"); |
| 7232 |
return (-1); |
| 7233 |
} |
| 7234 |
} |
| 7235 |
break; |
| 7236 |
default: |
| 7237 |
TIFFError("loadImage", "Unsupported image file format"); |
| 7238 |
return (-1); |
| 7239 |
break; |
| 7240 |
} |
| 7241 |
if ((dump->infile != NULL) && (dump->level == 2)) |
| 7242 |
{ |
| 7243 |
dump_info(dump->infile, dump->format, "loadImage", |
| 7244 |
"Image width %" PRIu32 ", length %" PRIu32 |
| 7245 |
", Raw image data, %4" TIFF_SSIZE_FORMAT " bytes", |
| 7246 |
width, length, buffsize); |
| 7247 |
dump_info(dump->infile, dump->format, "", |
| 7248 |
"Bits per sample %" PRIu16 ", Samples per pixel %" PRIu16, |
| 7249 |
bps, spp); |
| 7250 |
|
| 7251 |
if ((uint64_t)scanlinesize > 0x0ffffffffULL) |
| 7252 |
{ |
| 7253 |
dump_info( |
| 7254 |
dump->infile, dump->format, "loadImage", |
| 7255 |
"Attention: scanlinesize %" PRIu64 |
| 7256 |
" is larger than UINT32_MAX.\nFollowing dump might be wrong.", |
| 7257 |
(uint64_t)scanlinesize); |
| 7258 |
} |
| 7259 |
for (i = 0; i < length; i++) |
| 7260 |
dump_buffer(dump->infile, dump->format, 1, (uint32_t)scanlinesize, |
| 7261 |
i, read_buff + (i * scanlinesize)); |
| 7262 |
} |
| 7263 |
return (0); |
| 7264 |
} /* end loadImage */ |
| 7265 |
|
| 7266 |
static int correct_orientation(struct image_data *image, |
| 7267 |
unsigned char **work_buff_ptr) |
| 7268 |
{ |
| 7269 |
uint16_t mirror, rotation; |
| 7270 |
unsigned char *work_buff; |
| 7271 |
|
| 7272 |
work_buff = *work_buff_ptr; |
| 7273 |
if ((image == NULL) || (work_buff == NULL)) |
| 7274 |
{ |
| 7275 |
TIFFError("correct_orientatin", "Invalid image or buffer pointer"); |
| 7276 |
return (-1); |
| 7277 |
} |
| 7278 |
|
| 7279 |
if ((image->adjustments & MIRROR_HORIZ) || |
| 7280 |
(image->adjustments & MIRROR_VERT)) |
| 7281 |
{ |
| 7282 |
mirror = (uint16_t)(image->adjustments & MIRROR_BOTH); |
| 7283 |
if (mirrorImage(image->spp, image->bps, mirror, image->width, |
| 7284 |
image->length, work_buff)) |
| 7285 |
{ |
| 7286 |
TIFFError("correct_orientation", "Unable to mirror image"); |
| 7287 |
return (-1); |
| 7288 |
} |
| 7289 |
} |
| 7290 |
|
| 7291 |
if (image->adjustments & ROTATE_ANY) |
| 7292 |
{ |
| 7293 |
if (image->adjustments & ROTATECW_90) |
| 7294 |
rotation = (uint16_t)90; |
| 7295 |
else if (image->adjustments & ROTATECW_180) |
| 7296 |
rotation = (uint16_t)180; |
| 7297 |
else if (image->adjustments & ROTATECW_270) |
| 7298 |
rotation = (uint16_t)270; |
| 7299 |
else |
| 7300 |
{ |
| 7301 |
TIFFError("correct_orientation", "Invalid rotation value: %" PRIu16, |
| 7302 |
(uint16_t)(image->adjustments & ROTATE_ANY)); |
| 7303 |
return (-1); |
| 7304 |
} |
| 7305 |
/* Dummy variable in order not to switch two times the |
| 7306 |
* image->width,->length within rotateImage(), |
| 7307 |
* but switch xres, yres there. */ |
| 7308 |
uint32_t width = image->width; |
| 7309 |
uint32_t length = image->length; |
| 7310 |
if (rotateImage(rotation, image, &width, &length, work_buff_ptr, NULL, |
| 7311 |
TRUE)) |
| 7312 |
{ |
| 7313 |
TIFFError("correct_orientation", "Unable to rotate image"); |
| 7314 |
return (-1); |
| 7315 |
} |
| 7316 |
image->orientation = ORIENTATION_TOPLEFT; |
| 7317 |
} |
| 7318 |
|
| 7319 |
return (0); |
| 7320 |
} /* end correct_orientation */ |
| 7321 |
|
| 7322 |
/* Extract multiple zones from an image and combine into a single composite |
| 7323 |
* image */ |
| 7324 |
static int extractCompositeRegions(struct image_data *image, |
| 7325 |
struct crop_mask *crop, |
| 7326 |
unsigned char *read_buff, |
| 7327 |
unsigned char *crop_buff) |
| 7328 |
{ |
| 7329 |
int shift_width, bytes_per_sample, bytes_per_pixel; |
| 7330 |
uint32_t i, trailing_bits, prev_trailing_bits; |
| 7331 |
uint32_t row, first_row, last_row, first_col, last_col; |
| 7332 |
uint32_t src_rowsize, dst_rowsize, src_offset, dst_offset; |
| 7333 |
uint32_t crop_width, crop_length, img_width /*, img_length */; |
| 7334 |
uint32_t prev_length, prev_width, composite_width; |
| 7335 |
uint16_t bps, spp; |
| 7336 |
uint8_t *src, *dst; |
| 7337 |
tsample_t count, sample = 0; /* Update to extract one or more samples */ |
| 7338 |
|
| 7339 |
img_width = image->width; |
| 7340 |
/* img_length = image->length; */ |
| 7341 |
bps = image->bps; |
| 7342 |
spp = image->spp; |
| 7343 |
count = spp; |
| 7344 |
|
| 7345 |
bytes_per_sample = (bps + 7) / 8; |
| 7346 |
bytes_per_pixel = ((bps * spp) + 7) / 8; |
| 7347 |
if ((bps % 8) == 0) |
| 7348 |
shift_width = 0; |
| 7349 |
else |
| 7350 |
{ |
| 7351 |
if (bytes_per_pixel < (bytes_per_sample + 1)) |
| 7352 |
shift_width = bytes_per_pixel; |
| 7353 |
else |
| 7354 |
shift_width = bytes_per_sample + 1; |
| 7355 |
} |
| 7356 |
src = read_buff; |
| 7357 |
dst = crop_buff; |
| 7358 |
|
| 7359 |
/* These are setup for adding additional sections */ |
| 7360 |
prev_width = prev_length = 0; |
| 7361 |
prev_trailing_bits = trailing_bits = 0; |
| 7362 |
composite_width = crop->combined_width; |
| 7363 |
crop->combined_width = 0; |
| 7364 |
crop->combined_length = 0; |
| 7365 |
|
| 7366 |
/* If there is more than one region, check beforehand whether all the width |
| 7367 |
* and length values of the regions are the same, respectively. */ |
| 7368 |
switch (crop->edge_ref) |
| 7369 |
{ |
| 7370 |
default: |
| 7371 |
case EDGE_TOP: |
| 7372 |
case EDGE_BOTTOM: |
| 7373 |
for (i = 1; i < crop->selections; i++) |
| 7374 |
{ |
| 7375 |
uint32_t crop_width0 = |
| 7376 |
crop->regionlist[i - 1].x2 - crop->regionlist[i - 1].x1 + 1; |
| 7377 |
uint32_t crop_width1 = |
| 7378 |
crop->regionlist[i].x2 - crop->regionlist[i].x1 + 1; |
| 7379 |
if (crop_width0 != crop_width1) |
| 7380 |
{ |
| 7381 |
TIFFError("extractCompositeRegions", |
| 7382 |
"Only equal width regions can be combined for -E " |
| 7383 |
"top or bottom"); |
| 7384 |
return (1); |
| 7385 |
} |
| 7386 |
} |
| 7387 |
break; |
| 7388 |
case EDGE_LEFT: |
| 7389 |
case EDGE_RIGHT: |
| 7390 |
for (i = 1; i < crop->selections; i++) |
| 7391 |
{ |
| 7392 |
uint32_t crop_length0 = |
| 7393 |
crop->regionlist[i - 1].y2 - crop->regionlist[i - 1].y1 + 1; |
| 7394 |
uint32_t crop_length1 = |
| 7395 |
crop->regionlist[i].y2 - crop->regionlist[i].y1 + 1; |
| 7396 |
if (crop_length0 != crop_length1) |
| 7397 |
{ |
| 7398 |
TIFFError("extractCompositeRegions", |
| 7399 |
"Only equal length regions can be combined for " |
| 7400 |
"-E left or right"); |
| 7401 |
return (1); |
| 7402 |
} |
| 7403 |
} |
| 7404 |
} |
| 7405 |
|
| 7406 |
for (i = 0; i < crop->selections; i++) |
| 7407 |
{ |
| 7408 |
/* rows, columns, width, length are expressed in pixels */ |
| 7409 |
first_row = crop->regionlist[i].y1; |
| 7410 |
last_row = crop->regionlist[i].y2; |
| 7411 |
first_col = crop->regionlist[i].x1; |
| 7412 |
last_col = crop->regionlist[i].x2; |
| 7413 |
|
| 7414 |
crop_width = last_col - first_col + 1; |
| 7415 |
crop_length = last_row - first_row + 1; |
| 7416 |
|
| 7417 |
/* These should not be needed for composite images */ |
| 7418 |
crop->regionlist[i].width = crop_width; |
| 7419 |
crop->regionlist[i].length = crop_length; |
| 7420 |
|
| 7421 |
src_rowsize = ((img_width * bps * spp) + 7) / 8; |
| 7422 |
dst_rowsize = (((crop_width * bps * count) + 7) / 8); |
| 7423 |
|
| 7424 |
switch (crop->edge_ref) |
| 7425 |
{ |
| 7426 |
default: |
| 7427 |
case EDGE_TOP: |
| 7428 |
case EDGE_BOTTOM: |
| 7429 |
if ((crop->selections > i + 1) && |
| 7430 |
(crop_width != crop->regionlist[i + 1].width)) |
| 7431 |
{ |
| 7432 |
TIFFError("extractCompositeRegions", |
| 7433 |
"Only equal width regions can be combined for -E " |
| 7434 |
"top or bottom"); |
| 7435 |
return (1); |
| 7436 |
} |
| 7437 |
|
| 7438 |
crop->combined_width = crop_width; |
| 7439 |
crop->combined_length += crop_length; |
| 7440 |
|
| 7441 |
for (row = first_row; row <= last_row; row++) |
| 7442 |
{ |
| 7443 |
src_offset = row * src_rowsize; |
| 7444 |
dst_offset = (row - first_row) * dst_rowsize; |
| 7445 |
src = read_buff + src_offset; |
| 7446 |
dst = crop_buff + dst_offset + (prev_length * dst_rowsize); |
| 7447 |
switch (shift_width) |
| 7448 |
{ |
| 7449 |
case 0: |
| 7450 |
if (extractContigSamplesBytes( |
| 7451 |
src, dst, img_width, sample, spp, bps, |
| 7452 |
count, first_col, last_col + 1)) |
| 7453 |
{ |
| 7454 |
TIFFError("extractCompositeRegions", |
| 7455 |
"Unable to extract row %" PRIu32, |
| 7456 |
row); |
| 7457 |
return (1); |
| 7458 |
} |
| 7459 |
break; |
| 7460 |
case 1: |
| 7461 |
if (bps == 1) |
| 7462 |
{ |
| 7463 |
if (extractContigSamplesShifted8bits( |
| 7464 |
src, dst, img_width, sample, spp, bps, |
| 7465 |
count, first_col, last_col + 1, |
| 7466 |
prev_trailing_bits)) |
| 7467 |
{ |
| 7468 |
TIFFError("extractCompositeRegions", |
| 7469 |
"Unable to extract row %" PRIu32, |
| 7470 |
row); |
| 7471 |
return (1); |
| 7472 |
} |
| 7473 |
break; |
| 7474 |
} |
| 7475 |
else if (extractContigSamplesShifted16bits( |
| 7476 |
src, dst, img_width, sample, spp, bps, |
| 7477 |
count, first_col, last_col + 1, |
| 7478 |
prev_trailing_bits)) |
| 7479 |
{ |
| 7480 |
TIFFError("extractCompositeRegions", |
| 7481 |
"Unable to extract row %" PRIu32, |
| 7482 |
row); |
| 7483 |
return (1); |
| 7484 |
} |
| 7485 |
break; |
| 7486 |
case 2: |
| 7487 |
if (extractContigSamplesShifted24bits( |
| 7488 |
src, dst, img_width, sample, spp, bps, |
| 7489 |
count, first_col, last_col + 1, |
| 7490 |
prev_trailing_bits)) |
| 7491 |
{ |
| 7492 |
TIFFError("extractCompositeRegions", |
| 7493 |
"Unable to extract row %" PRIu32, |
| 7494 |
row); |
| 7495 |
return (1); |
| 7496 |
} |
| 7497 |
break; |
| 7498 |
case 3: |
| 7499 |
case 4: |
| 7500 |
case 5: |
| 7501 |
if (extractContigSamplesShifted32bits( |
| 7502 |
src, dst, img_width, sample, spp, bps, |
| 7503 |
count, first_col, last_col + 1, |
| 7504 |
prev_trailing_bits)) |
| 7505 |
{ |
| 7506 |
TIFFError("extractCompositeRegions", |
| 7507 |
"Unable to extract row %" PRIu32, |
| 7508 |
row); |
| 7509 |
return (1); |
| 7510 |
} |
| 7511 |
break; |
| 7512 |
default: |
| 7513 |
TIFFError("extractCompositeRegions", |
| 7514 |
"Unsupported bit depth %" PRIu16, bps); |
| 7515 |
return (1); |
| 7516 |
} |
| 7517 |
} |
| 7518 |
prev_length += crop_length; |
| 7519 |
break; |
| 7520 |
case EDGE_LEFT: /* splice the pieces of each row together, side by |
| 7521 |
side */ |
| 7522 |
case EDGE_RIGHT: |
| 7523 |
if ((crop->selections > i + 1) && |
| 7524 |
(crop_length != crop->regionlist[i + 1].length)) |
| 7525 |
{ |
| 7526 |
TIFFError("extractCompositeRegions", |
| 7527 |
"Only equal length regions can be combined for " |
| 7528 |
"-E left or right"); |
| 7529 |
return (1); |
| 7530 |
} |
| 7531 |
crop->combined_width += crop_width; |
| 7532 |
crop->combined_length = crop_length; |
| 7533 |
dst_rowsize = (((composite_width * bps * count) + 7) / 8); |
| 7534 |
trailing_bits = (crop_width * bps * count) % 8; |
| 7535 |
for (row = first_row; row <= last_row; row++) |
| 7536 |
{ |
| 7537 |
src_offset = row * src_rowsize; |
| 7538 |
dst_offset = (row - first_row) * dst_rowsize; |
| 7539 |
src = read_buff + src_offset; |
| 7540 |
dst = crop_buff + dst_offset + prev_width; |
| 7541 |
|
| 7542 |
switch (shift_width) |
| 7543 |
{ |
| 7544 |
case 0: |
| 7545 |
if (extractContigSamplesBytes( |
| 7546 |
src, dst, img_width, sample, spp, bps, |
| 7547 |
count, first_col, last_col + 1)) |
| 7548 |
{ |
| 7549 |
TIFFError("extractCompositeRegions", |
| 7550 |
"Unable to extract row %" PRIu32, |
| 7551 |
row); |
| 7552 |
return (1); |
| 7553 |
} |
| 7554 |
break; |
| 7555 |
case 1: |
| 7556 |
if (bps == 1) |
| 7557 |
{ |
| 7558 |
if (extractContigSamplesShifted8bits( |
| 7559 |
src, dst, img_width, sample, spp, bps, |
| 7560 |
count, first_col, last_col + 1, |
| 7561 |
prev_trailing_bits)) |
| 7562 |
{ |
| 7563 |
TIFFError("extractCompositeRegions", |
| 7564 |
"Unable to extract row %" PRIu32, |
| 7565 |
row); |
| 7566 |
return (1); |
| 7567 |
} |
| 7568 |
break; |
| 7569 |
} |
| 7570 |
else if (extractContigSamplesShifted16bits( |
| 7571 |
src, dst, img_width, sample, spp, bps, |
| 7572 |
count, first_col, last_col + 1, |
| 7573 |
prev_trailing_bits)) |
| 7574 |
{ |
| 7575 |
TIFFError("extractCompositeRegions", |
| 7576 |
"Unable to extract row %" PRIu32, |
| 7577 |
row); |
| 7578 |
return (1); |
| 7579 |
} |
| 7580 |
break; |
| 7581 |
case 2: |
| 7582 |
if (extractContigSamplesShifted24bits( |
| 7583 |
src, dst, img_width, sample, spp, bps, |
| 7584 |
count, first_col, last_col + 1, |
| 7585 |
prev_trailing_bits)) |
| 7586 |
{ |
| 7587 |
TIFFError("extractCompositeRegions", |
| 7588 |
"Unable to extract row %" PRIu32, |
| 7589 |
row); |
| 7590 |
return (1); |
| 7591 |
} |
| 7592 |
break; |
| 7593 |
case 3: |
| 7594 |
case 4: |
| 7595 |
case 5: |
| 7596 |
if (extractContigSamplesShifted32bits( |
| 7597 |
src, dst, img_width, sample, spp, bps, |
| 7598 |
count, first_col, last_col + 1, |
| 7599 |
prev_trailing_bits)) |
| 7600 |
{ |
| 7601 |
TIFFError("extractCompositeRegions", |
| 7602 |
"Unable to extract row %" PRIu32, |
| 7603 |
row); |
| 7604 |
return (1); |
| 7605 |
} |
| 7606 |
break; |
| 7607 |
default: |
| 7608 |
TIFFError("extractCompositeRegions", |
| 7609 |
"Unsupported bit depth %" PRIu16, bps); |
| 7610 |
return (1); |
| 7611 |
} |
| 7612 |
} |
| 7613 |
prev_width += (crop_width * bps * count) / 8; |
| 7614 |
prev_trailing_bits += trailing_bits; |
| 7615 |
if (prev_trailing_bits > 7) |
| 7616 |
prev_trailing_bits -= 8; |
| 7617 |
break; |
| 7618 |
} |
| 7619 |
} |
| 7620 |
if (crop->combined_width != composite_width) |
| 7621 |
TIFFError("combineSeparateRegions", |
| 7622 |
"Combined width does not match composite width"); |
| 7623 |
|
| 7624 |
return (0); |
| 7625 |
} /* end extractCompositeRegions */ |
| 7626 |
|
| 7627 |
/* Copy a single region of input buffer to an output buffer. |
| 7628 |
* The read functions used copy separate plane data into a buffer |
| 7629 |
* as interleaved samples rather than separate planes so the same |
| 7630 |
* logic works to extract regions regardless of the way the data |
| 7631 |
* are organized in the input file. This function can be used to |
| 7632 |
* extract one or more samples from the input image by updating the |
| 7633 |
* parameters for starting sample and number of samples to copy in the |
| 7634 |
* fifth and eighth arguments of the call to extractContigSamples. |
| 7635 |
* They would be passed as new elements of the crop_mask struct. |
| 7636 |
*/ |
| 7637 |
|
| 7638 |
static int extractSeparateRegion(struct image_data *image, |
| 7639 |
struct crop_mask *crop, |
| 7640 |
unsigned char *read_buff, |
| 7641 |
unsigned char *crop_buff, int region) |
| 7642 |
{ |
| 7643 |
int shift_width, prev_trailing_bits = 0; |
| 7644 |
uint32_t bytes_per_sample, bytes_per_pixel; |
| 7645 |
uint32_t src_rowsize, dst_rowsize; |
| 7646 |
uint32_t row, first_row, last_row, first_col, last_col; |
| 7647 |
uint32_t src_offset, dst_offset; |
| 7648 |
uint32_t crop_width, crop_length, img_width /*, img_length */; |
| 7649 |
uint16_t bps, spp; |
| 7650 |
uint8_t *src, *dst; |
| 7651 |
tsample_t count, sample = 0; /* Update to extract more or more samples */ |
| 7652 |
|
| 7653 |
img_width = image->width; |
| 7654 |
/* img_length = image->length; */ |
| 7655 |
bps = image->bps; |
| 7656 |
spp = image->spp; |
| 7657 |
count = spp; |
| 7658 |
|
| 7659 |
bytes_per_sample = (bps + 7) / 8; |
| 7660 |
bytes_per_pixel = ((bps * spp) + 7) / 8; |
| 7661 |
if ((bps % 8) == 0) |
| 7662 |
shift_width = 0; /* Byte aligned data only */ |
| 7663 |
else |
| 7664 |
{ |
| 7665 |
if (bytes_per_pixel < (bytes_per_sample + 1)) |
| 7666 |
shift_width = bytes_per_pixel; |
| 7667 |
else |
| 7668 |
shift_width = bytes_per_sample + 1; |
| 7669 |
} |
| 7670 |
|
| 7671 |
/* rows, columns, width, length are expressed in pixels */ |
| 7672 |
first_row = crop->regionlist[region].y1; |
| 7673 |
last_row = crop->regionlist[region].y2; |
| 7674 |
first_col = crop->regionlist[region].x1; |
| 7675 |
last_col = crop->regionlist[region].x2; |
| 7676 |
|
| 7677 |
crop_width = last_col - first_col + 1; |
| 7678 |
crop_length = last_row - first_row + 1; |
| 7679 |
|
| 7680 |
crop->regionlist[region].width = crop_width; |
| 7681 |
crop->regionlist[region].length = crop_length; |
| 7682 |
|
| 7683 |
src = read_buff; |
| 7684 |
dst = crop_buff; |
| 7685 |
src_rowsize = ((img_width * bps * spp) + 7) / 8; |
| 7686 |
dst_rowsize = (((crop_width * bps * spp) + 7) / 8); |
| 7687 |
|
| 7688 |
for (row = first_row; row <= last_row; row++) |
| 7689 |
{ |
| 7690 |
src_offset = row * src_rowsize; |
| 7691 |
dst_offset = (row - first_row) * dst_rowsize; |
| 7692 |
src = read_buff + src_offset; |
| 7693 |
dst = crop_buff + dst_offset; |
| 7694 |
|
| 7695 |
switch (shift_width) |
| 7696 |
{ |
| 7697 |
case 0: |
| 7698 |
if (extractContigSamplesBytes(src, dst, img_width, sample, spp, |
| 7699 |
bps, count, first_col, |
| 7700 |
last_col + 1)) |
| 7701 |
{ |
| 7702 |
TIFFError("extractSeparateRegion", |
| 7703 |
"Unable to extract row %" PRIu32, row); |
| 7704 |
return (1); |
| 7705 |
} |
| 7706 |
break; |
| 7707 |
case 1: |
| 7708 |
if (bps == 1) |
| 7709 |
{ |
| 7710 |
if (extractContigSamplesShifted8bits( |
| 7711 |
src, dst, img_width, sample, spp, bps, count, |
| 7712 |
first_col, last_col + 1, prev_trailing_bits)) |
| 7713 |
{ |
| 7714 |
TIFFError("extractSeparateRegion", |
| 7715 |
"Unable to extract row %" PRIu32, row); |
| 7716 |
return (1); |
| 7717 |
} |
| 7718 |
break; |
| 7719 |
} |
| 7720 |
else if (extractContigSamplesShifted16bits( |
| 7721 |
src, dst, img_width, sample, spp, bps, count, |
| 7722 |
first_col, last_col + 1, prev_trailing_bits)) |
| 7723 |
{ |
| 7724 |
TIFFError("extractSeparateRegion", |
| 7725 |
"Unable to extract row %" PRIu32, row); |
| 7726 |
return (1); |
| 7727 |
} |
| 7728 |
break; |
| 7729 |
case 2: |
| 7730 |
if (extractContigSamplesShifted24bits( |
| 7731 |
src, dst, img_width, sample, spp, bps, count, first_col, |
| 7732 |
last_col + 1, prev_trailing_bits)) |
| 7733 |
{ |
| 7734 |
TIFFError("extractSeparateRegion", |
| 7735 |
"Unable to extract row %" PRIu32, row); |
| 7736 |
return (1); |
| 7737 |
} |
| 7738 |
break; |
| 7739 |
case 3: |
| 7740 |
case 4: |
| 7741 |
case 5: |
| 7742 |
if (extractContigSamplesShifted32bits( |
| 7743 |
src, dst, img_width, sample, spp, bps, count, first_col, |
| 7744 |
last_col + 1, prev_trailing_bits)) |
| 7745 |
{ |
| 7746 |
TIFFError("extractSeparateRegion", |
| 7747 |
"Unable to extract row %" PRIu32, row); |
| 7748 |
return (1); |
| 7749 |
} |
| 7750 |
break; |
| 7751 |
default: |
| 7752 |
TIFFError("extractSeparateRegion", |
| 7753 |
"Unsupported bit depth %" PRIu16, bps); |
| 7754 |
return (1); |
| 7755 |
} |
| 7756 |
} |
| 7757 |
|
| 7758 |
return (0); |
| 7759 |
} /* end extractSeparateRegion */ |
| 7760 |
|
| 7761 |
static int extractImageSection(struct image_data *image, |
| 7762 |
struct pageseg *section, unsigned char *src_buff, |
| 7763 |
unsigned char *sect_buff) |
| 7764 |
{ |
| 7765 |
unsigned char bytebuff1, bytebuff2; |
| 7766 |
#ifdef DEVELMODE |
| 7767 |
/* unsigned char *src, *dst; */ |
| 7768 |
#endif |
| 7769 |
|
| 7770 |
uint32_t img_width, img_rowsize; |
| 7771 |
#ifdef DEVELMODE |
| 7772 |
uint32_t img_length; |
| 7773 |
#endif |
| 7774 |
uint32_t j, shift1, trailing_bits; |
| 7775 |
uint32_t row, first_row, last_row, first_col, last_col; |
| 7776 |
uint32_t src_offset, dst_offset, row_offset, col_offset; |
| 7777 |
uint32_t offset1, full_bytes; |
| 7778 |
uint32_t sect_width; |
| 7779 |
#ifdef DEVELMODE |
| 7780 |
uint32_t sect_length; |
| 7781 |
#endif |
| 7782 |
uint16_t bps, spp; |
| 7783 |
|
| 7784 |
#ifdef DEVELMODE |
| 7785 |
int k; |
| 7786 |
unsigned char bitset; |
| 7787 |
#endif |
| 7788 |
|
| 7789 |
img_width = image->width; |
| 7790 |
#ifdef DEVELMODE |
| 7791 |
img_length = image->length; |
| 7792 |
#endif |
| 7793 |
bps = image->bps; |
| 7794 |
spp = image->spp; |
| 7795 |
|
| 7796 |
#ifdef DEVELMODE |
| 7797 |
/* src = src_buff; */ |
| 7798 |
/* dst = sect_buff; */ |
| 7799 |
#endif |
| 7800 |
src_offset = 0; |
| 7801 |
dst_offset = 0; |
| 7802 |
|
| 7803 |
#ifdef DEVELMODE |
| 7804 |
char bitarray[39]; |
| 7805 |
#endif |
| 7806 |
|
| 7807 |
/* rows, columns, width, length are expressed in pixels |
| 7808 |
* first_row, last_row, .. are index into image array starting at 0 to |
| 7809 |
* width-1, last_col shall be also extracted. */ |
| 7810 |
first_row = section->y1; |
| 7811 |
last_row = section->y2; |
| 7812 |
first_col = section->x1; |
| 7813 |
last_col = section->x2; |
| 7814 |
|
| 7815 |
sect_width = last_col - first_col + 1; |
| 7816 |
#ifdef DEVELMODE |
| 7817 |
sect_length = last_row - first_row + 1; |
| 7818 |
#endif |
| 7819 |
/* The read function loadImage() used copy separate plane data into a buffer |
| 7820 |
* as interleaved samples rather than separate planes so the same logic |
| 7821 |
* works to extract regions regardless of the way the data are organized in |
| 7822 |
* the input file. Furthermore, bytes and bits are arranged in buffer |
| 7823 |
* according to COMPRESSION=1 and FILLORDER=1 |
| 7824 |
*/ |
| 7825 |
/* row size in full bytes of source image */ |
| 7826 |
img_rowsize = (((img_width * spp * bps) + 7) / 8); |
| 7827 |
/* number of COMPLETE bytes per row in section */ |
| 7828 |
full_bytes = (sect_width * spp * bps) / 8; |
| 7829 |
/* trailing bits within the last byte of destination buffer */ |
| 7830 |
trailing_bits = (sect_width * spp * bps) % 8; |
| 7831 |
|
| 7832 |
#ifdef DEVELMODE |
| 7833 |
TIFFError("", |
| 7834 |
"First row: %" PRIu32 ", last row: %" PRIu32 |
| 7835 |
", First col: %" PRIu32 ", last col: %" PRIu32 "\n", |
| 7836 |
first_row, last_row, first_col, last_col); |
| 7837 |
TIFFError("", |
| 7838 |
"Image width: %" PRIu32 ", Image length: %" PRIu32 |
| 7839 |
", bps: %" PRIu16 ", spp: %" PRIu16 "\n", |
| 7840 |
img_width, img_length, bps, spp); |
| 7841 |
TIFFError("", |
| 7842 |
"Sect width: %" PRIu32 ", Sect length: %" PRIu32 |
| 7843 |
", full bytes: %" PRIu32 " trailing bits %" PRIu32 "\n", |
| 7844 |
sect_width, sect_length, full_bytes, trailing_bits); |
| 7845 |
#endif |
| 7846 |
|
| 7847 |
if ((bps % 8) == 0) |
| 7848 |
{ |
| 7849 |
col_offset = (first_col * spp * bps) / 8; |
| 7850 |
for (row = first_row; row <= last_row; row++) |
| 7851 |
{ |
| 7852 |
row_offset = row * img_rowsize; |
| 7853 |
src_offset = row_offset + col_offset; |
| 7854 |
|
| 7855 |
#ifdef DEVELMODE |
| 7856 |
TIFFError("", "Src offset: %8" PRIu32 ", Dst offset: %8" PRIu32, |
| 7857 |
src_offset, dst_offset); |
| 7858 |
#endif |
| 7859 |
if (src_offset + full_bytes >= check_buffsize) |
| 7860 |
{ |
| 7861 |
printf( |
| 7862 |
"Bad input. Preventing reading outside of input buffer.\n"); |
| 7863 |
return (-1); |
| 7864 |
} |
| 7865 |
_TIFFmemcpy(sect_buff + dst_offset, src_buff + src_offset, |
| 7866 |
full_bytes); |
| 7867 |
dst_offset += full_bytes; |
| 7868 |
} |
| 7869 |
} |
| 7870 |
else |
| 7871 |
{ /* bps != 8 */ |
| 7872 |
shift1 = |
| 7873 |
((first_col * spp * bps) % |
| 7874 |
8); /* shift1 = bits to skip in the first byte of source buffer*/ |
| 7875 |
for (row = first_row; row <= last_row; row++) |
| 7876 |
{ |
| 7877 |
/* pull out the first byte */ |
| 7878 |
row_offset = row * img_rowsize; |
| 7879 |
offset1 = row_offset + ((first_col * spp * bps) / |
| 7880 |
8); /* offset1 = offset into source of byte |
| 7881 |
with first bits to be extracted */ |
| 7882 |
|
| 7883 |
#ifdef DEVELMODE |
| 7884 |
for (j = 0, k = 7; j < 8; j++, k--) |
| 7885 |
{ |
| 7886 |
bitset = |
| 7887 |
*(src_buff + offset1) & (((unsigned char)1 << k)) ? 1 : 0; |
| 7888 |
sprintf(&bitarray[j], (bitset) ? "1" : "0"); |
| 7889 |
} |
| 7890 |
sprintf(&bitarray[8], " "); |
| 7891 |
sprintf(&bitarray[9], " "); |
| 7892 |
for (j = 10, k = 7; j < 18; j++, k--) |
| 7893 |
{ |
| 7894 |
bitset = *(src_buff + offset1 + full_bytes) & |
| 7895 |
(((unsigned char)1 << k)) |
| 7896 |
? 1 |
| 7897 |
: 0; |
| 7898 |
sprintf(&bitarray[j], (bitset) ? "1" : "0"); |
| 7899 |
} |
| 7900 |
bitarray[18] = '\0'; |
| 7901 |
TIFFError( |
| 7902 |
"", |
| 7903 |
"Row: %3d Offset1: %" PRIu32 ", Shift1: %" PRIu32 |
| 7904 |
", Offset2: %" PRIu32 ", Trailing_bits: %" PRIu32 "\n", |
| 7905 |
row, offset1, shift1, offset1 + full_bytes, trailing_bits); |
| 7906 |
#endif |
| 7907 |
|
| 7908 |
bytebuff1 = bytebuff2 = 0; |
| 7909 |
if (shift1 == 0) /* the region is byte and sample aligned */ |
| 7910 |
{ |
| 7911 |
if (offset1 + full_bytes >= check_buffsize) |
| 7912 |
{ |
| 7913 |
printf("Bad input. Preventing reading outside of input " |
| 7914 |
"buffer.\n"); |
| 7915 |
return (-1); |
| 7916 |
} |
| 7917 |
_TIFFmemcpy(sect_buff + dst_offset, src_buff + offset1, |
| 7918 |
full_bytes); |
| 7919 |
|
| 7920 |
#ifdef DEVELMODE |
| 7921 |
TIFFError("", |
| 7922 |
" Aligned data src offset1: %8" PRIu32 |
| 7923 |
", Dst offset: %8" PRIu32 "\n", |
| 7924 |
offset1, dst_offset); |
| 7925 |
sprintf(&bitarray[18], "\n"); |
| 7926 |
sprintf(&bitarray[19], "\t"); |
| 7927 |
for (j = 20, k = 7; j < 28; j++, k--) |
| 7928 |
{ |
| 7929 |
bitset = |
| 7930 |
*(sect_buff + dst_offset) & (((unsigned char)1 << k)) |
| 7931 |
? 1 |
| 7932 |
: 0; |
| 7933 |
sprintf(&bitarray[j], (bitset) ? "1" : "0"); |
| 7934 |
} |
| 7935 |
bitarray[28] = ' '; |
| 7936 |
bitarray[29] = ' '; |
| 7937 |
#endif |
| 7938 |
dst_offset += full_bytes; |
| 7939 |
|
| 7940 |
if (trailing_bits != 0) |
| 7941 |
{ |
| 7942 |
/* Only copy higher bits of samples and mask lower bits of |
| 7943 |
* not wanted column samples to zero */ |
| 7944 |
if (offset1 + full_bytes >= check_buffsize) |
| 7945 |
{ |
| 7946 |
printf("Bad input. Preventing reading outside of input " |
| 7947 |
"buffer.\n"); |
| 7948 |
return (-1); |
| 7949 |
} |
| 7950 |
bytebuff2 = src_buff[offset1 + full_bytes] & |
| 7951 |
((unsigned char)255 << (8 - trailing_bits)); |
| 7952 |
sect_buff[dst_offset] = bytebuff2; |
| 7953 |
#ifdef DEVELMODE |
| 7954 |
TIFFError("", |
| 7955 |
" Trailing bits src offset: %8" PRIu32 |
| 7956 |
", Dst offset: %8" PRIu32 "\n", |
| 7957 |
offset1 + full_bytes, dst_offset); |
| 7958 |
for (j = 30, k = 7; j < 38; j++, k--) |
| 7959 |
{ |
| 7960 |
bitset = *(sect_buff + dst_offset) & |
| 7961 |
(((unsigned char)1 << k)) |
| 7962 |
? 1 |
| 7963 |
: 0; |
| 7964 |
sprintf(&bitarray[j], (bitset) ? "1" : "0"); |
| 7965 |
} |
| 7966 |
bitarray[38] = '\0'; |
| 7967 |
TIFFError("", |
| 7968 |
"\tFirst and last bytes before and after " |
| 7969 |
"masking:\n\t%s\n\n", |
| 7970 |
bitarray); |
| 7971 |
#endif |
| 7972 |
dst_offset++; |
| 7973 |
} |
| 7974 |
} |
| 7975 |
else /* each destination byte will have to be built from two source |
| 7976 |
bytes*/ |
| 7977 |
{ |
| 7978 |
#ifdef DEVELMODE |
| 7979 |
TIFFError("", |
| 7980 |
" Unalligned data src offset: %8" PRIu32 |
| 7981 |
", Dst offset: %8" PRIu32 "\n", |
| 7982 |
offset1, dst_offset); |
| 7983 |
#endif |
| 7984 |
for (j = 0; j <= full_bytes; j++) |
| 7985 |
{ |
| 7986 |
/* Skip the first shift1 bits and shift the source up by |
| 7987 |
* shift1 bits before save to destination.*/ |
| 7988 |
/* Attention: src_buff size needs to be some bytes larger |
| 7989 |
* than image size, because could read behind image here. */ |
| 7990 |
if (offset1 + j + 1 >= check_buffsize) |
| 7991 |
{ |
| 7992 |
printf("Bad input. Preventing reading outside of input " |
| 7993 |
"buffer.\n"); |
| 7994 |
return (-1); |
| 7995 |
} |
| 7996 |
bytebuff1 = |
| 7997 |
src_buff[offset1 + j] & ((unsigned char)255 >> shift1); |
| 7998 |
bytebuff2 = src_buff[offset1 + j + 1] & |
| 7999 |
((unsigned char)255 << (8 - shift1)); |
| 8000 |
sect_buff[dst_offset + j] = |
| 8001 |
(bytebuff1 << shift1) | (bytebuff2 >> (8 - shift1)); |
| 8002 |
} |
| 8003 |
#ifdef DEVELMODE |
| 8004 |
sprintf(&bitarray[18], "\n"); |
| 8005 |
sprintf(&bitarray[19], "\t"); |
| 8006 |
for (j = 20, k = 7; j < 28; j++, k--) |
| 8007 |
{ |
| 8008 |
bitset = |
| 8009 |
*(sect_buff + dst_offset) & (((unsigned char)1 << k)) |
| 8010 |
? 1 |
| 8011 |
: 0; |
| 8012 |
sprintf(&bitarray[j], (bitset) ? "1" : "0"); |
| 8013 |
} |
| 8014 |
bitarray[28] = ' '; |
| 8015 |
bitarray[29] = ' '; |
| 8016 |
#endif |
| 8017 |
dst_offset += full_bytes; |
| 8018 |
|
| 8019 |
/* Copy the trailing_bits for the last byte in the destination |
| 8020 |
buffer. Could come from one ore two bytes of the source |
| 8021 |
buffer. */ |
| 8022 |
if (trailing_bits != 0) |
| 8023 |
{ |
| 8024 |
#ifdef DEVELMODE |
| 8025 |
TIFFError("", |
| 8026 |
" Trailing bits %4" PRIu32 |
| 8027 |
" src offset: %8" PRIu32 |
| 8028 |
", Dst offset: %8" PRIu32 "\n", |
| 8029 |
trailing_bits, offset1 + full_bytes, dst_offset); |
| 8030 |
#endif |
| 8031 |
/* More than necessary bits are already copied into last |
| 8032 |
* destination buffer, only masking of last byte in |
| 8033 |
* destination buffer is necessary.*/ |
| 8034 |
sect_buff[dst_offset] &= |
| 8035 |
((uint8_t)0xFF << (8 - trailing_bits)); |
| 8036 |
} |
| 8037 |
#ifdef DEVELMODE |
| 8038 |
sprintf(&bitarray[28], " "); |
| 8039 |
sprintf(&bitarray[29], " "); |
| 8040 |
for (j = 30, k = 7; j < 38; j++, k--) |
| 8041 |
{ |
| 8042 |
bitset = |
| 8043 |
*(sect_buff + dst_offset) & (((unsigned char)1 << k)) |
| 8044 |
? 1 |
| 8045 |
: 0; |
| 8046 |
sprintf(&bitarray[j], (bitset) ? "1" : "0"); |
| 8047 |
} |
| 8048 |
bitarray[38] = '\0'; |
| 8049 |
TIFFError("", |
| 8050 |
"\tFirst and last bytes before and after " |
| 8051 |
"masking:\n\t%s\n\n", |
| 8052 |
bitarray); |
| 8053 |
#endif |
| 8054 |
dst_offset++; |
| 8055 |
} |
| 8056 |
} |
| 8057 |
} |
| 8058 |
|
| 8059 |
return (0); |
| 8060 |
} /* end extractImageSection */ |
| 8061 |
|
| 8062 |
static int writeSelections(TIFF *in, TIFF **out, struct crop_mask *crop, |
| 8063 |
struct image_data *image, struct dump_opts *dump, |
| 8064 |
struct buffinfo seg_buffs[], char *mp, |
| 8065 |
char *filename, unsigned int *page, |
| 8066 |
unsigned int total_pages) |
| 8067 |
{ |
| 8068 |
int i, page_count; |
| 8069 |
int autoindex = 0; |
| 8070 |
unsigned char *crop_buff = NULL; |
| 8071 |
|
| 8072 |
/* Where we open a new file depends on the export mode */ |
| 8073 |
switch (crop->exp_mode) |
| 8074 |
{ |
| 8075 |
case ONE_FILE_COMPOSITE: /* Regions combined into single image */ |
| 8076 |
autoindex = 0; |
| 8077 |
crop_buff = seg_buffs[0].buffer; |
| 8078 |
if (update_output_file(out, mp, autoindex, filename, page)) |
| 8079 |
return (1); |
| 8080 |
page_count = total_pages; |
| 8081 |
if (writeCroppedImage(in, *out, image, dump, crop->combined_width, |
| 8082 |
crop->combined_length, crop_buff, *page, |
| 8083 |
total_pages)) |
| 8084 |
{ |
| 8085 |
TIFFError("writeRegions", "Unable to write new image"); |
| 8086 |
return (-1); |
| 8087 |
} |
| 8088 |
break; |
| 8089 |
case ONE_FILE_SEPARATED: /* Regions as separated images */ |
| 8090 |
autoindex = 0; |
| 8091 |
if (update_output_file(out, mp, autoindex, filename, page)) |
| 8092 |
return (1); |
| 8093 |
page_count = crop->selections * total_pages; |
| 8094 |
for (i = 0; i < crop->selections; i++) |
| 8095 |
{ |
| 8096 |
crop_buff = seg_buffs[i].buffer; |
| 8097 |
if (writeCroppedImage(in, *out, image, dump, |
| 8098 |
crop->regionlist[i].width, |
| 8099 |
crop->regionlist[i].length, crop_buff, |
| 8100 |
*page, page_count)) |
| 8101 |
{ |
| 8102 |
TIFFError("writeRegions", "Unable to write new image"); |
| 8103 |
return (-1); |
| 8104 |
} |
| 8105 |
} |
| 8106 |
break; |
| 8107 |
case FILE_PER_IMAGE_COMPOSITE: /* Regions as composite image */ |
| 8108 |
autoindex = 1; |
| 8109 |
if (update_output_file(out, mp, autoindex, filename, page)) |
| 8110 |
return (1); |
| 8111 |
|
| 8112 |
crop_buff = seg_buffs[0].buffer; |
| 8113 |
if (writeCroppedImage(in, *out, image, dump, crop->combined_width, |
| 8114 |
crop->combined_length, crop_buff, *page, |
| 8115 |
total_pages)) |
| 8116 |
{ |
| 8117 |
TIFFError("writeRegions", "Unable to write new image"); |
| 8118 |
return (-1); |
| 8119 |
} |
| 8120 |
break; |
| 8121 |
case FILE_PER_IMAGE_SEPARATED: /* Regions as separated images */ |
| 8122 |
autoindex = 1; |
| 8123 |
page_count = crop->selections; |
| 8124 |
if (update_output_file(out, mp, autoindex, filename, page)) |
| 8125 |
return (1); |
| 8126 |
|
| 8127 |
for (i = 0; i < crop->selections; i++) |
| 8128 |
{ |
| 8129 |
crop_buff = seg_buffs[i].buffer; |
| 8130 |
/* Write the current region to the current file */ |
| 8131 |
if (writeCroppedImage(in, *out, image, dump, |
| 8132 |
crop->regionlist[i].width, |
| 8133 |
crop->regionlist[i].length, crop_buff, |
| 8134 |
*page, page_count)) |
| 8135 |
{ |
| 8136 |
TIFFError("writeRegions", "Unable to write new image"); |
| 8137 |
return (-1); |
| 8138 |
} |
| 8139 |
} |
| 8140 |
break; |
| 8141 |
case FILE_PER_SELECTION: |
| 8142 |
autoindex = 1; |
| 8143 |
page_count = 1; |
| 8144 |
for (i = 0; i < crop->selections; i++) |
| 8145 |
{ |
| 8146 |
if (update_output_file(out, mp, autoindex, filename, page)) |
| 8147 |
return (1); |
| 8148 |
|
| 8149 |
crop_buff = seg_buffs[i].buffer; |
| 8150 |
/* Write the current region to the current file */ |
| 8151 |
if (writeCroppedImage(in, *out, image, dump, |
| 8152 |
crop->regionlist[i].width, |
| 8153 |
crop->regionlist[i].length, crop_buff, |
| 8154 |
*page, page_count)) |
| 8155 |
{ |
| 8156 |
TIFFError("writeRegions", "Unable to write new image"); |
| 8157 |
return (-1); |
| 8158 |
} |
| 8159 |
} |
| 8160 |
break; |
| 8161 |
default: |
| 8162 |
return (1); |
| 8163 |
} |
| 8164 |
|
| 8165 |
return (0); |
| 8166 |
} /* end writeRegions */ |
| 8167 |
|
| 8168 |
static int writeImageSections(TIFF *in, TIFF *out, struct image_data *image, |
| 8169 |
struct pagedef *page, struct pageseg *sections, |
| 8170 |
struct dump_opts *dump, unsigned char *src_buff, |
| 8171 |
unsigned char **sect_buff_ptr) |
| 8172 |
{ |
| 8173 |
double hres, vres; |
| 8174 |
uint32_t i, k, width, length, sectsize; |
| 8175 |
unsigned char *sect_buff = *sect_buff_ptr; |
| 8176 |
|
| 8177 |
hres = page->hres; |
| 8178 |
vres = page->vres; |
| 8179 |
|
| 8180 |
k = page->cols * page->rows; |
| 8181 |
if ((k < 1) || (k > MAX_SECTIONS)) |
| 8182 |
{ |
| 8183 |
TIFFError("writeImageSections", |
| 8184 |
"%" PRIu32 " Rows and Columns exceed maximum " |
| 8185 |
"sections\nIncrease resolution or reduce sections", |
| 8186 |
k); |
| 8187 |
return (-1); |
| 8188 |
} |
| 8189 |
|
| 8190 |
for (i = 0; i < k; i++) |
| 8191 |
{ |
| 8192 |
width = sections[i].x2 - sections[i].x1 + 1; |
| 8193 |
length = sections[i].y2 - sections[i].y1 + 1; |
| 8194 |
sectsize = |
| 8195 |
(uint32_t)ceil((width * image->bps * image->spp + 7) / (double)8) * |
| 8196 |
length; |
| 8197 |
/* allocate a buffer if we don't have one already */ |
| 8198 |
if (createImageSection(sectsize, sect_buff_ptr)) |
| 8199 |
{ |
| 8200 |
TIFFError("writeImageSections", |
| 8201 |
"Unable to allocate section buffer"); |
| 8202 |
exit(EXIT_FAILURE); |
| 8203 |
} |
| 8204 |
sect_buff = *sect_buff_ptr; |
| 8205 |
|
| 8206 |
if (extractImageSection(image, §ions[i], src_buff, sect_buff)) |
| 8207 |
{ |
| 8208 |
TIFFError("writeImageSections", "Unable to extract image sections"); |
| 8209 |
exit(EXIT_FAILURE); |
| 8210 |
} |
| 8211 |
|
| 8212 |
/* call the write routine here instead of outside the loop */ |
| 8213 |
if (writeSingleSection(in, out, image, dump, width, length, hres, vres, |
| 8214 |
sect_buff)) |
| 8215 |
{ |
| 8216 |
TIFFError("writeImageSections", "Unable to write image section"); |
| 8217 |
exit(EXIT_FAILURE); |
| 8218 |
} |
| 8219 |
} |
| 8220 |
|
| 8221 |
return (0); |
| 8222 |
} /* end writeImageSections */ |
| 8223 |
|
| 8224 |
/* Code in this function is heavily indebted to code in tiffcp |
| 8225 |
* with modifications by Richard Nolde to handle orientation correctly. |
| 8226 |
* It will have to be updated significantly if support is added to |
| 8227 |
* extract one or more samples from original image since the |
| 8228 |
* original code assumes we are always copying all samples. |
| 8229 |
*/ |
| 8230 |
static int writeSingleSection(TIFF *in, TIFF *out, struct image_data *image, |
| 8231 |
struct dump_opts *dump, uint32_t width, |
| 8232 |
uint32_t length, double hres, double vres, |
| 8233 |
unsigned char *sect_buff) |
| 8234 |
{ |
| 8235 |
uint16_t bps, spp; |
| 8236 |
uint16_t input_compression, input_photometric; |
| 8237 |
uint16_t input_planar; |
| 8238 |
const struct cpTag *p; |
| 8239 |
|
| 8240 |
/* Calling this seems to reset the compression mode on the TIFF *in file. |
| 8241 |
TIFFGetField(in, TIFFTAG_JPEGCOLORMODE, &input_jpeg_colormode); |
| 8242 |
*/ |
| 8243 |
input_compression = image->compression; |
| 8244 |
input_photometric = image->photometric; |
| 8245 |
|
| 8246 |
spp = image->spp; |
| 8247 |
bps = image->bps; |
| 8248 |
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width); |
| 8249 |
TIFFSetField(out, TIFFTAG_IMAGELENGTH, length); |
| 8250 |
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, bps); |
| 8251 |
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, spp); |
| 8252 |
|
| 8253 |
#ifdef DEBUG2 |
| 8254 |
TIFFError("writeSingleSection", "Input compression: %s", |
| 8255 |
(input_compression == COMPRESSION_OJPEG) |
| 8256 |
? "Old Jpeg" |
| 8257 |
: ((input_compression == COMPRESSION_JPEG) ? "New Jpeg" |
| 8258 |
: "Non Jpeg")); |
| 8259 |
#endif |
| 8260 |
/* This is the global variable compression which is set |
| 8261 |
* if the user has specified a command line option for |
| 8262 |
* a compression option. Should be passed around in one |
| 8263 |
* of the parameters instead of as a global. If no user |
| 8264 |
* option specified it will still be (uint16_t) -1. */ |
| 8265 |
if (compression != (uint16_t)-1) |
| 8266 |
TIFFSetField(out, TIFFTAG_COMPRESSION, compression); |
| 8267 |
else |
| 8268 |
{ /* OJPEG is no longer supported for writing so upgrade to JPEG */ |
| 8269 |
if (input_compression == COMPRESSION_OJPEG) |
| 8270 |
{ |
| 8271 |
compression = COMPRESSION_JPEG; |
| 8272 |
jpegcolormode = JPEGCOLORMODE_RAW; |
| 8273 |
TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_JPEG); |
| 8274 |
} |
| 8275 |
else /* Use the compression from the input file */ |
| 8276 |
CopyField(TIFFTAG_COMPRESSION, compression); |
| 8277 |
} |
| 8278 |
|
| 8279 |
if (compression == COMPRESSION_JPEG) |
| 8280 |
{ |
| 8281 |
if ((input_photometric == |
| 8282 |
PHOTOMETRIC_PALETTE) || /* color map indexed */ |
| 8283 |
(input_photometric == PHOTOMETRIC_MASK)) /* holdout mask */ |
| 8284 |
{ |
| 8285 |
TIFFError("writeSingleSection", |
| 8286 |
"JPEG compression cannot be used with %s image data", |
| 8287 |
(input_photometric == PHOTOMETRIC_PALETTE) ? "palette" |
| 8288 |
: "mask"); |
| 8289 |
return (-1); |
| 8290 |
} |
| 8291 |
if (jpegcolormode == JPEGCOLORMODE_RGB && |
| 8292 |
input_photometric == PHOTOMETRIC_YCBCR) |
| 8293 |
{ |
| 8294 |
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); |
| 8295 |
} |
| 8296 |
else if (jpegcolormode == -1 && input_photometric == PHOTOMETRIC_RGB) |
| 8297 |
{ |
| 8298 |
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR); |
| 8299 |
} |
| 8300 |
else |
| 8301 |
{ |
| 8302 |
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, input_photometric); |
| 8303 |
} |
| 8304 |
} |
| 8305 |
else |
| 8306 |
{ |
| 8307 |
if (compression == COMPRESSION_SGILOG || |
| 8308 |
compression == COMPRESSION_SGILOG24) |
| 8309 |
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, |
| 8310 |
spp == 1 ? PHOTOMETRIC_LOGL : PHOTOMETRIC_LOGLUV); |
| 8311 |
else |
| 8312 |
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, image->photometric); |
| 8313 |
} |
| 8314 |
|
| 8315 |
#ifdef DEBUG2 |
| 8316 |
TIFFError("writeSingleSection", "Input photometric: %s", |
| 8317 |
(input_photometric == PHOTOMETRIC_RGB) |
| 8318 |
? "RGB" |
| 8319 |
: ((input_photometric == PHOTOMETRIC_YCBCR) |
| 8320 |
? "YCbCr" |
| 8321 |
: "Not RGB or YCbCr")); |
| 8322 |
#endif |
| 8323 |
|
| 8324 |
if (((input_photometric == PHOTOMETRIC_LOGL) || |
| 8325 |
(input_photometric == PHOTOMETRIC_LOGLUV)) && |
| 8326 |
((compression != COMPRESSION_SGILOG) && |
| 8327 |
(compression != COMPRESSION_SGILOG24))) |
| 8328 |
{ |
| 8329 |
TIFFError("writeSingleSection", "LogL and LogLuv source data require " |
| 8330 |
"SGI_LOG or SGI_LOG24 compression"); |
| 8331 |
return (-1); |
| 8332 |
} |
| 8333 |
|
| 8334 |
if (fillorder != 0) |
| 8335 |
TIFFSetField(out, TIFFTAG_FILLORDER, fillorder); |
| 8336 |
else |
| 8337 |
CopyTag(TIFFTAG_FILLORDER, 1, TIFF_SHORT); |
| 8338 |
|
| 8339 |
/* The loadimage function reads input orientation and sets |
| 8340 |
* image->orientation. The correct_image_orientation function |
| 8341 |
* applies the required rotation and mirror operations to |
| 8342 |
* present the data in TOPLEFT orientation and updates |
| 8343 |
* image->orientation if any transforms are performed, |
| 8344 |
* as per EXIF standard. |
| 8345 |
*/ |
| 8346 |
TIFFSetField(out, TIFFTAG_ORIENTATION, image->orientation); |
| 8347 |
|
| 8348 |
/* |
| 8349 |
* Choose tiles/strip for the output image according to |
| 8350 |
* the command line arguments (-tiles, -strips) and the |
| 8351 |
* structure of the input image. |
| 8352 |
*/ |
| 8353 |
if (outtiled == -1) |
| 8354 |
outtiled = TIFFIsTiled(in); |
| 8355 |
if (outtiled) |
| 8356 |
{ |
| 8357 |
/* |
| 8358 |
* Setup output file's tile width&height. If either |
| 8359 |
* is not specified, use either the value from the |
| 8360 |
* input image or, if nothing is defined, use the |
| 8361 |
* library default. |
| 8362 |
*/ |
| 8363 |
if (tilewidth == (uint32_t)0) |
| 8364 |
TIFFGetField(in, TIFFTAG_TILEWIDTH, &tilewidth); |
| 8365 |
if (tilelength == (uint32_t)0) |
| 8366 |
TIFFGetField(in, TIFFTAG_TILELENGTH, &tilelength); |
| 8367 |
|
| 8368 |
if (tilewidth == 0 || tilelength == 0) |
| 8369 |
TIFFDefaultTileSize(out, &tilewidth, &tilelength); |
| 8370 |
TIFFDefaultTileSize(out, &tilewidth, &tilelength); |
| 8371 |
TIFFSetField(out, TIFFTAG_TILEWIDTH, tilewidth); |
| 8372 |
TIFFSetField(out, TIFFTAG_TILELENGTH, tilelength); |
| 8373 |
} |
| 8374 |
else |
| 8375 |
{ |
| 8376 |
/* |
| 8377 |
* RowsPerStrip is left unspecified: use either the |
| 8378 |
* value from the input image or, if nothing is defined, |
| 8379 |
* use the library default. |
| 8380 |
*/ |
| 8381 |
if (rowsperstrip == (uint32_t)0) |
| 8382 |
{ |
| 8383 |
if (!TIFFGetField(in, TIFFTAG_ROWSPERSTRIP, &rowsperstrip)) |
| 8384 |
rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip); |
| 8385 |
if (compression != COMPRESSION_JPEG) |
| 8386 |
{ |
| 8387 |
if (rowsperstrip > length) |
| 8388 |
rowsperstrip = length; |
| 8389 |
} |
| 8390 |
} |
| 8391 |
else if (rowsperstrip == (uint32_t)-1) |
| 8392 |
rowsperstrip = length; |
| 8393 |
TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip); |
| 8394 |
} |
| 8395 |
|
| 8396 |
TIFFGetFieldDefaulted(in, TIFFTAG_PLANARCONFIG, &input_planar); |
| 8397 |
if (config != (uint16_t)-1) |
| 8398 |
TIFFSetField(out, TIFFTAG_PLANARCONFIG, config); |
| 8399 |
else |
| 8400 |
CopyField(TIFFTAG_PLANARCONFIG, config); |
| 8401 |
if (spp <= 4) |
| 8402 |
CopyTag(TIFFTAG_TRANSFERFUNCTION, 4, TIFF_SHORT); |
| 8403 |
CopyTag(TIFFTAG_COLORMAP, 4, TIFF_SHORT); |
| 8404 |
|
| 8405 |
/* SMinSampleValue & SMaxSampleValue */ |
| 8406 |
switch (compression) |
| 8407 |
{ |
| 8408 |
/* These are references to GLOBAL variables set by defaults |
| 8409 |
* and /or the compression flag |
| 8410 |
*/ |
| 8411 |
case COMPRESSION_JPEG: |
| 8412 |
if (((bps % 8) == 0) || ((bps % 12) == 0)) |
| 8413 |
{ |
| 8414 |
TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality); |
| 8415 |
TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB); |
| 8416 |
} |
| 8417 |
else |
| 8418 |
{ |
| 8419 |
TIFFError("writeSingleSection", |
| 8420 |
"JPEG compression requires 8 or 12 bits per sample"); |
| 8421 |
return (-1); |
| 8422 |
} |
| 8423 |
break; |
| 8424 |
case COMPRESSION_LZW: |
| 8425 |
case COMPRESSION_ADOBE_DEFLATE: |
| 8426 |
case COMPRESSION_DEFLATE: |
| 8427 |
if (predictor != (uint16_t)-1) |
| 8428 |
TIFFSetField(out, TIFFTAG_PREDICTOR, predictor); |
| 8429 |
else |
| 8430 |
CopyField(TIFFTAG_PREDICTOR, predictor); |
| 8431 |
break; |
| 8432 |
case COMPRESSION_CCITTFAX3: |
| 8433 |
case COMPRESSION_CCITTFAX4: |
| 8434 |
if (bps != 1) |
| 8435 |
{ |
| 8436 |
TIFFError("writeCroppedImage", |
| 8437 |
"Group 3/4 compression is not usable with bps > 1"); |
| 8438 |
return (-1); |
| 8439 |
} |
| 8440 |
if (compression == COMPRESSION_CCITTFAX3) |
| 8441 |
{ |
| 8442 |
if (g3opts != (uint32_t)-1) |
| 8443 |
TIFFSetField(out, TIFFTAG_GROUP3OPTIONS, g3opts); |
| 8444 |
else |
| 8445 |
CopyField(TIFFTAG_GROUP3OPTIONS, g3opts); |
| 8446 |
} |
| 8447 |
else |
| 8448 |
{ |
| 8449 |
CopyTag(TIFFTAG_GROUP4OPTIONS, 1, TIFF_LONG); |
| 8450 |
} |
| 8451 |
CopyTag(TIFFTAG_BADFAXLINES, 1, TIFF_LONG); |
| 8452 |
CopyTag(TIFFTAG_CLEANFAXDATA, 1, TIFF_LONG); |
| 8453 |
CopyTag(TIFFTAG_CONSECUTIVEBADFAXLINES, 1, TIFF_LONG); |
| 8454 |
CopyTag(TIFFTAG_FAXRECVPARAMS, 1, TIFF_LONG); |
| 8455 |
CopyTag(TIFFTAG_FAXRECVTIME, 1, TIFF_LONG); |
| 8456 |
CopyTag(TIFFTAG_FAXSUBADDRESS, 1, TIFF_ASCII); |
| 8457 |
break; |
| 8458 |
} |
| 8459 |
{ |
| 8460 |
uint32_t len32; |
| 8461 |
void **data; |
| 8462 |
if (TIFFGetField(in, TIFFTAG_ICCPROFILE, &len32, &data)) |
| 8463 |
TIFFSetField(out, TIFFTAG_ICCPROFILE, len32, data); |
| 8464 |
} |
| 8465 |
{ |
| 8466 |
uint16_t ninks; |
| 8467 |
const char *inknames; |
| 8468 |
if (TIFFGetField(in, TIFFTAG_NUMBEROFINKS, &ninks)) |
| 8469 |
{ |
| 8470 |
TIFFSetField(out, TIFFTAG_NUMBEROFINKS, ninks); |
| 8471 |
if (TIFFGetField(in, TIFFTAG_INKNAMES, &inknames)) |
| 8472 |
{ |
| 8473 |
int inknameslen = (int)strlen(inknames) + 1; |
| 8474 |
const char *cp = inknames; |
| 8475 |
while (ninks > 1) |
| 8476 |
{ |
| 8477 |
cp = strchr(cp, '\0'); |
| 8478 |
if (cp) |
| 8479 |
{ |
| 8480 |
cp++; |
| 8481 |
inknameslen += ((int)strlen(cp) + 1); |
| 8482 |
} |
| 8483 |
ninks--; |
| 8484 |
} |
| 8485 |
TIFFSetField(out, TIFFTAG_INKNAMES, inknameslen, inknames); |
| 8486 |
} |
| 8487 |
} |
| 8488 |
} |
| 8489 |
{ |
| 8490 |
unsigned short pg0, pg1; |
| 8491 |
if (TIFFGetField(in, TIFFTAG_PAGENUMBER, &pg0, &pg1)) |
| 8492 |
{ |
| 8493 |
if (pageNum < 0) /* only one input file */ |
| 8494 |
TIFFSetField(out, TIFFTAG_PAGENUMBER, pg0, pg1); |
| 8495 |
else |
| 8496 |
TIFFSetField(out, TIFFTAG_PAGENUMBER, pageNum++, 0); |
| 8497 |
} |
| 8498 |
} |
| 8499 |
|
| 8500 |
for (p = tags; p < &tags[NTAGS]; p++) |
| 8501 |
CopyTag(p->tag, p->count, p->type); |
| 8502 |
|
| 8503 |
/* Update these since they are overwritten from input res by loop above */ |
| 8504 |
TIFFSetField(out, TIFFTAG_XRESOLUTION, (float)hres); |
| 8505 |
TIFFSetField(out, TIFFTAG_YRESOLUTION, (float)vres); |
| 8506 |
|
| 8507 |
/* Compute the tile or strip dimensions and write to disk */ |
| 8508 |
if (outtiled) |
| 8509 |
{ |
| 8510 |
if (config == PLANARCONFIG_CONTIG) |
| 8511 |
writeBufferToContigTiles(out, sect_buff, length, width, spp, dump); |
| 8512 |
else |
| 8513 |
writeBufferToSeparateTiles(out, sect_buff, length, width, spp, |
| 8514 |
dump); |
| 8515 |
} |
| 8516 |
else |
| 8517 |
{ |
| 8518 |
if (config == PLANARCONFIG_CONTIG) |
| 8519 |
writeBufferToContigStrips(out, sect_buff, length); |
| 8520 |
else |
| 8521 |
writeBufferToSeparateStrips(out, sect_buff, length, width, spp, |
| 8522 |
dump); |
| 8523 |
} |
| 8524 |
|
| 8525 |
if (!TIFFWriteDirectory(out)) |
| 8526 |
{ |
| 8527 |
TIFFClose(out); |
| 8528 |
return (-1); |
| 8529 |
} |
| 8530 |
|
| 8531 |
return (0); |
| 8532 |
} /* end writeSingleSection */ |
| 8533 |
|
| 8534 |
/* Create a buffer to write one section at a time */ |
| 8535 |
static int createImageSection(uint32_t sectsize, unsigned char **sect_buff_ptr) |
| 8536 |
{ |
| 8537 |
unsigned char *sect_buff = NULL; |
| 8538 |
unsigned char *new_buff = NULL; |
| 8539 |
static uint32_t prev_sectsize = 0; |
| 8540 |
|
| 8541 |
sect_buff = *sect_buff_ptr; |
| 8542 |
|
| 8543 |
if (!sect_buff) |
| 8544 |
{ |
| 8545 |
sect_buff = |
| 8546 |
(unsigned char *)limitMalloc(sectsize + NUM_BUFF_OVERSIZE_BYTES); |
| 8547 |
if (!sect_buff) |
| 8548 |
{ |
| 8549 |
TIFFError("createImageSection", |
| 8550 |
"Unable to allocate/reallocate section buffer"); |
| 8551 |
return (-1); |
| 8552 |
} |
| 8553 |
_TIFFmemset(sect_buff, 0, sectsize + NUM_BUFF_OVERSIZE_BYTES); |
| 8554 |
} |
| 8555 |
else |
| 8556 |
{ |
| 8557 |
if (prev_sectsize < sectsize) |
| 8558 |
{ |
| 8559 |
new_buff = |
| 8560 |
_TIFFrealloc(sect_buff, sectsize + NUM_BUFF_OVERSIZE_BYTES); |
| 8561 |
if (!new_buff) |
| 8562 |
{ |
| 8563 |
_TIFFfree(sect_buff); |
| 8564 |
sect_buff = (unsigned char *)limitMalloc( |
| 8565 |
sectsize + NUM_BUFF_OVERSIZE_BYTES); |
| 8566 |
} |
| 8567 |
else |
| 8568 |
sect_buff = new_buff; |
| 8569 |
|
| 8570 |
if (!sect_buff) |
| 8571 |
{ |
| 8572 |
TIFFError("createImageSection", |
| 8573 |
"Unable to allocate/reallocate section buffer"); |
| 8574 |
return (-1); |
| 8575 |
} |
| 8576 |
_TIFFmemset(sect_buff, 0, sectsize + NUM_BUFF_OVERSIZE_BYTES); |
| 8577 |
} |
| 8578 |
} |
| 8579 |
|
| 8580 |
prev_sectsize = sectsize; |
| 8581 |
*sect_buff_ptr = sect_buff; |
| 8582 |
|
| 8583 |
return (0); |
| 8584 |
} /* end createImageSection */ |
| 8585 |
|
| 8586 |
/* Process selections defined by regions, zones, margins, or fixed sized areas |
| 8587 |
*/ |
| 8588 |
static int processCropSelections(struct image_data *image, |
| 8589 |
struct crop_mask *crop, |
| 8590 |
unsigned char **read_buff_ptr, |
| 8591 |
struct buffinfo seg_buffs[]) |
| 8592 |
{ |
| 8593 |
int i; |
| 8594 |
uint32_t width, length, total_width, total_length; |
| 8595 |
tsize_t cropsize; |
| 8596 |
unsigned char *crop_buff = NULL; |
| 8597 |
unsigned char *read_buff = NULL; |
| 8598 |
unsigned char *next_buff = NULL; |
| 8599 |
tsize_t prev_cropsize = 0; |
| 8600 |
|
| 8601 |
read_buff = *read_buff_ptr; |
| 8602 |
|
| 8603 |
if (crop->img_mode == COMPOSITE_IMAGES) |
| 8604 |
{ |
| 8605 |
cropsize = crop->bufftotal; |
| 8606 |
crop_buff = seg_buffs[0].buffer; |
| 8607 |
if (!crop_buff) |
| 8608 |
crop_buff = (unsigned char *)limitMalloc(cropsize + |
| 8609 |
NUM_BUFF_OVERSIZE_BYTES); |
| 8610 |
else |
| 8611 |
{ |
| 8612 |
prev_cropsize = seg_buffs[0].size; |
| 8613 |
if (prev_cropsize < cropsize) |
| 8614 |
{ |
| 8615 |
next_buff = |
| 8616 |
_TIFFrealloc(crop_buff, cropsize + NUM_BUFF_OVERSIZE_BYTES); |
| 8617 |
if (!next_buff) |
| 8618 |
{ |
| 8619 |
_TIFFfree(crop_buff); |
| 8620 |
crop_buff = (unsigned char *)limitMalloc( |
| 8621 |
cropsize + NUM_BUFF_OVERSIZE_BYTES); |
| 8622 |
} |
| 8623 |
else |
| 8624 |
crop_buff = next_buff; |
| 8625 |
} |
| 8626 |
} |
| 8627 |
|
| 8628 |
if (!crop_buff) |
| 8629 |
{ |
| 8630 |
TIFFError("processCropSelections", |
| 8631 |
"Unable to allocate/reallocate crop buffer"); |
| 8632 |
return (-1); |
| 8633 |
} |
| 8634 |
|
| 8635 |
_TIFFmemset(crop_buff, 0, cropsize + NUM_BUFF_OVERSIZE_BYTES); |
| 8636 |
seg_buffs[0].buffer = crop_buff; |
| 8637 |
seg_buffs[0].size = cropsize; |
| 8638 |
|
| 8639 |
/* Checks for matching width or length as required */ |
| 8640 |
if (extractCompositeRegions(image, crop, read_buff, crop_buff) != 0) |
| 8641 |
return (1); |
| 8642 |
|
| 8643 |
if (crop->crop_mode & CROP_INVERT) |
| 8644 |
{ |
| 8645 |
switch (crop->photometric) |
| 8646 |
{ |
| 8647 |
/* Just change the interpretation */ |
| 8648 |
case PHOTOMETRIC_MINISWHITE: |
| 8649 |
case PHOTOMETRIC_MINISBLACK: |
| 8650 |
image->photometric = crop->photometric; |
| 8651 |
break; |
| 8652 |
case INVERT_DATA_ONLY: |
| 8653 |
case INVERT_DATA_AND_TAG: |
| 8654 |
if (invertImage(image->photometric, image->spp, image->bps, |
| 8655 |
crop->combined_width, crop->combined_length, |
| 8656 |
crop_buff)) |
| 8657 |
{ |
| 8658 |
TIFFError("processCropSelections", |
| 8659 |
"Failed to invert colorspace for composite " |
| 8660 |
"regions"); |
| 8661 |
return (-1); |
| 8662 |
} |
| 8663 |
if (crop->photometric == INVERT_DATA_AND_TAG) |
| 8664 |
{ |
| 8665 |
switch (image->photometric) |
| 8666 |
{ |
| 8667 |
case PHOTOMETRIC_MINISWHITE: |
| 8668 |
image->photometric = PHOTOMETRIC_MINISBLACK; |
| 8669 |
break; |
| 8670 |
case PHOTOMETRIC_MINISBLACK: |
| 8671 |
image->photometric = PHOTOMETRIC_MINISWHITE; |
| 8672 |
break; |
| 8673 |
default: |
| 8674 |
break; |
| 8675 |
} |
| 8676 |
} |
| 8677 |
break; |
| 8678 |
default: |
| 8679 |
break; |
| 8680 |
} |
| 8681 |
} |
| 8682 |
|
| 8683 |
/* Mirror and Rotate will not work with multiple regions unless they are |
| 8684 |
* the same width */ |
| 8685 |
if (crop->crop_mode & CROP_MIRROR) |
| 8686 |
{ |
| 8687 |
if (mirrorImage(image->spp, image->bps, crop->mirror, |
| 8688 |
crop->combined_width, crop->combined_length, |
| 8689 |
crop_buff)) |
| 8690 |
{ |
| 8691 |
TIFFError("processCropSelections", |
| 8692 |
"Failed to mirror composite regions %s", |
| 8693 |
(crop->rotation == MIRROR_HORIZ) ? "horizontally" |
| 8694 |
: "vertically"); |
| 8695 |
return (-1); |
| 8696 |
} |
| 8697 |
} |
| 8698 |
|
| 8699 |
if (crop->crop_mode & CROP_ROTATE) /* rotate should be last as it can |
| 8700 |
reallocate the buffer */ |
| 8701 |
{ |
| 8702 |
/* rotateImage() set up a new buffer and calculates its size |
| 8703 |
* individually. Therefore, seg_buffs size needs to be updated |
| 8704 |
* accordingly. */ |
| 8705 |
size_t rot_buf_size = 0; |
| 8706 |
if (rotateImage(crop->rotation, image, &crop->combined_width, |
| 8707 |
&crop->combined_length, &crop_buff, &rot_buf_size, |
| 8708 |
FALSE)) |
| 8709 |
{ |
| 8710 |
TIFFError("processCropSelections", |
| 8711 |
"Failed to rotate composite regions by %" PRIu32 |
| 8712 |
" degrees", |
| 8713 |
crop->rotation); |
| 8714 |
return (-1); |
| 8715 |
} |
| 8716 |
seg_buffs[0].buffer = crop_buff; |
| 8717 |
seg_buffs[0].size = rot_buf_size; |
| 8718 |
} |
| 8719 |
} |
| 8720 |
else /* Separated Images */ |
| 8721 |
{ |
| 8722 |
total_width = total_length = 0; |
| 8723 |
for (i = 0; i < crop->selections; i++) |
| 8724 |
{ |
| 8725 |
|
| 8726 |
cropsize = crop->bufftotal; |
| 8727 |
crop_buff = seg_buffs[i].buffer; |
| 8728 |
if (!crop_buff) |
| 8729 |
crop_buff = (unsigned char *)limitMalloc( |
| 8730 |
cropsize + NUM_BUFF_OVERSIZE_BYTES); |
| 8731 |
else |
| 8732 |
{ |
| 8733 |
prev_cropsize = seg_buffs[i].size; |
| 8734 |
if (prev_cropsize < cropsize) |
| 8735 |
{ |
| 8736 |
next_buff = _TIFFrealloc( |
| 8737 |
crop_buff, cropsize + NUM_BUFF_OVERSIZE_BYTES); |
| 8738 |
if (!next_buff) |
| 8739 |
{ |
| 8740 |
_TIFFfree(crop_buff); |
| 8741 |
crop_buff = (unsigned char *)limitMalloc( |
| 8742 |
cropsize + NUM_BUFF_OVERSIZE_BYTES); |
| 8743 |
} |
| 8744 |
else |
| 8745 |
crop_buff = next_buff; |
| 8746 |
} |
| 8747 |
} |
| 8748 |
|
| 8749 |
if (!crop_buff) |
| 8750 |
{ |
| 8751 |
TIFFError("processCropSelections", |
| 8752 |
"Unable to allocate/reallocate crop buffer"); |
| 8753 |
return (-1); |
| 8754 |
} |
| 8755 |
|
| 8756 |
_TIFFmemset(crop_buff, 0, cropsize + NUM_BUFF_OVERSIZE_BYTES); |
| 8757 |
seg_buffs[i].buffer = crop_buff; |
| 8758 |
seg_buffs[i].size = cropsize; |
| 8759 |
|
| 8760 |
if (extractSeparateRegion(image, crop, read_buff, crop_buff, i)) |
| 8761 |
{ |
| 8762 |
TIFFError("processCropSelections", |
| 8763 |
"Unable to extract cropped region %d from image", i); |
| 8764 |
return (-1); |
| 8765 |
} |
| 8766 |
|
| 8767 |
width = crop->regionlist[i].width; |
| 8768 |
length = crop->regionlist[i].length; |
| 8769 |
|
| 8770 |
if (crop->crop_mode & CROP_INVERT) |
| 8771 |
{ |
| 8772 |
switch (crop->photometric) |
| 8773 |
{ |
| 8774 |
/* Just change the interpretation */ |
| 8775 |
case PHOTOMETRIC_MINISWHITE: |
| 8776 |
case PHOTOMETRIC_MINISBLACK: |
| 8777 |
image->photometric = crop->photometric; |
| 8778 |
break; |
| 8779 |
case INVERT_DATA_ONLY: |
| 8780 |
case INVERT_DATA_AND_TAG: |
| 8781 |
if (invertImage(image->photometric, image->spp, |
| 8782 |
image->bps, width, length, crop_buff)) |
| 8783 |
{ |
| 8784 |
TIFFError("processCropSelections", |
| 8785 |
"Failed to invert colorspace for region"); |
| 8786 |
return (-1); |
| 8787 |
} |
| 8788 |
if (crop->photometric == INVERT_DATA_AND_TAG) |
| 8789 |
{ |
| 8790 |
switch (image->photometric) |
| 8791 |
{ |
| 8792 |
case PHOTOMETRIC_MINISWHITE: |
| 8793 |
image->photometric = PHOTOMETRIC_MINISBLACK; |
| 8794 |
break; |
| 8795 |
case PHOTOMETRIC_MINISBLACK: |
| 8796 |
image->photometric = PHOTOMETRIC_MINISWHITE; |
| 8797 |
break; |
| 8798 |
default: |
| 8799 |
break; |
| 8800 |
} |
| 8801 |
} |
| 8802 |
break; |
| 8803 |
default: |
| 8804 |
break; |
| 8805 |
} |
| 8806 |
} |
| 8807 |
|
| 8808 |
if (crop->crop_mode & CROP_MIRROR) |
| 8809 |
{ |
| 8810 |
if (mirrorImage(image->spp, image->bps, crop->mirror, width, |
| 8811 |
length, crop_buff)) |
| 8812 |
{ |
| 8813 |
TIFFError("processCropSelections", |
| 8814 |
"Failed to mirror crop region %s", |
| 8815 |
(crop->rotation == MIRROR_HORIZ) ? "horizontally" |
| 8816 |
: "vertically"); |
| 8817 |
return (-1); |
| 8818 |
} |
| 8819 |
} |
| 8820 |
|
| 8821 |
if (crop->crop_mode & CROP_ROTATE) /* rotate should be last as it |
| 8822 |
can reallocate the buffer */ |
| 8823 |
{ |
| 8824 |
/* rotateImage() changes image->width, ->length, ->xres and |
| 8825 |
* ->yres, what it schouldn't do here, when more than one |
| 8826 |
* section is processed. ToDo: Therefore rotateImage() and its |
| 8827 |
* usage has to be reworked (e.g. like mirrorImage()) !! |
| 8828 |
* Furthermore, rotateImage() set up a new buffer and calculates |
| 8829 |
* its size individually. Therefore, seg_buffs size needs to be |
| 8830 |
* updated accordingly. */ |
| 8831 |
size_t rot_buf_size = 0; |
| 8832 |
if (rotateImage(crop->rotation, image, |
| 8833 |
&crop->regionlist[i].width, |
| 8834 |
&crop->regionlist[i].length, &crop_buff, |
| 8835 |
&rot_buf_size, FALSE)) |
| 8836 |
{ |
| 8837 |
TIFFError("processCropSelections", |
| 8838 |
"Failed to rotate crop region by %" PRIu16 |
| 8839 |
" degrees", |
| 8840 |
crop->rotation); |
| 8841 |
return (-1); |
| 8842 |
} |
| 8843 |
total_width += crop->regionlist[i].width; |
| 8844 |
total_length += crop->regionlist[i].length; |
| 8845 |
crop->combined_width = total_width; |
| 8846 |
crop->combined_length = total_length; |
| 8847 |
seg_buffs[i].buffer = crop_buff; |
| 8848 |
seg_buffs[i].size = rot_buf_size; |
| 8849 |
} |
| 8850 |
} /* for crop->selections loop */ |
| 8851 |
} /* Separated Images (else case) */ |
| 8852 |
return (0); |
| 8853 |
} /* end processCropSelections */ |
| 8854 |
|
| 8855 |
/* Copy the crop section of the data from the current image into a buffer |
| 8856 |
* and adjust the IFD values to reflect the new size. If no cropping is |
| 8857 |
* required, use the original read buffer as the crop buffer. |
| 8858 |
* |
| 8859 |
* There is quite a bit of redundancy between this routine and the more |
| 8860 |
* specialized processCropSelections, but this provides |
| 8861 |
* the most optimized path when no Zones or Regions are required. |
| 8862 |
*/ |
| 8863 |
static int createCroppedImage(struct image_data *image, struct crop_mask *crop, |
| 8864 |
unsigned char **read_buff_ptr, |
| 8865 |
unsigned char **crop_buff_ptr) |
| 8866 |
{ |
| 8867 |
tsize_t cropsize; |
| 8868 |
unsigned char *read_buff = NULL; |
| 8869 |
unsigned char *crop_buff = NULL; |
| 8870 |
unsigned char *new_buff = NULL; |
| 8871 |
static tsize_t prev_cropsize = 0; |
| 8872 |
|
| 8873 |
read_buff = *read_buff_ptr; |
| 8874 |
|
| 8875 |
/* Memory is freed before crop_buff_ptr is overwritten */ |
| 8876 |
if (*crop_buff_ptr != NULL) |
| 8877 |
{ |
| 8878 |
_TIFFfree(*crop_buff_ptr); |
| 8879 |
} |
| 8880 |
|
| 8881 |
/* process full image, no crop buffer needed */ |
| 8882 |
*crop_buff_ptr = read_buff; |
| 8883 |
crop->combined_width = image->width; |
| 8884 |
crop->combined_length = image->length; |
| 8885 |
|
| 8886 |
cropsize = crop->bufftotal; |
| 8887 |
crop_buff = *crop_buff_ptr; |
| 8888 |
if (!crop_buff) |
| 8889 |
{ |
| 8890 |
crop_buff = |
| 8891 |
(unsigned char *)limitMalloc(cropsize + NUM_BUFF_OVERSIZE_BYTES); |
| 8892 |
if (!crop_buff) |
| 8893 |
{ |
| 8894 |
TIFFError("createCroppedImage", |
| 8895 |
"Unable to allocate/reallocate crop buffer"); |
| 8896 |
return (-1); |
| 8897 |
} |
| 8898 |
_TIFFmemset(crop_buff, 0, cropsize + NUM_BUFF_OVERSIZE_BYTES); |
| 8899 |
prev_cropsize = cropsize; |
| 8900 |
} |
| 8901 |
else |
| 8902 |
{ |
| 8903 |
if (prev_cropsize < cropsize) |
| 8904 |
{ |
| 8905 |
new_buff = |
| 8906 |
_TIFFrealloc(crop_buff, cropsize + NUM_BUFF_OVERSIZE_BYTES); |
| 8907 |
if (!new_buff) |
| 8908 |
{ |
| 8909 |
free(crop_buff); |
| 8910 |
crop_buff = (unsigned char *)limitMalloc( |
| 8911 |
cropsize + NUM_BUFF_OVERSIZE_BYTES); |
| 8912 |
} |
| 8913 |
else |
| 8914 |
crop_buff = new_buff; |
| 8915 |
if (!crop_buff) |
| 8916 |
{ |
| 8917 |
TIFFError("createCroppedImage", |
| 8918 |
"Unable to allocate/reallocate crop buffer"); |
| 8919 |
return (-1); |
| 8920 |
} |
| 8921 |
_TIFFmemset(crop_buff, 0, cropsize + NUM_BUFF_OVERSIZE_BYTES); |
| 8922 |
} |
| 8923 |
} |
| 8924 |
|
| 8925 |
*crop_buff_ptr = crop_buff; |
| 8926 |
|
| 8927 |
if (crop->crop_mode & CROP_INVERT) |
| 8928 |
{ |
| 8929 |
switch (crop->photometric) |
| 8930 |
{ |
| 8931 |
/* Just change the interpretation */ |
| 8932 |
case PHOTOMETRIC_MINISWHITE: |
| 8933 |
case PHOTOMETRIC_MINISBLACK: |
| 8934 |
image->photometric = crop->photometric; |
| 8935 |
break; |
| 8936 |
case INVERT_DATA_ONLY: |
| 8937 |
case INVERT_DATA_AND_TAG: |
| 8938 |
if (invertImage(image->photometric, image->spp, image->bps, |
| 8939 |
crop->combined_width, crop->combined_length, |
| 8940 |
crop_buff)) |
| 8941 |
{ |
| 8942 |
TIFFError("createCroppedImage", |
| 8943 |
"Failed to invert colorspace for image or " |
| 8944 |
"cropped selection"); |
| 8945 |
return (-1); |
| 8946 |
} |
| 8947 |
if (crop->photometric == INVERT_DATA_AND_TAG) |
| 8948 |
{ |
| 8949 |
switch (image->photometric) |
| 8950 |
{ |
| 8951 |
case PHOTOMETRIC_MINISWHITE: |
| 8952 |
image->photometric = PHOTOMETRIC_MINISBLACK; |
| 8953 |
break; |
| 8954 |
case PHOTOMETRIC_MINISBLACK: |
| 8955 |
image->photometric = PHOTOMETRIC_MINISWHITE; |
| 8956 |
break; |
| 8957 |
default: |
| 8958 |
break; |
| 8959 |
} |
| 8960 |
} |
| 8961 |
break; |
| 8962 |
default: |
| 8963 |
break; |
| 8964 |
} |
| 8965 |
} |
| 8966 |
|
| 8967 |
if (crop->crop_mode & CROP_MIRROR) |
| 8968 |
{ |
| 8969 |
if (mirrorImage(image->spp, image->bps, crop->mirror, |
| 8970 |
crop->combined_width, crop->combined_length, crop_buff)) |
| 8971 |
{ |
| 8972 |
TIFFError("createCroppedImage", |
| 8973 |
"Failed to mirror image or cropped selection %s", |
| 8974 |
(crop->rotation == MIRROR_HORIZ) ? "horizontally" |
| 8975 |
: "vertically"); |
| 8976 |
return (-1); |
| 8977 |
} |
| 8978 |
} |
| 8979 |
|
| 8980 |
if (crop->crop_mode & |
| 8981 |
CROP_ROTATE) /* rotate should be last as it can reallocate the buffer */ |
| 8982 |
{ |
| 8983 |
if (rotateImage(crop->rotation, image, &crop->combined_width, |
| 8984 |
&crop->combined_length, crop_buff_ptr, NULL, TRUE)) |
| 8985 |
{ |
| 8986 |
TIFFError("createCroppedImage", |
| 8987 |
"Failed to rotate image or cropped selection by %" PRIu16 |
| 8988 |
" degrees", |
| 8989 |
crop->rotation); |
| 8990 |
return (-1); |
| 8991 |
} |
| 8992 |
} |
| 8993 |
|
| 8994 |
if (crop_buff == |
| 8995 |
read_buff) /* we used the read buffer for the crop buffer */ |
| 8996 |
*read_buff_ptr = NULL; /* so we don't try to free it later */ |
| 8997 |
|
| 8998 |
return (0); |
| 8999 |
} /* end createCroppedImage */ |
| 9000 |
|
| 9001 |
/* Code in this function is heavily indebted to code in tiffcp |
| 9002 |
* with modifications by Richard Nolde to handle orientation correctly. |
| 9003 |
* It will have to be updated significantly if support is added to |
| 9004 |
* extract one or more samples from original image since the |
| 9005 |
* original code assumes we are always copying all samples. |
| 9006 |
* Use of global variables for config, compression and others |
| 9007 |
* should be replaced by addition to the crop_mask struct (which |
| 9008 |
* will be renamed to proc_opts indicating that is controls |
| 9009 |
* user supplied processing options, not just cropping) and |
| 9010 |
* then passed in as an argument. |
| 9011 |
*/ |
| 9012 |
static int writeCroppedImage(TIFF *in, TIFF *out, struct image_data *image, |
| 9013 |
struct dump_opts *dump, uint32_t width, |
| 9014 |
uint32_t length, unsigned char *crop_buff, |
| 9015 |
int pagenum, int total_pages) |
| 9016 |
{ |
| 9017 |
uint16_t bps, spp; |
| 9018 |
uint16_t input_compression, input_photometric; |
| 9019 |
uint16_t input_planar; |
| 9020 |
const struct cpTag *p; |
| 9021 |
|
| 9022 |
input_compression = image->compression; |
| 9023 |
input_photometric = image->photometric; |
| 9024 |
spp = image->spp; |
| 9025 |
bps = image->bps; |
| 9026 |
|
| 9027 |
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width); |
| 9028 |
TIFFSetField(out, TIFFTAG_IMAGELENGTH, length); |
| 9029 |
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, bps); |
| 9030 |
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, spp); |
| 9031 |
|
| 9032 |
#ifdef DEBUG2 |
| 9033 |
TIFFError("writeCroppedImage", "Input compression: %s", |
| 9034 |
(input_compression == COMPRESSION_OJPEG) |
| 9035 |
? "Old Jpeg" |
| 9036 |
: ((input_compression == COMPRESSION_JPEG) ? "New Jpeg" |
| 9037 |
: "Non Jpeg")); |
| 9038 |
#endif |
| 9039 |
|
| 9040 |
if (compression != (uint16_t)-1) |
| 9041 |
TIFFSetField(out, TIFFTAG_COMPRESSION, compression); |
| 9042 |
else |
| 9043 |
{ |
| 9044 |
if (input_compression == COMPRESSION_OJPEG) |
| 9045 |
{ |
| 9046 |
compression = COMPRESSION_JPEG; |
| 9047 |
jpegcolormode = JPEGCOLORMODE_RAW; |
| 9048 |
TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_JPEG); |
| 9049 |
} |
| 9050 |
else |
| 9051 |
CopyField(TIFFTAG_COMPRESSION, compression); |
| 9052 |
} |
| 9053 |
|
| 9054 |
if (compression == COMPRESSION_JPEG) |
| 9055 |
{ |
| 9056 |
if ((input_photometric == |
| 9057 |
PHOTOMETRIC_PALETTE) || /* color map indexed */ |
| 9058 |
(input_photometric == PHOTOMETRIC_MASK)) /* $holdout mask */ |
| 9059 |
{ |
| 9060 |
TIFFError("writeCroppedImage", |
| 9061 |
"JPEG compression cannot be used with %s image data", |
| 9062 |
(input_photometric == PHOTOMETRIC_PALETTE) ? "palette" |
| 9063 |
: "mask"); |
| 9064 |
return (-1); |
| 9065 |
} |
| 9066 |
if (jpegcolormode == JPEGCOLORMODE_RGB && |
| 9067 |
input_photometric == PHOTOMETRIC_YCBCR) |
| 9068 |
{ |
| 9069 |
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); |
| 9070 |
} |
| 9071 |
else if (jpegcolormode == -1 && input_photometric == PHOTOMETRIC_RGB) |
| 9072 |
{ |
| 9073 |
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR); |
| 9074 |
} |
| 9075 |
else |
| 9076 |
{ |
| 9077 |
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, input_photometric); |
| 9078 |
} |
| 9079 |
} |
| 9080 |
else |
| 9081 |
{ |
| 9082 |
if (compression == COMPRESSION_SGILOG || |
| 9083 |
compression == COMPRESSION_SGILOG24) |
| 9084 |
{ |
| 9085 |
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, |
| 9086 |
spp == 1 ? PHOTOMETRIC_LOGL : PHOTOMETRIC_LOGLUV); |
| 9087 |
} |
| 9088 |
else |
| 9089 |
{ |
| 9090 |
if (input_compression == COMPRESSION_SGILOG || |
| 9091 |
input_compression == COMPRESSION_SGILOG24) |
| 9092 |
{ |
| 9093 |
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, |
| 9094 |
spp == 1 ? PHOTOMETRIC_LOGL : PHOTOMETRIC_LOGLUV); |
| 9095 |
} |
| 9096 |
else |
| 9097 |
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, image->photometric); |
| 9098 |
} |
| 9099 |
} |
| 9100 |
|
| 9101 |
if (((input_photometric == PHOTOMETRIC_LOGL) || |
| 9102 |
(input_photometric == PHOTOMETRIC_LOGLUV)) && |
| 9103 |
((compression != COMPRESSION_SGILOG) && |
| 9104 |
(compression != COMPRESSION_SGILOG24))) |
| 9105 |
{ |
| 9106 |
TIFFError("writeCroppedImage", "LogL and LogLuv source data require " |
| 9107 |
"SGI_LOG or SGI_LOG24 compression"); |
| 9108 |
return (-1); |
| 9109 |
} |
| 9110 |
|
| 9111 |
if (fillorder != 0) |
| 9112 |
TIFFSetField(out, TIFFTAG_FILLORDER, fillorder); |
| 9113 |
else |
| 9114 |
CopyTag(TIFFTAG_FILLORDER, 1, TIFF_SHORT); |
| 9115 |
|
| 9116 |
/* The loadimage function reads input orientation and sets |
| 9117 |
* image->orientation. The correct_image_orientation function |
| 9118 |
* applies the required rotation and mirror operations to |
| 9119 |
* present the data in TOPLEFT orientation and updates |
| 9120 |
* image->orientation if any transforms are performed, |
| 9121 |
* as per EXIF standard. |
| 9122 |
*/ |
| 9123 |
TIFFSetField(out, TIFFTAG_ORIENTATION, image->orientation); |
| 9124 |
|
| 9125 |
/* |
| 9126 |
* Choose tiles/strip for the output image according to |
| 9127 |
* the command line arguments (-tiles, -strips) and the |
| 9128 |
* structure of the input image. |
| 9129 |
*/ |
| 9130 |
if (outtiled == -1) |
| 9131 |
outtiled = TIFFIsTiled(in); |
| 9132 |
if (outtiled) |
| 9133 |
{ |
| 9134 |
/* |
| 9135 |
* Setup output file's tile width&height. If either |
| 9136 |
* is not specified, use either the value from the |
| 9137 |
* input image or, if nothing is defined, use the |
| 9138 |
* library default. |
| 9139 |
*/ |
| 9140 |
if (tilewidth == (uint32_t)0) |
| 9141 |
TIFFGetField(in, TIFFTAG_TILEWIDTH, &tilewidth); |
| 9142 |
if (tilelength == (uint32_t)0) |
| 9143 |
TIFFGetField(in, TIFFTAG_TILELENGTH, &tilelength); |
| 9144 |
|
| 9145 |
if (tilewidth == 0 || tilelength == 0) |
| 9146 |
TIFFDefaultTileSize(out, &tilewidth, &tilelength); |
| 9147 |
TIFFSetField(out, TIFFTAG_TILEWIDTH, tilewidth); |
| 9148 |
TIFFSetField(out, TIFFTAG_TILELENGTH, tilelength); |
| 9149 |
} |
| 9150 |
else |
| 9151 |
{ |
| 9152 |
/* |
| 9153 |
* RowsPerStrip is left unspecified: use either the |
| 9154 |
* value from the input image or, if nothing is defined, |
| 9155 |
* use the library default. |
| 9156 |
*/ |
| 9157 |
if (rowsperstrip == (uint32_t)0) |
| 9158 |
{ |
| 9159 |
if (!TIFFGetField(in, TIFFTAG_ROWSPERSTRIP, &rowsperstrip)) |
| 9160 |
rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip); |
| 9161 |
if (compression != COMPRESSION_JPEG) |
| 9162 |
{ |
| 9163 |
if (rowsperstrip > length) |
| 9164 |
rowsperstrip = length; |
| 9165 |
} |
| 9166 |
} |
| 9167 |
else if (rowsperstrip == (uint32_t)-1) |
| 9168 |
rowsperstrip = length; |
| 9169 |
TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip); |
| 9170 |
} |
| 9171 |
|
| 9172 |
TIFFGetFieldDefaulted(in, TIFFTAG_PLANARCONFIG, &input_planar); |
| 9173 |
if (config != (uint16_t)-1) |
| 9174 |
TIFFSetField(out, TIFFTAG_PLANARCONFIG, config); |
| 9175 |
else |
| 9176 |
CopyField(TIFFTAG_PLANARCONFIG, config); |
| 9177 |
if (spp <= 4) |
| 9178 |
CopyTag(TIFFTAG_TRANSFERFUNCTION, 4, TIFF_SHORT); |
| 9179 |
CopyTag(TIFFTAG_COLORMAP, 4, TIFF_SHORT); |
| 9180 |
|
| 9181 |
/* SMinSampleValue & SMaxSampleValue */ |
| 9182 |
switch (compression) |
| 9183 |
{ |
| 9184 |
case COMPRESSION_JPEG: |
| 9185 |
if (((bps % 8) == 0) || ((bps % 12) == 0)) |
| 9186 |
{ |
| 9187 |
TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality); |
| 9188 |
TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB); |
| 9189 |
} |
| 9190 |
else |
| 9191 |
{ |
| 9192 |
TIFFError("writeCroppedImage", |
| 9193 |
"JPEG compression requires 8 or 12 bits per sample"); |
| 9194 |
return (-1); |
| 9195 |
} |
| 9196 |
break; |
| 9197 |
case COMPRESSION_LZW: |
| 9198 |
case COMPRESSION_ADOBE_DEFLATE: |
| 9199 |
case COMPRESSION_DEFLATE: |
| 9200 |
if (predictor != (uint16_t)-1) |
| 9201 |
TIFFSetField(out, TIFFTAG_PREDICTOR, predictor); |
| 9202 |
else |
| 9203 |
CopyField(TIFFTAG_PREDICTOR, predictor); |
| 9204 |
break; |
| 9205 |
case COMPRESSION_CCITTFAX3: |
| 9206 |
case COMPRESSION_CCITTFAX4: |
| 9207 |
if (bps != 1) |
| 9208 |
{ |
| 9209 |
TIFFError("writeCroppedImage", |
| 9210 |
"Group 3/4 compression is not usable with bps > 1"); |
| 9211 |
return (-1); |
| 9212 |
} |
| 9213 |
if (compression == COMPRESSION_CCITTFAX3) |
| 9214 |
{ |
| 9215 |
if (g3opts != (uint32_t)-1) |
| 9216 |
TIFFSetField(out, TIFFTAG_GROUP3OPTIONS, g3opts); |
| 9217 |
else |
| 9218 |
CopyField(TIFFTAG_GROUP3OPTIONS, g3opts); |
| 9219 |
} |
| 9220 |
else |
| 9221 |
{ |
| 9222 |
CopyTag(TIFFTAG_GROUP4OPTIONS, 1, TIFF_LONG); |
| 9223 |
} |
| 9224 |
CopyTag(TIFFTAG_BADFAXLINES, 1, TIFF_LONG); |
| 9225 |
CopyTag(TIFFTAG_CLEANFAXDATA, 1, TIFF_LONG); |
| 9226 |
CopyTag(TIFFTAG_CONSECUTIVEBADFAXLINES, 1, TIFF_LONG); |
| 9227 |
CopyTag(TIFFTAG_FAXRECVPARAMS, 1, TIFF_LONG); |
| 9228 |
CopyTag(TIFFTAG_FAXRECVTIME, 1, TIFF_LONG); |
| 9229 |
CopyTag(TIFFTAG_FAXSUBADDRESS, 1, TIFF_ASCII); |
| 9230 |
break; |
| 9231 |
case COMPRESSION_NONE: |
| 9232 |
break; |
| 9233 |
default: |
| 9234 |
break; |
| 9235 |
} |
| 9236 |
{ |
| 9237 |
uint32_t len32; |
| 9238 |
void **data; |
| 9239 |
if (TIFFGetField(in, TIFFTAG_ICCPROFILE, &len32, &data)) |
| 9240 |
TIFFSetField(out, TIFFTAG_ICCPROFILE, len32, data); |
| 9241 |
} |
| 9242 |
{ |
| 9243 |
uint16_t ninks; |
| 9244 |
const char *inknames; |
| 9245 |
if (TIFFGetField(in, TIFFTAG_NUMBEROFINKS, &ninks)) |
| 9246 |
{ |
| 9247 |
TIFFSetField(out, TIFFTAG_NUMBEROFINKS, ninks); |
| 9248 |
if (TIFFGetField(in, TIFFTAG_INKNAMES, &inknames)) |
| 9249 |
{ |
| 9250 |
int inknameslen = (int)strlen(inknames) + 1; |
| 9251 |
const char *cp = inknames; |
| 9252 |
while (ninks > 1) |
| 9253 |
{ |
| 9254 |
cp = strchr(cp, '\0'); |
| 9255 |
if (cp) |
| 9256 |
{ |
| 9257 |
cp++; |
| 9258 |
inknameslen += ((int)strlen(cp) + 1); |
| 9259 |
} |
| 9260 |
ninks--; |
| 9261 |
} |
| 9262 |
TIFFSetField(out, TIFFTAG_INKNAMES, inknameslen, inknames); |
| 9263 |
} |
| 9264 |
} |
| 9265 |
} |
| 9266 |
{ |
| 9267 |
unsigned short pg0, pg1; |
| 9268 |
if (TIFFGetField(in, TIFFTAG_PAGENUMBER, &pg0, &pg1)) |
| 9269 |
{ |
| 9270 |
TIFFSetField(out, TIFFTAG_PAGENUMBER, pagenum, total_pages); |
| 9271 |
} |
| 9272 |
} |
| 9273 |
|
| 9274 |
for (p = tags; p < &tags[NTAGS]; p++) |
| 9275 |
CopyTag(p->tag, p->count, p->type); |
| 9276 |
|
| 9277 |
/* Compute the tile or strip dimensions and write to disk */ |
| 9278 |
if (outtiled) |
| 9279 |
{ |
| 9280 |
if (config == PLANARCONFIG_CONTIG) |
| 9281 |
{ |
| 9282 |
if (writeBufferToContigTiles(out, crop_buff, length, width, spp, |
| 9283 |
dump)) |
| 9284 |
TIFFError("", |
| 9285 |
"Unable to write contiguous tile data for page %d", |
| 9286 |
pagenum); |
| 9287 |
} |
| 9288 |
else |
| 9289 |
{ |
| 9290 |
if (writeBufferToSeparateTiles(out, crop_buff, length, width, spp, |
| 9291 |
dump)) |
| 9292 |
TIFFError("", "Unable to write separate tile data for page %d", |
| 9293 |
pagenum); |
| 9294 |
} |
| 9295 |
} |
| 9296 |
else |
| 9297 |
{ |
| 9298 |
if (config == PLANARCONFIG_CONTIG) |
| 9299 |
{ |
| 9300 |
if (writeBufferToContigStrips(out, crop_buff, length)) |
| 9301 |
TIFFError("", |
| 9302 |
"Unable to write contiguous strip data for page %d", |
| 9303 |
pagenum); |
| 9304 |
} |
| 9305 |
else |
| 9306 |
{ |
| 9307 |
if (writeBufferToSeparateStrips(out, crop_buff, length, width, spp, |
| 9308 |
dump)) |
| 9309 |
TIFFError("", "Unable to write separate strip data for page %d", |
| 9310 |
pagenum); |
| 9311 |
} |
| 9312 |
} |
| 9313 |
|
| 9314 |
if (!TIFFWriteDirectory(out)) |
| 9315 |
{ |
| 9316 |
TIFFError("", "Failed to write IFD for page number %d", pagenum); |
| 9317 |
return (-1); |
| 9318 |
} |
| 9319 |
|
| 9320 |
return (0); |
| 9321 |
} /* end writeCroppedImage */ |
| 9322 |
|
| 9323 |
static int rotateContigSamples8bits(uint16_t rotation, uint16_t spp, |
| 9324 |
uint16_t bps, uint32_t width, |
| 9325 |
uint32_t length, uint32_t col, uint8_t *src, |
| 9326 |
uint8_t *dst) |
| 9327 |
{ |
| 9328 |
int ready_bits = 0; |
| 9329 |
uint32_t src_byte = 0, src_bit = 0; |
| 9330 |
uint32_t row, rowsize = 0, bit_offset = 0; |
| 9331 |
uint8_t matchbits = 0, maskbits = 0; |
| 9332 |
uint8_t buff1 = 0, buff2 = 0; |
| 9333 |
uint8_t *next; |
| 9334 |
tsample_t sample; |
| 9335 |
|
| 9336 |
if ((src == NULL) || (dst == NULL)) |
| 9337 |
{ |
| 9338 |
TIFFError("rotateContigSamples8bits", |
| 9339 |
"Invalid src or destination buffer"); |
| 9340 |
return (1); |
| 9341 |
} |
| 9342 |
|
| 9343 |
rowsize = ((bps * spp * width) + 7) / 8; |
| 9344 |
ready_bits = 0; |
| 9345 |
maskbits = (uint8_t)-1 >> (8 - bps); |
| 9346 |
buff1 = buff2 = 0; |
| 9347 |
|
| 9348 |
for (row = 0; row < length; row++) |
| 9349 |
{ |
| 9350 |
bit_offset = col * bps * spp; |
| 9351 |
for (sample = 0; sample < spp; sample++) |
| 9352 |
{ |
| 9353 |
if (sample == 0) |
| 9354 |
{ |
| 9355 |
src_byte = bit_offset / 8; |
| 9356 |
src_bit = bit_offset % 8; |
| 9357 |
} |
| 9358 |
else |
| 9359 |
{ |
| 9360 |
src_byte = (bit_offset + (sample * bps)) / 8; |
| 9361 |
src_bit = (bit_offset + (sample * bps)) % 8; |
| 9362 |
} |
| 9363 |
|
| 9364 |
switch (rotation) |
| 9365 |
{ |
| 9366 |
case 90: |
| 9367 |
next = src + src_byte - (row * rowsize); |
| 9368 |
break; |
| 9369 |
case 270: |
| 9370 |
next = src + src_byte + (row * rowsize); |
| 9371 |
break; |
| 9372 |
default: |
| 9373 |
TIFFError("rotateContigSamples8bits", |
| 9374 |
"Invalid rotation %" PRIu16, rotation); |
| 9375 |
return (1); |
| 9376 |
} |
| 9377 |
matchbits = maskbits << (8 - src_bit - bps); |
| 9378 |
buff1 = ((*next) & matchbits) << (src_bit); |
| 9379 |
|
| 9380 |
/* If we have a full buffer's worth, write it out */ |
| 9381 |
if (ready_bits >= 8) |
| 9382 |
{ |
| 9383 |
*dst++ = buff2; |
| 9384 |
buff2 = buff1; |
| 9385 |
ready_bits -= 8; |
| 9386 |
} |
| 9387 |
else |
| 9388 |
{ |
| 9389 |
buff2 = (buff2 | (buff1 >> ready_bits)); |
| 9390 |
} |
| 9391 |
ready_bits += bps; |
| 9392 |
} |
| 9393 |
} |
| 9394 |
|
| 9395 |
if (ready_bits > 0) |
| 9396 |
{ |
| 9397 |
buff1 = (buff2 & ((unsigned int)255 << (8 - ready_bits))); |
| 9398 |
*dst++ = buff1; |
| 9399 |
} |
| 9400 |
|
| 9401 |
return (0); |
| 9402 |
} /* end rotateContigSamples8bits */ |
| 9403 |
|
| 9404 |
static int rotateContigSamples16bits(uint16_t rotation, uint16_t spp, |
| 9405 |
uint16_t bps, uint32_t width, |
| 9406 |
uint32_t length, uint32_t col, |
| 9407 |
uint8_t *src, uint8_t *dst) |
| 9408 |
{ |
| 9409 |
int ready_bits = 0; |
| 9410 |
uint32_t row, rowsize, bit_offset; |
| 9411 |
uint32_t src_byte = 0, src_bit = 0; |
| 9412 |
uint16_t matchbits = 0, maskbits = 0; |
| 9413 |
uint16_t buff1 = 0, buff2 = 0; |
| 9414 |
uint8_t bytebuff = 0; |
| 9415 |
uint8_t *next; |
| 9416 |
tsample_t sample; |
| 9417 |
|
| 9418 |
if ((src == NULL) || (dst == NULL)) |
| 9419 |
{ |
| 9420 |
TIFFError("rotateContigSamples16bits", |
| 9421 |
"Invalid src or destination buffer"); |
| 9422 |
return (1); |
| 9423 |
} |
| 9424 |
|
| 9425 |
rowsize = ((bps * spp * width) + 7) / 8; |
| 9426 |
ready_bits = 0; |
| 9427 |
maskbits = (uint16_t)-1 >> (16 - bps); |
| 9428 |
buff1 = buff2 = 0; |
| 9429 |
for (row = 0; row < length; row++) |
| 9430 |
{ |
| 9431 |
bit_offset = col * bps * spp; |
| 9432 |
for (sample = 0; sample < spp; sample++) |
| 9433 |
{ |
| 9434 |
if (sample == 0) |
| 9435 |
{ |
| 9436 |
src_byte = bit_offset / 8; |
| 9437 |
src_bit = bit_offset % 8; |
| 9438 |
} |
| 9439 |
else |
| 9440 |
{ |
| 9441 |
src_byte = (bit_offset + (sample * bps)) / 8; |
| 9442 |
src_bit = (bit_offset + (sample * bps)) % 8; |
| 9443 |
} |
| 9444 |
|
| 9445 |
switch (rotation) |
| 9446 |
{ |
| 9447 |
case 90: |
| 9448 |
next = src + src_byte - (row * rowsize); |
| 9449 |
break; |
| 9450 |
case 270: |
| 9451 |
next = src + src_byte + (row * rowsize); |
| 9452 |
break; |
| 9453 |
default: |
| 9454 |
TIFFError("rotateContigSamples8bits", |
| 9455 |
"Invalid rotation %" PRIu16, rotation); |
| 9456 |
return (1); |
| 9457 |
} |
| 9458 |
matchbits = maskbits << (16 - src_bit - bps); |
| 9459 |
if (little_endian) |
| 9460 |
buff1 = (next[0] << 8) | next[1]; |
| 9461 |
else |
| 9462 |
buff1 = (next[1] << 8) | next[0]; |
| 9463 |
|
| 9464 |
buff1 = (buff1 & matchbits) << (src_bit); |
| 9465 |
|
| 9466 |
/* If we have a full buffer's worth, write it out */ |
| 9467 |
if (ready_bits >= 8) |
| 9468 |
{ |
| 9469 |
bytebuff = (buff2 >> 8); |
| 9470 |
*dst++ = bytebuff; |
| 9471 |
ready_bits -= 8; |
| 9472 |
/* shift in new bits */ |
| 9473 |
buff2 = ((buff2 << 8) | (buff1 >> ready_bits)); |
| 9474 |
} |
| 9475 |
else |
| 9476 |
{ /* add another bps bits to the buffer */ |
| 9477 |
buff2 = (buff2 | (buff1 >> ready_bits)); |
| 9478 |
} |
| 9479 |
ready_bits += bps; |
| 9480 |
} |
| 9481 |
} |
| 9482 |
|
| 9483 |
if (ready_bits > 0) |
| 9484 |
{ |
| 9485 |
bytebuff = (buff2 >> 8); |
| 9486 |
*dst++ = bytebuff; |
| 9487 |
} |
| 9488 |
|
| 9489 |
return (0); |
| 9490 |
} /* end rotateContigSamples16bits */ |
| 9491 |
|
| 9492 |
static int rotateContigSamples24bits(uint16_t rotation, uint16_t spp, |
| 9493 |
uint16_t bps, uint32_t width, |
| 9494 |
uint32_t length, uint32_t col, |
| 9495 |
uint8_t *src, uint8_t *dst) |
| 9496 |
{ |
| 9497 |
int ready_bits = 0; |
| 9498 |
uint32_t row, rowsize, bit_offset; |
| 9499 |
uint32_t src_byte = 0, src_bit = 0; |
| 9500 |
uint32_t matchbits = 0, maskbits = 0; |
| 9501 |
uint32_t buff1 = 0, buff2 = 0; |
| 9502 |
uint8_t bytebuff1 = 0, bytebuff2 = 0; |
| 9503 |
uint8_t *next; |
| 9504 |
tsample_t sample; |
| 9505 |
|
| 9506 |
if ((src == NULL) || (dst == NULL)) |
| 9507 |
{ |
| 9508 |
TIFFError("rotateContigSamples24bits", |
| 9509 |
"Invalid src or destination buffer"); |
| 9510 |
return (1); |
| 9511 |
} |
| 9512 |
|
| 9513 |
rowsize = ((bps * spp * width) + 7) / 8; |
| 9514 |
ready_bits = 0; |
| 9515 |
maskbits = (uint32_t)-1 >> (32 - bps); |
| 9516 |
buff1 = buff2 = 0; |
| 9517 |
for (row = 0; row < length; row++) |
| 9518 |
{ |
| 9519 |
bit_offset = col * bps * spp; |
| 9520 |
for (sample = 0; sample < spp; sample++) |
| 9521 |
{ |
| 9522 |
if (sample == 0) |
| 9523 |
{ |
| 9524 |
src_byte = bit_offset / 8; |
| 9525 |
src_bit = bit_offset % 8; |
| 9526 |
} |
| 9527 |
else |
| 9528 |
{ |
| 9529 |
src_byte = (bit_offset + (sample * bps)) / 8; |
| 9530 |
src_bit = (bit_offset + (sample * bps)) % 8; |
| 9531 |
} |
| 9532 |
|
| 9533 |
switch (rotation) |
| 9534 |
{ |
| 9535 |
case 90: |
| 9536 |
next = src + src_byte - (row * rowsize); |
| 9537 |
break; |
| 9538 |
case 270: |
| 9539 |
next = src + src_byte + (row * rowsize); |
| 9540 |
break; |
| 9541 |
default: |
| 9542 |
TIFFError("rotateContigSamples8bits", |
| 9543 |
"Invalid rotation %" PRIu16, rotation); |
| 9544 |
return (1); |
| 9545 |
} |
| 9546 |
matchbits = maskbits << (32 - src_bit - bps); |
| 9547 |
if (little_endian) |
| 9548 |
buff1 = (next[0] << 24) | (next[1] << 16) | (next[2] << 8) | |
| 9549 |
next[3]; |
| 9550 |
else |
| 9551 |
buff1 = (next[3] << 24) | (next[2] << 16) | (next[1] << 8) | |
| 9552 |
next[0]; |
| 9553 |
buff1 = (buff1 & matchbits) << (src_bit); |
| 9554 |
|
| 9555 |
/* If we have a full buffer's worth, write it out */ |
| 9556 |
if (ready_bits >= 16) |
| 9557 |
{ |
| 9558 |
bytebuff1 = (buff2 >> 24); |
| 9559 |
*dst++ = bytebuff1; |
| 9560 |
bytebuff2 = (buff2 >> 16); |
| 9561 |
*dst++ = bytebuff2; |
| 9562 |
ready_bits -= 16; |
| 9563 |
|
| 9564 |
/* shift in new bits */ |
| 9565 |
buff2 = ((buff2 << 16) | (buff1 >> ready_bits)); |
| 9566 |
} |
| 9567 |
else |
| 9568 |
{ /* add another bps bits to the buffer */ |
| 9569 |
buff2 = (buff2 | (buff1 >> ready_bits)); |
| 9570 |
} |
| 9571 |
ready_bits += bps; |
| 9572 |
} |
| 9573 |
} |
| 9574 |
|
| 9575 |
/* catch any trailing bits at the end of the line */ |
| 9576 |
while (ready_bits > 0) |
| 9577 |
{ |
| 9578 |
bytebuff1 = (buff2 >> 24); |
| 9579 |
*dst++ = bytebuff1; |
| 9580 |
|
| 9581 |
buff2 = (buff2 << 8); |
| 9582 |
bytebuff2 = bytebuff1; |
| 9583 |
ready_bits -= 8; |
| 9584 |
} |
| 9585 |
|
| 9586 |
return (0); |
| 9587 |
} /* end rotateContigSamples24bits */ |
| 9588 |
|
| 9589 |
static int rotateContigSamples32bits(uint16_t rotation, uint16_t spp, |
| 9590 |
uint16_t bps, uint32_t width, |
| 9591 |
uint32_t length, uint32_t col, |
| 9592 |
uint8_t *src, uint8_t *dst) |
| 9593 |
{ |
| 9594 |
int ready_bits = 0 /*, shift_width = 0 */; |
| 9595 |
/* int bytes_per_sample, bytes_per_pixel; */ |
| 9596 |
uint32_t row, rowsize, bit_offset; |
| 9597 |
uint32_t src_byte, src_bit; |
| 9598 |
uint32_t longbuff1 = 0, longbuff2 = 0; |
| 9599 |
uint64_t maskbits = 0, matchbits = 0; |
| 9600 |
uint64_t buff1 = 0, buff2 = 0, buff3 = 0; |
| 9601 |
uint8_t bytebuff1 = 0, bytebuff2 = 0, bytebuff3 = 0, bytebuff4 = 0; |
| 9602 |
uint8_t *next; |
| 9603 |
tsample_t sample; |
| 9604 |
|
| 9605 |
if ((src == NULL) || (dst == NULL)) |
| 9606 |
{ |
| 9607 |
TIFFError("rotateContigSamples24bits", |
| 9608 |
"Invalid src or destination buffer"); |
| 9609 |
return (1); |
| 9610 |
} |
| 9611 |
|
| 9612 |
/* bytes_per_sample = (bps + 7) / 8; */ |
| 9613 |
/* bytes_per_pixel = ((bps * spp) + 7) / 8; */ |
| 9614 |
/* if (bytes_per_pixel < (bytes_per_sample + 1)) */ |
| 9615 |
/* shift_width = bytes_per_pixel; */ |
| 9616 |
/* else */ |
| 9617 |
/* shift_width = bytes_per_sample + 1; */ |
| 9618 |
|
| 9619 |
rowsize = ((bps * spp * width) + 7) / 8; |
| 9620 |
ready_bits = 0; |
| 9621 |
maskbits = (uint64_t)-1 >> (64 - bps); |
| 9622 |
buff1 = buff2 = 0; |
| 9623 |
for (row = 0; row < length; row++) |
| 9624 |
{ |
| 9625 |
bit_offset = col * bps * spp; |
| 9626 |
for (sample = 0; sample < spp; sample++) |
| 9627 |
{ |
| 9628 |
if (sample == 0) |
| 9629 |
{ |
| 9630 |
src_byte = bit_offset / 8; |
| 9631 |
src_bit = bit_offset % 8; |
| 9632 |
} |
| 9633 |
else |
| 9634 |
{ |
| 9635 |
src_byte = (bit_offset + (sample * bps)) / 8; |
| 9636 |
src_bit = (bit_offset + (sample * bps)) % 8; |
| 9637 |
} |
| 9638 |
|
| 9639 |
switch (rotation) |
| 9640 |
{ |
| 9641 |
case 90: |
| 9642 |
next = src + src_byte - (row * rowsize); |
| 9643 |
break; |
| 9644 |
case 270: |
| 9645 |
next = src + src_byte + (row * rowsize); |
| 9646 |
break; |
| 9647 |
default: |
| 9648 |
TIFFError("rotateContigSamples8bits", |
| 9649 |
"Invalid rotation %" PRIu16, rotation); |
| 9650 |
return (1); |
| 9651 |
} |
| 9652 |
matchbits = maskbits << (64 - src_bit - bps); |
| 9653 |
if (little_endian) |
| 9654 |
{ |
| 9655 |
longbuff1 = (next[0] << 24) | (next[1] << 16) | (next[2] << 8) | |
| 9656 |
next[3]; |
| 9657 |
longbuff2 = longbuff1; |
| 9658 |
} |
| 9659 |
else |
| 9660 |
{ |
| 9661 |
longbuff1 = (next[3] << 24) | (next[2] << 16) | (next[1] << 8) | |
| 9662 |
next[0]; |
| 9663 |
longbuff2 = longbuff1; |
| 9664 |
} |
| 9665 |
|
| 9666 |
buff3 = ((uint64_t)longbuff1 << 32) | longbuff2; |
| 9667 |
buff1 = (buff3 & matchbits) << (src_bit); |
| 9668 |
|
| 9669 |
if (ready_bits < 32) |
| 9670 |
{ /* add another bps bits to the buffer */ |
| 9671 |
buff2 = (buff2 | (buff1 >> ready_bits)); |
| 9672 |
} |
| 9673 |
else /* If we have a full buffer's worth, write it out */ |
| 9674 |
{ |
| 9675 |
bytebuff1 = (uint8_t)(buff2 >> 56); |
| 9676 |
*dst++ = bytebuff1; |
| 9677 |
bytebuff2 = (uint8_t)(buff2 >> 48); |
| 9678 |
*dst++ = bytebuff2; |
| 9679 |
bytebuff3 = (uint8_t)(buff2 >> 40); |
| 9680 |
*dst++ = bytebuff3; |
| 9681 |
bytebuff4 = (uint8_t)(buff2 >> 32); |
| 9682 |
*dst++ = bytebuff4; |
| 9683 |
ready_bits -= 32; |
| 9684 |
|
| 9685 |
/* shift in new bits */ |
| 9686 |
buff2 = ((buff2 << 32) | (buff1 >> ready_bits)); |
| 9687 |
} |
| 9688 |
ready_bits += bps; |
| 9689 |
} |
| 9690 |
} |
| 9691 |
while (ready_bits > 0) |
| 9692 |
{ |
| 9693 |
bytebuff1 = (buff2 >> 56); |
| 9694 |
*dst++ = bytebuff1; |
| 9695 |
buff2 = (buff2 << 8); |
| 9696 |
ready_bits -= 8; |
| 9697 |
} |
| 9698 |
|
| 9699 |
return (0); |
| 9700 |
} /* end rotateContigSamples32bits */ |
| 9701 |
|
| 9702 |
/* Rotate an image by a multiple of 90 degrees clockwise */ |
| 9703 |
static int rotateImage(uint16_t rotation, struct image_data *image, |
| 9704 |
uint32_t *img_width, uint32_t *img_length, |
| 9705 |
unsigned char **ibuff_ptr, size_t *rot_buf_size, |
| 9706 |
int rot_image_params) |
| 9707 |
{ |
| 9708 |
int shift_width; |
| 9709 |
uint32_t bytes_per_pixel, bytes_per_sample; |
| 9710 |
uint32_t row, rowsize, src_offset, dst_offset; |
| 9711 |
uint32_t i, col, width, length; |
| 9712 |
uint32_t colsize, col_offset, pix_offset; |
| 9713 |
tmsize_t buffsize; |
| 9714 |
unsigned char *ibuff; |
| 9715 |
unsigned char *src; |
| 9716 |
unsigned char *dst; |
| 9717 |
uint16_t spp, bps; |
| 9718 |
float res_temp; |
| 9719 |
unsigned char *rbuff = NULL; |
| 9720 |
|
| 9721 |
width = *img_width; |
| 9722 |
length = *img_length; |
| 9723 |
spp = image->spp; |
| 9724 |
bps = image->bps; |
| 9725 |
|
| 9726 |
if ((spp != 0 && bps != 0 && |
| 9727 |
width > (uint32_t)((UINT32_MAX - 7) / spp / bps)) || |
| 9728 |
(spp != 0 && bps != 0 && |
| 9729 |
length > (uint32_t)((UINT32_MAX - 7) / spp / bps))) |
| 9730 |
{ |
| 9731 |
TIFFError("rotateImage", "Integer overflow detected."); |
| 9732 |
return (-1); |
| 9733 |
} |
| 9734 |
rowsize = ((bps * spp * width) + 7) / 8; |
| 9735 |
colsize = ((bps * spp * length) + 7) / 8; |
| 9736 |
if ((colsize * width) > (rowsize * length)) |
| 9737 |
{ |
| 9738 |
if (((tmsize_t)colsize + 1) != 0 && |
| 9739 |
(tmsize_t)width > ((TIFF_TMSIZE_T_MAX - NUM_BUFF_OVERSIZE_BYTES) / |
| 9740 |
((tmsize_t)colsize + 1))) |
| 9741 |
{ |
| 9742 |
TIFFError("rotateImage", |
| 9743 |
"Integer overflow when calculating buffer size."); |
| 9744 |
return (-1); |
| 9745 |
} |
| 9746 |
buffsize = ((tmsize_t)colsize + 1) * width; |
| 9747 |
} |
| 9748 |
else |
| 9749 |
{ |
| 9750 |
if (((tmsize_t)rowsize + 1) != 0 && |
| 9751 |
(tmsize_t)length > ((TIFF_TMSIZE_T_MAX - NUM_BUFF_OVERSIZE_BYTES) / |
| 9752 |
((tmsize_t)rowsize + 1))) |
| 9753 |
{ |
| 9754 |
TIFFError("rotateImage", |
| 9755 |
"Integer overflow when calculating buffer size."); |
| 9756 |
return (-1); |
| 9757 |
} |
| 9758 |
buffsize = ((tmsize_t)rowsize + 1) * length; |
| 9759 |
} |
| 9760 |
|
| 9761 |
bytes_per_sample = (bps + 7) / 8; |
| 9762 |
bytes_per_pixel = ((bps * spp) + 7) / 8; |
| 9763 |
if (bytes_per_pixel < (bytes_per_sample + 1)) |
| 9764 |
shift_width = bytes_per_pixel; |
| 9765 |
else |
| 9766 |
shift_width = bytes_per_sample + 1; |
| 9767 |
|
| 9768 |
switch (rotation) |
| 9769 |
{ |
| 9770 |
case 0: |
| 9771 |
case 360: |
| 9772 |
return (0); |
| 9773 |
case 90: |
| 9774 |
case 180: |
| 9775 |
case 270: |
| 9776 |
break; |
| 9777 |
default: |
| 9778 |
TIFFError("rotateImage", "Invalid rotation angle %" PRIu16, |
| 9779 |
rotation); |
| 9780 |
return (-1); |
| 9781 |
} |
| 9782 |
|
| 9783 |
/* Add 3 padding bytes for extractContigSamplesShifted32bits */ |
| 9784 |
if (!(rbuff = |
| 9785 |
(unsigned char *)limitMalloc(buffsize + NUM_BUFF_OVERSIZE_BYTES))) |
| 9786 |
{ |
| 9787 |
TIFFError("rotateImage", |
| 9788 |
"Unable to allocate rotation buffer of %" TIFF_SSIZE_FORMAT |
| 9789 |
" bytes ", |
| 9790 |
buffsize + NUM_BUFF_OVERSIZE_BYTES); |
| 9791 |
return (-1); |
| 9792 |
} |
| 9793 |
_TIFFmemset(rbuff, '\0', buffsize + NUM_BUFF_OVERSIZE_BYTES); |
| 9794 |
if (rot_buf_size != NULL) |
| 9795 |
*rot_buf_size = buffsize; |
| 9796 |
|
| 9797 |
ibuff = *ibuff_ptr; |
| 9798 |
switch (rotation) |
| 9799 |
{ |
| 9800 |
case 180: |
| 9801 |
if ((bps % 8) == 0) /* byte aligned data */ |
| 9802 |
{ |
| 9803 |
src = ibuff; |
| 9804 |
pix_offset = (spp * bps) / 8; |
| 9805 |
for (row = 0; row < length; row++) |
| 9806 |
{ |
| 9807 |
dst_offset = (length - row - 1) * rowsize; |
| 9808 |
for (col = 0; col < width; col++) |
| 9809 |
{ |
| 9810 |
col_offset = (width - col - 1) * pix_offset; |
| 9811 |
dst = rbuff + dst_offset + col_offset; |
| 9812 |
|
| 9813 |
for (i = 0; i < bytes_per_pixel; i++) |
| 9814 |
*dst++ = *src++; |
| 9815 |
} |
| 9816 |
} |
| 9817 |
} |
| 9818 |
else |
| 9819 |
{ /* non 8 bit per sample data */ |
| 9820 |
for (row = 0; row < length; row++) |
| 9821 |
{ |
| 9822 |
src_offset = row * rowsize; |
| 9823 |
dst_offset = (length - row - 1) * rowsize; |
| 9824 |
src = ibuff + src_offset; |
| 9825 |
dst = rbuff + dst_offset; |
| 9826 |
switch (shift_width) |
| 9827 |
{ |
| 9828 |
case 1: |
| 9829 |
if (bps == 1) |
| 9830 |
{ |
| 9831 |
if (reverseSamples8bits(spp, bps, width, src, |
| 9832 |
dst)) |
| 9833 |
{ |
| 9834 |
_TIFFfree(rbuff); |
| 9835 |
return (-1); |
| 9836 |
} |
| 9837 |
break; |
| 9838 |
} |
| 9839 |
if (reverseSamples16bits(spp, bps, width, src, dst)) |
| 9840 |
{ |
| 9841 |
_TIFFfree(rbuff); |
| 9842 |
return (-1); |
| 9843 |
} |
| 9844 |
break; |
| 9845 |
case 2: |
| 9846 |
if (reverseSamples24bits(spp, bps, width, src, dst)) |
| 9847 |
{ |
| 9848 |
_TIFFfree(rbuff); |
| 9849 |
return (-1); |
| 9850 |
} |
| 9851 |
break; |
| 9852 |
case 3: |
| 9853 |
case 4: |
| 9854 |
case 5: |
| 9855 |
if (reverseSamples32bits(spp, bps, width, src, dst)) |
| 9856 |
{ |
| 9857 |
_TIFFfree(rbuff); |
| 9858 |
return (-1); |
| 9859 |
} |
| 9860 |
break; |
| 9861 |
default: |
| 9862 |
TIFFError("rotateImage", |
| 9863 |
"Unsupported bit depth %" PRIu16, bps); |
| 9864 |
_TIFFfree(rbuff); |
| 9865 |
return (-1); |
| 9866 |
} |
| 9867 |
} |
| 9868 |
} |
| 9869 |
_TIFFfree(ibuff); |
| 9870 |
*(ibuff_ptr) = rbuff; |
| 9871 |
break; |
| 9872 |
|
| 9873 |
case 90: |
| 9874 |
if ((bps % 8) == 0) /* byte aligned data */ |
| 9875 |
{ |
| 9876 |
for (col = 0; col < width; col++) |
| 9877 |
{ |
| 9878 |
src_offset = |
| 9879 |
((length - 1) * rowsize) + (col * bytes_per_pixel); |
| 9880 |
dst_offset = col * colsize; |
| 9881 |
src = ibuff + src_offset; |
| 9882 |
dst = rbuff + dst_offset; |
| 9883 |
for (row = length; row > 0; row--) |
| 9884 |
{ |
| 9885 |
for (i = 0; i < bytes_per_pixel; i++) |
| 9886 |
*dst++ = *(src + i); |
| 9887 |
src -= rowsize; |
| 9888 |
} |
| 9889 |
} |
| 9890 |
} |
| 9891 |
else |
| 9892 |
{ /* non 8 bit per sample data */ |
| 9893 |
for (col = 0; col < width; col++) |
| 9894 |
{ |
| 9895 |
src_offset = (length - 1) * rowsize; |
| 9896 |
dst_offset = col * colsize; |
| 9897 |
src = ibuff + src_offset; |
| 9898 |
dst = rbuff + dst_offset; |
| 9899 |
switch (shift_width) |
| 9900 |
{ |
| 9901 |
case 1: |
| 9902 |
if (bps == 1) |
| 9903 |
{ |
| 9904 |
if (rotateContigSamples8bits(rotation, spp, bps, |
| 9905 |
width, length, col, |
| 9906 |
src, dst)) |
| 9907 |
{ |
| 9908 |
_TIFFfree(rbuff); |
| 9909 |
return (-1); |
| 9910 |
} |
| 9911 |
break; |
| 9912 |
} |
| 9913 |
if (rotateContigSamples16bits(rotation, spp, bps, |
| 9914 |
width, length, col, |
| 9915 |
src, dst)) |
| 9916 |
{ |
| 9917 |
_TIFFfree(rbuff); |
| 9918 |
return (-1); |
| 9919 |
} |
| 9920 |
break; |
| 9921 |
case 2: |
| 9922 |
if (rotateContigSamples24bits(rotation, spp, bps, |
| 9923 |
width, length, col, |
| 9924 |
src, dst)) |
| 9925 |
{ |
| 9926 |
_TIFFfree(rbuff); |
| 9927 |
return (-1); |
| 9928 |
} |
| 9929 |
break; |
| 9930 |
case 3: |
| 9931 |
case 4: |
| 9932 |
case 5: |
| 9933 |
if (rotateContigSamples32bits(rotation, spp, bps, |
| 9934 |
width, length, col, |
| 9935 |
src, dst)) |
| 9936 |
{ |
| 9937 |
_TIFFfree(rbuff); |
| 9938 |
return (-1); |
| 9939 |
} |
| 9940 |
break; |
| 9941 |
default: |
| 9942 |
TIFFError("rotateImage", |
| 9943 |
"Unsupported bit depth %" PRIu16, bps); |
| 9944 |
_TIFFfree(rbuff); |
| 9945 |
return (-1); |
| 9946 |
} |
| 9947 |
} |
| 9948 |
} |
| 9949 |
_TIFFfree(ibuff); |
| 9950 |
*(ibuff_ptr) = rbuff; |
| 9951 |
|
| 9952 |
*img_width = length; |
| 9953 |
*img_length = width; |
| 9954 |
/* Only toggle image parameters if whole input image is rotated. */ |
| 9955 |
if (rot_image_params) |
| 9956 |
{ |
| 9957 |
image->width = length; |
| 9958 |
image->length = width; |
| 9959 |
res_temp = image->xres; |
| 9960 |
image->xres = image->yres; |
| 9961 |
image->yres = res_temp; |
| 9962 |
} |
| 9963 |
break; |
| 9964 |
|
| 9965 |
case 270: |
| 9966 |
if ((bps % 8) == 0) /* byte aligned data */ |
| 9967 |
{ |
| 9968 |
for (col = 0; col < width; col++) |
| 9969 |
{ |
| 9970 |
src_offset = col * bytes_per_pixel; |
| 9971 |
dst_offset = (width - col - 1) * colsize; |
| 9972 |
src = ibuff + src_offset; |
| 9973 |
dst = rbuff + dst_offset; |
| 9974 |
for (row = length; row > 0; row--) |
| 9975 |
{ |
| 9976 |
for (i = 0; i < bytes_per_pixel; i++) |
| 9977 |
*dst++ = *(src + i); |
| 9978 |
src += rowsize; |
| 9979 |
} |
| 9980 |
} |
| 9981 |
} |
| 9982 |
else |
| 9983 |
{ /* non 8 bit per sample data */ |
| 9984 |
for (col = 0; col < width; col++) |
| 9985 |
{ |
| 9986 |
src_offset = 0; |
| 9987 |
dst_offset = (width - col - 1) * colsize; |
| 9988 |
src = ibuff + src_offset; |
| 9989 |
dst = rbuff + dst_offset; |
| 9990 |
switch (shift_width) |
| 9991 |
{ |
| 9992 |
case 1: |
| 9993 |
if (bps == 1) |
| 9994 |
{ |
| 9995 |
if (rotateContigSamples8bits(rotation, spp, bps, |
| 9996 |
width, length, col, |
| 9997 |
src, dst)) |
| 9998 |
{ |
| 9999 |
_TIFFfree(rbuff); |
| 10000 |
return (-1); |
| 10001 |
} |
| 10002 |
break; |
| 10003 |
} |
| 10004 |
if (rotateContigSamples16bits(rotation, spp, bps, |
| 10005 |
width, length, col, |
| 10006 |
src, dst)) |
| 10007 |
{ |
| 10008 |
_TIFFfree(rbuff); |
| 10009 |
return (-1); |
| 10010 |
} |
| 10011 |
break; |
| 10012 |
case 2: |
| 10013 |
if (rotateContigSamples24bits(rotation, spp, bps, |
| 10014 |
width, length, col, |
| 10015 |
src, dst)) |
| 10016 |
{ |
| 10017 |
_TIFFfree(rbuff); |
| 10018 |
return (-1); |
| 10019 |
} |
| 10020 |
break; |
| 10021 |
case 3: |
| 10022 |
case 4: |
| 10023 |
case 5: |
| 10024 |
if (rotateContigSamples32bits(rotation, spp, bps, |
| 10025 |
width, length, col, |
| 10026 |
src, dst)) |
| 10027 |
{ |
| 10028 |
_TIFFfree(rbuff); |
| 10029 |
return (-1); |
| 10030 |
} |
| 10031 |
break; |
| 10032 |
default: |
| 10033 |
TIFFError("rotateImage", |
| 10034 |
"Unsupported bit depth %" PRIu16, bps); |
| 10035 |
_TIFFfree(rbuff); |
| 10036 |
return (-1); |
| 10037 |
} |
| 10038 |
} |
| 10039 |
} |
| 10040 |
_TIFFfree(ibuff); |
| 10041 |
*(ibuff_ptr) = rbuff; |
| 10042 |
|
| 10043 |
*img_width = length; |
| 10044 |
*img_length = width; |
| 10045 |
/* Only toggle image parameters if whole input image is rotated. */ |
| 10046 |
if (rot_image_params) |
| 10047 |
{ |
| 10048 |
image->width = length; |
| 10049 |
image->length = width; |
| 10050 |
res_temp = image->xres; |
| 10051 |
image->xres = image->yres; |
| 10052 |
image->yres = res_temp; |
| 10053 |
} |
| 10054 |
break; |
| 10055 |
default: |
| 10056 |
break; |
| 10057 |
} |
| 10058 |
|
| 10059 |
return (0); |
| 10060 |
} /* end rotateImage */ |
| 10061 |
|
| 10062 |
static int reverseSamples8bits(uint16_t spp, uint16_t bps, uint32_t width, |
| 10063 |
uint8_t *ibuff, uint8_t *obuff) |
| 10064 |
{ |
| 10065 |
int ready_bits = 0; |
| 10066 |
uint32_t col; |
| 10067 |
uint32_t src_byte, src_bit; |
| 10068 |
uint32_t bit_offset = 0; |
| 10069 |
uint8_t match_bits = 0, mask_bits = 0; |
| 10070 |
uint8_t buff1 = 0, buff2 = 0; |
| 10071 |
unsigned char *src; |
| 10072 |
unsigned char *dst; |
| 10073 |
tsample_t sample; |
| 10074 |
|
| 10075 |
if ((ibuff == NULL) || (obuff == NULL)) |
| 10076 |
{ |
| 10077 |
TIFFError("reverseSamples8bits", "Invalid image or work buffer"); |
| 10078 |
return (1); |
| 10079 |
} |
| 10080 |
|
| 10081 |
ready_bits = 0; |
| 10082 |
mask_bits = (uint8_t)-1 >> (8 - bps); |
| 10083 |
dst = obuff; |
| 10084 |
for (col = width; col > 0; col--) |
| 10085 |
{ |
| 10086 |
/* Compute src byte(s) and bits within byte(s) */ |
| 10087 |
bit_offset = (col - 1) * bps * spp; |
| 10088 |
for (sample = 0; sample < spp; sample++) |
| 10089 |
{ |
| 10090 |
if (sample == 0) |
| 10091 |
{ |
| 10092 |
src_byte = bit_offset / 8; |
| 10093 |
src_bit = bit_offset % 8; |
| 10094 |
} |
| 10095 |
else |
| 10096 |
{ |
| 10097 |
src_byte = (bit_offset + (sample * bps)) / 8; |
| 10098 |
src_bit = (bit_offset + (sample * bps)) % 8; |
| 10099 |
} |
| 10100 |
|
| 10101 |
src = ibuff + src_byte; |
| 10102 |
match_bits = mask_bits << (8 - src_bit - bps); |
| 10103 |
buff1 = ((*src) & match_bits) << (src_bit); |
| 10104 |
|
| 10105 |
if (ready_bits < 8) |
| 10106 |
buff2 = (buff2 | (buff1 >> ready_bits)); |
| 10107 |
else /* If we have a full buffer's worth, write it out */ |
| 10108 |
{ |
| 10109 |
*dst++ = buff2; |
| 10110 |
buff2 = buff1; |
| 10111 |
ready_bits -= 8; |
| 10112 |
} |
| 10113 |
ready_bits += bps; |
| 10114 |
} |
| 10115 |
} |
| 10116 |
if (ready_bits > 0) |
| 10117 |
{ |
| 10118 |
buff1 = (buff2 & ((unsigned int)255 << (8 - ready_bits))); |
| 10119 |
*dst++ = buff1; |
| 10120 |
} |
| 10121 |
|
| 10122 |
return (0); |
| 10123 |
} /* end reverseSamples8bits */ |
| 10124 |
|
| 10125 |
static int reverseSamples16bits(uint16_t spp, uint16_t bps, uint32_t width, |
| 10126 |
uint8_t *ibuff, uint8_t *obuff) |
| 10127 |
{ |
| 10128 |
int ready_bits = 0; |
| 10129 |
uint32_t col; |
| 10130 |
uint32_t src_byte = 0, high_bit = 0; |
| 10131 |
uint32_t bit_offset = 0; |
| 10132 |
uint16_t match_bits = 0, mask_bits = 0; |
| 10133 |
uint16_t buff1 = 0, buff2 = 0; |
| 10134 |
uint8_t bytebuff = 0; |
| 10135 |
unsigned char *src; |
| 10136 |
unsigned char *dst; |
| 10137 |
tsample_t sample; |
| 10138 |
|
| 10139 |
if ((ibuff == NULL) || (obuff == NULL)) |
| 10140 |
{ |
| 10141 |
TIFFError("reverseSample16bits", "Invalid image or work buffer"); |
| 10142 |
return (1); |
| 10143 |
} |
| 10144 |
|
| 10145 |
ready_bits = 0; |
| 10146 |
mask_bits = (uint16_t)-1 >> (16 - bps); |
| 10147 |
dst = obuff; |
| 10148 |
for (col = width; col > 0; col--) |
| 10149 |
{ |
| 10150 |
/* Compute src byte(s) and bits within byte(s) */ |
| 10151 |
bit_offset = (col - 1) * bps * spp; |
| 10152 |
for (sample = 0; sample < spp; sample++) |
| 10153 |
{ |
| 10154 |
if (sample == 0) |
| 10155 |
{ |
| 10156 |
src_byte = bit_offset / 8; |
| 10157 |
high_bit = bit_offset % 8; |
| 10158 |
} |
| 10159 |
else |
| 10160 |
{ |
| 10161 |
src_byte = (bit_offset + (sample * bps)) / 8; |
| 10162 |
high_bit = (bit_offset + (sample * bps)) % 8; |
| 10163 |
} |
| 10164 |
|
| 10165 |
src = ibuff + src_byte; |
| 10166 |
match_bits = mask_bits << (16 - high_bit - bps); |
| 10167 |
if (little_endian) |
| 10168 |
buff1 = (src[0] << 8) | src[1]; |
| 10169 |
else |
| 10170 |
buff1 = (src[1] << 8) | src[0]; |
| 10171 |
buff1 = (buff1 & match_bits) << (high_bit); |
| 10172 |
|
| 10173 |
if (ready_bits < 8) |
| 10174 |
{ /* add another bps bits to the buffer */ |
| 10175 |
buff2 = (buff2 | (buff1 >> ready_bits)); |
| 10176 |
} |
| 10177 |
else /* If we have a full buffer's worth, write it out */ |
| 10178 |
{ |
| 10179 |
bytebuff = (buff2 >> 8); |
| 10180 |
*dst++ = bytebuff; |
| 10181 |
ready_bits -= 8; |
| 10182 |
/* shift in new bits */ |
| 10183 |
buff2 = ((buff2 << 8) | (buff1 >> ready_bits)); |
| 10184 |
} |
| 10185 |
ready_bits += bps; |
| 10186 |
} |
| 10187 |
} |
| 10188 |
|
| 10189 |
if (ready_bits > 0) |
| 10190 |
{ |
| 10191 |
bytebuff = (buff2 >> 8); |
| 10192 |
*dst++ = bytebuff; |
| 10193 |
} |
| 10194 |
|
| 10195 |
return (0); |
| 10196 |
} /* end reverseSamples16bits */ |
| 10197 |
|
| 10198 |
static int reverseSamples24bits(uint16_t spp, uint16_t bps, uint32_t width, |
| 10199 |
uint8_t *ibuff, uint8_t *obuff) |
| 10200 |
{ |
| 10201 |
int ready_bits = 0; |
| 10202 |
uint32_t col; |
| 10203 |
uint32_t src_byte = 0, high_bit = 0; |
| 10204 |
uint32_t bit_offset = 0; |
| 10205 |
uint32_t match_bits = 0, mask_bits = 0; |
| 10206 |
uint32_t buff1 = 0, buff2 = 0; |
| 10207 |
uint8_t bytebuff1 = 0, bytebuff2 = 0; |
| 10208 |
unsigned char *src; |
| 10209 |
unsigned char *dst; |
| 10210 |
tsample_t sample; |
| 10211 |
|
| 10212 |
if ((ibuff == NULL) || (obuff == NULL)) |
| 10213 |
{ |
| 10214 |
TIFFError("reverseSamples24bits", "Invalid image or work buffer"); |
| 10215 |
return (1); |
| 10216 |
} |
| 10217 |
|
| 10218 |
ready_bits = 0; |
| 10219 |
mask_bits = (uint32_t)-1 >> (32 - bps); |
| 10220 |
dst = obuff; |
| 10221 |
for (col = width; col > 0; col--) |
| 10222 |
{ |
| 10223 |
/* Compute src byte(s) and bits within byte(s) */ |
| 10224 |
bit_offset = (col - 1) * bps * spp; |
| 10225 |
for (sample = 0; sample < spp; sample++) |
| 10226 |
{ |
| 10227 |
if (sample == 0) |
| 10228 |
{ |
| 10229 |
src_byte = bit_offset / 8; |
| 10230 |
high_bit = bit_offset % 8; |
| 10231 |
} |
| 10232 |
else |
| 10233 |
{ |
| 10234 |
src_byte = (bit_offset + (sample * bps)) / 8; |
| 10235 |
high_bit = (bit_offset + (sample * bps)) % 8; |
| 10236 |
} |
| 10237 |
|
| 10238 |
src = ibuff + src_byte; |
| 10239 |
match_bits = mask_bits << (32 - high_bit - bps); |
| 10240 |
if (little_endian) |
| 10241 |
buff1 = |
| 10242 |
(src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3]; |
| 10243 |
else |
| 10244 |
buff1 = |
| 10245 |
(src[3] << 24) | (src[2] << 16) | (src[1] << 8) | src[0]; |
| 10246 |
buff1 = (buff1 & match_bits) << (high_bit); |
| 10247 |
|
| 10248 |
if (ready_bits < 16) |
| 10249 |
{ /* add another bps bits to the buffer */ |
| 10250 |
buff2 = (buff2 | (buff1 >> ready_bits)); |
| 10251 |
} |
| 10252 |
else /* If we have a full buffer's worth, write it out */ |
| 10253 |
{ |
| 10254 |
bytebuff1 = (buff2 >> 24); |
| 10255 |
*dst++ = bytebuff1; |
| 10256 |
bytebuff2 = (buff2 >> 16); |
| 10257 |
*dst++ = bytebuff2; |
| 10258 |
ready_bits -= 16; |
| 10259 |
|
| 10260 |
/* shift in new bits */ |
| 10261 |
buff2 = ((buff2 << 16) | (buff1 >> ready_bits)); |
| 10262 |
} |
| 10263 |
ready_bits += bps; |
| 10264 |
} |
| 10265 |
} |
| 10266 |
|
| 10267 |
/* catch any trailing bits at the end of the line */ |
| 10268 |
while (ready_bits > 0) |
| 10269 |
{ |
| 10270 |
bytebuff1 = (buff2 >> 24); |
| 10271 |
*dst++ = bytebuff1; |
| 10272 |
|
| 10273 |
buff2 = (buff2 << 8); |
| 10274 |
bytebuff2 = bytebuff1; |
| 10275 |
ready_bits -= 8; |
| 10276 |
} |
| 10277 |
|
| 10278 |
return (0); |
| 10279 |
} /* end reverseSamples24bits */ |
| 10280 |
|
| 10281 |
static int reverseSamples32bits(uint16_t spp, uint16_t bps, uint32_t width, |
| 10282 |
uint8_t *ibuff, uint8_t *obuff) |
| 10283 |
{ |
| 10284 |
int ready_bits = 0 /*, shift_width = 0 */; |
| 10285 |
/* int bytes_per_sample, bytes_per_pixel; */ |
| 10286 |
uint32_t bit_offset; |
| 10287 |
uint32_t src_byte = 0, high_bit = 0; |
| 10288 |
uint32_t col; |
| 10289 |
uint32_t longbuff1 = 0, longbuff2 = 0; |
| 10290 |
uint64_t mask_bits = 0, match_bits = 0; |
| 10291 |
uint64_t buff1 = 0, buff2 = 0, buff3 = 0; |
| 10292 |
uint8_t bytebuff1 = 0, bytebuff2 = 0, bytebuff3 = 0, bytebuff4 = 0; |
| 10293 |
unsigned char *src; |
| 10294 |
unsigned char *dst; |
| 10295 |
tsample_t sample; |
| 10296 |
|
| 10297 |
if ((ibuff == NULL) || (obuff == NULL)) |
| 10298 |
{ |
| 10299 |
TIFFError("reverseSamples32bits", "Invalid image or work buffer"); |
| 10300 |
return (1); |
| 10301 |
} |
| 10302 |
|
| 10303 |
ready_bits = 0; |
| 10304 |
mask_bits = (uint64_t)-1 >> (64 - bps); |
| 10305 |
dst = obuff; |
| 10306 |
|
| 10307 |
/* bytes_per_sample = (bps + 7) / 8; */ |
| 10308 |
/* bytes_per_pixel = ((bps * spp) + 7) / 8; */ |
| 10309 |
/* if (bytes_per_pixel < (bytes_per_sample + 1)) */ |
| 10310 |
/* shift_width = bytes_per_pixel; */ |
| 10311 |
/* else */ |
| 10312 |
/* shift_width = bytes_per_sample + 1; */ |
| 10313 |
|
| 10314 |
for (col = width; col > 0; col--) |
| 10315 |
{ |
| 10316 |
/* Compute src byte(s) and bits within byte(s) */ |
| 10317 |
bit_offset = (col - 1) * bps * spp; |
| 10318 |
for (sample = 0; sample < spp; sample++) |
| 10319 |
{ |
| 10320 |
if (sample == 0) |
| 10321 |
{ |
| 10322 |
src_byte = bit_offset / 8; |
| 10323 |
high_bit = bit_offset % 8; |
| 10324 |
} |
| 10325 |
else |
| 10326 |
{ |
| 10327 |
src_byte = (bit_offset + (sample * bps)) / 8; |
| 10328 |
high_bit = (bit_offset + (sample * bps)) % 8; |
| 10329 |
} |
| 10330 |
|
| 10331 |
src = ibuff + src_byte; |
| 10332 |
match_bits = mask_bits << (64 - high_bit - bps); |
| 10333 |
if (little_endian) |
| 10334 |
{ |
| 10335 |
longbuff1 = |
| 10336 |
(src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3]; |
| 10337 |
longbuff2 = longbuff1; |
| 10338 |
} |
| 10339 |
else |
| 10340 |
{ |
| 10341 |
longbuff1 = |
| 10342 |
(src[3] << 24) | (src[2] << 16) | (src[1] << 8) | src[0]; |
| 10343 |
longbuff2 = longbuff1; |
| 10344 |
} |
| 10345 |
buff3 = ((uint64_t)longbuff1 << 32) | longbuff2; |
| 10346 |
buff1 = (buff3 & match_bits) << (high_bit); |
| 10347 |
|
| 10348 |
if (ready_bits < 32) |
| 10349 |
{ /* add another bps bits to the buffer */ |
| 10350 |
buff2 = (buff2 | (buff1 >> ready_bits)); |
| 10351 |
} |
| 10352 |
else /* If we have a full buffer's worth, write it out */ |
| 10353 |
{ |
| 10354 |
bytebuff1 = (uint8_t)(buff2 >> 56); |
| 10355 |
*dst++ = bytebuff1; |
| 10356 |
bytebuff2 = (uint8_t)(buff2 >> 48); |
| 10357 |
*dst++ = bytebuff2; |
| 10358 |
bytebuff3 = (uint8_t)(buff2 >> 40); |
| 10359 |
*dst++ = bytebuff3; |
| 10360 |
bytebuff4 = (uint8_t)(buff2 >> 32); |
| 10361 |
*dst++ = bytebuff4; |
| 10362 |
ready_bits -= 32; |
| 10363 |
|
| 10364 |
/* shift in new bits */ |
| 10365 |
buff2 = ((buff2 << 32) | (buff1 >> ready_bits)); |
| 10366 |
} |
| 10367 |
ready_bits += bps; |
| 10368 |
} |
| 10369 |
} |
| 10370 |
while (ready_bits > 0) |
| 10371 |
{ |
| 10372 |
bytebuff1 = (buff2 >> 56); |
| 10373 |
*dst++ = bytebuff1; |
| 10374 |
buff2 = (buff2 << 8); |
| 10375 |
ready_bits -= 8; |
| 10376 |
} |
| 10377 |
|
| 10378 |
return (0); |
| 10379 |
} /* end reverseSamples32bits */ |
| 10380 |
|
| 10381 |
static int reverseSamplesBytes(uint16_t spp, uint16_t bps, uint32_t width, |
| 10382 |
uint8_t *src, uint8_t *dst) |
| 10383 |
{ |
| 10384 |
int i; |
| 10385 |
uint32_t col, bytes_per_pixel, col_offset; |
| 10386 |
uint8_t bytebuff1; |
| 10387 |
unsigned char swapbuff[32]; |
| 10388 |
|
| 10389 |
if ((src == NULL) || (dst == NULL)) |
| 10390 |
{ |
| 10391 |
TIFFError("reverseSamplesBytes", "Invalid input or output buffer"); |
| 10392 |
return (1); |
| 10393 |
} |
| 10394 |
|
| 10395 |
bytes_per_pixel = ((bps * spp) + 7) / 8; |
| 10396 |
if (bytes_per_pixel > sizeof(swapbuff)) |
| 10397 |
{ |
| 10398 |
TIFFError("reverseSamplesBytes", "bytes_per_pixel too large"); |
| 10399 |
return (1); |
| 10400 |
} |
| 10401 |
switch (bps / 8) |
| 10402 |
{ |
| 10403 |
case 8: /* Use memcpy for multiple bytes per sample data */ |
| 10404 |
case 4: |
| 10405 |
case 3: |
| 10406 |
case 2: |
| 10407 |
for (col = 0; col < (width / 2); col++) |
| 10408 |
{ |
| 10409 |
col_offset = col * bytes_per_pixel; |
| 10410 |
_TIFFmemcpy(swapbuff, src + col_offset, bytes_per_pixel); |
| 10411 |
_TIFFmemcpy(src + col_offset, |
| 10412 |
dst - col_offset - bytes_per_pixel, |
| 10413 |
bytes_per_pixel); |
| 10414 |
_TIFFmemcpy(dst - col_offset - bytes_per_pixel, swapbuff, |
| 10415 |
bytes_per_pixel); |
| 10416 |
} |
| 10417 |
break; |
| 10418 |
case 1: /* Use byte copy only for single byte per sample data */ |
| 10419 |
for (col = 0; col < (width / 2); col++) |
| 10420 |
{ |
| 10421 |
for (i = 0; i < spp; i++) |
| 10422 |
{ |
| 10423 |
bytebuff1 = *src; |
| 10424 |
*src++ = *(dst - spp + i); |
| 10425 |
*(dst - spp + i) = bytebuff1; |
| 10426 |
} |
| 10427 |
dst -= spp; |
| 10428 |
} |
| 10429 |
break; |
| 10430 |
default: |
| 10431 |
TIFFError("reverseSamplesBytes", "Unsupported bit depth %" PRIu16, |
| 10432 |
bps); |
| 10433 |
return (1); |
| 10434 |
} |
| 10435 |
return (0); |
| 10436 |
} /* end reverseSamplesBytes */ |
| 10437 |
|
| 10438 |
/* Mirror an image horizontally or vertically */ |
| 10439 |
static int mirrorImage(uint16_t spp, uint16_t bps, uint16_t mirror, |
| 10440 |
uint32_t width, uint32_t length, unsigned char *ibuff) |
| 10441 |
{ |
| 10442 |
int shift_width; |
| 10443 |
uint32_t bytes_per_pixel, bytes_per_sample; |
| 10444 |
uint32_t row, rowsize, row_offset; |
| 10445 |
unsigned char *line_buff = NULL; |
| 10446 |
unsigned char *src; |
| 10447 |
unsigned char *dst; |
| 10448 |
|
| 10449 |
src = ibuff; |
| 10450 |
rowsize = ((width * bps * spp) + 7) / 8; |
| 10451 |
switch (mirror) |
| 10452 |
{ |
| 10453 |
case MIRROR_BOTH: |
| 10454 |
case MIRROR_VERT: |
| 10455 |
line_buff = |
| 10456 |
(unsigned char *)limitMalloc(rowsize + NUM_BUFF_OVERSIZE_BYTES); |
| 10457 |
if (line_buff == NULL) |
| 10458 |
{ |
| 10459 |
TIFFError("mirrorImage", |
| 10460 |
"Unable to allocate mirror line buffer of %1u bytes", |
| 10461 |
rowsize + NUM_BUFF_OVERSIZE_BYTES); |
| 10462 |
return (-1); |
| 10463 |
} |
| 10464 |
_TIFFmemset(line_buff, '\0', rowsize + NUM_BUFF_OVERSIZE_BYTES); |
| 10465 |
|
| 10466 |
dst = ibuff + (rowsize * (length - 1)); |
| 10467 |
for (row = 0; row < length / 2; row++) |
| 10468 |
{ |
| 10469 |
_TIFFmemcpy(line_buff, src, rowsize); |
| 10470 |
_TIFFmemcpy(src, dst, rowsize); |
| 10471 |
_TIFFmemcpy(dst, line_buff, rowsize); |
| 10472 |
src += (rowsize); |
| 10473 |
dst -= (rowsize); |
| 10474 |
} |
| 10475 |
if (line_buff) |
| 10476 |
_TIFFfree(line_buff); |
| 10477 |
if (mirror == MIRROR_VERT) |
| 10478 |
break; |
| 10479 |
/* Fall through */ |
| 10480 |
case MIRROR_HORIZ: |
| 10481 |
if ((bps % 8) == 0) /* byte aligned data */ |
| 10482 |
{ |
| 10483 |
for (row = 0; row < length; row++) |
| 10484 |
{ |
| 10485 |
row_offset = row * rowsize; |
| 10486 |
src = ibuff + row_offset; |
| 10487 |
dst = ibuff + row_offset + rowsize; |
| 10488 |
if (reverseSamplesBytes(spp, bps, width, src, dst)) |
| 10489 |
{ |
| 10490 |
return (-1); |
| 10491 |
} |
| 10492 |
} |
| 10493 |
} |
| 10494 |
else |
| 10495 |
{ /* non 8 bit per sample data */ |
| 10496 |
if (!(line_buff = (unsigned char *)limitMalloc( |
| 10497 |
rowsize + NUM_BUFF_OVERSIZE_BYTES))) |
| 10498 |
{ |
| 10499 |
TIFFError("mirrorImage", |
| 10500 |
"Unable to allocate mirror line buffer"); |
| 10501 |
return (-1); |
| 10502 |
} |
| 10503 |
_TIFFmemset(line_buff, '\0', rowsize + NUM_BUFF_OVERSIZE_BYTES); |
| 10504 |
bytes_per_sample = (bps + 7) / 8; |
| 10505 |
bytes_per_pixel = ((bps * spp) + 7) / 8; |
| 10506 |
if (bytes_per_pixel < (bytes_per_sample + 1)) |
| 10507 |
shift_width = bytes_per_pixel; |
| 10508 |
else |
| 10509 |
shift_width = bytes_per_sample + 1; |
| 10510 |
|
| 10511 |
for (row = 0; row < length; row++) |
| 10512 |
{ |
| 10513 |
row_offset = row * rowsize; |
| 10514 |
src = ibuff + row_offset; |
| 10515 |
_TIFFmemset(line_buff, '\0', |
| 10516 |
rowsize + NUM_BUFF_OVERSIZE_BYTES); |
| 10517 |
switch (shift_width) |
| 10518 |
{ |
| 10519 |
case 1: |
| 10520 |
if (reverseSamples16bits(spp, bps, width, src, |
| 10521 |
line_buff)) |
| 10522 |
{ |
| 10523 |
_TIFFfree(line_buff); |
| 10524 |
return (-1); |
| 10525 |
} |
| 10526 |
_TIFFmemcpy(src, line_buff, rowsize); |
| 10527 |
break; |
| 10528 |
case 2: |
| 10529 |
if (reverseSamples24bits(spp, bps, width, src, |
| 10530 |
line_buff)) |
| 10531 |
{ |
| 10532 |
_TIFFfree(line_buff); |
| 10533 |
return (-1); |
| 10534 |
} |
| 10535 |
_TIFFmemcpy(src, line_buff, rowsize); |
| 10536 |
break; |
| 10537 |
case 3: |
| 10538 |
case 4: |
| 10539 |
case 5: |
| 10540 |
if (reverseSamples32bits(spp, bps, width, src, |
| 10541 |
line_buff)) |
| 10542 |
{ |
| 10543 |
_TIFFfree(line_buff); |
| 10544 |
return (-1); |
| 10545 |
} |
| 10546 |
_TIFFmemcpy(src, line_buff, rowsize); |
| 10547 |
break; |
| 10548 |
default: |
| 10549 |
TIFFError("mirrorImage", |
| 10550 |
"Unsupported bit depth %" PRIu16, bps); |
| 10551 |
_TIFFfree(line_buff); |
| 10552 |
return (-1); |
| 10553 |
} |
| 10554 |
} |
| 10555 |
if (line_buff) |
| 10556 |
_TIFFfree(line_buff); |
| 10557 |
} |
| 10558 |
break; |
| 10559 |
|
| 10560 |
default: |
| 10561 |
TIFFError("mirrorImage", "Invalid mirror axis %" PRIu16, mirror); |
| 10562 |
return (-1); |
| 10563 |
break; |
| 10564 |
} |
| 10565 |
|
| 10566 |
return (0); |
| 10567 |
} |
| 10568 |
|
| 10569 |
/* Invert the light and dark values for a bilevel or grayscale image */ |
| 10570 |
static int invertImage(uint16_t photometric, uint16_t spp, uint16_t bps, |
| 10571 |
uint32_t width, uint32_t length, |
| 10572 |
unsigned char *work_buff) |
| 10573 |
{ |
| 10574 |
uint32_t row, col; |
| 10575 |
unsigned char *src; |
| 10576 |
uint16_t *src_uint16; |
| 10577 |
uint32_t *src_uint32; |
| 10578 |
|
| 10579 |
if (spp != 1) |
| 10580 |
{ |
| 10581 |
TIFFError( |
| 10582 |
"invertImage", |
| 10583 |
"Image inversion not supported for more than one sample per pixel"); |
| 10584 |
return (-1); |
| 10585 |
} |
| 10586 |
|
| 10587 |
if (photometric != PHOTOMETRIC_MINISWHITE && |
| 10588 |
photometric != PHOTOMETRIC_MINISBLACK) |
| 10589 |
{ |
| 10590 |
TIFFError("invertImage", |
| 10591 |
"Only black and white and grayscale images can be inverted"); |
| 10592 |
return (-1); |
| 10593 |
} |
| 10594 |
|
| 10595 |
src = work_buff; |
| 10596 |
if (src == NULL) |
| 10597 |
{ |
| 10598 |
TIFFError("invertImage", "Invalid crop buffer passed to invertImage"); |
| 10599 |
return (-1); |
| 10600 |
} |
| 10601 |
|
| 10602 |
switch (bps) |
| 10603 |
{ |
| 10604 |
case 32: |
| 10605 |
src_uint32 = (uint32_t *)src; |
| 10606 |
for (row = 0; row < length; row++) |
| 10607 |
for (col = 0; col < width; col++) |
| 10608 |
{ |
| 10609 |
*src_uint32 = ~(*src_uint32); |
| 10610 |
src_uint32++; |
| 10611 |
} |
| 10612 |
break; |
| 10613 |
case 16: |
| 10614 |
src_uint16 = (uint16_t *)src; |
| 10615 |
for (row = 0; row < length; row++) |
| 10616 |
for (col = 0; col < width; col++) |
| 10617 |
{ |
| 10618 |
*src_uint16 = ~(*src_uint16); |
| 10619 |
src_uint16++; |
| 10620 |
} |
| 10621 |
break; |
| 10622 |
case 8: |
| 10623 |
case 4: |
| 10624 |
case 2: |
| 10625 |
case 1: |
| 10626 |
for (row = 0; row < length; row++) |
| 10627 |
for (col = 0; col < width; col += 8 / bps) |
| 10628 |
{ |
| 10629 |
*src = ~(*src); |
| 10630 |
src++; |
| 10631 |
} |
| 10632 |
break; |
| 10633 |
default: |
| 10634 |
TIFFError("invertImage", "Unsupported bit depth %" PRIu16, bps); |
| 10635 |
return (-1); |
| 10636 |
} |
| 10637 |
|
| 10638 |
return (0); |
| 10639 |
} |