xref: /haiku/src/add-ons/translators/gif/GIFTranslator.cpp (revision ed6250c95736c0b55da79d6e9dd01369532260c0)
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 //	File: GIFTranslator.cpp
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 #include "GIFTranslator.h"
17 #include "GIFWindow.h"
18 #include "GIFView.h"
19 #include "GIFSave.h"
20 #include "GIFLoad.h"
21 #include <ByteOrder.h>
22 #include <TypeConstants.h>
23 #include <DataIO.h>
24 #include <TranslatorAddOn.h>
25 #include <TranslatorFormats.h>
26 #include <InterfaceDefs.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #ifndef GIF_TYPE
32 #define GIF_TYPE 'GIF '
33 #endif
34 
35 // This global will be externed in other files - set once here
36 // for the entire translator
37 bool debug = false;
38 
39 bool DetermineType(BPositionIO *source, bool *is_gif);
40 status_t GetBitmap(BPositionIO *in, BBitmap **out);
41 
42 /* Required data */
43 char translatorName[] = "GIF Images";
44 char translatorInfo[] = "GIF image translator v1.4";
45 int32 translatorVersion = 0x140;
46 
47 translation_format inputFormats[] = {
48 	{ GIF_TYPE, B_TRANSLATOR_BITMAP, 0.8, 0.8, "image/gif", "GIF image" },
49 	{ B_TRANSLATOR_BITMAP, B_TRANSLATOR_BITMAP, 0.3, 0.3, "image/x-be-bitmap", "Be Bitmap image" },
50 	{ 0 }
51 };
52 
53 translation_format outputFormats[] = {
54 	{ GIF_TYPE, B_TRANSLATOR_BITMAP, 0.8, 0.8, "image/gif", "GIF image" },
55 	{ B_TRANSLATOR_BITMAP, B_TRANSLATOR_BITMAP, 0.3, 0.3, "image/x-be-bitmap", "Be Bitmap image" },
56 	{ 0 }
57 };
58 
59 
60 /* Build a pretty view for DataTranslations */
61 status_t
62 MakeConfig(BMessage *ioExtension, BView **outView, BRect *outExtent)
63 {
64 	outExtent->Set(0, 0, 239, 239);
65 	GIFView *gifview = new GIFView(*outExtent, "TranslatorView");
66 	*outView = gifview;
67 	return B_OK;
68 }
69 
70 
71 /* Look at first few bytes in stream to determine type - throw it back
72    if it is not a GIF or a BBitmap that we understand */
73 bool
74 DetermineType(BPositionIO *source, bool *is_gif)
75 {
76 	unsigned char header[7];
77 	*is_gif = true;
78 	if (source->Read(header, 6) != 6) return false;
79 	header[6] = 0x00;
80 
81 	if (strcmp((char *)header, "GIF87a") != 0 && strcmp((char *)header, "GIF89a") != 0) {
82 		*is_gif = false;
83 		int32 magic = (header[0] << 24) + (header[1] << 16) + (header[2] << 8) + header[3];
84 		if (magic != B_TRANSLATOR_BITMAP) return false;
85 		source->Seek(5 * 4 - 2, SEEK_CUR);
86 		color_space cs;
87 		if (source->Read(&cs, 4) != 4) return false;
88 		cs = (color_space)B_BENDIAN_TO_HOST_INT32(cs);
89 		if (cs != B_RGB32 && cs != B_RGBA32 && cs != B_RGB32_BIG && cs != B_RGBA32_BIG) return false;
90 	}
91 
92 	source->Seek(0, SEEK_SET);
93 	return true;
94 }
95 
96 
97 /* Dump data from stream into a BBitmap */
98 status_t
99 GetBitmap(BPositionIO *in, BBitmap **out)
100 {
101 	TranslatorBitmap header;
102 
103 	status_t err = in->Read(&header, sizeof(header));
104 	if (err != sizeof(header))
105 		return B_IO_ERROR;
106 
107 	header.magic = B_BENDIAN_TO_HOST_INT32(header.magic);
108 	header.bounds.left = B_BENDIAN_TO_HOST_FLOAT(header.bounds.left);
109 	header.bounds.top = B_BENDIAN_TO_HOST_FLOAT(header.bounds.top);
110 	header.bounds.right = B_BENDIAN_TO_HOST_FLOAT(header.bounds.right);
111 	header.bounds.bottom = B_BENDIAN_TO_HOST_FLOAT(header.bounds.bottom);
112 	header.rowBytes = B_BENDIAN_TO_HOST_INT32(header.rowBytes);
113 	header.colors = (color_space)B_BENDIAN_TO_HOST_INT32(header.colors);
114 	header.dataSize = B_BENDIAN_TO_HOST_INT32(header.dataSize);
115 
116 	BBitmap *bitmap = new BBitmap(header.bounds, header.colors);
117 	*out = bitmap;
118 	if (bitmap == NULL) return B_NO_MEMORY;
119 	unsigned char *bits = (unsigned char *)bitmap->Bits();
120 	if (bits == NULL) {
121 		delete bitmap;
122 		return B_NO_MEMORY;
123 	}
124 	err = in->Read(bits, header.dataSize);
125 	if (err == (status_t)header.dataSize) return B_OK;
126 	else return B_IO_ERROR;
127 }
128 
129 
130 /* Required Identify function - may need to read entire header, not sure */
131 status_t
132 Identify(BPositionIO *inSource, const translation_format *inFormat,
133 	BMessage *ioExtension, translator_info *outInfo, uint32 outType)
134 {
135 
136 	const char *debug_text = getenv("GIF_TRANSLATOR_DEBUG");
137 	if ((debug_text != NULL) && (atoi(debug_text) != 0)) debug = true;
138 
139 	if (outType == 0) outType = B_TRANSLATOR_BITMAP;
140 	if (outType != GIF_TYPE && outType != B_TRANSLATOR_BITMAP) return B_NO_TRANSLATOR;
141 
142 	bool is_gif;
143 	if (!DetermineType(inSource, &is_gif)) return B_NO_TRANSLATOR;
144 	if (!is_gif && inFormat != NULL && inFormat->type != B_TRANSLATOR_BITMAP)
145 		return B_NO_TRANSLATOR;
146 
147 	outInfo->group = B_TRANSLATOR_BITMAP;
148 	if (is_gif) {
149 		outInfo->type = GIF_TYPE;
150 		outInfo->quality = 0.8;
151 		outInfo->capability = 0.8;
152 		strcpy(outInfo->name, "GIF image");
153 		strcpy(outInfo->MIME, "image/gif");
154 	}
155 	else {
156 		outInfo->type = B_TRANSLATOR_BITMAP;
157 		outInfo->quality = 0.3;
158 		outInfo->capability = 0.3;
159 		strcpy(outInfo->name, "Be Bitmap image");
160 		strcpy(outInfo->MIME, "image/x-be-bitmap");
161 	}
162 	return B_OK;
163 }
164 
165 
166 /* Main required function - assumes that an incoming GIF must be translated
167    to a BBitmap, and vice versa - this could be improved */
168 status_t
169 Translate(BPositionIO *inSource, const translator_info *inInfo,
170 	BMessage *ioExtension, uint32 outType, BPositionIO *outDestination)
171 {
172 
173 	const char *debug_text = getenv("GIF_TRANSLATOR_DEBUG");
174 	if ((debug_text != NULL) && (atoi(debug_text) != 0)) debug = true;
175 
176 	if (outType == 0) outType = B_TRANSLATOR_BITMAP;
177 	if (outType != GIF_TYPE && outType != B_TRANSLATOR_BITMAP) {
178 		return B_NO_TRANSLATOR;
179 	}
180 
181 	bool is_gif;
182 	if (!DetermineType(inSource, &is_gif)) return B_NO_TRANSLATOR;
183 	if (!is_gif && inInfo->type != B_TRANSLATOR_BITMAP) return B_NO_TRANSLATOR;
184 
185 	status_t err = B_OK;
186 	bigtime_t now = system_time();
187 	// Going from BBitmap to GIF
188 	if (!is_gif) {
189 		BBitmap **b = (BBitmap **)malloc(4);
190 		*b = NULL;
191 		err = GetBitmap(inSource, b);
192 		if (err != B_OK) return err;
193 		GIFSave *gs = new GIFSave(*b, outDestination);
194 		if (gs->fatalerror) {
195 			delete gs;
196 			if (*b != NULL) delete *b;
197 			delete b;
198 			return B_NO_MEMORY;
199 		}
200 		delete gs;
201 		delete *b;
202 		delete b;
203 	} else { // GIF to BBitmap
204 		GIFLoad *gl = new GIFLoad(inSource, outDestination);
205 		if (gl->fatalerror) {
206 			delete gl;
207 			return B_NO_MEMORY;
208 		}
209 		delete gl;
210 	}
211 
212 	if (debug) {
213 		now = system_time() - now;
214 		printf("Translate() - Translation took %Ld microseconds\n", now);
215 	}
216 	return B_OK;
217 }
218 
219 
220 GIFTranslator::GIFTranslator()
221 	: BApplication("application/x-vnd.Jules-GIFTranslator")
222 {
223 	BRect rect(100, 100, 339, 339);
224 	gifwindow = new GIFWindow(rect, "GIF Settings");
225 	gifwindow->Show();
226 }
227 
228 
229 int
230 main()
231 {
232 	GIFTranslator myapp;
233 	myapp.Run();
234 	return 0;
235 }
236 
237