BPicture: Fix archive constructor.
[haiku.git] / src / add-ons / translators / ico / ICOTranslator.cpp
blobb38de1d8e672f51be8412155b8168949b2fec332
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 */
7 #include "ICOTranslator.h"
9 #include <Catalog.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
14 #include <Catalog.h>
16 #include "ConfigView.h"
17 #include "ICO.h"
20 #undef B_TRANSLATION_CONTEXT
21 #define B_TRANSLATION_CONTEXT "ICOTranslator"
24 const char *kDocumentCount = "/documentCount";
25 const char *kDocumentIndex = "/documentIndex";
27 #define kICOMimeType "image/vnd.microsoft.icon"
28 #define kICOName "Windows icon"
29 // I'm lazy - structure initializers don't like const variables...
32 // The input formats that this translator supports.
33 static const translation_format sInputFormats[] = {
35 ICO_IMAGE_FORMAT,
36 B_TRANSLATOR_BITMAP,
37 ICO_IN_QUALITY,
38 ICO_IN_CAPABILITY,
39 kICOMimeType,
40 kICOName
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)"
52 // The output formats that this translator supports.
53 static const translation_format sOutputFormats[] = {
55 ICO_IMAGE_FORMAT,
56 B_TRANSLATOR_BITMAP,
57 ICO_OUT_QUALITY,
58 ICO_OUT_CAPABILITY,
59 kICOMimeType,
60 kICOName
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)"
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}
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);
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)
96 ICOTranslator::~ICOTranslator()
101 status_t
102 ICOTranslator::DerivedIdentify(BPositionIO *stream,
103 const translation_format *format, BMessage *ioExtension,
104 translator_info *info, uint32 outType)
106 if (!outType)
107 outType = B_TRANSLATOR_BITMAP;
108 if (outType != B_TRANSLATOR_BITMAP && outType != ICO_IMAGE_FORMAT)
109 return B_NO_TRANSLATOR;
111 int32 bitsPerPixel;
112 uint8 type;
113 if (ICO::identify(ioExtension, *stream, type, bitsPerPixel) != B_OK)
114 return B_NO_TRANSLATOR;
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),
121 B_TRANSLATE("Windows %s %ld bit image"),
122 type == ICO::kTypeIcon ? B_TRANSLATE("Icon") : B_TRANSLATE("Cursor"),
123 bitsPerPixel);
124 strcpy(info->MIME, kICOMimeType);
126 return B_OK;
130 status_t
131 ICOTranslator::DerivedTranslate(BPositionIO *source,
132 const translator_info *info, BMessage *ioExtension,
133 uint32 outType, BPositionIO *target, int32 baseType)
135 if (!outType)
136 outType = B_TRANSLATOR_BITMAP;
137 if (outType != B_TRANSLATOR_BITMAP && outType != ICO_IMAGE_FORMAT)
138 return B_NO_TRANSLATOR;
140 switch (baseType) {
141 case 1:
143 if (outType != ICO_IMAGE_FORMAT)
144 return B_NO_TRANSLATOR;
146 // Source is in bits format - this has to be done here, because
147 // identify_bits_header() is a member of the BaseTranslator class...
148 TranslatorBitmap bitsHeader;
149 status_t status = identify_bits_header(source, NULL, &bitsHeader);
150 if (status != B_OK)
151 return status;
153 return ICO::convert_bits_to_ico(ioExtension, *source, bitsHeader, *target);
156 case 0:
158 // source is NOT in bits format
159 if (outType != B_TRANSLATOR_BITMAP)
160 return B_NO_TRANSLATOR;
162 return ICO::convert_ico_to_bits(ioExtension, *source, *target);
165 default:
166 return B_NO_TRANSLATOR;
171 status_t
172 ICOTranslator::DerivedCanHandleImageSize(float width, float height) const
174 if (!ICO::is_valid_size((int)width) || !ICO::is_valid_size((int)height))
175 return B_NO_TRANSLATOR;
176 return B_OK;
180 BView *
181 ICOTranslator::NewConfigView(TranslatorSettings *settings)
183 return new ConfigView();
187 // #pragma mark -
190 BTranslator *
191 make_nth_translator(int32 n, image_id you, uint32 flags, ...)
193 if (n != 0)
194 return NULL;
196 return new ICOTranslator();