File: tiffsave.c

package info (click to toggle)
minidjvu 0.8.svn.2010.05.06+dfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 2,372 kB
  • ctags: 1,082
  • sloc: sh: 9,230; ansic: 5,788; cpp: 2,400; makefile: 259; python: 42
file content (77 lines) | stat: -rw-r--r-- 1,971 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * tiffsave.c - saving TIFF bitmaps
 */

#include "../base/mdjvucfg.h"

#ifdef HAVE_LIBTIFF
    #include <tiffio.h>
    #define MDJVU_USE_TIFFIO
#endif

#include <minidjvu/minidjvu.h>

#ifdef HAVE_LIBTIFF

#ifndef COMPRESSION_PACKBITS
    #define COMPRESSION_PACKBITS 32771
#endif

static int save_tiff(mdjvu_bitmap_t bitmap, const char *path, mdjvu_error_t *perr)
{
    int32 w = mdjvu_bitmap_get_width(bitmap);
    int32 h = mdjvu_bitmap_get_height(bitmap);
    int32 compression = COMPRESSION_NONE;
    int32 i;
    TIFF * tiff;

    *perr = NULL;

    if (TIFFFindCODEC(COMPRESSION_PACKBITS))
        compression = COMPRESSION_PACKBITS;

    tiff = TIFFOpen(path, "w");

    if (!tiff)
    {
        *perr = mdjvu_get_error(mdjvu_error_fopen_write);
        return 0;
    }

    /* FIXME: save resolution */
    TIFFSetField(tiff, TIFFTAG_IMAGEWIDTH, (uint32) w);
    TIFFSetField(tiff, TIFFTAG_IMAGELENGTH, (uint32) h);
    TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, (uint16) 1);
    TIFFSetField(tiff, TIFFTAG_SAMPLESPERPIXEL, (uint16) 1);
    TIFFSetField(tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
    TIFFSetField(tiff, TIFFTAG_COMPRESSION, compression);
    TIFFSetField(tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);

    if (mdjvu_bitmap_get_packed_row_size(bitmap) != TIFFScanlineSize(tiff))
    {
        /* FIXME: not very accurate error reporting */
        *perr = mdjvu_get_error(mdjvu_error_fopen_write);
        return 0;
    }

    for (i = 0; i < h; i++)
        TIFFWriteScanline(tiff,
                          mdjvu_bitmap_access_packed_row(bitmap, i), i,
                          0);

    TIFFClose(tiff);

    return 1;
}

#endif /* HAVE_LIBTIFF */

MDJVU_IMPLEMENT int mdjvu_save_tiff(mdjvu_bitmap_t bitmap, const char *path, mdjvu_error_t *perr)
{
    #ifdef HAVE_LIBTIFF
        return save_tiff(bitmap, path, perr);
    #else
        *perr = mdjvu_get_error(mdjvu_error_tiff_support_disabled);
        return 0;
    #endif
}