XWDReader.st
changeset 44 c6cf7d0d6337
parent 42 ab4cc6362a80
child 45 f94fc6118d0a
--- a/XWDReader.st	Sat Feb 18 16:58:20 1995 +0100
+++ b/XWDReader.st	Sat Feb 18 18:56:08 1995 +0100
@@ -1,4 +1,14 @@
-'From Smalltalk/X, Version:2.10.4 on 18-feb-1995 at 3:27:03 am'!
+"
+ COPYRIGHT (c) 1995 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.
+"
 
 ImageReader subclass:#XWDReader
 	 instanceVariableNames:''
@@ -7,12 +17,49 @@
 	 category:'Graphics-Images support'
 !
 
+XWDReader comment:'
+COPYRIGHT (c) 1995 by Claus Gittinger
+	      All Rights Reserved
+
+$Header: /cvs/stx/stx/libview2/XWDReader.st,v 1.2 1995-02-18 17:56:08 claus Exp $
+'!
+
+!XWDReader class methodsFor:'documentation'!
+
+copyright
+"
+ COPYRIGHT (c) 1995 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.
+"
+!
+
+version
+"
+$Header: /cvs/stx/stx/libview2/XWDReader.st,v 1.2 1995-02-18 17:56:08 claus Exp $
+"
+!
+
+documentation
+"
+    this class provides methods for loading x-window dump (xwd) images.
+"
+! !
+
 !XWDReader methodsFor:'image reading'!
 
 fromStream: aStream 
-        "read an image in XWD (X Window Dump) format."
+	"read an image in XWD (X Window Dump) format."
 
-    |header nColors palette res colors pad srcRowByteSize bitsPerPixel mask colormapSize depth |
+    |header nColors palette res colors pad 
+     srcRowByteSize bytesPerRow bitsPerPixel mask colormapSize depth 
+     dstIndex|
 
     aStream binary.
 
@@ -27,39 +74,53 @@
     pad := header at: 11.
     bitsPerPixel := header at: 12.
     bitsPerPixel == 24 ifTrue:[
-        bitsPerSample := #(8 8 8).
-        samplesPerPixel := 3.
-        photometric := #rgb
+	bitsPerSample := #(8 8 8).
+	samplesPerPixel := 3.
+	photometric := #rgb
     ] ifFalse:[
-        bitsPerSample := Array with:bitsPerPixel.
-        samplesPerPixel := 1.
-        photometric := #palette
+	bitsPerSample := Array with:bitsPerPixel.
+	samplesPerPixel := 1.
+	photometric := #palette
     ].
 
     colormapSize := header at: 19.
     nColors := header at: 20.
 
-    colorMap := Array new:depth * depth.
+    colorMap := Array new:colormapSize.
 
     1 to:nColors do:[:i |
-        |clr|
+	|clr|
+
+	aStream nextLong.
+	clr := ColorValue scaledRed: (aStream nextWord bitShift: -3)
+			scaledGreen: (aStream nextWord bitShift: -3)
+			 scaledBlue: (aStream nextWord bitShift: -3).
+	colorMap at:i put:clr.
+	aStream nextWord.
+    ].
+
+    nColors+1 to:colormapSize do: [:i | colorMap at:i put:Color black].
 
-        aStream nextLong.
-        clr := Color scaledRed: (aStream nextWord bitShift: -3)
-                     scaledGreen: (aStream nextWord bitShift: -3)
-                     scaledBlue: (aStream nextWord bitShift: -3).
-        colorMap at:i put:clr.
-        aStream nextWord.
-   ].
+    bytesPerRow := width * bitsPerPixel // 8.
+    ((width * bitsPerPixel \\ 8) ~~ 0) ifTrue:[
+	bytesPerRow := bytesPerRow + 1
+    ].
+    srcRowByteSize := width * bitsPerPixel + pad - 1 // pad * (pad / 8).
 
-    nColors+1 to:(depth * depth) do: [:i | colorMap at:i put:Color black].
-
-    srcRowByteSize := width * bitsPerPixel + pad - 1 // pad * (pad / 8).
     data := ByteArray uninitializedNew: srcRowByteSize * height.
-    aStream nextBytes:srcRowByteSize * height into:data.
-
+    srcRowByteSize == bytesPerRow ifTrue:[
+	aStream nextBytes:srcRowByteSize * height into:data.
+    ] ifFalse:[
+	dstIndex := 1.
+	1 to:height do:[:y |
+	    aStream nextBytes:bytesPerRow into:data startingAt:dstIndex.
+	    aStream next:(srcRowByteSize - bytesPerRow).
+	    dstIndex := dstIndex + bytesPerRow.
+	].
+	self halt.
+    ]
     "
-     XWDReader fromFile:'/phys/clam/claus/smalltalk/private_classes/hpdst/pixmaps/bike.xwd'
+     XWDReader fromFile:'testfile.xwd'
     "
 ! !