xref: /haiku/src/add-ons/translators/ico/ICOTranslator.cpp (revision 23d878482ed22e55dad6d1fca1df7bea42eb157c)
1 /*
2  * Copyright 2005-2006, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "ICOTranslator.h"
8 
9 #include <Catalog.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 
14 #include <Catalog.h>
15 
16 #include "ConfigView.h"
17 #include "ICO.h"
18 
19 
20 #undef B_TRANSLATION_CONTEXT
21 #define B_TRANSLATION_CONTEXT "ICOTranslator"
22 
23 
24 const char *kDocumentCount = "/documentCount";
25 const char *kDocumentIndex = "/documentIndex";
26 
27 #define kICOMimeType "image/vnd.microsoft.icon"
28 	// I'm lazy - structure initializers don't like const variables...
29 
30 
31 // The input formats that this translator supports.
32 static const translation_format sInputFormats[] = {
33 	{
34 		ICO_IMAGE_FORMAT,
35 		B_TRANSLATOR_BITMAP,
36 		ICO_IN_QUALITY,
37 		ICO_IN_CAPABILITY,
38 		kICOMimeType,
39 		"Windows Icon image"
40 	},
41 	{
42 		B_TRANSLATOR_BITMAP,
43 		B_TRANSLATOR_BITMAP,
44 		BITS_IN_QUALITY,
45 		BITS_IN_CAPABILITY,
46 		"image/x-be-bitmap",
47 		"Be Bitmap Format (ICOTranslator)"
48 	},
49 };
50 
51 // The output formats that this translator supports.
52 static const translation_format sOutputFormats[] = {
53 	{
54 		ICO_IMAGE_FORMAT,
55 		B_TRANSLATOR_BITMAP,
56 		ICO_OUT_QUALITY,
57 		ICO_OUT_CAPABILITY,
58 		kICOMimeType,
59 		"Windows Icon image"
60 	},
61 	{
62 		B_TRANSLATOR_BITMAP,
63 		B_TRANSLATOR_BITMAP,
64 		BITS_OUT_QUALITY,
65 		BITS_OUT_CAPABILITY,
66 		"image/x-be-bitmap",
67 		"Be Bitmap Format (ICOTranslator)"
68 	},
69 };
70 
71 // Default settings for the Translator
72 static const TranSetting sDefaultSettings[] = {
73 	{B_TRANSLATOR_EXT_HEADER_ONLY, TRAN_SETTING_BOOL, false},
74 	{B_TRANSLATOR_EXT_DATA_ONLY, TRAN_SETTING_BOOL, false}
75 };
76 
77 const uint32 kNumInputFormats = sizeof(sInputFormats) / sizeof(translation_format);
78 const uint32 kNumOutputFormats = sizeof(sOutputFormats) / sizeof(translation_format);
79 const uint32 kNumDefaultSettings = sizeof(sDefaultSettings) / sizeof(TranSetting);
80 
81 
82 ICOTranslator::ICOTranslator()
83 	: BaseTranslator(B_TRANSLATE("Windows icon images"),
84 		B_TRANSLATE("Windows icon translator"),
85 		ICO_TRANSLATOR_VERSION,
86 		sInputFormats, kNumInputFormats,
87 		sOutputFormats, kNumOutputFormats,
88 		"ICOTranslator_Settings",
89 		sDefaultSettings, kNumDefaultSettings,
90 		B_TRANSLATOR_BITMAP, ICO_IMAGE_FORMAT)
91 {
92 }
93 
94 
95 ICOTranslator::~ICOTranslator()
96 {
97 }
98 
99 
100 status_t
101 ICOTranslator::DerivedIdentify(BPositionIO *stream,
102 	const translation_format *format, BMessage *ioExtension,
103 	translator_info *info, uint32 outType)
104 {
105 	if (!outType)
106 		outType = B_TRANSLATOR_BITMAP;
107 	if (outType != B_TRANSLATOR_BITMAP && outType != ICO_IMAGE_FORMAT)
108 		return B_NO_TRANSLATOR;
109 
110 	int32 bitsPerPixel;
111 	uint8 type;
112 	if (ICO::identify(ioExtension, *stream, type, bitsPerPixel) != B_OK)
113 		return B_NO_TRANSLATOR;
114 
115 	info->type = ICO_IMAGE_FORMAT;
116 	info->group = B_TRANSLATOR_BITMAP;
117 	info->quality = ICO_IN_QUALITY;
118 	info->capability = ICO_IN_CAPABILITY;
119 	snprintf(info->name, sizeof(info->name),
120 		B_TRANSLATE("Windows %s %ld bit image"),
121 		type == ICO::kTypeIcon ? B_TRANSLATE("Icon") : B_TRANSLATE("Cursor"),
122 		bitsPerPixel);
123 	strcpy(info->MIME, kICOMimeType);
124 
125 	return B_OK;
126 }
127 
128 
129 status_t
130 ICOTranslator::DerivedTranslate(BPositionIO *source,
131 	const translator_info *info, BMessage *ioExtension,
132 	uint32 outType, BPositionIO *target, int32 baseType)
133 {
134 	if (!outType)
135 		outType = B_TRANSLATOR_BITMAP;
136 	if (outType != B_TRANSLATOR_BITMAP && outType != ICO_IMAGE_FORMAT)
137 		return B_NO_TRANSLATOR;
138 
139 	switch (baseType) {
140 		case 1:
141 		{
142 			if (outType != ICO_IMAGE_FORMAT)
143 				return B_NO_TRANSLATOR;
144 
145 			// Source is in bits format - this has to be done here, because
146 			// identify_bits_header() is a member of the BaseTranslator class...
147 			TranslatorBitmap bitsHeader;
148 			status_t status = identify_bits_header(source, NULL, &bitsHeader);
149 			if (status != B_OK)
150 				return status;
151 
152 			return ICO::convert_bits_to_ico(ioExtension, *source, bitsHeader, *target);
153 		}
154 
155 		case 0:
156 		{
157 			// source is NOT in bits format
158 			if (outType != B_TRANSLATOR_BITMAP)
159 				return B_NO_TRANSLATOR;
160 
161 			return ICO::convert_ico_to_bits(ioExtension, *source, *target);
162 		}
163 
164 		default:
165 			return B_NO_TRANSLATOR;
166 	}
167 }
168 
169 
170 status_t
171 ICOTranslator::DerivedCanHandleImageSize(float width, float height) const
172 {
173 	if (!ICO::is_valid_size((int)width) || !ICO::is_valid_size((int)height))
174 		return B_NO_TRANSLATOR;
175 	return B_OK;
176 }
177 
178 
179 BView *
180 ICOTranslator::NewConfigView(TranslatorSettings *settings)
181 {
182 	return new ConfigView();
183 }
184 
185 
186 //	#pragma mark -
187 
188 
189 BTranslator *
190 make_nth_translator(int32 n, image_id you, uint32 flags, ...)
191 {
192 	if (n != 0)
193 		return NULL;
194 
195 	return new ICOTranslator();
196 }
197 
198