File: GIF_Support.cpp

package info (click to toggle)
exempi 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 9,276 kB
  • sloc: cpp: 49,877; sh: 10,986; makefile: 272; ansic: 93
file content (348 lines) | stat: -rw-r--r-- 9,696 bytes parent folder | download | duplicates (4)
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// =================================================================================================
// ADOBE SYSTEMS INCORPORATED
// Copyright 2002-2007 Adobe Systems Incorporated
// All Rights Reserved
//
// NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms
// of the Adobe license agreement accompanying it.
//
// Derived from PNG_Support.cpp by Ian Jacobi
// =================================================================================================

#include "GIF_Support.hpp"
#include <string.h>

typedef std::basic_string<unsigned char> filebuffer;

// Don't need CRC.

namespace GIF_Support
{
        // This only really counts for extension blocks.
	enum blockType {
		bGraphicControl = 0xF9,
		bComment        = 0xFE,
		bPlainText      = 0x01,
		bApplication    = 0xFF,
		// Hacky.  Don't like the following, but there's no easy way.
		bImage          = 0x2C,
		bExtension      = 0x21,
		bTerminator     = 0x3B,
		bHeader         = 0x47
	};

	// =============================================================================================

	long OpenGIF ( LFA_FileRef fileRef, BlockState & inOutBlockState )
	{
		XMP_Uns64 pos = 0;
		unsigned char name;
		XMP_Uns32 len;
		BlockData newBlock;
	
		pos = LFA_Seek ( fileRef, 0, SEEK_SET );
		// header needs to be a block, mostly for our safe write.
		pos = ReadHeader ( fileRef );
		if (pos < 13) return 0;
		
		newBlock.pos = 0;
		newBlock.len = pos;
		newBlock.type = bHeader;
		inOutBlockState.blocks.push_back(newBlock);
	
		// read first and following blocks
		while ( ReadBlock ( fileRef, inOutBlockState, &name, &len, pos) ) {}
	
		return inOutBlockState.blocks.size();

	}

	// =============================================================================================

	long ReadHeader ( LFA_FileRef fileRef )
	{
		long bytesRead;
		long headerSize;
		long tableSize = 0;
		long bytesPerColor = 0;
		unsigned char buffer[768];
		
		headerSize = 0;
		bytesRead = LFA_Read ( fileRef, buffer, GIF_SIGNATURE_LEN );
		if ( bytesRead != GIF_SIGNATURE_LEN ) return 0;
		if ( memcmp ( buffer, GIF_SIGNATURE_DATA, GIF_SIGNATURE_LEN) != 0 ) return 0;
		headerSize += bytesRead;
		bytesRead = LFA_Read ( fileRef, buffer, 3 );
		if ( bytesRead != 3 ) return 0;
		if ( memcmp ( buffer, "87a", 3 ) != 0 && memcmp ( buffer, "89a", 3 ) != 0 ) return 0;
		headerSize += bytesRead;
		bytesRead = LFA_Read ( fileRef, buffer, 4 );
		if ( bytesRead != 4 ) return 0;
		headerSize += bytesRead;
		bytesRead = LFA_Read ( fileRef, buffer, 3 );
		if ( bytesRead != 3 ) return 0;
		headerSize += bytesRead;
		if ( buffer[0] & 0x80 ) tableSize = (1 << ((buffer[0] & 0x07) + 1)) * 3;
		bytesRead = LFA_Read ( fileRef, buffer, tableSize );
		if ( bytesRead != tableSize ) return 0;
		headerSize += bytesRead;
		
		return headerSize;
	}

	// =============================================================================================

