3121
|
1 |
"
|
|
2 |
COPYRIGHT (c) 2013 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
"{ Package: 'stx:libview2' }"
|
|
13 |
|
|
14 |
ImageReader subclass:#MacOSXIconReader
|
|
15 |
instanceVariableNames:'image'
|
|
16 |
classVariableNames:''
|
|
17 |
poolDictionaries:''
|
|
18 |
category:'Graphics-Images-Readers'
|
|
19 |
!
|
|
20 |
|
|
21 |
!MacOSXIconReader class methodsFor:'documentation'!
|
|
22 |
|
|
23 |
copyright
|
|
24 |
"
|
|
25 |
COPYRIGHT (c) 2013 by Claus Gittinger
|
|
26 |
All Rights Reserved
|
|
27 |
|
|
28 |
This software is furnished under a license and may be used
|
|
29 |
only in accordance with the terms of that license and with the
|
|
30 |
inclusion of the above copyright notice. This software may not
|
|
31 |
be provided or otherwise made available to, or used by, any
|
|
32 |
other person. No title to or ownership of the software is
|
|
33 |
hereby transferred.
|
|
34 |
"
|
|
35 |
!
|
|
36 |
|
|
37 |
documentation
|
|
38 |
"
|
|
39 |
Reader for mac osx icon files.
|
|
40 |
|
|
41 |
These files are actually bundles of a sequence of icons (in possibly different resolutions,
|
|
42 |
colors and sizes).
|
|
43 |
When such a file is read, I return the first image as usual,
|
|
44 |
and all images as an imagesequence.
|
|
45 |
|
|
46 |
Only a subset of the supported image formats are supported by this reader
|
|
47 |
(i.e. JPEG and PNG based image encodings only). This means, that only 10.8-and later
|
|
48 |
icon files are really supported.
|
|
49 |
|
|
50 |
[See also:]
|
|
51 |
Image Form Icon
|
|
52 |
GIFReader JPEGReader PNGReader TIFFReader WindowsIconReader
|
|
53 |
"
|
|
54 |
! !
|
|
55 |
|
|
56 |
!MacOSXIconReader class methodsFor:'initialization'!
|
|
57 |
|
|
58 |
initialize
|
|
59 |
"install myself in the Image classes fileFormat table
|
|
60 |
for the `.icns' extension."
|
|
61 |
|
|
62 |
"/ MIMETypes defineImageType:'image/x-icns' suffix:'icns' reader:self.
|
|
63 |
MIMETypes defineImageType:nil suffix:'icns' reader:self.
|
|
64 |
! !
|
|
65 |
|
|
66 |
!MacOSXIconReader class methodsFor:'testing'!
|
|
67 |
|
|
68 |
canRepresent:anImage
|
|
69 |
"return true, if anImage can be represented in my file format.
|
|
70 |
Assuming that we store in PNG format, delegate that decision."
|
|
71 |
|
|
72 |
^ PNGReader canRepresent:anImage
|
|
73 |
!
|
|
74 |
|
|
75 |
isValidImageFile:aFileName
|
|
76 |
"return true, if aFileName is an apple osx icon file"
|
|
77 |
|
|
78 |
|id inStream|
|
|
79 |
|
|
80 |
inStream := self streamReadingFile:aFileName.
|
|
81 |
inStream isNil ifTrue:[^ false].
|
|
82 |
|
|
83 |
inStream text.
|
|
84 |
|
|
85 |
id := String new:4.
|
|
86 |
inStream nextBytes:4 into:id.
|
|
87 |
inStream close.
|
|
88 |
|
|
89 |
^ (id = 'icns')
|
|
90 |
! !
|
|
91 |
|
|
92 |
!MacOSXIconReader methodsFor:'image reading'!
|
|
93 |
|
|
94 |
fromStream:aStream
|
|
95 |
"read a stream containing an icon image (or a collection of images).
|
|
96 |
Leave image description in instance variables."
|
|
97 |
|
|
98 |
|sizeRemaining id img firstImage frame imageCount chunkType numChunkBytes chunkData|
|
|
99 |
|
|
100 |
inStream := aStream.
|
|
101 |
aStream text.
|
|
102 |
|
|
103 |
"icon-files are always msb"
|
|
104 |
byteOrder := #msb.
|
|
105 |
|
|
106 |
id := aStream nextBytes:4.
|
|
107 |
id size ~~ 4 ifTrue:[
|
|
108 |
^ self fileFormatError:'not an icns file (short read)'.
|
|
109 |
].
|
|
110 |
id := id asString.
|
|
111 |
|
|
112 |
(id ~= 'icns') ifTrue:[
|
|
113 |
^ self fileFormatError:('not an icns file (id=''' , id , ''')').
|
|
114 |
].
|
|
115 |
|
|
116 |
imageCount := 0.
|
|
117 |
sizeRemaining := aStream nextUnsignedLongMSB:true.
|
|
118 |
sizeRemaining := sizeRemaining - 4 - 4. "/ file magic and size are included in count
|
|
119 |
[ sizeRemaining > 0 ] whileTrue:[
|
|
120 |
chunkType := aStream nextBytes:4.
|
|
121 |
chunkType size ~~ 4 ifTrue:[
|
|
122 |
^ self fileFormatError:'not an icns file (short read on icon type)'.
|
|
123 |
].
|
|
124 |
chunkType := chunkType asString.
|
|
125 |
|
|
126 |
numChunkBytes := aStream nextUnsignedLongMSB:true.
|
|
127 |
numChunkBytes := numChunkBytes - 4 - 4. "/ type and size are included in count
|
|
128 |
|
|
129 |
chunkData := aStream next:numChunkBytes.
|
|
130 |
sizeRemaining := sizeRemaining - 4 - numChunkBytes.
|
|
131 |
|
|
132 |
img := self readSingleIcon:chunkType from:chunkData.
|
|
133 |
imageCount == 0 ifTrue:[
|
|
134 |
firstImage := image := img.
|
|
135 |
] ifFalse:[
|
|
136 |
imageCount == 1 ifTrue:[
|
|
137 |
imageSequence := ImageSequence new.
|
|
138 |
img imageSequence:imageSequence.
|
|
139 |
|
|
140 |
"/ add frame for first image.
|
|
141 |
frame := ImageFrame new image:firstImage.
|
|
142 |
imageSequence add:frame.
|
|
143 |
].
|
|
144 |
img imageSequence:imageSequence.
|
|
145 |
|
|
146 |
"/ add frame for this image.
|
|
147 |
frame := ImageFrame new image:img.
|
|
148 |
imageSequence add:frame.
|
|
149 |
].
|
|
150 |
imageCount := imageCount + 1.
|
|
151 |
].
|
|
152 |
|
|
153 |
"
|
|
154 |
Image fromFile:'/Applications/TextEdit.app/Contents/Resources/txt.icns'
|
|
155 |
"
|
|
156 |
!
|
|
157 |
|
|
158 |
readSingleIcon:iconType from:iconBytes
|
|
159 |
"read a single image from the inputStream."
|
|
160 |
|
|
161 |
^ self
|
|
162 |
perform:('read_',(iconType copyReplaceAll:$# with:$_),'_from:') asSymbol
|
|
163 |
with:iconBytes
|
|
164 |
ifNotUnderstood:[
|
|
165 |
self breakPoint:#cg.
|
|
166 |
self halt.
|
|
167 |
('MacOSXIconReader: unsupported icon format: ',iconType) infoPrintCR.
|
|
168 |
^ self fileFormatError:'unsupported icon format: ',iconType
|
|
169 |
].
|
|
170 |
|
|
171 |
"
|
|
172 |
self fromFile:'/Applications/TextEdit.app/Contents/Resources/txt.icns'
|
|
173 |
self fromFile:'test.icns'
|
|
174 |
"
|
|
175 |
! !
|
|
176 |
|
|
177 |
!MacOSXIconReader methodsFor:'image writing'!
|
|
178 |
|
|
179 |
save:anImage onStream:aStream
|
|
180 |
"save an icon to aStream."
|
|
181 |
|
|
182 |
self saveAll:(Array with:anImage) onStream:aStream.
|
|
183 |
|
|
184 |
"
|
|
185 |
self
|
|
186 |
save:(Image fromScreen:(0@0 corner:16@16))
|
|
187 |
onFile:'test.icns'
|
|
188 |
"
|
|
189 |
!
|
|
190 |
|
|
191 |
saveAll:aCollectionOfImages onStream:aStream
|
|
192 |
"save a number of images to aStream."
|
|
193 |
|
|
194 |
|tempFile tempFileStream|
|
|
195 |
|
|
196 |
(aCollectionOfImages conform:
|
|
197 |
[:eachImage |
|
|
198 |
(eachImage width = eachImage height)
|
|
199 |
and:[ (#(16 32 128 256 512 1024) includes:eachImage width)]
|
|
200 |
]
|
|
201 |
) ifFalse:[
|
|
202 |
^ self fileFormatError:'unsupported image size (must be square and width 16, 32, 128, 512 or 1024)'.
|
|
203 |
].
|
|
204 |
|
|
205 |
tempFile := Filename newTemporary asAutoDeletedFilename.
|
|
206 |
tempFileStream := tempFile writeStream.
|
|
207 |
tempFileStream binary.
|
|
208 |
|
|
209 |
aCollectionOfImages do:[:eachImage |
|
|
210 |
|typeCode s data|
|
|
211 |
|
|
212 |
s := WriteStream on:(ByteArray new:1024).
|
|
213 |
PNGReader save:eachImage onStream:s.
|
|
214 |
data := s contents.
|
|
215 |
self assert:data notEmptyOrNil.
|
|
216 |
|
|
217 |
typeCode := #(16 32 128 256 512 1024)
|
|
218 |
map: #('ipc4' 'ipc5' 'ic07' 'ic08' 'ic09' 'ic10')
|
|
219 |
at:eachImage width ifAbsent:[self error].
|
|
220 |
tempFileStream nextPutBytes:typeCode.
|
|
221 |
tempFileStream nextPutLong:(data size + 4 + 4) MSB:true.
|
|
222 |
tempFileStream nextPutAll:data.
|
|
223 |
].
|
|
224 |
tempFileStream close.
|
|
225 |
|
|
226 |
aStream binary.
|
|
227 |
aStream nextPutBytes:'icns'.
|
|
228 |
aStream nextPutLong:(tempFile fileSize + 4 + 4) MSB:true.
|
|
229 |
aStream nextPutAll:(tempFile binaryContentsOfEntireFile).
|
|
230 |
tempFile delete.
|
|
231 |
|
|
232 |
"
|
|
233 |
self
|
|
234 |
save:(Image fromScreen:(0@0 corner:16@16))
|
|
235 |
onFile:'test.icns'
|
|
236 |
"
|
|
237 |
! !
|
|
238 |
|
|
239 |
!MacOSXIconReader methodsFor:'private'!
|
|
240 |
|
|
241 |
makeImage
|
|
242 |
"image is already made"
|
|
243 |
|
|
244 |
^ image
|
|
245 |
! !
|
|
246 |
|
|
247 |
!MacOSXIconReader methodsFor:'private reading'!
|
|
248 |
|
|
249 |
readPNGOrJPEGFrom:bytes expectedSize:expectedSizeOrNil
|
|
250 |
"read a PNG or JPEG image.
|
|
251 |
Helper for ipc4, ipc5, ic07, ic09, ic10 formats"
|
|
252 |
|
|
253 |
|img|
|
|
254 |
|
|
255 |
img := PNGReader fromStream:(bytes readStream).
|
|
256 |
img isNil ifTrue:[
|
|
257 |
img := JPEGReader fromStream:(bytes readStream).
|
|
258 |
].
|
|
259 |
expectedSizeOrNil notNil ifTrue:[
|
|
260 |
self assert:(img width = expectedSizeOrNil).
|
|
261 |
self assert:(img height = expectedSizeOrNil).
|
|
262 |
].
|
|
263 |
^ img
|
|
264 |
!
|
|
265 |
|
|
266 |
read_ICN__from:bytes
|
|
267 |
"read an ICN# format icon"
|
|
268 |
|
|
269 |
self halt.
|
|
270 |
^ nil
|
|
271 |
|
|
272 |
"
|
|
273 |
self fromFile:'/Applications/TextEdit.app/Contents/Resources/txt.icns'
|
|
274 |
"
|
|
275 |
!
|
|
276 |
|
|
277 |
read_ic07_from:bytes
|
|
278 |
"read an ic07 (PNG or JPEG, 128) format icon"
|
|
279 |
|
|
280 |
^ self readPNGOrJPEGFrom:bytes expectedSize:128.
|
|
281 |
|
|
282 |
"
|
|
283 |
self fromFile:'/Applications/TextEdit.app/Contents/Resources/txt.icns'
|
|
284 |
self fromFile:'test.icns'
|
|
285 |
"
|
|
286 |
!
|
|
287 |
|
|
288 |
read_ic08_from:bytes
|
|
289 |
"read an ic08 (PNG or JPEG, 256) format icon"
|
|
290 |
|
|
291 |
^ self readPNGOrJPEGFrom:bytes expectedSize:256.
|
|
292 |
|
|
293 |
"
|
|
294 |
self fromFile:'/Applications/TextEdit.app/Contents/Resources/txt.icns'
|
|
295 |
self fromFile:'test.icns'
|
|
296 |
"
|
|
297 |
!
|
|
298 |
|
|
299 |
read_ic09_from:bytes
|
|
300 |
"read an ic09 (PNG or JPEG, 512) format icon"
|
|
301 |
|
|
302 |
^ self readPNGOrJPEGFrom:bytes expectedSize:512.
|
|
303 |
|
|
304 |
"
|
|
305 |
self fromFile:'/Applications/TextEdit.app/Contents/Resources/txt.icns'
|
|
306 |
self fromFile:'test.icns'
|
|
307 |
"
|
|
308 |
!
|
|
309 |
|
|
310 |
read_ic10_from:bytes
|
|
311 |
"read an ic10 (PNG or JPEG, 1024) format icon"
|
|
312 |
|
|
313 |
^ self readPNGOrJPEGFrom:bytes expectedSize:1024.
|
|
314 |
|
|
315 |
"
|
|
316 |
self fromFile:'/Applications/TextEdit.app/Contents/Resources/txt.icns'
|
|
317 |
self fromFile:'test.icns'
|
|
318 |
"
|
|
319 |
!
|
|
320 |
|
|
321 |
read_icl8_from:bytes
|
|
322 |
"read an icl8 format icon"
|
|
323 |
|
|
324 |
self halt.
|
|
325 |
^ nil
|
|
326 |
|
|
327 |
"
|
|
328 |
self fromFile:'/Applications/TextEdit.app/Contents/Resources/txt.icns'
|
|
329 |
"
|
|
330 |
!
|
|
331 |
|
|
332 |
read_ics8_from:bytes
|
|
333 |
"read an ics8 format icon"
|
|
334 |
|
|
335 |
self halt.
|
|
336 |
^ nil
|
|
337 |
|
|
338 |
"
|
|
339 |
self fromFile:'/Applications/TextEdit.app/Contents/Resources/txt.icns'
|
|
340 |
"
|
|
341 |
!
|
|
342 |
|
|
343 |
read_ics__from:bytes
|
|
344 |
"read an ics# format icon"
|
|
345 |
|
|
346 |
self halt.
|
|
347 |
^ nil
|
|
348 |
|
|
349 |
"
|
|
350 |
self fromFile:'/Applications/TextEdit.app/Contents/Resources/txt.icns'
|
|
351 |
"
|
|
352 |
!
|
|
353 |
|
|
354 |
read_il32_from:bytes
|
|
355 |
"read an il32 format icon"
|
|
356 |
|
|
357 |
self halt.
|
|
358 |
^ nil
|
|
359 |
|
|
360 |
"
|
|
361 |
self fromFile:'/Applications/TextEdit.app/Contents/Resources/txt.icns'
|
|
362 |
"
|
|
363 |
!
|
|
364 |
|
|
365 |
read_ipc4_from:bytes
|
|
366 |
"read an ipc4 (PNG or JPEG, 16x16) format icon"
|
|
367 |
|
|
368 |
^ self readPNGOrJPEGFrom:bytes expectedSize:16.
|
|
369 |
|
|
370 |
"
|
|
371 |
self fromFile:'/Applications/TextEdit.app/Contents/Resources/txt.icns'
|
|
372 |
self fromFile:'test.icns'
|
|
373 |
"
|
|
374 |
!
|
|
375 |
|
|
376 |
read_ipc5_from:bytes
|
|
377 |
"read an ipc5 (PNG or JPEG, 32) format icon"
|
|
378 |
|
|
379 |
^ self readPNGOrJPEGFrom:bytes expectedSize:32.
|
|
380 |
|
|
381 |
"
|
|
382 |
self fromFile:'/Applications/TextEdit.app/Contents/Resources/txt.icns'
|
|
383 |
self fromFile:'test.icns'
|
|
384 |
"
|
|
385 |
!
|
|
386 |
|
|
387 |
read_is32_from:bytes
|
|
388 |
"read an is32 format icon"
|
|
389 |
|
|
390 |
self halt.
|
|
391 |
^ nil
|
|
392 |
|
|
393 |
"
|
|
394 |
self fromFile:'/Applications/TextEdit.app/Contents/Resources/txt.icns'
|
|
395 |
"
|
|
396 |
!
|
|
397 |
|
|
398 |
read_it32_from:bytes
|
|
399 |
"read an it32 format icon"
|
|
400 |
|
|
401 |
self halt.
|
|
402 |
^ nil
|
|
403 |
|
|
404 |
"
|
|
405 |
self fromFile:'/Applications/TextEdit.app/Contents/Resources/txt.icns'
|
|
406 |
"
|
|
407 |
!
|
|
408 |
|
|
409 |
read_l8mk_from:bytes
|
|
410 |
"read an l8mk format icon"
|
|
411 |
|
|
412 |
self halt.
|
|
413 |
^ nil
|
|
414 |
|
|
415 |
"
|
|
416 |
self fromFile:'/Applications/TextEdit.app/Contents/Resources/txt.icns'
|
|
417 |
"
|
|
418 |
!
|
|
419 |
|
|
420 |
read_s8mk_from:bytes
|
|
421 |
"read an s8mk format icon"
|
|
422 |
|
|
423 |
self halt.
|
|
424 |
^ nil
|
|
425 |
|
|
426 |
"
|
|
427 |
self fromFile:'/Applications/TextEdit.app/Contents/Resources/txt.icns'
|
|
428 |
"
|
|
429 |
!
|
|
430 |
|
|
431 |
read_t8mk_from:bytes
|
|
432 |
"read an t8mk format icon"
|
|
433 |
|
|
434 |
self halt.
|
|
435 |
^ nil
|
|
436 |
|
|
437 |
"
|
|
438 |
self fromFile:'/Applications/TextEdit.app/Contents/Resources/txt.icns'
|
|
439 |
"
|
|
440 |
! !
|
|
441 |
|
|
442 |
!MacOSXIconReader class methodsFor:'documentation'!
|
|
443 |
|
|
444 |
version
|
|
445 |
^ '$Header: /cvs/stx/stx/libview2/MacOSXIconReader.st,v 1.1 2013-05-06 14:37:12 cg Exp $'
|
|
446 |
!
|
|
447 |
|
|
448 |
version_CVS
|
|
449 |
^ '$Header: /cvs/stx/stx/libview2/MacOSXIconReader.st,v 1.1 2013-05-06 14:37:12 cg Exp $'
|
|
450 |
! !
|
|
451 |
|
|
452 |
|
|
453 |
MacOSXIconReader initialize!
|