xref: /haiku/src/add-ons/translators/gif/GIFLoad.h (revision cda5b8808fd0262f0fac472f6cfa809f846a83cf)
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 //	File: GIFLoad.h
4 //
5 //	Date: December 1999
6 //
7 //	Author: Daniel Switkin
8 //
9 //	Copyright 2003 (c) by Daniel Switkin. This file is made publically available
10 //	under the BSD license, with the stipulations that this complete header must
11 //	remain at the top of the file indefinitely, and credit must be given to the
12 //	original author in any about box using this software.
13 //
14 ////////////////////////////////////////////////////////////////////////////////
15 
16 #ifndef GIFLOAD_H
17 #define GIFLOAD_H
18 
19 #include <DataIO.h>
20 #include "LoadPalette.h"
21 
22 class Memblock {
23 	public:
24 		uchar data[4096];
25 		int offset;
26 		Memblock *next;
27 };
28 
29 const int gl_pass_starts_at[] = {0, 4, 2, 1, 0};
30 const int gl_increment_pass_by[] = {8, 8, 4, 2, 0};
31 
32 class GIFLoad {
33 	public:
34 		GIFLoad(BPositionIO *input, BPositionIO *output);
35 		~GIFLoad();
36 		bool fatalerror;
37 
38 	private:
39 		bool ReadGIFHeader();
40 		bool ReadGIFLoopBlock();
41 		bool ReadGIFControlBlock();
42 		bool ReadGIFImageHeader();
43 		bool ReadGIFImageData();
44 		bool ReadGIFCommentBlock();
45 		bool ReadGIFUnknownBlock(unsigned char c);
46 
47 		void Init();
48 		bool InitFrame(int size);
49 		short NextCode();
50 		void ResetTable();
51 
52 		uchar *MemblockAllocate(int size);
53 		void MemblockDeleteAll();
54 
55 		inline bool OutputColor(unsigned char *string, int size) {
56 			int bpr = width << 2;
57 
58 			for (int x = 0; x < size; x++) {
59 				scanline[scanline_position] = palette->ColorForIndex(string[x]);
60 				scanline_position++;
61 
62 				if (scanline_position >= width) {
63 					if (output->WriteAt(32 + (row * bpr), scanline, bpr) < bpr) return false;
64 					scanline_position = 0;
65 					if (interlaced) {
66 						row += gl_increment_pass_by[pass];
67 						while (row >= height) {
68 							pass++;
69 							if (pass > 3) return true;
70 							row = gl_pass_starts_at[pass];
71 						}
72 					} else row++;
73 				}
74 			}
75 			return true;
76 		}
77 
78 		BPositionIO *input, *output;
79 		LoadPalette *palette;
80 		bool interlaced;
81 		int pass, row, width, height;
82 
83 		unsigned char old_code[4096];
84 		int old_code_length;
85 		short new_code;
86 		int BITS, max_code, code_size;
87 		short clear_code, end_code, next_code;
88 
89 		unsigned char *table[4096];
90 		short entry_size[4096];
91 		Memblock *head_memblock;
92 
93 		int bit_count;
94 		unsigned int bit_buffer;
95 		unsigned char byte_count;
96 		unsigned char byte_buffer[255];
97 
98 		uint32 *scanline;
99 		int scanline_position;
100 };
101 
102 #endif
103 
104