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