~cosme/ubuntu/precise/freeimage/freeimage-3.15.1

« back to all changes in this revision

Viewing changes to Source/LibTIFF/tif_predict.c

  • Committer: Bazaar Package Importer
  • Author(s): Cosme Domínguez Díaz
  • Date: 2010-07-20 13:42:15 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100720134215-xt1454zaedv3b604
Tags: 3.13.1-0ubuntu1
* New upstream release. Closes: (LP: #607800)
 - Updated debian/freeimage-get-orig-source script.
 - Removing no longer necessary debian/patches/* and
   the patch system in debian/rules.
 - Updated debian/rules to work with the new Makefiles.
 - Drop from -O3 to -O2 and use lzma compression saves
   ~10 MB of free space. 
* lintian stuff
 - fixed debhelper-but-no-misc-depends
 - fixed ldconfig-symlink-missing-for-shlib

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: tif_predict.c,v 1.20 2007/11/10 18:41:26 drolon Exp $ */
 
1
/* $Id: tif_predict.c,v 1.28 2009/11/07 19:18:27 drolon Exp $ */
2
2
 
3
3
/*
4
4
 * Copyright (c) 1988-1997 Sam Leffler
36
36
 
37
37
static  void horAcc8(TIFF*, tidata_t, tsize_t);
38
38
static  void horAcc16(TIFF*, tidata_t, tsize_t);
 
39
static  void horAcc32(TIFF*, tidata_t, tsize_t);
39
40
static  void swabHorAcc16(TIFF*, tidata_t, tsize_t);
 
41
static  void swabHorAcc32(TIFF*, tidata_t, tsize_t);
40
42
static  void horDiff8(TIFF*, tidata_t, tsize_t);
41
43
static  void horDiff16(TIFF*, tidata_t, tsize_t);
 
44
static  void horDiff32(TIFF*, tidata_t, tsize_t);
42
45
static  void fpAcc(TIFF*, tidata_t, tsize_t);
43
46
static  void fpDiff(TIFF*, tidata_t, tsize_t);
44
47
static  int PredictorDecodeRow(TIFF*, tidata_t, tsize_t, tsample_t);
60
63
                        return 1;
61
64
                case PREDICTOR_HORIZONTAL:
62
65
                        if (td->td_bitspersample != 8
63
 
                            && td->td_bitspersample != 16) {
 
66
                            && td->td_bitspersample != 16
 
67
                            && td->td_bitspersample != 32) {
64
68
                                TIFFErrorExt(tif->tif_clientdata, module,
65
69
    "Horizontal differencing \"Predictor\" not supported with %d-bit samples",
66
70
                                          td->td_bitspersample);
105
109
 
106
110
        if (sp->predictor == 2) {
107
111
                switch (td->td_bitspersample) {
108
 
                        case 8:  sp->pfunc = horAcc8; break;
109
 
                        case 16: sp->pfunc = horAcc16; break;
 
112
                        case 8:  sp->decodepfunc = horAcc8; break;
 
113
                        case 16: sp->decodepfunc = horAcc16; break;
 
114
                        case 32: sp->decodepfunc = horAcc32; break;
110
115
                }
111
116
                /*
112
117
                 * Override default decoding method with one that does the
113
118
                 * predictor stuff.
114
119
                 */
115
 
                sp->coderow = tif->tif_decoderow;
116
 
                tif->tif_decoderow = PredictorDecodeRow;
117
 
                sp->codestrip = tif->tif_decodestrip;
118
 
                tif->tif_decodestrip = PredictorDecodeTile;
119
 
                sp->codetile = tif->tif_decodetile;
120
 
                tif->tif_decodetile = PredictorDecodeTile;
 
120
                if( tif->tif_decoderow != PredictorDecodeRow )
 
121
                {
 
122
                    sp->decoderow = tif->tif_decoderow;
 
123
                    tif->tif_decoderow = PredictorDecodeRow;
 
124
                    sp->decodestrip = tif->tif_decodestrip;
 
125
                    tif->tif_decodestrip = PredictorDecodeTile;
 
126
                    sp->decodetile = tif->tif_decodetile;
 
127
                    tif->tif_decodetile = PredictorDecodeTile;
 
128
                }
121
129
                /*
122
130
                 * If the data is horizontally differenced 16-bit data that
123
131
                 * requires byte-swapping, then it must be byte swapped before
126
134
                 * the library setup when the directory was read.
127
135
                 */
128
136
                if (tif->tif_flags & TIFF_SWAB) {
129
 
                        if (sp->pfunc == horAcc16) {
130
 
                                sp->pfunc = swabHorAcc16;
131
 
                                tif->tif_postdecode = _TIFFNoPostDecode;
132
 
                        } /* else handle 32-bit case... */
 
137
                        if (sp->decodepfunc == horAcc16) {
 
138
                                sp->decodepfunc = swabHorAcc16;
 
139
                                tif->tif_postdecode = _TIFFNoPostDecode;
 
140
                        } else if (sp->decodepfunc == horAcc32) {
 
141
                                sp->decodepfunc = swabHorAcc32;
 
142
                                tif->tif_postdecode = _TIFFNoPostDecode;
 
143
                        }
133
144
                }
134
145
        }
