aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/evas/src/modules/savers/tiff/evas_image_save_tiff.c
blob: 8148f519b5a6deafae1455619aca0d7b404e7949 (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "evas_common.h"
#include "evas_private.h"

#include <tiffio.h>

static int evas_image_save_file_tiff(RGBA_Image *im, const char *file, const char *key, int quality, int compress);

static Evas_Image_Save_Func evas_image_save_tiff_func =
{
   evas_image_save_file_tiff
};

static int
save_image_tiff(RGBA_Image *im, const char *file, int compress __UNUSED__, int interlace __UNUSED__)
{
   TIFF               *tif = NULL;
   uint8              *buf = NULL;
   DATA32              pixel;
   DATA32             *data;
   uint32              x, y;
   uint8               r, g, b, a = 0;
   int                 i = 0;
   int                 has_alpha;

   if (!im || !im->image.data || !file)
      return 0;

   has_alpha = im->cache_entry.flags.alpha;
   data = im->image.data;

   tif = TIFFOpen(file, "w");
   if (!tif)
      return 0;

   /* None of the TIFFSetFields are checked for errors, but since they */
   /* shouldn't fail, this shouldn't be a problem */

   TIFFSetField(tif, TIFFTAG_IMAGELENGTH, im->cache_entry.h);
   TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, im->cache_entry.w);
   TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
   TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
   TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
   TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE);

   /* By default uses patent-free use COMPRESSION_DEFLATE,
    * another lossless compression technique */
   TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE);
   
   if (has_alpha)
     {
        uint16 extras[] = { EXTRASAMPLE_ASSOCALPHA };
        TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 4);
        TIFFSetField(tif, TIFFTAG_EXTRASAMPLES, 1, extras);
     }
   else
     {
        TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
     }

   TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
   TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tif, 0));

   buf = (uint8 *) _TIFFmalloc(TIFFScanlineSize(tif));
   if (!buf)
     {
        TIFFClose(tif);
        return 0;
     }

   for (y = 0; y < im->cache_entry.h; y++)
     {
        i = 0;
        for (x = 0; x < im->cache_entry.w; x++)
          {
             pixel = data[(y * im->cache_entry.w) + x];

             r = (pixel >> 16) & 0xff;
             g = (pixel >> 8) & 0xff;
             b = pixel & 0xff;
             if (has_alpha)
                a = (pixel >> 24) & 0xff;

             /* This might be endian dependent */
             buf[i++] = r;
             buf[i++] = g;
             buf[i++] = b;
             if (has_alpha)
                buf[i++] = a;
          }

        if (!TIFFWriteScanline(tif, buf, y, 0))
          {
             _TIFFfree(buf);
             TIFFClose(tif);
             return 0;
          }
     }

   _TIFFfree(buf);
   TIFFClose(tif);

   return 1;
}

static int evas_image_save_file_tiff(RGBA_Image *im, const char *file, const char *key __UNUSED__, int quality __UNUSED__, int compress)
{
   return save_image_tiff(im, file, compress, 0);
}

static int
module_open(Evas_Module *em)
{
   if (!em) return 0;
   em->functions = (void *)(&evas_image_save_tiff_func);
   return 1;
}

static void
module_close(Evas_Module *em __UNUSED__)
{
}

static Evas_Module_Api evas_modapi =
{
   EVAS_MODULE_API_VERSION,
   "tiff",
   "none",
   {
     module_open,
     module_close
   }
};

EVAS_MODULE_DEFINE(EVAS_MODULE_TYPE_IMAGE_SAVER, image_saver, tiff);

#ifndef EVAS_STATIC_BUILD_TIFF
EVAS_EINA_MODULE_DEFINE(image_saver, tiff);
#endif