--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/PICTReader.st Mon Sep 01 11:52:06 2003 +0200
@@ -0,0 +1,260 @@
+"
+ COPYRIGHT (c) 1993 by Claus Gittinger
+ All Rights Reserved
+
+ This software is furnished under a license and may be used
+ only in accordance with the terms of that license and with the
+ inclusion of the above copyright notice. This software may not
+ be provided or otherwise made available to, or used by, any
+ other person. No title to or ownership of the software is
+ hereby transferred.
+"
+
+"{ Package: 'stx:libview2' }"
+
+ImageReader subclass:#PICTReader
+ instanceVariableNames:''
+ classVariableNames:''
+ poolDictionaries:''
+ category:'Graphics-Images-Readers'
+!
+
+!PICTReader class methodsFor:'documentation'!
+
+copyright
+"
+ COPYRIGHT (c) 1993 by Claus Gittinger
+ All Rights Reserved
+
+ This software is furnished under a license and may be used
+ only in accordance with the terms of that license and with the
+ inclusion of the above copyright notice. This software may not
+ be provided or otherwise made available to, or used by, any
+ other person. No title to or ownership of the software is
+ hereby transferred.
+"
+!
+
+documentation
+"
+ this class provides methods for loading Sun Raster and
+ Sun Icon file images.
+
+ No image writing is implemented.
+
+ [See also:]
+ Image Form Icon
+ BlitImageReader FaceReader JPEGReader GIFReader PBMReader PCXReader
+ ST80FormReader TargaReader TIFFReader WindowsIconReader
+ XBMReader XPMReader XWDReader
+"
+!
+
+examples
+"
+ example8
+ --- Version 1 PICTure ---
+
+ | array image |
+ array := #[
+ 16r00 16r4F
+ 16r00 16r02 16r00 16r02 16r00 16r6E 16r00 16rAA
+ 16r11
+ 16r01
+ 16r01
+ 16r00 16r0A
+ 16r00 16r02 16r00 16r02 16r00 16r6E 16r00 16rAA
+ 16r0A
+ 16r77 16rDD 16r77 16rDD 16r77 16rDD 16r77 16rDD
+ 16r34
+ 16r00 16r02 16r00 16r02 16r00 16r6E 16r00 16rAA
+ 16r0A
+ 16r88 16r22 16r88 16r22 16r88 16r22 16r88 16r22
+ 16r5C
+ 16r71
+ 16r00 16r1A
+ 16r00 16r02 16r00 16r02 16r00 16r6E 16r00 16rAA
+ 16r00 16r6E 16r00 16r02 16r00 16r02 16r00 16r54 16r00 16r6E 16r00 16rAA 16r00 16r6E 16r00 16r02
+ 16rFF
+ ].
+ image := PICTReader fromStream: (ReadStream on: array).
+"
+! !
+
+!PICTReader class methodsFor:'initialization'!
+
+initialize
+ "install myself in the Image classes fileFormat table
+ for the `.icon' and '.im8' extensions."
+
+ MIMETypes defineImageType:nil suffix:'icon' reader:self.
+ MIMETypes defineImageType:nil suffix:'im8' reader:self.
+
+ "Modified: 1.2.1997 / 15:08:40 / cg"
+! !
+
+!PICTReader class methodsFor:'testing'!
+
+isValidImageFile:aFileName
+ "return true, if aFileName contains a sunraster image"
+
+ |inStream nr|
+
+ inStream := self streamReadingFile:aFileName.
+ inStream isNil ifTrue:[^ false].
+
+ "try sun raster"
+ inStream binary.
+ ((inStream nextWord == 16r59A6)
+ and:[inStream nextWord == 16r6A95]) ifTrue: [
+ inStream close.
+ ^ true
+ ].
+
+ inStream isPositionable ifFalse:[^ false].
+
+ "try sun bitmap image format"
+ inStream text.
+ inStream reset.
+
+ "must start with a comment"
+ inStream skipSeparators.
+ inStream next ~~ $/ ifTrue:[^ false].
+ inStream next ~~ $* ifTrue:[^ false].
+
+ (inStream skipThroughAll: 'idth') isNil ifTrue: [
+ inStream close.
+ ^ false
+ ].
+ inStream next; skipSeparators.
+ nr := Integer readFrom: inStream.
+ (nr isNil or:[nr <= 0]) ifTrue: [
+ inStream close.
+ ^ false
+ ].
+
+ (inStream skipThroughAll: 'eight') isNil ifTrue: [
+ inStream close.
+ ^ false
+ ].
+ inStream next; skipSeparators.
+ nr := Integer readFrom: inStream.
+ (nr isNil or:[nr <= 0]) ifTrue: [
+ inStream close.
+ ^ false
+ ].
+
+ inStream close.
+ ^ true
+! !
+
+!PICTReader methodsFor:'reading from file'!
+
+fromStream: aStream
+ "read an image in my format from aStream.
+ Dtermine if its a raster or icon file."
+
+ | rasterType mapType mapBytes imageWords form depth
+ rMap gMap bMap mapLen
+ a b c index pos|
+
+ inStream := aStream.
+
+ aStream binary.
+
+ pos := aStream position.
+ ((aStream nextWord == 16r59A6)
+ and:[aStream nextWord == 16r6A95]) ifFalse: [
+"/ 'SUNReader: not a SunRaster file' errorPrintNL.
+ aStream position:pos.
+ ^ self fromSunIconStream:aStream
+ ].
+
+ width := aStream nextLong.
+ height := aStream nextLong.
+
+ depth := aStream nextLong.
+ aStream nextLong. "Ignore the image length since I can't rely on it anyway."
+ rasterType := aStream nextLong.
+ mapType := aStream nextLong. "Ignore the raster maptype."
+ mapBytes := aStream nextLong.
+
+ depth = 8 ifTrue: [
+ mapLen := (mapBytes // 3).
+ rMap := aStream nextBytes:mapLen.
+ gMap := aStream nextBytes:mapLen.
+ bMap := aStream nextBytes:mapLen.
+ colorMap := MappedPalette redVector:rMap greenVector:gMap blueVector:bMap.
+
+ data := ByteArray uninitializedNew:(width * height).
+ aStream nextBytes:(width * height) into:data.
+
+ photometric := #palette.
+ samplesPerPixel := 1.
+ bitsPerSample := #(8).
+
+ ^ self
+ ].
+ depth ~~ 1 ifTrue: [
+ ^ self fileFormatError:'only depth 1 and 8 supported'.
+ ].
+
+ form := nil.
+
+ aStream skip: mapBytes. "Skip the color map."
+ imageWords := (width / 16) ceiling * height.
+ data := ByteArray uninitializedNew:(imageWords * 2).
+
+ (rasterType between: 0 and: 2) ifFalse: [
+ ^ self fileFormatError:'Unknown raster file rasterType'.
+ ].
+
+ (rasterType = 2) ifFalse: [
+ "no compression of bytes"
+ aStream nextBytes:(imageWords * 2) into:data
+ ] ifTrue: [
+ "run length compression of bytes"
+
+ index := 1.
+ a := aStream next.
+ [a notNil] whileTrue: [
+ (a = 128) ifFalse: [
+ data at:index put: a.
+ index := index + 1
+ ] ifTrue: [
+ b := aStream next.
+ b = 0 ifTrue: [
+ data at:index put:128 .
+ index := index + 1
+ ] ifFalse: [
+ c := aStream next.
+ 1 to:(b+1) do:[:i |
+ data at:index put:c.
+ index := index + 1
+ ]
+ ]
+ ].
+ a := aStream next
+ ].
+ ].
+ photometric := #whiteIs0.
+ samplesPerPixel := 1.
+ bitsPerSample := #(1).
+
+ "
+ Image fromFile:'bitmaps/founders.im8'
+ Image fromFile:'bitmaps/bf.im8'
+ SunRasterReader fromStream:'bitmaps/founders.im8' asFilename readStream
+ SunRasterReader fromStream:'bitmaps/bf.im8' asFilename readStream
+ "
+
+ "Modified: / 3.2.1998 / 18:00:35 / cg"
+! !
+
+!PICTReader class methodsFor:'documentation'!
+
+version
+ ^ '$Header: /cvs/stx/stx/libview2/PICTReader.st,v 1.1 2003-09-01 09:52:06 cg Exp $'
+! !
+
+PICTReader initialize!