w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

tif_predict.c
Go to the documentation of this file.
1 /* $Header: /cvsroot/osrs/libtiff/libtiff/tif_predict.c,v 1.4 2003/07/08 16:40:46 warmerda Exp $ */
2 
3 /*
4  * Copyright (c) 1988-1997 Sam Leffler
5  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and
8  * its documentation for any purpose is hereby granted without fee, provided
9  * that (i) the above copyright notices and this permission notice appear in
10  * all copies of the software and related documentation, and (ii) the names of
11  * Sam Leffler and Silicon Graphics may not be used in any advertising or
12  * publicity relating to the software without the specific, prior written
13  * permission of Sam Leffler and Silicon Graphics.
14  *
15  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18  *
19  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24  * OF THIS SOFTWARE.
25  */
26 
27 /*
28  * TIFF Library.
29  *
30  * Predictor Tag Support (used by multiple codecs).
31  */
32 #include "tiffiop.h"
33 #include "tif_predict.h"
34 
35 #include <assert.h>
36 
37 #define PredictorState(tif) ((TIFFPredictorState*) (tif)->tif_data)
38 
39 static void horAcc8(TIFF*, tidata_t, tsize_t);
40 static void horAcc16(TIFF*, tidata_t, tsize_t);
41 static void swabHorAcc16(TIFF*, tidata_t, tsize_t);
42 static void horDiff8(TIFF*, tidata_t, tsize_t);
43 static void horDiff16(TIFF*, tidata_t, tsize_t);
48 
49 static int
51 {
53  TIFFDirectory* td = &tif->tif_dir;
54 
55  if (sp->predictor == 1) /* no differencing */
56  return (1);
57  if (sp->predictor != 2) {
58  TIFFError(tif->tif_name, "\"Predictor\" value %d not supported",
59  sp->predictor);
60  return (0);
61  }
62  if (td->td_bitspersample != 8 && td->td_bitspersample != 16) {
63  TIFFError(tif->tif_name,
64  "Horizontal differencing \"Predictor\" not supported with %d-bit samples",
65  td->td_bitspersample);
66  return (0);
67  }
68  sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
69  td->td_samplesperpixel : 1);
70  /*
71  * Calculate the scanline/tile-width size in bytes.
72  */
73  if (isTiled(tif))
74  sp->rowsize = TIFFTileRowSize(tif);
75  else
76  sp->rowsize = TIFFScanlineSize(tif);
77  return (1);
78 }
79 
80 static int
82 {
84  TIFFDirectory* td = &tif->tif_dir;
85 
86  if (!(*sp->setupdecode)(tif) || !PredictorSetup(tif))
87  return (0);
88  if (sp->predictor == 2) {
89  switch (td->td_bitspersample) {
90  case 8: sp->pfunc = horAcc8; break;
91  case 16: sp->pfunc = horAcc16; break;
92  }
93  /*
94  * Override default decoding method with
95  * one that does the predictor stuff.
96  */
97  sp->coderow = tif->tif_decoderow;
99  sp->codestrip = tif->tif_decodestrip;
101  sp->codetile = tif->tif_decodetile;
103  /*
104  * If the data is horizontally differenced
105  * 16-bit data that requires byte-swapping,
106  * then it must be byte swapped before the
107  * accumulation step. We do this with a
108  * special-purpose routine and override the
109  * normal post decoding logic that the library
110  * setup when the directory was read.
111  */
112  if (tif->tif_flags&TIFF_SWAB) {
113  if (sp->pfunc == horAcc16) {
114  sp->pfunc = swabHorAcc16;
116  } /* else handle 32-bit case... */
117  }
118  }
119  return (1);
120 }
121 
122 static int
124 {
126  TIFFDirectory* td = &tif->tif_dir;
127 
128  if (!(*sp->setupencode)(tif) || !PredictorSetup(tif))
129  return (0);
130  if (sp->predictor == 2) {
131  switch (td->td_bitspersample) {
132  case 8: sp->pfunc = horDiff8; break;
133  case 16: sp->pfunc = horDiff16; break;
134  }
135  /*
136  * Override default encoding method with
137  * one that does the predictor stuff.
138  */
139  sp->coderow = tif->tif_encoderow;
141  sp->codestrip = tif->tif_encodestrip;
143  sp->codetile = tif->tif_encodetile;
145  }
146  return (1);
147 }
148 
149 #define REPEAT4(n, op) \
150  switch (n) { \
151  default: { int i; for (i = n-4; i > 0; i--) { op; } } \
152  case 4: op; \
153  case 3: op; \
154  case 2: op; \
155  case 1: op; \
156  case 0: ; \
157  }
158 
159 static void
160 horAcc8(TIFF* tif, tidata_t cp0, tsize_t cc)
161 {
163  tsize_t stride = sp->stride;
164 
165  char* cp = (char*) cp0;
166  if (cc > stride) {
167  cc -= stride;
168  /*
169  * Pipeline the most common cases.
170  */
171  if (stride == 3) {
172  u_int cr = cp[0];
173  u_int cg = cp[1];
174  u_int cb = cp[2];
175  do {
176  cc -= 3, cp += 3;
177  cp[0] = (char) (cr += cp[0]);
178  cp[1] = (char) (cg += cp[1]);
179  cp[2] = (char) (cb += cp[2]);
180  } while ((int32) cc > 0);
181  } else if (stride == 4) {
182  u_int cr = cp[0];
183  u_int cg = cp[1];
184  u_int cb = cp[2];
185  u_int ca = cp[3];
186  do {
187  cc -= 4, cp += 4;
188  cp[0] = (char) (cr += cp[0]);
189  cp[1] = (char) (cg += cp[1]);
190  cp[2] = (char) (cb += cp[2]);
191  cp[3] = (char) (ca += cp[3]);
192  } while ((int32) cc > 0);
193  } else {
194  do {
195  REPEAT4(stride, cp[stride] = (char) (cp[stride] + *cp); cp++)
196  cc -= stride;
197  } while ((int32) cc > 0);
198  }
199  }
200 }
201 
202 static void
204 {
206  tsize_t stride = sp->stride;
207  uint16* wp = (uint16*) cp0;
208  tsize_t wc = cc / 2;
209 
210  if (wc > stride) {
211  TIFFSwabArrayOfShort(wp, wc);
212  wc -= stride;
213  do {
214  REPEAT4(stride, wp[stride] += wp[0]; wp++)
215  wc -= stride;
216  } while ((int32) wc > 0);
217  }
218 }
219 
220 static void
222 {
223  tsize_t stride = PredictorState(tif)->stride;
224  uint16* wp = (uint16*) cp0;
225  tsize_t wc = cc / 2;
226 
227  if (wc > stride) {
228  wc -= stride;
229  do {
230  REPEAT4(stride, wp[stride] += wp[0]; wp++)
231  wc -= stride;
232  } while ((int32) wc > 0);
233  }
234 }
235 
236 /*
237  * Decode a scanline and apply the predictor routine.
238  */
239 static int
241 {
243 
244  assert(sp != NULL);
245  assert(sp->coderow != NULL);
246  assert(sp->pfunc != NULL);
247  if ((*sp->coderow)(tif, op0, occ0, s)) {
248  (*sp->pfunc)(tif, op0, occ0);
249  return (1);
250  } else
251  return (0);
252 }
253 
254 /*
255  * Decode a tile/strip and apply the predictor routine.
256  * Note that horizontal differencing must be done on a
257  * row-by-row basis. The width of a "row" has already
258  * been calculated at pre-decode time according to the
259  * strip/tile dimensions.
260  */
261 static int
263 {
265 
266  assert(sp != NULL);
267  assert(sp->codetile != NULL);
268  if ((*sp->codetile)(tif, op0, occ0, s)) {
269  tsize_t rowsize = sp->rowsize;
270  assert(rowsize > 0);
271  assert(sp->pfunc != NULL);
272  while ((long)occ0 > 0) {
273  (*sp->pfunc)(tif, op0, (tsize_t) rowsize);
274  occ0 -= rowsize;
275  op0 += rowsize;
276  }
277  return (1);
278  } else
279  return (0);
280 }
281 
282 static void
284 {
286  tsize_t stride = sp->stride;
287  char* cp = (char*) cp0;
288 
289  if (cc > stride) {
290  cc -= stride;
291  /*
292  * Pipeline the most common cases.
293  */
294  if (stride == 3) {
295  int r1, g1, b1;
296  int r2 = cp[0];
297  int g2 = cp[1];
298  int b2 = cp[2];
299  do {
300  r1 = cp[3]; cp[3] = r1-r2; r2 = r1;
301  g1 = cp[4]; cp[4] = g1-g2; g2 = g1;
302  b1 = cp[5]; cp[5] = b1-b2; b2 = b1;
303  cp += 3;
304  } while ((int32)(cc -= 3) > 0);
305  } else if (stride == 4) {
306  int r1, g1, b1, a1;
307  int r2 = cp[0];
308  int g2 = cp[1];
309  int b2 = cp[2];
310  int a2 = cp[3];
311  do {
312  r1 = cp[4]; cp[4] = r1-r2; r2 = r1;
313  g1 = cp[5]; cp[5] = g1-g2; g2 = g1;
314  b1 = cp[6]; cp[6] = b1-b2; b2 = b1;
315  a1 = cp[7]; cp[7] = a1-a2; a2 = a1;
316  cp += 4;
317  } while ((int32)(cc -= 4) > 0);
318  } else {
319  cp += cc - 1;
320  do {
321  REPEAT4(stride, cp[stride] -= cp[0]; cp--)
322  } while ((int32)(cc -= stride) > 0);
323  }
324  }
325 }
326 
327 static void
329 {
331  tsize_t stride = sp->stride;
332  int16 *wp = (int16*) cp0;
333  tsize_t wc = cc/2;
334 
335  if (wc > stride) {
336  wc -= stride;
337  wp += wc - 1;
338  do {
339  REPEAT4(stride, wp[stride] -= wp[0]; wp--)
340  wc -= stride;
341  } while ((int32) wc > 0);
342  }
343 }
344 
345 static int
347 {
349 
350  assert(sp != NULL);
351  assert(sp->pfunc != NULL);
352  assert(sp->coderow != NULL);
353 /* XXX horizontal differencing alters user's data XXX */
354  (*sp->pfunc)(tif, bp, cc);
355  return ((*sp->coderow)(tif, bp, cc, s));
356 }
357 
358 static int
360 {
362  tsize_t cc = cc0, rowsize;
363  u_char* bp = bp0;
364 
365  assert(sp != NULL);
366  assert(sp->pfunc != NULL);
367  assert(sp->codetile != NULL);
368  rowsize = sp->rowsize;
369  assert(rowsize > 0);
370  while ((long)cc > 0) {
371  (*sp->pfunc)(tif, bp, (tsize_t) rowsize);
372  cc -= rowsize;
373  bp += rowsize;
374  }
375  return ((*sp->codetile)(tif, bp0, cc0, s));
376 }
377 
378 #define FIELD_PREDICTOR (FIELD_CODEC+0) /* XXX */
379 
380 static const TIFFFieldInfo predictFieldInfo[] = {
382  FALSE, FALSE, "Predictor" },
383 };
384 #define N(a) (sizeof (a) / sizeof (a[0]))
385 
386 static int
388 {
390 
391  switch (tag) {
392  case TIFFTAG_PREDICTOR:
393  sp->predictor = (uint16) va_arg(ap, int);
395  break;
396  default:
397  return (*sp->vsetparent)(tif, tag, ap);
398  }
399  tif->tif_flags |= TIFF_DIRTYDIRECT;
400  return (1);
401 }
402 
403 static int
405 {
407 
408  switch (tag) {
409  case TIFFTAG_PREDICTOR:
410  *va_arg(ap, uint16*) = sp->predictor;
411  break;
412  default:
413  return (*sp->vgetparent)(tif, tag, ap);
414  }
415  return (1);
416 }
417 
418 static void
420 {
422 
423  (void) flags;
424  if (TIFFFieldSet(tif,FIELD_PREDICTOR)) {
425  fprintf(fd, " Predictor: ");
426  switch (sp->predictor) {
427  case 1: fprintf(fd, "none "); break;
428  case 2: fprintf(fd, "horizontal differencing "); break;
429  }
430  fprintf(fd, "%u (0x%x)\n", sp->predictor, sp->predictor);
431  }
432  if (sp->printdir)
433  (*sp->printdir)(tif, fd, flags);
434 }
435 
436 int
438 {
440 
441  /*
442  * Merge codec-specific tag information and
443  * override parent get/set field methods.
444  */
446  sp->vgetparent = tif->tif_tagmethods.vgetfield;
448  PredictorVGetField;/* hook for predictor tag */
449  sp->vsetparent = tif->tif_tagmethods.vsetfield;
451  PredictorVSetField;/* hook for predictor tag */
452  sp->printdir = tif->tif_tagmethods.printdir;
453  tif->tif_tagmethods.printdir =
454  PredictorPrintDir; /* hook for predictor tag */
455 
456  sp->setupdecode = tif->tif_setupdecode;
458  sp->setupencode = tif->tif_setupencode;
460 
461  sp->predictor = 1; /* default value */
462  sp->pfunc = NULL; /* no predictor routine */
463  return (1);
464 }
bp
Definition: action.c:1035
cp
Definition: action.c:1035
int * g2
Definition: bmpfont.h:57
static struct brw_reg stride(struct brw_reg reg, uint32_t vstride, uint32_t width, uint32_t hstride)
#define ap
@ FALSE
Definition: dd.h:101
unsigned int u_int
Definition: defines.h:59
unsigned char u_char
Definition: defines.h:140
static void
Definition: fpif.c:118
#define a1
#define a2
#define s
Definition: afcover.h:80
#define r1
#define r2
#define TIFF_SHORT
Definition: gvceps.c:721
assert(pcxLoadImage24((char *)((void *) 0), fp, pinfo, hdr))
#define NULL
Definition: ftobjs.h:61
#define fprintf
Definition: mendex.h:64
#define b1
Definition: texmfmem.h:169
#define b2
Definition: texmfmem.h:170
#define TIFFFieldSet(tif, field)
Definition: tif_dir.h:249
#define TIFFSetFieldBit(tif, field)
Definition: tif_dir.h:250
void TIFFError(const char *module, const char *fmt,...)
Definition: tif_error.c:41
int TIFFPredictorInit(TIFF *tif)
Definition: tif_predict.c:437
void _TIFFNoPostDecode(TIFF *tif, tidata_t buf, tsize_t cc)
Definition: tif_read.c:599
tsize_t TIFFScanlineSize(TIFF *tif)
Definition: tif_strip.c:162
void TIFFSwabArrayOfShort(uint16 *wp, register u_long n)
Definition: tif_swab.c:59
tsize_t TIFFTileRowSize(TIFF *tif)
Definition: tif_tile.c:134
unsigned short uint16
Definition: tiff.h:62
short int16
Definition: tiff.h:61
long int32
Definition: tiff.h:67
#define TIFFTAG_PREDICTOR
Definition: tiff.h:254
#define PLANARCONFIG_CONTIG
Definition: tiff.h:218
int32 tsize_t
Definition: tiffio.h:73
uint32 ttag_t
Definition: tiffio.h:68
uint16 tsample_t
Definition: tiffio.h:70
#define isTiled(tif)
Definition: tiffiop.h:159
tidataval_t * tidata_t
Definition: tiffiop.h:56
#define TIFF_DIRTYDIRECT
Definition: tiffiop.h:77
#define TIFF_SWAB
Definition: tiffiop.h:81
#define uint16
Definition: unibasics.h:51
#define cb(n)
#define flags
uint16 td_samplesperpixel
Definition: tif_dir.h:51
uint16 td_bitspersample
Definition: tif_dir.h:44
uint16 td_planarconfig
Definition: tif_dir.h:57
TIFFVGetMethod vgetfield
Definition: tiffio.h:466
TIFFVSetMethod vsetfield
Definition: tiffio.h:465
TIFFPrintMethod printdir
Definition: tiffio.h:467
Definition: xmlparse.c:179
Definition: tiffiop.h:70
TIFFCodeMethod tif_encodestrip
Definition: tiffiop.h:119
TIFFCodeMethod tif_encodetile
Definition: tiffiop.h:121
TIFFTagMethods tif_tagmethods
Definition: tiffiop.h:159
TIFFCodeMethod tif_decodestrip
Definition: tiffiop.h:118
TIFFPostMethod tif_postdecode
Definition: tiffiop.h:148
uint32 tif_flags
Definition: tiffiop.h:74
TIFFCodeMethod tif_decoderow
Definition: tiffiop.h:116
TIFFBoolMethod tif_setupencode
Definition: tiffiop.h:113
TIFFDirectory tif_dir
Definition: tiffiop.h:92
TIFFCodeMethod tif_encoderow
Definition: tiffiop.h:117
TIFFBoolMethod tif_setupdecode
Definition: tiffiop.h:111
TIFFCodeMethod tif_decodetile
Definition: tiffiop.h:120
char * tif_name
Definition: tiffiop.h:71
#define FILE
Definition: t1stdio.h:34
tag int tag
Definition: tex4ht.c:2554
#define sp
Definition: stack.c:11
#define _TIFFMergeFieldInfo
Definition: tif_dir.h:260
static void horAcc16(TIFF *, tidata_t, tsize_t)
Definition: tif_predict.c:221
static void swabHorAcc16(TIFF *, tidata_t, tsize_t)
Definition: tif_predict.c:203
static const TIFFFieldInfo predictFieldInfo[]
Definition: tif_predict.c:380
#define PredictorState(tif)
Definition: tif_predict.c:37
static int PredictorVSetField(TIFF *tif, ttag_t tag, va_list ap)
Definition: tif_predict.c:387
static int PredictorDecodeTile(TIFF *, tidata_t, tsize_t, tsample_t)
Definition: tif_predict.c:262
static void horDiff8(TIFF *, tidata_t, tsize_t)
Definition: tif_predict.c:283
#define REPEAT4(n, op)
Definition: tif_predict.c:149
static void PredictorPrintDir(TIFF *tif, FILE *fd, long flags)
Definition: tif_predict.c:419
static int PredictorSetupDecode(TIFF *tif)
Definition: tif_predict.c:81
static int PredictorEncodeTile(TIFF *, tidata_t, tsize_t, tsample_t)
Definition: tif_predict.c:359
#define FIELD_PREDICTOR
Definition: tif_predict.c:378
static void horAcc8(TIFF *, tidata_t, tsize_t)
Definition: tif_predict.c:160
static int PredictorSetupEncode(TIFF *tif)
Definition: tif_predict.c:123
static int PredictorSetup(TIFF *tif)
Definition: tif_predict.c:50
static int PredictorDecodeRow(TIFF *, tidata_t, tsize_t, tsample_t)
Definition: tif_predict.c:240
#define N(a)
Definition: tif_predict.c:384
static int PredictorEncodeRow(TIFF *, tidata_t, tsize_t, tsample_t)
Definition: tif_predict.c:346
static void horDiff16(TIFF *, tidata_t, tsize_t)
Definition: tif_predict.c:328
static int PredictorVGetField(TIFF *tif, ttag_t tag, va_list ap)
Definition: tif_predict.c:404
#define va_arg(pvar, type)
Definition: varargs.h:35
char * va_list
Definition: varargs.h:22
char cr[]
Definition: vlna.c:107