--- a/WindowsIconReader.st Sat Sep 16 01:50:04 1995 +0200
+++ b/WindowsIconReader.st Sun Sep 17 20:01:10 1995 +0200
@@ -35,7 +35,7 @@
version
"
-$Header: /cvs/stx/stx/libview2/WindowsIconReader.st,v 1.15 1995-08-30 17:54:33 claus Exp $
+$Header: /cvs/stx/stx/libview2/WindowsIconReader.st,v 1.16 1995-09-17 18:01:10 claus Exp $
"
!
@@ -53,6 +53,55 @@
Image fileFormats at:'.ico' put:self.
! !
+!WindowsIconReader class methodsFor:'testing'!
+
+isValidImageFile:aFileName
+ "return true, if aFileName contains a valid windows bitmap-file image"
+
+ |inStream header ok|
+
+ inStream := self streamReadingFile:aFileName.
+ inStream isNil ifTrue:[^ false].
+
+ inStream binary.
+ inStream size < 16 ifTrue:[
+ ^ false
+ ].
+
+ header := ByteArray uninitializedNew:4.
+ inStream nextBytes:4 into:header.
+
+ ok := false.
+ (header startsWith:#(66 77)) ifTrue:[ "BM"
+ ok := true.
+"/ 'WINREADER: Win3.x or OS/2 vsn 2 BM format' infoPrintNL.
+ ].
+ (header startsWith:#(66 65)) ifTrue:[ "BA"
+ ok := true.
+"/ 'WINREADER: OS/2 vsn 2 BA format' infoPrintNL.
+ ].
+ (header startsWith:#(73 67)) ifTrue:[ "IC"
+ ok := true.
+"/ 'WINREADER: OS/2 IC format' infoPrintNL.
+ ].
+ (header startsWith:#(80 84)) ifTrue:[ "PT"
+ ok := true.
+"/ 'WINREADER: OS/2 PT format' infoPrintNL.
+ ].
+ (header startsWith:#(0 0 1 0)) ifTrue:[
+ ok := true.
+"/ 'WINREADER: Win3.x ICO format' infoPrintNL.
+ ].
+ ok ifFalse:[^ false].
+ ^ true
+
+ "
+ WindowsIconReader isValidImageFile:'/phys/clam2/LocalLibrary/Images/OS2_icons/dos.ico'
+ "
+
+ "Created: 17.9.1995 / 17:14:20 / claus"
+! !
+
!WindowsIconReader methodsFor:'reading from file'!
fromOS2File: aFilename
@@ -263,48 +312,100 @@
]
].
- "
- currently only normal (non-rle) bitmaps
- supported
- "
+"/ "
+"/ currently only normal (non-rle) bitmaps
+"/ supported
+"/ "
+"/ compression ~~ 0 ifTrue:[
+"/ 'BMP compression type ' errorPrint. compression errorPrint.
+"/ 'not supported' errorPrintNL.
+"/ ^ nil
+"/ ].
+"/ inPlanes ~~ 1 ifTrue:[
+"/ 'BMP only 1 plane images supported' errorPrintNL.
+"/ ^ nil
+"/ ].
+"/
+"/ "read the data bits"
+"/
+"/ bytesPerRow := width * inDepth + 7 // 8.
+"/ data4 := ByteArray uninitializedNew:(height * bytesPerRow).
+"/
+"/ aStream position:(dataStart + 1).
+"/ aStream nextBytes:(height * bytesPerRow) into:data4.
+"/
+"/ "read mask"
+"/
+"/"
+"/ mask := ByteArray new:(width * height / 8).
+"/ aStream nextBytes:(width * height / 8) into:mask.
+"/"
+"/
+"/ "stupid: last row first"
+"/
+"/ tmp := ByteArray uninitializedNew:(height * bytesPerRow).
+"/ srcIndex := 1.
+"/ dstIndex := (height - 1) * bytesPerRow + 1.
+"/ 1 to:height do:[:row |
+"/ tmp replaceFrom:dstIndex to:(dstIndex + bytesPerRow - 1)
+"/ with:data4 startingAt:srcIndex.
+"/ srcIndex := srcIndex + bytesPerRow.
+"/ dstIndex := dstIndex - bytesPerRow.
+"/ ].
+"/ data4 := tmp.
+
compression ~~ 0 ifTrue:[
- 'BMP compression type ' errorPrint. compression errorPrint.
- 'not supported' errorPrintNL.
- ^ nil
+ "/ some compression
+ compression == 1 ifTrue:[
+ "/ RLE8 - must be depth-8
+ inDepth == 8 ifFalse:[
+ 'BMP: RLE8 compression only allowed with depth8 images' errorPrintNL.
+ ^ nil
+ ].
+ ].
+ compression == 2 ifTrue:[
+ "/ RLE4 - must be depth-4
+ inDepth == 4 ifFalse:[
+ 'BMP: RLE4 compression only allowed with depth4 images' errorPrintNL.
+ ^ nil
+ ].
+ ].
].
+
inPlanes ~~ 1 ifTrue:[
- 'BMP only 1 plane images supported' errorPrintNL.
+ 'BMP: only 1 plane images supported' errorPrintNL.
^ nil
].
- "read the data bits"
-
bytesPerRow := width * inDepth + 7 // 8.
- data4 := ByteArray uninitializedNew:(height * bytesPerRow).
-
+ "/ bmp data is always 32bit aligned; if required,
+ inBytesPerRow := ((bytesPerRow + 3) // 4) * 4.
aStream position:(dataStart + 1).
- aStream nextBytes:(height * bytesPerRow) into:data4.
-
- "read mask"
-"
- mask := ByteArray new:(width * height / 8).
- aStream nextBytes:(width * height / 8) into:mask.
-"
-
- "stupid: last row first"
-
- tmp := ByteArray uninitializedNew:(height * bytesPerRow).
- srcIndex := 1.
- dstIndex := (height - 1) * bytesPerRow + 1.
- 1 to:height do:[:row |
- tmp replaceFrom:dstIndex to:(dstIndex + bytesPerRow - 1)
- with:data4 startingAt:srcIndex.
- srcIndex := srcIndex + bytesPerRow.
- dstIndex := dstIndex - bytesPerRow.
+ data := ByteArray uninitializedNew:(height * width "bytesPerRow").
+ inDepth == 8 ifTrue:[
+ (self class loadBMP8Width:width height:height compression:compression from:aStream into:data) ifFalse:[
+ 'BMP: read/decompression failed' errorPrintNL.
+ ^ nil
+ ]
+ ] ifFalse:[
+ inDepth == 4 ifTrue:[
+ (self class loadBMP4to8Width:width height:height compression:compression from:aStream into:data) ifFalse:[
+ 'BMP: read/decompression failed' errorPrintNL.
+ ^ nil
+ ]
+ ] ifFalse:[
+ inDepth == 1 ifTrue:[
+ (self class loadBMP1to8Width:width height:height from:aStream into:data) ifFalse:[
+ 'BMP: read failed' errorPrintNL.
+ ^ nil
+ ]
+ ] ifFalse:[
+ 'BMP: unsupported depth:' errorPrint. inDepth errorPrintNL.
+ ^ nil
+ ]
+ ]
].
- data4 := tmp.
-
"expand into bytes"
data := ByteArray new:(width * height).