image saving - use save:onStream:
authorClaus Gittinger <cg@exept.de>
Fri, 12 Sep 2003 13:43:19 +0200
changeset 1814 2f204c2a957d
parent 1813 aa74c7bd22ad
child 1815 5f6fb98a3479
image saving - use save:onStream:
PBMReader.st
TIFFReader.st
XBMReader.st
XPMReader.st
XWDReader.st
--- a/PBMReader.st	Fri Sep 12 13:40:49 2003 +0200
+++ b/PBMReader.st	Fri Sep 12 13:43:19 2003 +0200
@@ -477,7 +477,7 @@
 
 !PBMReader methodsFor:'writing'!
 
-save:image onFile:aFileName
+save:image onStream:aStream
     "save image as PBM/PGM/PNM file on aFileName"
 
     |bitsPerPixel|
@@ -488,13 +488,7 @@
             errorString:('PBM format does not support an imageMask').
     ].
 
-    outStream := FileStream newFileNamed:aFileName.
-    outStream isNil ifTrue:[
-        'PBMReader [error]: file create error' errorPrintNL. 
-        ^ Image fileCreationErrorSignal 
-            raiseWith:image
-            errorString:('file creation error: ' , aFileName asString).
-    ].
+    outStream := aStream.
 
     width := image width.
     height := image height.
@@ -516,6 +510,7 @@
             ^ self writePGMFileOn:outStream
         ].
     ].
+
     ^ Image cannotRepresentImageSignal 
         raiseWith:image
         errorString:('PBMReader cannot represent this image').
@@ -655,7 +650,7 @@
 !PBMReader class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview2/PBMReader.st,v 1.39 2003-09-01 14:47:44 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libview2/PBMReader.st,v 1.40 2003-09-12 11:42:53 cg Exp $'
 ! !
 
 PBMReader initialize!
--- a/TIFFReader.st	Fri Sep 12 13:40:49 2003 +0200
+++ b/TIFFReader.st	Fri Sep 12 13:43:19 2003 +0200
@@ -2111,23 +2111,6 @@
 
 !TIFFReader methodsFor:'writing'!
 
-save:image onFile:aFileName
-    "save image as (uncompressed) TIFF file on aFileName"
-
-    [
-        outStream := aFileName asFilename newReadWriteStream.
-    ] on:FileStream openErrorSignal do:[:ex|
-        ^ Image fileCreationErrorSignal 
-            raiseWith:image
-            errorString:('file creation error: ' , aFileName asString).
-    ].
-    [
-        self save:image onStream:outStream.
-    ] ensure:[
-        outStream close.
-    ].
-!
-
 save:image onStream:aStream
     "save image as (uncompressed) TIFF file on aFileName"
 
@@ -2221,7 +2204,7 @@
 !TIFFReader class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview2/TIFFReader.st,v 1.75 2003-09-01 14:47:53 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libview2/TIFFReader.st,v 1.76 2003-09-12 11:43:19 cg Exp $'
 ! !
 
 TIFFReader initialize!
--- a/XBMReader.st	Fri Sep 12 13:40:49 2003 +0200
+++ b/XBMReader.st	Fri Sep 12 13:43:19 2003 +0200
@@ -316,48 +316,6 @@
 
 !XBMReader methodsFor:'writing'!
 
-save:image onFile:aFileName
-    "save image as XBM file on aFileName.
-     Only depth1 b&w images can be represented in this format."
-
-    |stream|
-
-    "sigh - must check before creating file"
-    (self class canRepresent:image) ifFalse:[
-        ^ Image cannotRepresentImageSignal 
-            raiseWith:image
-            errorString:('XBM format only supports monochrome images').
-    ].
-
-    image mask notNil ifTrue:[
-        Image informationLostQuerySignal
-            raiseWith:image
-            errorString:('XBM format does not support an imageMask').
-    ].
-
-    [
-        stream := aFileName asFilename newReadWriteStream.
-    ] on:FileStream openErrorSignal do:[:ex|
-        ^ Image fileCreationErrorSignal 
-            raiseWith:image
-            errorString:('file creation error: ' , aFileName asString).
-    ].
-
-    self save:image onStream:stream.
-    stream close
-
-    "
-     XBMReader save:(Image fromFile:'../../goodies/bitmaps/xbmBitmaps/TicTacToe.xbm') onFile:'/tmp/test.xbm'
-    "
-    "
-     convert sun icon to XBM format:
-
-     XBMReader save:(Image fromFile:'bitmaps/hello_world.icon') onFile:'test.xbm'
-    "
-
-    "Modified: 27.2.1997 / 12:46:49 / cg"
-!
-
 save:image onStream:aStream
     "save image as XBM cdata on aStream.
      Only depth1 b&w images can be represented in this format."
@@ -429,7 +387,7 @@
 !XBMReader class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview2/XBMReader.st,v 1.49 2003-09-12 10:38:06 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libview2/XBMReader.st,v 1.50 2003-09-12 11:42:56 cg Exp $'
 ! !
 
 XBMReader initialize!
--- a/XPMReader.st	Fri Sep 12 13:40:49 2003 +0200
+++ b/XPMReader.st	Fri Sep 12 13:43:19 2003 +0200
@@ -13,7 +13,7 @@
 "{ Package: 'stx:libview2' }"
 
 ImageReader subclass:#XPMReader
-	instanceVariableNames:'charsPerPixel maskPixelValue characterTranslation'
+	instanceVariableNames:'charsPerPixel maskPixelValue characterTranslation imageName'
 	classVariableNames:''
 	poolDictionaries:''
 	category:'Graphics-Images-Readers'
@@ -504,69 +504,14 @@
      Caveat: currently, only a maximum of roughly 50 colors is handled
              (i.e. very simple images)"
 
