File: gif.cc

package info (click to toggle)
exactimage 1.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,940 kB
  • sloc: cpp: 35,715; ansic: 1,730; xml: 1,447; makefile: 323; perl: 138; sh: 108; python: 45; php: 37; ruby: 12
file content (295 lines) | stat: -rw-r--r-- 8,603 bytes parent folder | download
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
 * Copyright (C) 2006 - 2015 René Rebe
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2. A copy of the GNU General
 * Public License can be found in the file LICENSE.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANT-
 * ABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 *
 * Alternatively, commercial licensing options are available from the
 * copyright holder ExactCODE GmbH Germany.
 */

#include <gif_lib.h>

#if (GIFLIB_MAJOR > 5) || (GIFLIB_MINOR >= 1)
#define Internal_EGifCloseFile(f) EGifCloseFile(f, NULL)
#else
#define Internal_EGifCloseFile(f) EGifCloseFile(f)
#endif

#include "gif.hh"
#include "Colorspace.hh"

#include <iostream>

/* The way Interlaced image should. */
static const int InterlacedOffset[] = { 0, 4, 2, 1 };

/* be read - offsets and jumps... */
static const int InterlacedJumps[] = { 8, 8, 4, 2 };

static int GIFInputFunc (GifFileType* t, GifByteType* mem, int len)
{
  std::istream* stream = (std::istream*)t->UserData;
  size_t nbytes = stream->tellg();
  stream->read((char*)mem, len);
  nbytes = (size_t)stream->tellg() - nbytes;
  return nbytes;
}

static int GIFOutputFunc (GifFileType* t, const GifByteType* mem, int len)
{
  std::ostream* stream = (std::ostream*)t->UserData;
  stream->write ((char*)mem, len);
  return len;
}

int GIFCodec::readImage (std::istream* stream, Image& image, const std::string& decompress)
{
  { // quick magic check
    char buf [3];
    stream->read (buf, sizeof (buf));
    stream->seekg (0);
    if (buf[0] != 'G' || buf[1] != 'I' || buf[2] != 'F')
      return false;
  }
  
  GifFileType* GifFile;
  GifRecordType RecordType;
  GifByteType* Extension;
  ColorMapObject *ColorMap = NULL;
  int GifError, ExtCode;
  
#if (GIFLIB_MAJOR > 5) || (GIFLIB_MINOR >= 1)
  if ((GifFile = DGifOpen (stream, &GIFInputFunc, &GifError)) == NULL)
#else
  if ((GifFile = DGifOpen (stream, &GIFInputFunc)) == NULL)
#endif
    {
#if (GIFLIB_MAJOR > 5) || (GIFLIB_MINOR >= 1)
      //std::cerr << "Error: " << GifErrorString(GifError) << std::endl;
#else
      //PrintGifError();
#endif
      return false;
    }
  
  image.spp = 1;
  image.bps = 8;
  image.setResolution(0, 0);
  image.resize (GifFile->SWidth,GifFile->SHeight);
  
  /* Scan the content of the GIF file and load the image(s) in: */
  do {
    if (DGifGetRecordType(GifFile, &RecordType) == GIF_ERROR) {
#if (GIFLIB_MAJOR > 5) || (GIFLIB_MINOR >= 1)
      //std::cerr << "DGifGetRecordType error: " << GifErrorString(GifFile->Error) << std::endl;
#else
      //PrintGifError();
#endif
      return false;
    }
    
    int Row, Col, Width, Height;
    
    switch (RecordType) {
    case IMAGE_DESC_RECORD_TYPE:
      if (DGifGetImageDesc(GifFile) == GIF_ERROR) {
#if (GIFLIB_MAJOR > 5) || (GIFLIB_MINOR >= 1)
        //std::cerr << "DGifGetImageDesc error: " << GifErrorString(GifFile->Error) << std::endl;
#else
        //PrintGifError();
#endif
	return false;
      }
      
      Row = GifFile->Image.Top; /* Image Position relative to Screen. */
      Col = GifFile->Image.Left;
      Width = GifFile->Image.Width;
      Height = GifFile->Image.Height;
      
      if (GifFile->Image.Left + GifFile->Image.Width > GifFile->SWidth ||
	  GifFile->Image.Top + GifFile->Image.Height > GifFile->SHeight) {
	std::cerr << "Image not in screen dimension, aborted." << std::endl;
	return false;
      }
      if (GifFile->Image.Interlace) {
	/* Need to perform 4 passes on the images: */
	for (int i = 0; i < 4; ++i)
	  for (int j = Row + InterlacedOffset[i]; j < Row + Height;
	       j += InterlacedJumps[i]) {
	    if (DGifGetLine(GifFile, &image.getRawData()[j*image.stride()+Col],
			    Width) == GIF_ERROR) {
#if (GIFLIB_MAJOR > 5) || (GIFLIB_MINOR >= 1)
	      //std::cerr << "DGifGetLine error: " << GifErrorString(GifFile->Error) << std::endl;
#else
	      //PrintGifError();
#endif
	      return false;
	    }
	  }
      }
      else {
	for (int i = 0; i < Height; ++i) {
	  if (DGifGetLine(GifFile, &image.getRawData()[Row++ * image.stride()+Col],
			  Width) == GIF_ERROR) {
#if (GIFLIB_MAJOR > 5) || (GIFLIB_MINOR >= 1)
	    //std::cerr << "DGifGetLine error: " << GifErrorString(GifFile->Error) << std::endl;
#else
	    //PrintGifError();
#endif
	    return false;
	  }
	}
      }
      break;
    case EXTENSION_RECORD_TYPE:
      /* Skip any extension blocks in file: */
      if (DGifGetExtension(GifFile, &ExtCode, &Extension) == GIF_ERROR) {
#if (GIFLIB_MAJOR > 5) || (GIFLIB_MINOR >= 1)
	//std::cerr << "DGifGetExtension error: " << GifErrorString(GifFile->Error) << std::endl;
#else
	//PrintGifError();
#endif
	return false;
      }
      while (Extension != 0) {
	if (DGifGetExtensionNext(GifFile, &Extension) == GIF_ERROR) {
#if (GIFLIB_MAJOR > 5) || (GIFLIB_MINOR >= 1)
	  //std::cerr << "DGifGetExtensionNext error: " << GifErrorString(GifFile->Error) << std::endl;
#else
	  //PrintGifError();
#endif
	  return false;
	}
      }
      break;
    case TERMINATE_RECORD_TYPE:
      break;
    default:		    /* Should be traps by DGifGetRecordType. */
      break;
    }
  }
  while (RecordType != TERMINATE_RECORD_TYPE);
  
  ColorMap = (GifFile->Image.ColorMap ? GifFile->Image.ColorMap :
	      GifFile->SColorMap);
  
  uint16_t rmap [ColorMap->ColorCount];
  uint16_t gmap [ColorMap->ColorCount];
  uint16_t bmap [ColorMap->ColorCount];
  for (int i = 0; i < ColorMap->ColorCount; ++i) {
    rmap[i] = ColorMap->Colors[i].Red << 8;
    gmap[i] = ColorMap->Colors[i].Green << 8;
    bmap[i] = ColorMap->Colors[i].Blue << 8;
  }
  
  // convert colormap to our 16bit "TIFF"format
  colorspace_de_palette (image, ColorMap->ColorCount, rmap, gmap, bmap);
  
  Internal_EGifCloseFile(GifFile);

  return true;
}

