2 * Copyright 2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 * Copyright 2012, Gerasim Troeglazov, 3dEyes@gmail.com. All rights reserved.
4 * Distributed under the terms of the MIT License.
7 #include "ICNSTranslator.h"
13 #include "ConfigView.h"
14 #include "ICNSLoader.h"
18 #undef B_TRANSLATION_CONTEXT
19 #define B_TRANSLATION_CONTEXT "ICNSTranslator"
25 const char *kDocumentCount
= "/documentCount";
26 const char *kDocumentIndex
= "/documentIndex";
28 #define kICNSMimeType "image/icns"
29 #define kICNSName "Apple Icon"
31 // The input formats that this translator supports.
32 static const translation_format sInputFormats
[] = {
47 "Be Bitmap Format (ICNSTranslator)"
51 // The output formats that this translator supports.
52 static const translation_format sOutputFormats
[] = {
67 "Be Bitmap Format (ICNSTranslator)"
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}
77 const uint32 kNumInputFormats
= sizeof(sInputFormats
)
78 / sizeof(translation_format
);
79 const uint32 kNumOutputFormats
= sizeof(sOutputFormats
)
80 / sizeof(translation_format
);
81 const uint32 kNumDefaultSettings
= sizeof(sDefaultSettings
)
82 / sizeof(TranSetting
);
85 ICNSTranslator::ICNSTranslator()
86 : BaseTranslator(B_TRANSLATE("Apple icons"),
87 B_TRANSLATE("Apple icon translator"),
88 ICNS_TRANSLATOR_VERSION
,
89 sInputFormats
, kNumInputFormats
,
90 sOutputFormats
, kNumOutputFormats
,
92 sDefaultSettings
, kNumDefaultSettings
,
93 B_TRANSLATOR_BITMAP
, ICNS_IMAGE_FORMAT
)
98 ICNSTranslator::~ICNSTranslator()
104 ICNSTranslator::DerivedIdentify(BPositionIO
*stream
,
105 const translation_format
*format
, BMessage
*ioExtension
,
106 translator_info
*info
, uint32 outType
)
109 outType
= B_TRANSLATOR_BITMAP
;
110 if (outType
!= B_TRANSLATOR_BITMAP
&& outType
!= ICNS_IMAGE_FORMAT
)
111 return B_NO_TRANSLATOR
;
113 uint32 signatureData
;
114 ssize_t signatureSize
= 4;
115 if (stream
->Read(&signatureData
, signatureSize
) != signatureSize
)
118 const uint32 kICNSMagic
= B_HOST_TO_BENDIAN_INT32('icns');
120 if (signatureData
!= kICNSMagic
)
121 return B_ILLEGAL_DATA
;
124 ICNSLoader
icnsFile(stream
);
125 if(!icnsFile
.IsLoaded() || icnsFile
.IconsCount() <= 0)
126 return B_ILLEGAL_DATA
;
128 int32 documentCount
= icnsFile
.IconsCount();
129 int32 documentIndex
= 1;
132 if (ioExtension
->FindInt32(DOCUMENT_INDEX
, &documentIndex
) != B_OK
)
134 if (documentIndex
< 1 || documentIndex
> documentCount
)
135 return B_NO_TRANSLATOR
;
139 ioExtension
->RemoveName(DOCUMENT_COUNT
);
140 ioExtension
->AddInt32(DOCUMENT_COUNT
, documentCount
);
143 info
->type
= ICNS_IMAGE_FORMAT
;
144 info
->group
= B_TRANSLATOR_BITMAP
;
145 info
->quality
= ICNS_IN_QUALITY
;
146 info
->capability
= ICNS_IN_CAPABILITY
;
147 BString
iconName("Apple icon");
148 if (documentCount
> 1)
149 iconName
<< " #" << documentIndex
;
150 snprintf(info
->name
, sizeof(info
->name
), iconName
.String());
151 strcpy(info
->MIME
, kICNSMimeType
);
158 ICNSTranslator::DerivedTranslate(BPositionIO
*source
,
159 const translator_info
*info
, BMessage
*ioExtension
,
160 uint32 outType
, BPositionIO
*target
, int32 baseType
)
163 outType
= B_TRANSLATOR_BITMAP
;
164 if (outType
!= B_TRANSLATOR_BITMAP
&& outType
!= ICNS_IMAGE_FORMAT
)
165 return B_NO_TRANSLATOR
;
170 TranslatorBitmap bitsHeader
;
172 result
= identify_bits_header(source
, NULL
, &bitsHeader
);
174 return B_NO_TRANSLATOR
;
176 icns_type_t type
= ICNSFormat(bitsHeader
.bounds
.Width() + 1,
177 bitsHeader
.bounds
.Height() + 1, bitsHeader
.colors
);
178 if (type
== ICNS_NULL_TYPE
)
179 return B_NO_TRANSLATOR
;
181 if (outType
== ICNS_IMAGE_FORMAT
) {
182 ICNSSaver
icnsFile(source
, bitsHeader
.rowBytes
, type
);
183 if (icnsFile
.IsCreated())
184 return icnsFile
.SaveData(target
);
187 return B_NO_TRANSLATOR
;
192 if (outType
!= B_TRANSLATOR_BITMAP
)
193 return B_NO_TRANSLATOR
;
195 ICNSLoader
icnsFile(source
);
196 if (!icnsFile
.IsLoaded() || icnsFile
.IconsCount() <= 0)
197 return B_NO_TRANSLATOR
;
199 int32 documentIndex
= 1;
202 if (ioExtension
->FindInt32(DOCUMENT_INDEX
, &documentIndex
) != B_OK
)
206 return icnsFile
.GetIcon(target
, documentIndex
);
210 return B_NO_TRANSLATOR
;
216 ICNSTranslator::DerivedCanHandleImageSize(float width
, float height
) const
218 if (ICNSFormat(width
, height
, B_RGBA32
) == ICNS_NULL_TYPE
)
219 return B_NO_TRANSLATOR
;
225 ICNSTranslator::NewConfigView(TranslatorSettings
*settings
)
227 return new ConfigView(settings
);
235 make_nth_translator(int32 n
, image_id you
, uint32 flags
, ...)
240 return new ICNSTranslator();