2 * Copyright 2009, Haiku, Inc.
3 * Distributed under the terms of the MIT license.
6 * Michael Lotz, mmlr@mlotz.ch
9 #include "HVIFTranslator.h"
13 #include <BitmapStream.h>
15 #include <IconUtils.h>
21 #define HVIF_FORMAT_CODE 'HVIF'
22 #define HVIF_TRANSLATION_QUALITY 1.0
23 #define HVIF_TRANSLATION_CAPABILITY 1.0
25 #undef B_TRANSLATION_CONTEXT
26 #define B_TRANSLATION_CONTEXT "HVIFTranslator"
29 static const translation_format sInputFormats
[] = {
33 HVIF_TRANSLATION_QUALITY
,
34 HVIF_TRANSLATION_CAPABILITY
,
35 "application/x-vnd.Haiku-icon",
36 "Native Haiku vector icon"
41 static const translation_format sOutputFormats
[] = {
48 "Be Bitmap format (HVIFTranslator)"
53 static const TranSetting sDefaultSettings
[] = {
54 { HVIF_SETTING_RENDER_SIZE
, TRAN_SETTING_INT32
, 64 }
57 const uint32 kNumInputFormats
= sizeof(sInputFormats
) / sizeof(translation_format
);
58 const uint32 kNumOutputFormats
= sizeof(sOutputFormats
) / sizeof(translation_format
);
59 const uint32 kNumDefaultSettings
= sizeof(sDefaultSettings
) / sizeof(TranSetting
);
64 make_nth_translator(int32 n
, image_id image
, uint32 flags
, ...)
67 return new HVIFTranslator();
72 HVIFTranslator::HVIFTranslator()
73 : BaseTranslator(B_TRANSLATE("HVIF icons"),
74 B_TRANSLATE("Haiku vector icon translator"),
75 HVIF_TRANSLATOR_VERSION
,
76 sInputFormats
, kNumInputFormats
,
77 sOutputFormats
, kNumOutputFormats
,
78 "HVIFTranslator_Settings",
79 sDefaultSettings
, kNumDefaultSettings
,
80 B_TRANSLATOR_BITMAP
, HVIF_FORMAT_CODE
)
85 HVIFTranslator::~HVIFTranslator()
91 HVIFTranslator::DerivedIdentify(BPositionIO
*inSource
,
92 const translation_format
*inFormat
, BMessage
*ioExtension
,
93 translator_info
*outInfo
, uint32 outType
)
95 // TODO: we do the fully work twice!
96 if (outType
!= B_TRANSLATOR_BITMAP
)
97 return B_NO_TRANSLATOR
;
99 // filter out invalid sizes
100 off_t size
= inSource
->Seek(0, SEEK_END
);
101 if (size
<= 0 || size
> 256 * 1024 || inSource
->Seek(0, SEEK_SET
) != 0)
102 return B_NO_TRANSLATOR
;
104 uint8
*buffer
= (uint8
*)malloc(size
);
108 if (inSource
->Read(buffer
, size
) != size
) {
110 return B_NO_TRANSLATOR
;
113 BBitmap
dummy(BRect(0, 0, 1, 1), B_BITMAP_NO_SERVER_LINK
, B_RGBA32
);
114 if (BIconUtils::GetVectorIcon(buffer
, size
, &dummy
) != B_OK
) {
116 return B_NO_TRANSLATOR
;
120 outInfo
->type
= sInputFormats
[0].type
;
121 outInfo
->group
= sInputFormats
[0].group
;
122 outInfo
->quality
= sInputFormats
[0].quality
;
123 outInfo
->capability
= sInputFormats
[0].capability
;
124 strcpy(outInfo
->MIME
, sInputFormats
[0].MIME
);
125 strcpy(outInfo
->name
, sInputFormats
[0].name
);
134 HVIFTranslator::DerivedTranslate(BPositionIO
*inSource
,
135 const translator_info
*inInfo
, BMessage
*ioExtension
, uint32 outType
,
136 BPositionIO
*outDestination
, int32 baseType
)
138 if (outType
!= B_TRANSLATOR_BITMAP
)
139 return B_NO_TRANSLATOR
;
141 // filter out invalid sizes
142 off_t size
= inSource
->Seek(0, SEEK_END
);
143 if (size
<= 0 || size
> 256 * 1024 || inSource
->Seek(0, SEEK_SET
) != 0)
144 return B_NO_TRANSLATOR
;
146 uint8
*buffer
= (uint8
*)malloc(size
);
150 if (inSource
->Read(buffer
, size
) != size
) {
152 return B_NO_TRANSLATOR
;
155 int32 renderSize
= fSettings
->SetGetInt32(HVIF_SETTING_RENDER_SIZE
);
156 if (renderSize
<= 0 || renderSize
> 1024)
159 BBitmap
rendered(BRect(0, 0, renderSize
- 1, renderSize
- 1),
160 B_BITMAP_NO_SERVER_LINK
, B_RGBA32
);
161 if (BIconUtils::GetVectorIcon(buffer
, size
, &rendered
) != B_OK
) {
163 return B_NO_TRANSLATOR
;
166 BBitmapStream
stream(&rendered
);
167 stream
.Seek(0, SEEK_SET
);
170 while ((read
= stream
.Read(buffer
, size
)) > 0)
171 outDestination
->Write(buffer
, read
);
173 BBitmap
*dummy
= NULL
;
174 stream
.DetachBitmap(&dummy
);
181 HVIFTranslator::NewConfigView(TranslatorSettings
*settings
)
183 return new HVIFView(B_TRANSLATE("HVIFTranslator Settings"),
184 B_WILL_DRAW
, settings
);