-    |nColorsUsed nColors imageName imageMask stream|
-
-    "sigh - check this before creating the file"
-    nColors := nColorsUsed := image usedColors size.
-    (imageMask := image mask) notNil ifTrue:[
-        nColors := nColors + 1.
-    ].
-    nColors > 256 ifTrue:[
-        ^ Image cannotRepresentImageSignal 
-            raiseWith:image
-            errorString:('XPMReader cannot represent this image (too many colors)').
-    ].
-
-    [
-        stream := aFileName asFilename newReadWriteStream.
-    ] on:FileStream openErrorSignal do:[:ex|
-        ^ Image fileCreationErrorSignal 
-            raiseWith:image
-            errorString:('file creation error: ' , aFileName asString).
-    ].
-
     imageName := aFileName asFilename baseName asFilename withoutSuffix asString.
     imageName replaceAll:$. with:$_.
 
-    self save:image onStream:stream named:imageName.
-
-    stream close.
+    super save:image onFile:aFileName.
 !
 
 save:image onStream:aStream
     "save image as XPM file on aStream.
-     Caveat: currently, only a maximum of roughly 50 colors is handled
-            (i.e. very simple images)"
-
-    ^ self
-        save:image 
-        onStream:aStream 
-        named:'unnamed'
-! !
-
-!XPMReader methodsFor:'writing-private'!
-
-colorNameOf:aColor
-    "generate a name for a color. If its a standard color,
-     return its name; otherwise return the hex representation."
-
-    #(white black red green blue
-      yellow magenta cyan orange) do:[:aStandardColorName |
-        aColor = (Color name:aStandardColorName) ifTrue:[
-            ^ aStandardColorName.
-        ]
-    ].
-    ^ '#' 
-     , (aColor redByte hexPrintString:2)
-     , (aColor greenByte hexPrintString:2)
-     , (aColor blueByte hexPrintString:2)
-
-    "Created: / 27.2.1997 / 11:48:40 / cg"
-    "Modified: / 6.6.1998 / 20:58:49 / cg"
-!
-
-save:image onStream:aStream named:imageName
-    "save image as XPM file on aStream.
      Caveat: currently, only a maximum of 256 colors is handled
             (i.e. very simple images)"
 
@@ -602,7 +547,7 @@
     outStream := aStream.
 
     outStream nextPutLine:'/* XPM */'.
-    outStream nextPutLine:'static char *' , imageName , '_xpm[] = {'.
+    outStream nextPutLine:'static char *' , (imageName ? 'unnamed') , '_xpm[] = {'.
     outStream nextPutLine:'/* width height ncolors chars_per_pixel */'.
     outStream nextPutLine:'"' , image width printString , ' '
                               , image height printString , ' '
@@ -653,10 +598,31 @@
     "Modified: / 28.7.1998 / 21:52:13 / cg"
 ! !
 
+!XPMReader methodsFor:'writing-private'!
+
+colorNameOf:aColor
+    "generate a name for a color. If its a standard color,
+     return its name; otherwise return the hex representation."
+
+    #(white black red green blue
+      yellow magenta cyan orange) do:[:aStandardColorName |
+        aColor = (Color name:aStandardColorName) ifTrue:[
+            ^ aStandardColorName.
+        ]
+    ].
+    ^ '#' 
+     , (aColor redByte hexPrintString:2)
+     , (aColor greenByte hexPrintString:2)
+     , (aColor blueByte hexPrintString:2)
+
+    "Created: / 27.2.1997 / 11:48:40 / cg"
+    "Modified: / 6.6.1998 / 20:58:49 / cg"
+! !
+
 !XPMReader class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview2/XPMReader.st,v 1.55 2003-09-12 10:38:04 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libview2/XPMReader.st,v 1.56 2003-09-12 11:42:42 cg Exp $'
 ! !
 
 XPMReader initialize!
--- a/XWDReader.st	Fri Sep 12 13:40:49 2003 +0200
+++ b/XWDReader.st	Fri Sep 12 13:43:19 2003 +0200
@@ -165,7 +165,7 @@
 
 !XWDReader methodsFor:'writing'!
 
-save:image onFile:fileName
+save:image onStream:aStream
     "Save as a version 7 color X11 window dump file (xwd) to the file fileName.
      This produces a mapped color table with 16 bit color.  The xwd file can be 
      viewed by the xwud program and printed with xpr.  
@@ -189,7 +189,7 @@
         thanks to Brad for giving us the base for this mehtod.
     "
 
-    |aStream rgbColor paletteColors ncolors dumpName headerSize|
+    |rgbColor paletteColors ncolors dumpName headerSize|
 
     (self class canRepresent:image) ifFalse:[
         ^ Image cannotRepresentImageSignal 
@@ -209,12 +209,6 @@
     ncolors := paletteColors size.
 
     "create the header (each item is 32 bits long)"
-    aStream := fileName asFilename writeStream.
-    aStream isNil ifTrue:[
-        ^ Image fileCreationErrorSignal 
-            raiseWith:image
-            errorString:('file creation error: ' , fileName asString).
-    ].
 
     aStream binary.
     aStream nextLongPut: headerSize.                                "total header size in bytes"
@@ -286,8 +280,6 @@
 "/    ].
     aStream nextPutAll:image bits.
 
-    aStream close
-
     "
      XWDReader save:(Image fromUser) onFile: '/tmp/st.xwd' 
      (Image fromFile: '/tmp/st.xwd') inspect 
@@ -299,7 +291,7 @@
 !XWDReader class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview2/XWDReader.st,v 1.25 2003-09-01 14:47:38 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libview2/XWDReader.st,v 1.26 2003-09-12 11:42:48 cg Exp $'
 ! !
 
 XWDReader initialize!