2 * Copyright 2006-2007, Haiku. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Stephan Aßmus <superstippi@gmx.de>
10 #include "FlatIconFormat.h"
12 #include "LittleEndianBuffer.h"
18 const uint32 FLAT_ICON_MAGIC
= 'ficn';
20 const char* kVectorAttrNodeName
= "BEOS:ICON";
21 const char* kVectorAttrMimeName
= "META:ICON";
26 read_coord(LittleEndianBuffer
& buffer
, float& coord
)
29 if (!buffer
.Read(value
))
33 // high bit set, the next byte is part of the coord
35 if (!buffer
.Read(lowValue
))
38 uint16 coordValue
= (value
<< 8) | lowValue
;
39 coord
= (float)coordValue
/ 102.0 - 128.0;
42 coord
= (float)value
- 32.0;
49 write_coord(LittleEndianBuffer
& buffer
, float coord
)
57 if (int(coord
* 100.0) == (int)coord
* 100
58 && coord
>= - 32.0 && coord
<= 95.0) {
59 // saving coord in 7 bit is sufficient
60 uint8 value
= (uint8
)(coord
+ 32.0);
61 return buffer
.Write(value
);
63 // needing to save coord in 15 bits
64 uint16 value
= (uint16
)((coord
+ 128.0) * 102.0);
65 // set high bit to indicate there is only one byte
67 uint8 highValue
= value
>> 8;
68 uint8 lowValue
= value
& 255;
69 return buffer
.Write(highValue
) && buffer
.Write(lowValue
);
75 read_float_24(LittleEndianBuffer
& buffer
, float& _value
)
78 if (!buffer
.Read(bufferValue
[0]) || !buffer
.Read(bufferValue
[1])
79 || !buffer
.Read(bufferValue
[2]))
82 int shortValue
= (bufferValue
[0] << 16)
83 | (bufferValue
[1] << 8) | bufferValue
[2];
85 int sign
= (shortValue
& 0x800000) >> 23;
86 int exponent
= ((shortValue
& 0x7e0000) >> 17) - 32;
87 int mantissa
= (shortValue
& 0x01ffff) << 6;
92 uint32 value
= (sign
<< 31) | ((exponent
+ 127) << 23) | mantissa
;
94 _value
= (float&)value
;
102 write_float_24(LittleEndianBuffer
& buffer
, float _value
)
107 // TODO: fixme for non-IEEE 754 architectures
108 uint32 value
= (uint32
&)_value
;
110 int sign
= (value
& 0x80000000) >> 31;
111 int exponent
= ((value
& 0x7f800000) >> 23) - 127;
112 int mantissa
= value
& 0x007fffff;
114 if (exponent
>= 32 || exponent
< -32) {
116 return buffer
.Write(zero
) && buffer
.Write(zero
)
117 && buffer
.Write(zero
);
120 int shortValue
= (sign
<< 23)
121 | ((exponent
+ 32) << 17)
124 return buffer
.Write((uint8
)(shortValue
>> 16))
125 && buffer
.Write((uint8
)((shortValue
>> 8) & 0xff))
126 && buffer
.Write((uint8
)(shortValue
& 0xff));