xref: /haiku/src/add-ons/translators/gif/GIFTranslator.cpp (revision 62f5ba006a08b0df30631375878effaf67ae5dbc)
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 Format (GIFTranslator)" },
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 Format (GIFTranslator)" },
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 	GIFView *gifview = new GIFView("TranslatorView");
65 	*outView = gifview;
66 	gifview->ResizeTo(gifview->ExplicitPreferredSize());
67 	*outExtent = gifview->Bounds();
68 	return B_OK;
69 }
70 
71 
72 /* Look at first few bytes in stream to determine type - throw it back
73    if it is not a GIF or a BBitmap that we understand */
74 bool
75 DetermineType(BPositionIO *source, bool *is_gif)
76 {
77 	unsigned char header[7];
78 	*is_gif = true;
79 	if (source->Read(header, 6) != 6) return false;
80 	header[6] = 0x00;
81 
82 	if (strcmp((char *)header, "GIF87a") != 0 && strcmp((char *)header, "GIF89a") != 0) {
83 		*is_gif = false;
84 		int32 magic = (header[0] << 24) + (header[1] << 16) + (header[2] << 8) + header[3];
85 		if (magic != B_TRANSLATOR_BITMAP) return false;
86 		source->Seek(5 * 4 - 2, SEEK_CUR);
87 		color_space cs;
88 		if (source->Read(&cs, 4) != 4) return false;
89 		cs = (color_space)B_BENDIAN_TO_HOST_INT32(cs);
90 		if (cs != B_RGB32 && cs != B_RGBA32 && cs != B_RGB32_BIG && cs != B_RGBA32_BIG) return false;
91 	}
92 
93 	source->Seek(0, SEEK_SET);
94 	return true;
95 }
96 
97 
98 /* Dump data from stream into a BBitmap */
99 status_t
100 GetBitmap(BPositionIO *in, BBitmap **out)
101 {
102 	TranslatorBitmap header;
103 
104 	status_t err = in->Read(&header, sizeof(header));
105 	if (err != sizeof(header))
106 		return B_IO_ERROR;
107 
108 	header.magic = B_BENDIAN_TO_HOST_INT32(header.magic);
109 	header.bounds.left = B_BENDIAN_TO_HOST_FLOAT(header.bounds.left);
110 	header.bounds.top = B_BENDIAN_TO_HOST_FLOAT(header.bounds.top);
111 	header.bounds.right = B_BENDIAN_TO_HOST_FLOAT(header.bounds.right);
112 	header.bounds.bottom = B_BENDIAN_TO_HOST_FLOAT(header.bounds.bottom);
113 	header.rowBytes = B_BENDIAN_TO_HOST_INT32(header.rowBytes);
114 	header.colors = (color_space)B_BENDIAN_TO_HOST_INT32(header.colors);
115 	header.dataSize = B_BENDIAN_TO_HOST_INT32(header.dataSize);
116 
117 	BBitmap *bitmap = new BBitmap(header.bounds, header.colors);
118 	*out = bitmap;
119 	if (bitmap == NULL) return B_NO_MEMORY;
120 	unsigned char *bits = (unsigned char *)bitmap->Bits();
121 	if (bits == NULL) {
122 		delete bitmap;
123 		return B_NO_MEMORY;
124 	}
125 	err = in->Read(bits, header.dataSize);
126 	if (err == (status_t)header.dataSize) return B_OK;
127 	else return B_IO_ERROR;
128 }
129 
130 
131 /* Required Identify function - may need to read entire header, not sure */
132 status_t
133 Identify(BPositionIO *inSource, const translation_format *inFormat,
134 	BMessage *ioExtension, translator_info *outInfo, uint32 outType)
135 {
136 
137 	const char *debug_text = getenv("GIF_TRANSLATOR_DEBUG");
138 	if ((debug_text != NULL) && (atoi(debug_text) != 0)) debug = true;
139 
140 	if (outType == 0) outType = B_TRANSLATOR_BITMAP;
141 	if (outType != GIF_TYPE && outType != B_TRANSLATOR_BITMAP) return B_NO_TRANSLATOR;
142 
143 	bool is_gif;
144 	if (!DetermineType(inSource, &is_gif)) return B_NO_TRANSLATOR;
145 	if (!is_gif && inFormat != NULL && inFormat->type != B_TRANSLATOR_BITMAP)
146 		return B_NO_TRANSLATOR;
147 
148 	outInfo->group = B_TRANSLATOR_BITMAP;
149 	if (is_gif) {
150 		outInfo->type = GIF_TYPE;
151 		outInfo->quality = 0.8;
152 		outInfo->capability = 0.8;
153 		strcpy(outInfo->name, "GIF image");
154 		strcpy(outInfo->MIME, "image/gif");
155 	}
156 	else {
157 		outInfo->type = B_TRANSLATOR_BITMAP;
158 		outInfo->quality = 0.3;
159 		outInfo->capability = 0.3;
160 		strcpy(outInfo->name, "Be Bitmap Format (GIFTranslator)");
161 		strcpy(outInfo->MIME, "image/x-be-bitmap");
162 	}
163 	return B_OK;
164 }
165 
166 
167 /* Main required function - assumes that an incoming GIF must be translated
168    to a BBitmap, and vice versa - this could be improved */
169 status_t
170 Translate(BPositionIO *inSource, const translator_info *inInfo,
171 	BMessage *ioExtension, uint32 outType, BPositionIO *outDestination)
172 {
173 
174 	const char *debug_text = getenv("GIF_TRANSLATOR_DEBUG");
175 	if ((debug_text != NULL) && (atoi(debug_text) != 0)) debug = true;
176 
177 	if (outType == 0) outType = B_TRANSLATOR_BITMAP;
178 	if (outType != GIF_TYPE && outType != B_TRANSLATOR_BITMAP) {
179 		return B_NO_TRANSLATOR;
180 	}
181 
182 	bool is_gif;
183 	if (!DetermineType(inSource, &is_gif)) return B_NO_TRANSLATOR;
184 	if (!is_gif && inInfo->type != B_TRANSLATOR_BITMAP) return B_NO_TRANSLATOR;
185 
186 	status_t err = B_OK;
187 	bigtime_t now = system_time();
188 	// Going from BBitmap to GIF
189 	if (!is_gif) {
190 		BBitmap *bitmap = NULL;
191 		err = GetBitmap(inSource, &bitmap);
192 		if (err != B_OK)
193 			return err;
194 		GIFSave *gs = new GIFSave(bitmap, outDestination);
195 		if (gs->fatalerror) {
196 			delete gs;
197 			delete bitmap;
198 			return B_NO_MEMORY;
199 		}
200 		delete gs;
201 		delete bitmap;
202 	} else { // GIF to BBitmap
203 		GIFLoad *gl = new GIFLoad(inSource, outDestination);
204 		if (gl->fatalerror) {
205 			delete gl;
206 			return B_NO_MEMORY;
207 		}
208 		delete gl;
209 	}
210 
211 	if (debug) {
212 		now = system_time() - now;
213 		printf("Translate() - Translation took %Ld microseconds\n", now);
214 	}
215 	return B_OK;
216 }
217 
218 
219 GIFTranslator::GIFTranslator()
220 	: BApplication("application/x-vnd.Haiku-GIFTranslator")
221 {
222 	BRect rect(100, 100, 339, 339);
223 	gifwindow = new GIFWindow(rect, "GIF Settings");
224 	gifwindow->Show();
225 }
226 
227 
228 int
229 main()
230 {
231 	GIFTranslator myapp;
232 	myapp.Run();
233 	return 0;
234 }
235 
236