	bool  ReadBlock ( LFA_FileRef fileRef, BlockState & inOutBlockState, unsigned char * blockType, XMP_Uns32 * blockLength, XMP_Uns64 & inOutPosition )
	{
		try
		{
			XMP_Uns64 startPosition = inOutPosition;
			long bytesRead;
			long blockSize;
			unsigned char buffer[768];

			bytesRead = LFA_Read ( fileRef, buffer, 1 );
			if ( bytesRead != 1 ) return false;
			inOutPosition += 1;
			if ( buffer[0] == bImage )
			{
				// Image is a special case.
			        long tableSize = 0;
				bytesRead = LFA_Read ( fileRef, buffer, 4 );
				if ( bytesRead != 4 ) return false;
				inOutPosition += 4;
				bytesRead = LFA_Read ( fileRef, buffer, 4 );
				if ( bytesRead != 4 ) return false;
				inOutPosition += 4;
				bytesRead = LFA_Read ( fileRef, buffer, 1 );
				if ( bytesRead != 1 ) return false;
				inOutPosition += 1;
				if ( buffer[0] & 0x80 ) tableSize = (1 << ((buffer[0] & 0x07) + 1)) * 3;
				bytesRead = LFA_Read ( fileRef, buffer, tableSize );
				if ( bytesRead != tableSize ) return 0;
				inOutPosition += tableSize;
				bytesRead = LFA_Read ( fileRef, buffer, 1 );
				if ( bytesRead != 1 ) return false;
				inOutPosition += 1;
				bytesRead = LFA_Read ( fileRef, buffer, 1 );
				if ( bytesRead != 1 ) return false;
				inOutPosition += 1;
				tableSize = buffer[0];
				while ( tableSize != 0x00 )
				{
					bytesRead = LFA_Read ( fileRef, buffer, tableSize );
					if ( bytesRead != tableSize ) return false;
					inOutPosition += tableSize;
					bytesRead = LFA_Read ( fileRef, buffer, 1 );
					if ( bytesRead != 1 ) return false;
					inOutPosition += 1;
					tableSize = buffer[0];
				}

				BlockData newBlock;

				newBlock.pos = startPosition;
				newBlock.len = inOutPosition - startPosition;
				newBlock.type = bImage;

				inOutBlockState.blocks.push_back ( newBlock );
			}
			else if ( buffer[0] == bExtension )
			{
				unsigned char type;
				long tableSize = 0;

				BlockData newBlock;

				newBlock.pos = startPosition;

				bytesRead = LFA_Read ( fileRef, buffer, 1 );
				if ( bytesRead != 1 ) return false;
				inOutPosition += 1;
				type = buffer[0];
				newBlock.type = type;

				bytesRead = LFA_Read ( fileRef, buffer, 1 );
				if ( bytesRead != 1 ) return false;
				inOutPosition += 1;
				tableSize = buffer[0];
				while ( tableSize != 0x00 )
				{
					bytesRead = LFA_Read ( fileRef, buffer, tableSize );
					if ( bytesRead != tableSize ) return false;
					inOutPosition += tableSize;
					if ( inOutPosition - startPosition == 14 && type == bApplication )
					{
						// Check for XMP identifier...
						CheckApplicationBlockHeader ( fileRef, inOutBlockState, newBlock, inOutPosition );

						if ( newBlock.xmp == true )
						{
							newBlock.len = inOutPosition - startPosition;

							inOutBlockState.blocks.push_back ( newBlock );

							return true;
						}
					}
					bytesRead = LFA_Read ( fileRef, buffer, 1 );
					if ( bytesRead != 1 ) return false;
					inOutPosition += 1;
					tableSize = buffer[0];
				}

				newBlock.len = inOutPosition - startPosition;

				inOutBlockState.blocks.push_back ( newBlock );
			}
			else if ( buffer[0] == bTerminator )
			{
				BlockData newBlock;

				newBlock.pos = startPosition;
				newBlock.len = 1;
				newBlock.type = buffer[0];

				inOutBlockState.blocks.push_back ( newBlock );
			}

		} catch ( ... ) {

			return false;

		}
	
		return true;

	}

	// =============================================================================================

