6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
29 Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30 of Be Incorporated in the United States and other countries. Other brand product
31 names are registered trademarks or trademarks of their respective holders.
36 #include "Utilities.h"
43 #include <IconUtils.h>
45 #include <SupportDefs.h>
48 // #pragma mark - BImageResources
51 BImageResources::BImageResources(void* memAddr
)
53 image_id image
= find_image(memAddr
);
55 if (get_image_info(image
, &info
) == B_OK
) {
56 #if _SUPPORTS_RESOURCES
57 BFile
file(&info
.name
[0], B_READ_ONLY
);
59 BString
name(&info
.name
[0]);
61 BFile
file(name
.String(), B_READ_ONLY
);
63 if (file
.InitCheck() == B_OK
)
64 fResources
.SetTo(&file
);
69 BImageResources::~BImageResources()
75 BImageResources::ViewResources() const
77 if (fLock
.Lock() != B_OK
)
85 BImageResources::ViewResources()
87 if (fLock
.Lock() != B_OK
)
95 BImageResources::FinishResources(BResources
* res
) const
97 ASSERT(res
== &fResources
);
98 if (res
!= &fResources
)
108 BImageResources::LoadResource(type_code type
, int32 id
,
109 size_t* out_size
) const
111 // Serialize execution.
112 // Looks like BResources is not really thread safe. We should
113 // clean that up in the future and remove the locking from here.
114 BAutolock
lock(fLock
);
115 if (!lock
.IsLocked())
118 // Return the resource. Because we never change the BResources
119 // object, the returned data will not change until TTracker is
121 return const_cast<BResources
*>(&fResources
)->LoadResource(type
, id
,
127 BImageResources::LoadResource(type_code type
, const char* name
,
128 size_t* out_size
) const
130 // Serialize execution.
131 BAutolock
lock(fLock
);
132 if (!lock
.IsLocked())
135 // Return the resource. Because we never change the BResources
136 // object, the returned data will not change until TTracker is
138 return const_cast<BResources
*>(&fResources
)->LoadResource(type
, name
,
144 BImageResources::GetIconResource(int32 id
, icon_size size
,
150 // try to load vector icon
151 data
= LoadResource(B_VECTOR_ICON_TYPE
, id
, &length
);
153 && BIconUtils::GetVectorIcon((uint8
*)data
, length
, dest
) == B_OK
) {
157 // fall back to R5 icon
158 if (size
!= B_LARGE_ICON
&& size
!= B_MINI_ICON
)
162 data
= LoadResource(size
== B_LARGE_ICON
? 'ICON' : 'MICN', id
, &length
);
165 || length
!= (size_t)(size
== B_LARGE_ICON
? 1024 : 256)) {
170 if (dest
->ColorSpace() != B_CMAP8
) {
171 return BIconUtils::ConvertFromCMAP8((uint8
*)data
, size
, size
,
175 dest
->SetBits(data
, (int32
)length
, 0, B_CMAP8
);
181 BImageResources::GetIconResource(int32 id
, const uint8
** iconData
,
182 size_t* iconSize
) const
184 // try to load vector icon data from resources
186 const void* data
= LoadResource(B_VECTOR_ICON_TYPE
, id
, &length
);
190 *iconData
= (const uint8
*)data
;
198 BImageResources::find_image(void* memAddr
) const
202 while (get_next_image_info(0, &cookie
, &info
) == B_OK
) {
203 if ((info
.text
<= memAddr
204 && (((uint8
*)info
.text
)+info
.text_size
) > memAddr
)
205 || (info
.data
<= memAddr
206 && (((uint8
*)info
.data
)+info
.data_size
) > memAddr
)) {
217 BImageResources::GetBitmapResource(type_code type
, int32 id
,
223 const void* data
= LoadResource(type
, id
, &len
);
230 BMemoryIO
stream(data
, len
);
232 // Try to read as an archived bitmap.
233 stream
.Seek(0, SEEK_SET
);
235 status_t result
= archive
.Unflatten(&stream
);
239 *out
= new BBitmap(&archive
);
243 result
= (*out
)->InitCheck();
244 if (result
!= B_OK
) {
253 static BLocker resLock
;
254 static BImageResources
* resources
= NULL
;
256 // This class is used as a static instance to delete the resources
257 // global object when the image is getting unloaded.
258 class _TTrackerCleanupResources
{
260 _TTrackerCleanupResources()
264 ~_TTrackerCleanupResources()
274 static _TTrackerCleanupResources CleanupResources
;
276 BImageResources
* GetTrackerResources()
279 BAutolock
lock(&resLock
);
280 resources
= new BImageResources(&resources
);
286 } // namespace BPrivate