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

« back to all changes in this revision

Viewing changes to Source/LibTIFF/tif_getimage.c

  • Committer: Stefano Rivera
  • Date: 2010-07-24 15:35:51 UTC
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: stefanor@ubuntu.com-20100724153551-6s3fth1653huk31a
Tags: upstream-3.13.1
ImportĀ upstreamĀ versionĀ 3.31.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: tif_getimage.c,v 1.22 2007/11/10 18:41:12 drolon Exp $ */
 
1
/* $Id: tif_getimage.c,v 1.30 2009/11/07 19:18:27 drolon Exp $ */
2
2
 
3
3
/*
4
4
 * Copyright (c) 1991-1997 Sam Leffler
38
38
static int gtStripSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
39
39
static int PickContigCase(TIFFRGBAImage*);
40
40
static int PickSeparateCase(TIFFRGBAImage*);
41
 
 
42
 
static int BuildMapUaToAa(TIFFRGBAImage* img);
43
 
static int BuildMapBitdepth16To8(TIFFRGBAImage* img);
44
 
 
45
41
static const char photoTag[] = "PhotometricInterpretation";
46
42
 
47
43
/* 
206
202
                _TIFFfree(img->ycbcr), img->ycbcr = NULL;
207
203
        if (img->cielab)
208
204
                _TIFFfree(img->cielab), img->cielab = NULL;
209
 
        if (img->UaToAa)
210
 
                _TIFFfree(img->UaToAa), img->UaToAa = NULL;
211
 
        if (img->Bitdepth16To8)
212
 
                _TIFFfree(img->Bitdepth16To8), img->Bitdepth16To8 = NULL;
213
 
 
214
205
        if( img->redcmap ) {
215
206
                _TIFFfree( img->redcmap );
216
207
                _TIFFfree( img->greencmap );
431
422
                            photoTag, img->photometric);
432
423
                        return (0);
433
424
        }
434
 
        img->SubsamplingHor = 1;
435
 
        img->SubsamplingVer = 1;
436
425
        img->Map = NULL;
437
426
        img->BWmap = NULL;
438
427
        img->PALmap = NULL;
439
428
        img->ycbcr = NULL;
440
429
        img->cielab = NULL;
441
 
        img->UaToAa = NULL;
442
 
        img->Bitdepth16To8 = NULL;
443
430
        TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);
444
431
        TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);
445
432
        TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);
494
481
                        rwidth, img.height);
495
482
                TIFFRGBAImageEnd(&img);
496
483
        } else {
497
 
                TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), emsg);
 
484
                TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
498
485
                ok = 0;
499
486
    }
500
487
    return (ok);
806
793
        uint32 pos;
807
794
        unsigned char* buf;
808
795
        uint32 rowsperstrip;
 
796
        uint16 subsamplinghor,subsamplingver;
809
797
        uint32 imagewidth = img->width;
810
798
        tsize_t scanline;
811
799
        int32 fromskew, toskew;
828
816
        }
829
817
 
830
818
        TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
 
819
        TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, &subsamplinghor, &subsamplingver);
831
820
        scanline = TIFFNewScanlineSize(tif);
832
821
        fromskew = (w < imagewidth ? imagewidth - w : 0);
833
822
        for (row = 0; row < h; row += nrow)
835
824
                rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
836
825
                nrow = (row + rowstoread > h ? h - row : rowstoread);
837
826
                nrowsub = nrow;
838
 
                if ((nrowsub%(img->SubsamplingVer))!=0)
839
 
                        nrowsub+=img->SubsamplingVer-nrowsub%(img->SubsamplingVer);
 
827
                if ((nrowsub%subsamplingver)!=0)
 
828
                        nrowsub+=subsamplingver-nrowsub%subsamplingver;
840
829
                if (TIFFReadEncodedStrip(tif,
841
830
                    TIFFComputeStrip(tif,row+img->row_offset, 0),
842
831
                    buf,
1047
1036
#define PACK4(r,g,b,a)  \
1048
1037
        ((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|((uint32)(a)<<24))
1049
1038
#define W2B(v) (((v)>>8)&0xff)
1050
 
/* TODO: PACKW should have be made redundant in favor of Bitdepth16To8 LUT */
1051
1039
#define PACKW(r,g,b)    \
1052
1040
        ((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|A1)
1053
1041
#define PACKW4(r,g,b,a) \
1278
1266
        fromskew *= samplesperpixel;
1279
1267
        while (h-- > 0) {
1280
1268
                uint32 r, g, b, a;
1281
 
                uint8* m;
1282
1269
                for (x = w; x-- > 0;) {
1283
1270
                        a = pp[3];
1284
 
                        m = img->UaToAa+(a<<8);
1285
 
                        r = m[pp[0]];
1286
 
                        g = m[pp[1]];
1287
 
                        b = m[pp[2]];
 
1271
                        r = (a*pp[0] + 127) / 255;
 
1272
                        g = (a*pp[1] + 127) / 255;
 
1273
                        b = (a*pp[2] + 127) / 255;
1288
1274
                        *cp++ = PACK4(r,g,b,a);
1289
1275
                        pp += samplesperpixel;
1290
1276
                }
1304
1290
        fromskew *= samplesperpixel;
1305
1291
        while (h-- > 0) {
1306
1292
                for (x = w; x-- > 0;) {
1307
 
                        *cp++ = PACK(img->Bitdepth16To8[wp[0]],
1308
 
                            img->Bitdepth16To8[wp[1]],
1309
 
                            img->Bitdepth16To8[wp[2]]);
1310
 
                        wp += samplesperpixel;
 
1293
                    *cp++ = PACKW(wp[0],wp[1],wp[2]);
 
1294
                    wp += samplesperpixel;
1311
1295
                }
1312
1296
                cp += toskew;
1313
1297
                wp += fromskew;
1326
1310
        fromskew *= samplesperpixel;
1327
1311
        while (h-- > 0) {
1328
1312
                for (x = w; x-- > 0;) {
1329
 
                        *cp++ = PACK4(img->Bitdepth16To8[wp[0]],
1330
 
                            img->Bitdepth16To8[wp[1]],
1331
 
                            img->Bitdepth16To8[wp[2]],
1332
 
                            img->Bitdepth16To8[wp[3]]);
1333
 
                        wp += samplesperpixel;
 
1313
                    *cp++ = PACKW4(wp[0],wp[1],wp[2],wp[3]);
 
1314
                    wp += samplesperpixel;
1334
1315
                }
1335
1316
                cp += toskew;
1336
1317
                wp += fromskew;
1349
1330
        fromskew *= samplesperpixel;
1350
1331
        while (h-- > 0) {
1351
1332
                uint32 r,g,b,a;
1352
 
                uint8* m;
1353
1333
                for (x = w; x-- > 0;) {
1354
 
                        a = img->Bitdepth16To8[wp[3]];
1355
 
                        m = img->UaToAa+(a<<8);
1356
 
                        r = m[img->Bitdepth16To8[wp[0]]];
1357
 
                        g = m[img->Bitdepth16To8[wp[1]]];
1358
 
                        b = m[img->Bitdepth16To8[wp[2]]];
1359
 
                        *cp++ = PACK4(r,g,b,a);
1360
 
                        wp += samplesperpixel;
 
1334
                    a = W2B(wp[3]);
 
1335
                    r = (a*W2B(wp[0]) + 127) / 255;
 
1336
                    g = (a*W2B(wp[1]) + 127) / 255;
 
1337
                    b = (a*W2B(wp[2]) + 127) / 255;
 
1338
                    *cp++ = PACK4(r,g,b,a);
 
1339
                    wp += samplesperpixel;
1361
1340
                }
1362
1341
                cp += toskew;
1363
1342
                wp += fromskew;
1460
1439
        (void) img; (void) y;
1461
1440
        while (h-- > 0) {
1462
1441
                uint32 rv, gv, bv, av;
1463
 
                uint8* m;
1464
1442
                for (x = w; x-- > 0;) {
1465
1443
                        av = *a++;
1466
 
                        m = img->UaToAa+(av<<8);
1467
 
                        rv = m[*r++];
1468
 
                        gv = m[*g++];
1469
 
                        bv = m[*b++];
 
1444
                        rv = (av* *r++ + 127) / 255;
 
1445
                        gv = (av* *g++ + 127) / 255;
 
1446
                        bv = (av* *b++ + 127) / 255;
1470
1447
                        *cp++ = PACK4(rv,gv,bv,av);
1471
1448
                }
1472
1449
                SKEW4(r, g, b, a, fromskew);
1485
1462
        (void) img; (void) y; (void) a;
1486
1463
        while (h-- > 0) {
1487
1464
                for (x = 0; x < w; x++)
1488
 
                        *cp++ = PACK(img->Bitdepth16To8[*wr++],
1489
 
                            img->Bitdepth16To8[*wg++],
1490
 
                            img->Bitdepth16To8[*wb++]);
 
1465
                    *cp++ = PACKW(*wr++,*wg++,*wb++);
1491
1466
                SKEW(wr, wg, wb, fromskew);
1492
1467
                cp += toskew;
1493
1468
        }
1505
1480
        (void) img; (void) y;
1506
1481
        while (h-- > 0) {
1507
1482
                for (x = 0; x < w; x++)
1508
 
                        *cp++ = PACK4(img->Bitdepth16To8[*wr++],
1509
 
                            img->Bitdepth16To8[*wg++],
1510
 
                            img->Bitdepth16To8[*wb++],
1511
 
                            img->Bitdepth16To8[*wa++]);
 
1483
                    *cp++ = PACKW4(*wr++,*wg++,*wb++,*wa++);
1512
1484
                SKEW4(wr, wg, wb, wa, fromskew);
1513
1485
                cp += toskew;
1514
1486
        }
1526
1498
        (void) img; (void) y;
1527
1499
        while (h-- > 0) {
1528
1500
                uint32 r,g,b,a;
1529
 
                uint8* m;
1530
1501
                for (x = w; x-- > 0;) {
1531
 
                        a = img->Bitdepth16To8[*wa++];
1532
 
                        m = img->UaToAa+(a<<8);
1533
 
                        r = m[img->Bitdepth16To8[*wr++]];
1534
 
                        g = m[img->Bitdepth16To8[*wg++]];
1535
 
                        b = m[img->Bitdepth16To8[*wb++]];
1536
 
                        *cp++ = PACK4(r,g,b,a);
 
1502
                    a = W2B(*wa++);
 
1503
                    r = (a*W2B(*wr++) + 127) / 255;
 
1504
                    g = (a*W2B(*wg++) + 127) / 255;
 
1505
                    b = (a*W2B(*wb++) + 127) / 255;
 
1506
                    *cp++ = PACK4(r,g,b,a);
1537
1507
                }
1538
1508
                SKEW4(wr, wg, wb, wa, fromskew);
1539
1509
                cp += toskew;
2355
2325
                                                img->put.contig = putRGBAAcontig8bittile;
2356
2326
                                        else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
2357
2327
                                        {
2358
 
                                                if (BuildMapUaToAa(img))
2359
 
                                                        img->put.contig = putRGBUAcontig8bittile;
 
2328
                                            img->put.contig = putRGBUAcontig8bittile;
2360
2329
                                        }
2361
2330
                                        else
2362
 
                                                img->put.contig = putRGBcontig8bittile;
 
2331
                                            img->put.contig = putRGBcontig8bittile;
2363
2332
                                        break;
2364
2333
                                case 16:
2365
2334
                                        if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
2366
2335
                                        {
2367
 
                                                if (BuildMapBitdepth16To8(img))
2368
 
                                                        img->put.contig = putRGBAAcontig16bittile;
 
2336
                                            img->put.contig = putRGBAAcontig16bittile;
2369
2337
                                        }
2370
2338
                                        else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
2371
2339
                                        {
2372
 
                                                if (BuildMapBitdepth16To8(img) &&
2373
 
                                                    BuildMapUaToAa(img))
2374
 
                                                        img->put.contig = putRGBUAcontig16bittile;
 
2340
                                            img->put.contig = putRGBUAcontig16bittile;
2375
2341
                                        }
2376
2342
                                        else
2377
2343
                                        {
2378
 
                                                if (BuildMapBitdepth16To8(img))
2379
 
                                                        img->put.contig = putRGBcontig16bittile;
 
2344
                                            img->put.contig = putRGBcontig16bittile;
2380
2345
                                        }
2381
2346
                                        break;
2382
2347
                        }
2445
2410
                                         * Joris: added support for the [1,2] case, nonetheless, to accomodate
2446
2411
                                         * some OJPEG files
2447
2412
                                         */
2448
 
                                        TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &img->SubsamplingHor, &img->SubsamplingVer);
2449
 
                                        switch ((img->SubsamplingHor<<4)|img->SubsamplingVer) {
 
2413
                                        uint16 SubsamplingHor;
 
2414
                                        uint16 SubsamplingVer;
 
2415
                                        TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &SubsamplingHor, &SubsamplingVer);
 
2416
                                        switch ((SubsamplingHor<<4)|SubsamplingVer) {
2450
2417
                                                case 0x44:
2451
2418
                                                        img->put.contig = putcontig8bitYCbCr44tile;
2452
2419
                                                        break;
2501
2468
                                                img->put.separate = putRGBAAseparate8bittile;
2502
2469
                                        else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
2503
2470
                                        {
2504
 
                                                if (BuildMapUaToAa(img))
2505
 
                                                        img->put.separate = putRGBUAseparate8bittile;
 
2471
                                            img->put.separate = putRGBUAseparate8bittile;
2506
2472
                                        }
2507
2473
                                        else
2508
2474
                                                img->put.separate = putRGBseparate8bittile;
2510
2476
                                case 16:
2511
2477
                                        if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
2512
2478
                                        {
2513
 
                                                if (BuildMapBitdepth16To8(img))
2514
 
                                                        img->put.separate = putRGBAAseparate16bittile;
 
2479
                                            img->put.separate = putRGBAAseparate16bittile;
2515
2480
                                        }
2516
2481
                                        else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
2517
2482
                                        {
2518
 
                                                if (BuildMapBitdepth16To8(img) &&
2519
 
                                                    BuildMapUaToAa(img))
2520
 
                                                        img->put.separate = putRGBUAseparate16bittile;
 
2483
                                            img->put.separate = putRGBUAseparate16bittile;
2521
2484
                                        }
2522
2485
                                        else
2523
2486
                                        {
2524
 
                                                if (BuildMapBitdepth16To8(img))
2525
 
                                                        img->put.separate = putRGBseparate16bittile;
 
2487
                                            img->put.separate = putRGBseparate16bittile;
2526
2488
                                        }
2527
2489
                                        break;
2528
2490
                        }
2547
2509
        return ((img->get!=NULL) && (img->put.separate!=NULL));
2548
2510
}
2549
2511
 
2550
 
static int
2551
 
BuildMapUaToAa(TIFFRGBAImage* img)
2552
 
{
2553
 
        static const char module[]="BuildMapUaToAa";
2554
 
        uint8* m;
2555
 
        uint16 na,nv;
2556
 
        assert(img->UaToAa==NULL);
2557
 
        img->UaToAa=_TIFFmalloc(65536);
2558
 
        if (img->UaToAa==NULL)
2559
 
        {
2560
 
                TIFFErrorExt(img->tif,module,"Out of memory");
2561
 
                return(0);
2562
 
        }
2563
 
        m=img->UaToAa;
2564
 
        for (na=0; na<256; na++)
2565
 
        {
2566
 
                for (nv=0; nv<256; nv++)
2567
 
                        *m++=(nv*na+127)/255;
2568
 
        }
2569
 
        return(1);
2570
 
}
2571
 
 
2572
 
static int
2573
 
BuildMapBitdepth16To8(TIFFRGBAImage* img)
2574
 
{
2575
 
        static const char module[]="BuildMapBitdepth16To8";
2576
 
        uint8* m;
2577
 
        uint32 n;
2578
 
        assert(img->Bitdepth16To8==NULL);
2579
 
        img->Bitdepth16To8=_TIFFmalloc(65536);
2580
 
        if (img->Bitdepth16To8==NULL)
2581
 
        {
2582
 
                TIFFErrorExt(img->tif,module,"Out of memory");
2583
 
                return(0);
2584
 
        }
2585
 
        m=img->Bitdepth16To8;
2586
 
        for (n=0; n<65536; n++)
2587
 
                *m++=(n+128)/257;
2588
 
        return(1);
2589
 
}
2590
 
 
2591
 
 
2592
2512
/*
2593
2513
 * Read a whole strip off data from the file, and convert to RGBA form.
2594
2514
 * If this is the last strip, then it will only contain the portion of
2635
2555
        
2636
2556
        TIFFRGBAImageEnd(&img);
2637
2557
    } else {
2638
 
                TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), emsg);
 
2558
                TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
2639
2559
                ok = 0;
2640
2560
    }
2641
2561
    
2687
2607
    
2688
2608
    if (!TIFFRGBAImageOK(tif, emsg) 
2689
2609
        || !TIFFRGBAImageBegin(&img, tif, 0, emsg)) {
2690
 
            TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), emsg);
 
2610
            TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
2691
2611
            return( 0 );
2692
2612
    }
2693
2613