	bool WriteXMPBlock ( LFA_FileRef fileRef, XMP_Uns32 len, const char* inBuffer )
	{
		bool ret = false;
		unsigned long datalen = (APPLICATION_HEADER_LEN + len + MAGIC_TRAILER_LEN);
		unsigned char* buffer = new unsigned char[datalen];

		try
		{
			size_t pos = 0;
			memcpy(&buffer[pos], APPLICATION_HEADER_DATA, APPLICATION_HEADER_LEN);
			pos += APPLICATION_HEADER_LEN;
			memcpy(&buffer[pos], inBuffer, len);
			pos += len;
			memcpy(&buffer[pos], MAGIC_TRAILER_DATA, MAGIC_TRAILER_LEN);

			LFA_Write(fileRef, buffer, datalen);

			ret = true;
		}
		catch ( ... ) {}

		delete [] buffer;

		return ret;
	}

	// =============================================================================================

	bool CopyBlock ( LFA_FileRef sourceRef, LFA_FileRef destRef, BlockData& block )
	{
		try
		{
			LFA_Seek (sourceRef, block.pos, SEEK_SET );
			LFA_Copy (sourceRef, destRef, (block.len));

		} catch ( ... ) {

			return false;

		}
	
		return true;
	}

	// =============================================================================================

	// Don't need CRC.

	// =============================================================================================

	unsigned long CheckApplicationBlockHeader ( LFA_FileRef fileRef, BlockState& inOutBlockState, BlockData& inOutBlockData, XMP_Uns64& inOutPosition )
	{
		try
		{
			LFA_Seek(fileRef, (inOutBlockData.pos), SEEK_SET);

			unsigned char buffer[256];
			long bytesRead = LFA_Read ( fileRef, buffer, APPLICATION_HEADER_LEN );

			if (bytesRead == APPLICATION_HEADER_LEN)
			{
				if (memcmp(buffer, APPLICATION_HEADER_DATA, APPLICATION_HEADER_LEN) == 0)
				{
					// We still have to go through all of the data...
					long tableSize = 0;
					long xmpSize;
					
					inOutPosition = inOutBlockData.pos + APPLICATION_HEADER_LEN;
					inOutBlockState.xmpPos = inOutPosition;
					bytesRead = LFA_Read ( fileRef, buffer, 1 );
					if ( bytesRead != 1 ) return 0;
					inOutPosition += 1;
					tableSize = buffer[0];
					while ( tableSize != 0x00 )
					{
						bytesRead = LFA_Read ( fileRef, buffer, tableSize );
						if ( bytesRead != tableSize ) return false;
						inOutPosition += tableSize;
						bytesRead = LFA_Read ( fileRef, buffer, 1 );
						if ( bytesRead != 1 ) return false;
						inOutPosition += 1;
						tableSize = buffer[0];
					}
					inOutBlockState.xmpLen = inOutPosition - inOutBlockData.pos - APPLICATION_HEADER_LEN - MAGIC_TRAILER_LEN;
					inOutBlockState.xmpBlock = inOutBlockData;
					inOutBlockData.xmp = true;
				}
			}
		}
		catch ( ... ) {}
	
		return 0;
	}

	bool ReadBuffer ( LFA_FileRef fileRef, XMP_Uns64 & pos, XMP_Uns32 len, char * outBuffer )
	{
		try
		{
			if ( (fileRef == 0) || (outBuffer == 0) ) return false;
		
			LFA_Seek (fileRef, pos, SEEK_SET );
			long bytesRead = LFA_Read ( fileRef, outBuffer, len );
			if ( XMP_Uns32(bytesRead) != len ) return false;
		
			return true;
		}
		catch ( ... ) {}

		return false;
	}

	bool WriteBuffer ( LFA_FileRef fileRef, XMP_Uns64 & pos, XMP_Uns32 len, const char * inBuffer )
	{
		try
		{
			if ( (fileRef == 0) || (inBuffer == 0) ) return false;
		
			LFA_Seek (fileRef, pos, SEEK_SET );
			LFA_Write( fileRef, inBuffer, len );
		
			return true;
		}
		catch ( ... ) {}

		return false;
	}

} // namespace GIF_Support