xref: /haiku/src/add-ons/translators/ico/ICOTranslator.cpp (revision 7eab6b486ebadb54ca3c306601f4b04dd92359fa)
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 #define kICOName "Windows icon"
29 	// I'm lazy - structure initializers don't like const variables...
30 
31 
32 // The input formats that this translator supports.
33 static const translation_format sInputFormats[] = {
34 	{
35 		ICO_IMAGE_FORMAT,
36 		B_TRANSLATOR_BITMAP,
37 		ICO_IN_QUALITY,
38 		ICO_IN_CAPABILITY,
39 		kICOMimeType,
40 		kICOName
41 	},
42 	{
43 		B_TRANSLATOR_BITMAP,
44 		B_TRANSLATOR_BITMAP,
45 		BITS_IN_QUALITY,
46 		BITS_IN_CAPABILITY,
47 		"image/x-be-bitmap",
48 		"Be Bitmap Format (ICOTranslator)"
49 	},
50 };
51 
52 // The output formats that this translator supports.
53 static const translation_format sOutputFormats[] = {
54 	{
55 		ICO_IMAGE_FORMAT,
56 		B_TRANSLATOR_BITMAP,
57 		ICO_OUT_QUALITY,
58 		ICO_OUT_CAPABILITY,
59 		kICOMimeType,
60 		kICOName
61 	},
62 	{
63 		B_TRANSLATOR_BITMAP,
64 		B_TRANSLATOR_BITMAP,
65 		BITS_OUT_QUALITY,
66 		BITS_OUT_CAPABILITY,
67 		"image/x-be-bitmap",
68 		"Be Bitmap Format (ICOTranslator)"
69 	},
70 };
71 
72 // Default settings for the Translator
73 static const TranSetting sDefaultSettings[] = {
74 	{B_TRANSLATOR_EXT_HEADER_ONLY, TRAN_SETTING_BOOL, false},
75 	{B_TRANSLATOR_EXT_DATA_ONLY, TRAN_SETTING_BOOL, false}
76 };
77 
78 const uint32 kNumInputFormats = sizeof(sInputFormats) / sizeof(translation_format);
79 const uint32 kNumOutputFormats = sizeof(sOutputFormats) / sizeof(translation_format);
80 const uint32 kNumDefaultSettings = sizeof(sDefaultSettings) / sizeof(TranSetting);
81 
82 
ICOTranslator()83 ICOTranslator::ICOTranslator()
84 	: BaseTranslator(B_TRANSLATE("Windows icons"),
85 		B_TRANSLATE("Windows icon translator"),
86 		ICO_TRANSLATOR_VERSION,
87 		sInputFormats, kNumInputFormats,
88 		sOutputFormats, kNumOutputFormats,
89 		"ICOTranslator_Settings",
90 		sDefaultSettings, kNumDefaultSettings,
91 		B_TRANSLATOR_BITMAP, ICO_IMAGE_FORMAT)
92 {
93 }
94 
95 
~ICOTranslator()96 ICOTranslator::~ICOTranslator()
97 {
98 }
99 
100 
101 status_t
DerivedIdentify(BPositionIO * stream,const translation_format * format,BMessage * ioExtension,translator_info * info,uint32 outType)102 ICOTranslator::DerivedIdentify(BPositionIO *stream,
103 	const translation_format *format, BMessage *ioExtension,
104 	translator_info *info, uint32 outType)
105 {
106 	if (!outType)
107 		outType = B_TRANSLATOR_BITMAP;
108 	if (outType != B_TRANSLATOR_BITMAP && outType != ICO_IMAGE_FORMAT)
109 		return B_NO_TRANSLATOR;
110 
111 	int32 bitsPerPixel;
112 	uint8 type;
113 	if (ICO::identify(ioExtension, *stream, type, bitsPerPixel) != B_OK)
114 		return B_NO_TRANSLATOR;
115 
116 	info->type = ICO_IMAGE_FORMAT;
117 	info->group = B_TRANSLATOR_BITMAP;
118 	info->quality = ICO_IN_QUALITY;
119 	info->capability = ICO_IN_CAPABILITY;
120 	snprintf(info->name, sizeof(info->name), B_TRANSLATE("Windows %s %ld bit image"),
121 		type == ICO::kTypeIcon ? B_TRANSLATE("Icon") : B_TRANSLATE("Cursor"),
122 		(long int)bitsPerPixel);
123 	strcpy(info->MIME, kICOMimeType);
124 
125 	return B_OK;
126 }
127 
128 
129 status_t
DerivedTranslate(BPositionIO * source,const translator_info * info,BMessage * ioExtension,uint32 outType,BPositionIO * target,int32 baseType)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
DerivedCanHandleImageSize(float width,float height) const171 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 *
NewConfigView(TranslatorSettings * settings)180 ICOTranslator::NewConfigView(TranslatorSettings *settings)
181 {
182 	return new ConfigView();
183 }
184 
185 
186 //	#pragma mark -
187 
188 
189 BTranslator *
make_nth_translator(int32 n,image_id you,uint32 flags,...)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