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