135
146
 
136
147
        else if (sp->predictor == 3) {
137
 
                sp->pfunc = fpAcc;
 
148
                sp->decodepfunc = fpAcc;
138
149
                /*
139
150
                 * Override default decoding method with one that does the
140
151
                 * predictor stuff.
141
152
                 */
142
 
                sp->coderow = tif->tif_decoderow;
143
 
                tif->tif_decoderow = PredictorDecodeRow;
144
 
                sp->codestrip = tif->tif_decodestrip;
145
 
                tif->tif_decodestrip = PredictorDecodeTile;
146
 
                sp->codetile = tif->tif_decodetile;
147
 
                tif->tif_decodetile = PredictorDecodeTile;
 
153
                if( tif->tif_decoderow != PredictorDecodeRow )
 
154
                {
 
155
                    sp->decoderow = tif->tif_decoderow;
 
156
                    tif->tif_decoderow = PredictorDecodeRow;
 
157
                    sp->decodestrip = tif->tif_decodestrip;
 
158
                    tif->tif_decodestrip = PredictorDecodeTile;
 
159
                    sp->decodetile = tif->tif_decodetile;
 
160
                    tif->tif_decodetile = PredictorDecodeTile;
 
161
                }
148
162
                /*
149
163
                 * The data should not be swapped outside of the floating
150
164
                 * point predictor, the accumulation routine should return
173
187
 
174
188
        if (sp->predictor == 2) {
175
189
                switch (td->td_bitspersample) {
176
 
                        case 8:  sp->pfunc = horDiff8; break;
177
 
                        case 16: sp->pfunc = horDiff16; break;
 
190
                        case 8:  sp->encodepfunc = horDiff8; break;
 
191
                        case 16: sp->encodepfunc = horDiff16; break;
 
192
                        case 32: sp->encodepfunc = horDiff32; break;
178
193
                }
179
194
                /*
180
195
                 * Override default encoding method with one that does the
181
196
                 * predictor stuff.
182
197
                 */
183
 
                sp->coderow = tif->tif_encoderow;
184
 
                tif->tif_encoderow = PredictorEncodeRow;
185
 
                sp->codestrip = tif->tif_encodestrip;
186
 
                tif->tif_encodestrip = PredictorEncodeTile;
187
 
                sp->codetile = tif->tif_encodetile;
188
 
                tif->tif_encodetile = PredictorEncodeTile;
 
198
                if( tif->tif_encoderow != PredictorEncodeRow )
 
199
                {
 
200
                    sp->encoderow = tif->tif_encoderow;
 
201
                    tif->tif_encoderow = PredictorEncodeRow;
 
202
                    sp->encodestrip = tif->tif_encodestrip;
 
203
                    tif->tif_encodestrip = PredictorEncodeTile;
 
204
                    sp->encodetile = tif->tif_encodetile;
 
205
                    tif->tif_encodetile = PredictorEncodeTile;
 
206
                }
189
207
        }
190
208
        
