PBMReader.st
author ca
Fri, 05 Sep 1997 11:59:23 +0200
changeset 688 b95c0e8c9573
parent 679 0f94c493751c
child 709 813553f7bd20
permissions -rw-r--r--
Call open instead of realize to assign a window group.

"
 COPYRIGHT (c) 1992 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:#PBMReader
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'Graphics-Images-Support'
!

!PBMReader class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1992 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 and saving Portable BitMap-file 
    images (Jef Poskanzers portable bitmap package).

    Reading is supported for 1bit (pbm), greyscale (pgm) and 24bit (ppm) formats.
    (i.e. P1, P3, P4, P5 and P6 formats)
    Currently, only writing of 1-bit images (pbm) is supported.

    Q: should we bring this one to perfection and base all others on
       pipe-readers to the various pbmplus converters ?

    [See also:]
        Image Form Icon
        BlitImageReader FaceReader GIFReader JPEGReader PCXReader 
        ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader 
        XBMReader XPMReader XWDReader 
"
! !

!PBMReader class methodsFor:'initialization'!

initialize
    "install myself in the Image classes fileFormat table
     for the `.pbm', '.pgm' and '.pnm' extensions."

    MIMETypes defineImageType:'image/x-portable-bitmap'  suffix:'pbm' reader:self.
    MIMETypes defineImageType:'image/x-portable-graymap' suffix:'pgm' reader:self.
    MIMETypes defineImageType:'image/x-portable-anymap'  suffix:'pnm' reader:self.
    MIMETypes defineImageType:'image/x-portable-pixmap'  suffix:'ppm' reader:self.

    "Modified: 1.2.1997 / 15:02:14 / cg"
! !

!PBMReader class methodsFor:'testing'!

isValidImageFile:aFileName
    "return true, if aFileName contains a PBM image"

    |inStream pnmType|

    inStream := self streamReadingFile:aFileName.
    inStream isNil ifTrue:[^ false].

    inStream text.

    inStream next ~~ $P ifTrue:[^ false].

    pnmType := inStream next.
    (#( $1 $3 $4 $5 $6 ) includes:pnmType) ifFalse:[^ false].
    ^ true

    "Created: 18.6.1996 / 13:58:59 / cg"
    "Modified: 4.4.1997 / 11:17:11 / cg"
! !

!PBMReader methodsFor:'private'!

skipPBMJunkOn:aStream 
    "this method removes any superfluous characters from the input stream."

    | char foundNL|

    [
        char := aStream peek.
        char == $# ifTrue:[
            "Start of a comment. Skip to end-of-line."
"/            foundNL := (aStream skipUpTo: Character cr) notNil.
            foundNL := (aStream skipThrough: Character cr) notNil.
            foundNL ifFalse: [
                "Must be EOF"
                ^self
            ].
            char := aStream peek].
            aStream atEnd not and: [char isSeparator]
    ] whileTrue: [aStream next]
!

skipXPMJunkOn:aStream
    "this method removes any superfluous characters from the input stream."

    | char |

    [       
        char := aStream peek. 
        aStream atEnd not and: [char isSeparator not]
    ] whileTrue: [aStream next].

    [aStream atEnd not and: [char isSeparator]] whileTrue: [
        aStream next. char := aStream peek
    ].
    aStream atEnd ifTrue: [^char].
    (char isDigit) ifTrue: [ ^char ].
    (char == $") ifTrue: [ 
        aStream next. 
        char := aStream peek. 
        (char isLetterOrDigit 
         or: [(char == $#) 
         or: [char == Character space]]) ifFalse:[
            ^ self skipXPMJunkOn: aStream 
        ] ifTrue: [^char]
    ].

    ^self skipXPMJunkOn: aStream.
! !

!PBMReader methodsFor:'reading from file'!

fromStream:aStream
    "read a Portable bitmap file format as of Jeff Poskanzers Portable Bitmap Package.
     supported are PBM, PGB and PNM files." 

    | pnmType |

    inStream := aStream.
    inStream text.

    inStream next == $P ifFalse:[
        'PBMREADER: PNM format' errorPrintNL.
        ^nil
    ].
    pnmType := inStream next.

    (pnmType == $1) ifTrue: [
        ^ self readDepth1AsciiPBMStream:aStream
    ].
    (pnmType == $3) ifTrue: [
        ^ self readDepth24AsciiPBMStream:aStream
    ].
    (pnmType == $4) ifTrue: [
        ^ self readDepth1PBMStream:aStream
    ].
    (pnmType == $5) ifTrue: [
        ^ self readDepth8PGMStream:aStream
    ].
    (pnmType == $6) ifTrue: [
        ^ self readDepth24PPMStream:aStream
    ].
    'PBMREADER: No recognized PNM file format' errorPrintNL.
    ^ nil

    "
     PBMReader fromFile:'bitmaps/testimg.ppm'
     PBMReader fromFile:'../../fileIn/bitmaps/keyboard.pbm'
     PBMReader fromFile:'/home2/cg/ppm2fli_b1-92/jeff.001'
    "

    "Modified: 4.4.1997 / 11:17:24 / cg"
!

readDepth1AsciiPBMStream:aStream 
    "import portable bitmap ascii (PBM, P1 format); P1 is already read"

    |n bits rowIdx dstIdx bytesPerRow char|

    self skipPBMJunkOn:aStream.
    width := Integer readFrom:aStream.
    width > 0 ifFalse: [
        'PBMREADER: Invalid width' errorPrintNL.
        ^ nil
    ].

    self skipPBMJunkOn:aStream.
    height := Integer readFrom:aStream.
    height > 0 ifFalse: [
        'PBMREADER: Invalid height' errorPrintNL.
        ^ nil
    ].

    aStream nextLine "skipThrough: Character cr".

    bytesPerRow := (width + 7) // 8.
    data := ByteArray new:bytesPerRow * height.
    rowIdx := 1.

    1 to:height do:[:row |
        dstIdx := rowIdx.
        bits := 0. n := 0.
        1 to:width do:[:col |
            char := aStream next.
            [char notNil and:[char isSeparator]] whileTrue:[
                char := aStream next
            ].
            bits := bits bitShift:1.
            char == $1 ifTrue:[
                bits := bits bitOr:1
            ].
            n := n + 1.
            n == 8 ifTrue:[
                data at:dstIdx put:bits.
                dstIdx := dstIdx + 1.
                bits := 0.
                n := 0.
            ].
        ].
        n ~~ 0 ifTrue:[
            data at:dstIdx put:bits.
        ].
        rowIdx := rowIdx + bytesPerRow
    ].

    photometric := #whiteIs0.
    samplesPerPixel := 1.
    bitsPerSample := #(1).

    "Modified: 4.4.1997 / 11:15:51 / cg"
!

readDepth1PBMStream:aStream 
    "import portable bitmap (PBM, P4 format); P4 is already read"

    |bytesPerRow|

    self skipPBMJunkOn:aStream.
    width := Integer readFrom:aStream onError:0.
    width > 0 ifFalse: [
        'PBMREADER: Invalid width' errorPrintNL.
        ^ nil
    ].

    self skipPBMJunkOn:aStream.
    height := Integer readFrom:aStream onError:0.
    height > 0 ifFalse: [
        'PBMREADER: Invalid height' errorPrintNL.
        ^ nil
    ].

    aStream nextLine "skipThrough: Character cr".

    bytesPerRow := width // 8.
    ((width \\ 8) ~~ 0) ifTrue:[
        bytesPerRow := bytesPerRow + 1
    ].

    "/ the rest is the binary image data ...
    aStream binary.
    data := ByteArray uninitializedNew:(bytesPerRow*height).
    aStream nextBytes:(data size) into:data.

    photometric := #blackIs0.
    samplesPerPixel := 1.
    bitsPerSample := #(1).

    "Modified: 11.4.1997 / 16:48:54 / cg"
!

readDepth24AsciiPBMStream:aStream
    "import ascii portable pixmap (PBM, P3 format); P3 is already read"

    |maxval 
     nBytes "{Class: SmallInteger }" 
     v      "{Class: SmallInteger }"
     c|

    self skipPBMJunkOn:aStream.
    width := Integer readFrom:aStream.
    width > 0 ifFalse: [
        'PBMREADER: Invalid width' errorPrintNL.
        ^ nil
    ].

    self skipPBMJunkOn:aStream.
    height := Integer readFrom:aStream.
    height > 0 ifFalse: [
        'PBMREADER: Invalid height' errorPrintNL.
        ^ nil
    ].

    self skipPBMJunkOn:aStream.
    maxval := Integer readFrom:aStream.
    maxval >= 256 ifTrue: [
        'PBMREADER: format error' errorPrintNL.
        ^ nil
    ].

    aStream skipThrough: Character cr.

    nBytes := width*height*3.
    data := ByteArray new:nBytes.
    1 to:nBytes do:[:i |
        aStream skipSeparators.
        v := 0.
        c := aStream next.
        [c isDigit] whileTrue:[
            v := v * 10 + (c digitValue).
            c := aStream next.
        ].
        data at:i put:v
    ].

    photometric := #rgb.
    samplesPerPixel := 3.
    bitsPerSample := #(8 8 8).

    "Created: 4.4.1997 / 11:08:38 / cg"
    "Modified: 4.4.1997 / 11:16:07 / cg"
!

readDepth24PPMStream:aStream
    "import portable pixmap (PPM, P6 format); P6 is already read"

    | maxval |

    self skipPBMJunkOn:aStream.
    width := Integer readFrom:aStream.
    width > 0 ifFalse: [
        'PBMREADER: Invalid width' errorPrintNL.
        ^ nil
    ].

    self skipPBMJunkOn:aStream.
    height := Integer readFrom:aStream.
    height > 0 ifFalse: [
        'PBMREADER: Invalid height' errorPrintNL.
        ^ nil
    ].

    self skipPBMJunkOn:aStream.
    maxval := Integer readFrom:aStream.
    maxval >= 256 ifTrue: [
        'PBMREADER: format error' errorPrintNL.
        ^ nil
    ].

    aStream skipThrough: Character cr.

    "/ the rest is the binary image data ...
    aStream binary.
    data := ByteArray uninitializedNew:(width*height*3).
    aStream nextBytes:(data size) into:data.

    photometric := #rgb.
    samplesPerPixel := 3.
    bitsPerSample := #(8 8 8).

    "Modified: 11.4.1997 / 16:36:51 / cg"
!

readDepth8PGMStream:aStream 
    "import portable gray map (PGM, P5 format); P5 is already read"

    |maxval|

    self skipPBMJunkOn:aStream.
    width := Integer readFrom:aStream.
    width > 0 ifFalse:[ 
        'PBMREADER: Invalid width' errorPrintNL.
        ^ nil
    ].
    self skipPBMJunkOn:aStream.
    height := Integer readFrom:aStream.
    height > 0 ifFalse:[ 
        'PBMREADER: Invalid height' errorPrintNL.
        ^ nil
    ].
    self skipPBMJunkOn:aStream.
    maxval := Integer readFrom:aStream.
    maxval >= 256 ifTrue:[
        'PBMREADER: Invalid format' errorPrintNL.
        ^ nil
    ].
    aStream skipThrough: Character cr.

    "/ the rest is the binary image data ...
    aStream binary.
    data := ByteArray uninitializedNew:(width*height).
    aStream nextBytes:(data size) into:data.

    photometric := #blackIs0.
    samplesPerPixel := 1.
    bitsPerSample := #(8).

    "Modified: 11.4.1997 / 16:47:13 / cg"
! !

!PBMReader methodsFor:'testing '!

canRepresent:anImage
    "return true, if anImage can be represented in my file format.
     Currently only B&W and Depth8 images are supported."

    |depth|

    anImage photometric == #rgb ifTrue:[
	^ false  "/ not yet implemented
    ].
    (depth := anImage depth) == 1 ifTrue:[^ true].
    depth == 8 ifTrue:[^ true].
    ^ false
! !

!PBMReader methodsFor:'writing to file'!

save:image onFile:aFileName
    "save image as PBM/PGM/PNM file on aFileName"

    outStream := FileStream newFileNamed:aFileName.
    outStream isNil ifTrue:[
        'PBMReader [error]: file create error' errorPrintNL. 
        ^ Image fileCreationErrorSignal 
            raiseWith:image
            errorString:('file creation error: ' , aFileName asString).
    ].

    image mask notNil ifTrue:[
        Image informationLostQuerySignal
            raiseWith:image
            errorString:('PBM format does not support an imageMask').
    ].

    width := image width.
    height := image height.
    photometric := image photometric.
    samplesPerPixel := image samplesPerPixel.
    bitsPerSample := image bitsPerSample.
    colorMap := image colorMap.
    data := image bits.

    photometric == #rgb ifTrue:[
        ^ self writePNMFileOn:outStream
    ].
    samplesPerPixel == 1 ifTrue:[
        ((bitsPerSample at:1) == 1) ifTrue:[
            ^ self writePBMFileOn:outStream
        ].
        ((bitsPerSample at:1) == 8) ifTrue:[
            ^ self writePGMFileOn:outStream
        ].
    ].
    ^ Image cannotRepresentImageSignal 
        raiseWith:image
        errorString:('PBMReader cannot represent this image').

    "
     |img|

     img := Image fromFile:'bitmaps/SBrowser.xbm'.
     img inspect.
     PBMReader save:img onFile:'test.pbm'.
     img := Image fromFile:'test.pbm'.
     img inspect.


     |img mono|

     img := Image fromFile:'bitmaps/garfield.gif'.
     img inspect.
     mono := img asMonochromeFormOn:Display.
     img := mono asImage.
     img inspect.
     PBMReader save:img onFile:'test.pbm'.
     img := Image fromFile:'test.pbm'.
     img inspect.
    "

    "Modified: 27.2.1997 / 12:45:42 / cg"
!

writePBMFileOn:aStream
    "Saves the receivers image on the file fileName in Portable Bitmap format.
     See the class method pbmSyntax for details of the format."

    aStream nextPutAll:'P4'; cr.
    aStream nextPutAll:'#  Converted from Smalltalk Form on '.
    aStream nextPutAll:Date today printString.
    aStream nextPutAll:' at ', Time now printString.
    aStream cr.
    aStream nextPutAll:width printString.
    aStream space.
    aStream nextPutAll:height printString.
    aStream cr.

    aStream binary.
    photometric == #blackIs0 ifTrue:[
	aStream nextPutAll:data.
    ] ifFalse:[
	data invert.
	aStream nextPutAll:data.
	data invert.
    ].
    aStream close.
!

writePGMFileOn:outStream
    "raise an error - this is not yet implemented"

    self error:'not yet implemented'
!

writePNMFileOn:outStream
    "raise an error - this is not yet implemented"

    self error:'not yet implemented'
! !

!PBMReader class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libview2/PBMReader.st,v 1.28 1997-07-28 11:18:36 cg Exp $'
! !
PBMReader initialize!