bool GIFCodec::writeImage (std::ostream* stream, Image& image, int quality,
			   const std::string& compress)
{
  GifFileType* GifFile;
  GifByteType* Ptr;
  int GifError;

#if (GIFLIB_MAJOR > 5) || (GIFLIB_MINOR >= 1)
  if ((GifFile = EGifOpen (stream, &GIFOutputFunc, &GifError)) == NULL)
#else
  if ((GifFile = EGifOpen (stream, &GIFOutputFunc)) == NULL)
#endif
    {
#if (GIFLIB_MAJOR > 5) || (GIFLIB_MINOR >= 1)
      std::cerr << "Error preparing GIF file for writing: " << GifErrorString(GifError) << std::endl;
#else
      std::cerr << "Error preparing GIF file for writing." << std::endl;
#endif
      return false;
    }
  
  int ColorMapSize = 256;
  
  // later use our own colormap generation
#if (GIFLIB_MAJOR > 5) || (GIFLIB_MINOR >= 1)
  ColorMapObject* OutputColorMap = GifMakeMapObject(ColorMapSize, NULL);
#else
  ColorMapObject* OutputColorMap = MakeMapObject(ColorMapSize, NULL);
#endif
  if (!OutputColorMap)
    return false;
  
  GifByteType* OutputBuffer =
    (GifByteType*) malloc(image.w * image.h *  sizeof(GifByteType));
  if (!OutputBuffer)
    return false;
  
  GifByteType
    *RedBuffer = new GifByteType [image.w*image.h],
    *GreenBuffer = new GifByteType [image.w*image.h],
    *BlueBuffer = new GifByteType [image.w*image.h];
  GifByteType
    *rptr = RedBuffer,
    *gptr = GreenBuffer,
    *bptr = BlueBuffer;
 
  for (Image::iterator it = image.begin(); it != image.end(); ++it) {
    uint16_t r = 0, g = 0, b = 0;
    *it;
    it.getRGB (&r, &g, &b);
    *rptr++ = r;
    *gptr++ = g;
    *bptr++ = b;
  }
   
#if (GIFLIB_MAJOR > 5) || (GIFLIB_MINOR >= 1)
  if (GifQuantizeBuffer(image.w, image.h, &ColorMapSize,
#else
  if (QuantizeBuffer(image.w, image.h, &ColorMapSize,
#endif
		     RedBuffer, GreenBuffer, BlueBuffer,
		     OutputBuffer, OutputColorMap->Colors) == GIF_ERROR) {
    return false;
  }
  
  std::cerr << "Writing uncompressed GIF file with "
	    << (int)ColorMapSize << " colors." << std::endl;
  
  if (EGifPutScreenDesc(GifFile, image.w, image.h,
			ColorMapSize, 0, OutputColorMap) == GIF_ERROR ||
      EGifPutImageDesc(GifFile, 0, 0, image.w, image.h,
		       false, NULL) == GIF_ERROR)
    {
      std::cerr << "Error writing GIF header." << std::endl;
      return false;
    }
  Ptr = OutputBuffer;
  for (int i = 0; i < image.h; ++i) {
    if (EGifPutLine(GifFile, Ptr, image.w) == GIF_ERROR)
      {
	std::cerr << "Error writing GIF header." << std::endl;
	return false;
      }
    
    Ptr += image.w;
  }
  free (OutputBuffer);

  delete[] RedBuffer; delete[] GreenBuffer; delete[] BlueBuffer;

  Internal_EGifCloseFile(GifFile);
  return true;
}

GIFCodec gif_loader;