191
209
        else if (sp->predictor == 3) {
192
 
                sp->pfunc = fpDiff;
 
210
                sp->encodepfunc = fpDiff;
193
211
                /*
194
212
                 * Override default encoding method with one that does the
195
213
                 * predictor stuff.
196
214
                 */
197
 
                sp->coderow = tif->tif_encoderow;
198
 
                tif->tif_encoderow = PredictorEncodeRow;
199
 
                sp->codestrip = tif->tif_encodestrip;
200
 
                tif->tif_encodestrip = PredictorEncodeTile;
201
 
                sp->codetile = tif->tif_encodetile;
202
 
                tif->tif_encodetile = PredictorEncodeTile;
 
215
                if( tif->tif_encoderow != PredictorEncodeRow )
 
216
                {
 
217
                    sp->encoderow = tif->tif_encoderow;
 
218
                    tif->tif_encoderow = PredictorEncodeRow;
 
219
                    sp->encodestrip = tif->tif_encodestrip;
 
220
                    tif->tif_encodestrip = PredictorEncodeTile;
 
221
                    sp->encodetile = tif->tif_encodetile;
 
222
                    tif->tif_encodetile = PredictorEncodeTile;
 
223
                }
203
224
        }
204
225
 
205
226
        return 1;
291
312
        }
292
313
}
293
314
 
 
315
static void
 
316
swabHorAcc32(TIFF* tif, tidata_t cp0, tsize_t cc)
 
317
{
 
318
        tsize_t stride = PredictorState(tif)->stride;
 
319
        uint32* wp = (uint32*) cp0;
 
320
        tsize_t wc = cc / 4;
 
321
 
 
322
        if (wc > stride) {
 
323
                TIFFSwabArrayOfLong(wp, wc);
 
324
                wc -= stride;
 
325
                do {
 
326
                        REPEAT4(stride, wp[stride] += wp[0]; wp++)
 
327
                        wc -= stride;
 
328
                } while ((int32) wc > 0);
 
329
        }
 
330
}
 
331
 
 
332
static void
 
333
horAcc32(TIFF* tif, tidata_t cp0, tsize_t cc)
 
334
{
 
335
        tsize_t stride = PredictorState(tif)->stride;
 
336
        uint32* wp = (uint32*) cp0;
 
337
        tsize_t wc = cc / 4;
 
338
 
 
339
        if (wc > stride) {
 
340
                wc -= stride;
 
341
                do {
 
342
                        REPEAT4(stride, wp[stride] += wp[0]; wp++)
 
343
                        wc -= stride;
 
344
                } while ((int32) wc > 0);
 
345
        }
 
346
}
 
347
 
294
348
/*
295
349
 * Floating point predictor accumulation routine.
296
350
 */
337
391
        TIFFPredictorState *sp = PredictorState(tif);
338
392
 
339
393
        assert(sp != NULL);
340
 
        assert(sp->coderow != NULL);
341
 
        assert(sp->pfunc != NULL);
 
394
        assert(sp->decoderow != NULL);
 
395
        assert(sp->decodepfunc != NULL);
342
396
 
343
 
        if ((*sp->coderow)(tif, op0, occ0, s)) {
344
 
                (*sp->pfunc)(tif, op0, occ0);
 
397
        if ((*sp->decoderow)(tif, op0, occ0, s)) {
 
398
                (*sp->decodepfunc)(tif, op0, occ0);
345
399
                return 1;
346
400
        } else
347
401
                return 0;
360
414
        TIFFPredictorState *sp = PredictorState(tif);
361
415
 
362
416
        assert(sp != NULL);
363
 
        assert(sp->codetile != NULL);
 
417
        assert(sp->decodetile != NULL);
364
418
 
365
 
        if ((*sp->codetile)(tif, op0, occ0, s)) {
 
419
        if ((*sp->decodetile)(tif, op0, occ0, s)) {
366
420
                tsize_t rowsize = sp->rowsize;
367
421
                assert(rowsize > 0);
368
 
                assert(sp->pfunc != NULL);
 
422
                assert(sp->decodepfunc != NULL);
369
423
                while ((long)occ0 > 0) {
370
 
                        (*sp->pfunc)(tif, op0, (tsize_t) rowsize);
 
424
                        (*sp->decodepfunc)(tif, op0, (tsize_t) rowsize);
371
425
                        occ0 -= rowsize;
372
426
                        op0 += rowsize;
373
427
                }
439
493
        }
440
494
}
441
495
 
 
496
static void
 
497
horDiff32(TIFF* tif, tidata_t cp0, tsize_t cc)
 
498
{
 
499
        TIFFPredictorState* sp = PredictorState(tif);
 
500
        tsize_t stride = sp->stride;
 
501
        int32 *wp = (int32*) cp0;
 
502
        tsize_t wc = cc/4;
 
503
 
 
504
        if (wc > stride) {
 
505
                wc -= stride;
 
506
                wp += wc - 1;
 
507
                do {
 
508
                        REPEAT4(stride, wp[stride] -= wp[0]; wp--)
 
509
                        wc -= stride;
 
510
                } while ((int32) wc > 0);
 
511
        }
 
512
}
 
513
 
442
514
/*
443
515
 * Floating point predictor differencing routine.
444
516
 */
481
553
        TIFFPredictorState *sp = PredictorState(tif);
482
554
 
483
555
        assert(sp != NULL);
484
 
        assert(sp->pfunc != NULL);
485
 
        assert(sp->coderow != NULL);
 
556
        assert(sp->encodepfunc != NULL);
 
557
        assert(sp->encoderow != NULL);
486
558
 
487
559
        /* XXX horizontal differencing alters user's data XXX */
488
 
        (*sp->pfunc)(tif, bp, cc);
489
 
        return (*sp->coderow)(tif, bp, cc, s);
 
560
        (*sp->encodepfunc)(tif, bp, cc);
 
561
        return (*sp->encoderow)(tif, bp, cc, s);
490
562
}
491
563
 
492
564
static int
493
565
PredictorEncodeTile(TIFF* tif, tidata_t bp0, tsize_t cc0, tsample_t s)
494
566
{
 
567
        static const char module[] = "PredictorEncodeTile";
495
568
        TIFFPredictorState *sp = PredictorState(tif);
 
569
        uint8 *working_copy;
496
570
        tsize_t cc = cc0, rowsize;
497
 
        unsigned char* bp = bp0;
 
571
        unsigned char* bp;
 
572
        int result_code;
498
573
 
499
574
        assert(sp != NULL);
500
 
        assert(sp->pfunc != NULL);
501
 
        assert(sp->codetile != NULL);
 
575
        assert(sp->encodepfunc != NULL);
 
576
        assert(sp->encodetile != NULL);
 
577
 
 
578
        /* 
 
579
         * Do predictor manipulation in a working buffer to avoid altering
 
580
         * the callers buffer. http://trac.osgeo.org/gdal/ticket/1965
 
581
         */
 
582
        working_copy = (uint8*) _TIFFmalloc(cc0);
 
583
        if( working_copy == NULL )
 
584
        {
 
585
            TIFFErrorExt(tif->tif_clientdata, module, 
 
586
                         "Out of memory allocating %d byte temp buffer.",
 
587
                         cc0 );
 
588
            return 0;
 
589
        }
 
590
        memcpy( working_copy, bp0, cc0 );
 
591
        bp = working_copy;
502
592
 
503
593
        rowsize = sp->rowsize;
504
594
        assert(rowsize > 0);
505
 
        while ((long)cc > 0) {
506
 
                (*sp->pfunc)(tif, bp, (tsize_t) rowsize);
 
595
        assert((cc0%rowsize)==0);
 
596
        while (cc > 0) {
 
597
                (*sp->encodepfunc)(tif, bp, rowsize);
507
598
                cc -= rowsize;
508
599
                bp += rowsize;
509
600
        }
510
 
        return (*sp->codetile)(tif, bp0, cc0, s);
 
601
        result_code = (*sp->encodetile)(tif, working_copy, cc0, s);
 
602
 
 
603
        _TIFFfree( working_copy );
 
604
 
 
605
        return result_code;
511
606
}
512
607
 
513
608
#define FIELD_PREDICTOR (FIELD_CODEC+0)         /* XXX */
610
705
        tif->tif_setupencode = PredictorSetupEncode;
611
706
 
612
707
        sp->predictor = 1;                      /* default value */
613
 
        sp->pfunc = NULL;                       /* no predictor routine */
 
708
        sp->encodepfunc = NULL;                 /* no predictor routine */
 
709
        sp->decodepfunc = NULL;                 /* no predictor routine */
614
710
        return 1;
615
711
}
616
712