XWDReader.st
author Claus Gittinger <cg@exept.de>
Sat, 12 May 2018 14:23:45 +0200
changeset 4088 bbf9b58f99c8
parent 4009 bece1481d314
permissions -rw-r--r--
#FEATURE by cg class: MIMETypes class changed: #initializeFileInfoMappings class: MIMETypes::MIMEType added: #asMimeType #isCHeaderType #isCPPSourceType #isCSourceType
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44
c6cf7d0d6337 *** empty log message ***
claus
parents: 42
diff changeset
     1
"
204
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
     2
     COPYRIGHT (c) 1995 by Claus Gittinger
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
     3
              All Rights Reserved
44
c6cf7d0d6337 *** empty log message ***
claus
parents: 42
diff changeset
     4
204
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
     5
     This software is furnished under a license and may be used
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
     6
     only in accordance with the terms of that license and with the
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
     7
     inclusion of the above copyright notice.   This software may not
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
     8
     be provided or otherwise made available to, or used by, any
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
     9
     other person.  No title to or ownership of the software is
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    10
     hereby transferred.
51
ac84315b8181 *** empty log message ***
claus
parents: 49
diff changeset
    11
ac84315b8181 *** empty log message ***
claus
parents: 49
diff changeset
    12
204
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    13
     The above copyright does not apply to:
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    14
        XWDReader>>save:onFile:
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    15
     which was written by Brad Schoening <brad@boole.com> 
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    16
     who placed it into the public domain.
44
c6cf7d0d6337 *** empty log message ***
claus
parents: 42
diff changeset
    17
"
4009
bece1481d314 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
    18
"{ Package: 'stx:libview2' }"
42
ab4cc6362a80 Initial revision
claus
parents:
diff changeset
    19
4009
bece1481d314 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
    20
"{ NameSpace: Smalltalk }"
1736
a8f1fcc3e6bc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    21
391
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
    22
ImageReader subclass:#XWDReader
204
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    23
	instanceVariableNames:''
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    24
	classVariableNames:''
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    25
	poolDictionaries:''
1745
4fa0fad2a463 code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents: 1736
diff changeset
    26
	category:'Graphics-Images-Readers'
42
ab4cc6362a80 Initial revision
claus
parents:
diff changeset
    27
!
ab4cc6362a80 Initial revision
claus
parents:
diff changeset
    28
391
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
    29
!XWDReader class methodsFor:'documentation'!
44
c6cf7d0d6337 *** empty log message ***
claus
parents: 42
diff changeset
    30
c6cf7d0d6337 *** empty log message ***
claus
parents: 42
diff changeset
    31
copyright
c6cf7d0d6337 *** empty log message ***
claus
parents: 42
diff changeset
    32
"
204
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    33
     COPYRIGHT (c) 1995 by Claus Gittinger
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    34
              All Rights Reserved
44
c6cf7d0d6337 *** empty log message ***
claus
parents: 42
diff changeset
    35
204
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    36
     This software is furnished under a license and may be used
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    37
     only in accordance with the terms of that license and with the
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    38
     inclusion of the above copyright notice.   This software may not
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    39
     be provided or otherwise made available to, or used by, any
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    40
     other person.  No title to or ownership of the software is
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    41
     hereby transferred.
51
ac84315b8181 *** empty log message ***
claus
parents: 49
diff changeset
    42
ac84315b8181 *** empty log message ***
claus
parents: 49
diff changeset
    43
204
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    44
     The above copyright does not apply to:
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    45
        XWDReader>>save:onFile:
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    46
     which was written by Brad Schoening <brad@boole.com> 
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    47
     who placed it into the public domain.
44
c6cf7d0d6337 *** empty log message ***
claus
parents: 42
diff changeset
    48
"
c6cf7d0d6337 *** empty log message ***
claus
parents: 42
diff changeset
    49
!
c6cf7d0d6337 *** empty log message ***
claus
parents: 42
diff changeset
    50
c6cf7d0d6337 *** empty log message ***
claus
parents: 42
diff changeset
    51
documentation
c6cf7d0d6337 *** empty log message ***
claus
parents: 42
diff changeset
    52
"
205
ddb3c0dfcc0d commentary
Claus Gittinger <cg@exept.de>
parents: 204
diff changeset
    53
    this class provides methods for loading/saving of x-window dump (xwd) images.
204
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    54
    Both reading and writing of images is supported.
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    55
4009
bece1481d314 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
    56
    Notice, that an xserver can be started on a mapped xwd-format file with
bece1481d314 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
    57
        Xvfb :1 -fbdir /var/tmp
bece1481d314 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
    58
bece1481d314 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
    59
    then an application started on it with:
bece1481d314 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
    60
        xterm -display :1
bece1481d314 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
    61
bece1481d314 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
    62
    and finally, a screenshot be taken by me, with:
bece1481d314 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
    63
        XWDReader fromFile:'/var/tmp/Xvfb_screen0'
bece1481d314 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
    64
        
bece1481d314 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
    65
204
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    66
    [See also:]
234
b6352d13e792 xrefs in documentation
Claus Gittinger <cg@exept.de>
parents: 210
diff changeset
    67
        Image Form Icon
204
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    68
        BlitImageReader FaceReader GIFReader JPEGReader PBMReader PCXReader 
210
5405de794686 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 205
diff changeset
    69
        ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader 
204
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    70
        XBMReader XPMReader 
44
c6cf7d0d6337 *** empty log message ***
claus
parents: 42
diff changeset
    71
"
c6cf7d0d6337 *** empty log message ***
claus
parents: 42
diff changeset
    72
! !
c6cf7d0d6337 *** empty log message ***
claus
parents: 42
diff changeset
    73
398
aef700d15416 new suffix-table
Claus Gittinger <cg@exept.de>
parents: 391
diff changeset
    74
!XWDReader class methodsFor:'initialization'!
aef700d15416 new suffix-table
Claus Gittinger <cg@exept.de>
parents: 391
diff changeset
    75
aef700d15416 new suffix-table
Claus Gittinger <cg@exept.de>
parents: 391
diff changeset
    76
initialize
aef700d15416 new suffix-table
Claus Gittinger <cg@exept.de>
parents: 391
diff changeset
    77
    "tell Image-class, that a new fileReader is present
aef700d15416 new suffix-table
Claus Gittinger <cg@exept.de>
parents: 391
diff changeset
    78
     for the '.xwd' extension."
aef700d15416 new suffix-table
Claus Gittinger <cg@exept.de>
parents: 391
diff changeset
    79
647
6f26c76aa0c9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 623
diff changeset
    80
    MIMETypes defineImageType:'image/x-xwindowdump' suffix:'xwd' reader:self
398
aef700d15416 new suffix-table
Claus Gittinger <cg@exept.de>
parents: 391
diff changeset
    81
aef700d15416 new suffix-table
Claus Gittinger <cg@exept.de>
parents: 391
diff changeset
    82
    "Created: 1.2.1997 / 15:04:46 / cg"
aef700d15416 new suffix-table
Claus Gittinger <cg@exept.de>
parents: 391
diff changeset
    83
! !
aef700d15416 new suffix-table
Claus Gittinger <cg@exept.de>
parents: 391
diff changeset
    84
391
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
    85
!XWDReader class methodsFor:'queries'!
51
ac84315b8181 *** empty log message ***
claus
parents: 49
diff changeset
    86
ac84315b8181 *** empty log message ***
claus
parents: 49
diff changeset
    87
canRepresent:anImage
391
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
    88
    "return true, if anImage can be represented in my file format.
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
    89
     Only depth8 palette images are supported."
51
ac84315b8181 *** empty log message ***
claus
parents: 49
diff changeset
    90
391
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
    91
    anImage depth ~~ 8 ifTrue:[^ false].
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
    92
    anImage photometric ~~ #palette ifTrue:[^ false].
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
    93
    ^ true
51
ac84315b8181 *** empty log message ***
claus
parents: 49
diff changeset
    94
! !
ac84315b8181 *** empty log message ***
claus
parents: 49
diff changeset
    95
1805
93f557cbe600 category changes
Claus Gittinger <cg@exept.de>
parents: 1754
diff changeset
    96
!XWDReader methodsFor:'reading'!
42
ab4cc6362a80 Initial revision
claus
parents:
diff changeset
    97
1736
a8f1fcc3e6bc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    98
readImage
a8f1fcc3e6bc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    99
    "read an image in XWD (X Window Dump) format from my inStream."
42
ab4cc6362a80 Initial revision
claus
parents:
diff changeset
   100
391
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   101
    |header nColors pad 
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   102
     srcRowByteSize bytesPerRow bitsPerPixel colormapSize depth 
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   103
     dstIndex|
42
ab4cc6362a80 Initial revision
claus
parents:
diff changeset
   104
1736
a8f1fcc3e6bc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   105
    inStream binary.
42
ab4cc6362a80 Initial revision
claus
parents:
diff changeset
   106
1736
a8f1fcc3e6bc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   107
    header := (1 to: 25) collect: [:i | inStream nextLong].
391
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   108
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   109
    "skip ..."
1736
a8f1fcc3e6bc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   110
    101 to:(header at: 1) do: [:i | inStream next].
391
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   111
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   112
    depth := header at: 4.
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   113
    width := header at: 5.
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   114
    height := header at: 6.
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   115
    pad := header at: 11.
42
ab4cc6362a80 Initial revision
claus
parents:
diff changeset
   116
1846
d29322944b05 dimensionReport
Claus Gittinger <cg@exept.de>
parents: 1814
diff changeset
   117
    self reportDimension.
d29322944b05 dimensionReport
Claus Gittinger <cg@exept.de>
parents: 1814
diff changeset
   118
391
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   119
    bitsPerPixel := header at: 12.
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   120
    bitsPerPixel == 24 ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   121
        bitsPerSample := #(8 8 8).
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   122
        samplesPerPixel := 3.
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   123
        photometric := #rgb
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   124
    ] ifFalse:[
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   125
        bitsPerSample := Array with:bitsPerPixel.
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   126
        samplesPerPixel := 1.
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   127
        photometric := #palette
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   128
    ].
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   129
"/  depth ~~ bitsPerPixel ifTrue:[self halt].
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   130
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   131
    colormapSize := header at: 19.
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   132
    nColors := header at: 20.
44
c6cf7d0d6337 *** empty log message ***
claus
parents: 42
diff changeset
   133
391
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   134
    colorMap := Array new:colormapSize.
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   135
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   136
    1 to:nColors do:[:i |
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   137
        |clr r g b|
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   138
1736
a8f1fcc3e6bc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   139
        inStream nextLong.
4009
bece1481d314 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   140
        r := inStream nextUnsignedInt16MSB:true.
bece1481d314 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   141
        g := inStream nextUnsignedInt16MSB:true.
bece1481d314 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   142
        b := inStream nextUnsignedInt16MSB:true.
391
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   143
        clr := ColorValue scaledRed: (r bitShift: -3)
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   144
                        scaledGreen: (g bitShift: -3)
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   145
                         scaledBlue: (b bitShift: -3).
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   146
        colorMap at:i put:clr.
4009
bece1481d314 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   147
        inStream nextUnsignedInt16MSB:true.
44
c6cf7d0d6337 *** empty log message ***
claus
parents: 42
diff changeset
   148
    ].
c6cf7d0d6337 *** empty log message ***
claus
parents: 42
diff changeset
   149
391
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   150
    nColors+1 to:colormapSize do: [:i | colorMap at:i put:Color black].
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   151
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   152
    bytesPerRow := width * bitsPerPixel // 8.
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   153
    ((width * bitsPerPixel \\ 8) ~~ 0) ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   154
        bytesPerRow := bytesPerRow + 1
44
c6cf7d0d6337 *** empty log message ***
claus
parents: 42
diff changeset
   155
    ].
391
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   156
    srcRowByteSize := width * bitsPerPixel + pad - 1 // pad * (pad / 8).
42
ab4cc6362a80 Initial revision
claus
parents:
diff changeset
   157
391
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   158
    data := ByteArray uninitializedNew: srcRowByteSize * height.
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   159
    srcRowByteSize == bytesPerRow ifTrue:[
1736
a8f1fcc3e6bc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   160
        inStream nextBytes:srcRowByteSize * height into:data.
391
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   161
    ] ifFalse:[
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   162
        dstIndex := 1.
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   163
        1 to:height do:[:y |
1736
a8f1fcc3e6bc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   164
            inStream nextBytes:bytesPerRow into:data startingAt:dstIndex.
a8f1fcc3e6bc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   165
            inStream next:(srcRowByteSize - bytesPerRow).
391
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   166
            dstIndex := dstIndex + bytesPerRow.
390
d00bee0b624a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 259
diff changeset
   167
        ].
391
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   168
    ]
1736
a8f1fcc3e6bc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   169
391
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   170
    "
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   171
     XWDReader fromFile:'testfile.xwd'
42
ab4cc6362a80 Initial revision
claus
parents:
diff changeset
   172
    "
49
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   173
    "
391
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   174
     XWDReader save:(Image fromUser) onFile: '/tmp/st.xwd' 
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   175
     (Image fromFile: '/tmp/st.xwd') inspect 
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   176
    "
4009
bece1481d314 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   177
bece1481d314 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   178
    "Modified: / 01-09-2017 / 15:35:34 / cg"
42
ab4cc6362a80 Initial revision
claus
parents:
diff changeset
   179
! !
ab4cc6362a80 Initial revision
claus
parents:
diff changeset
   180
1805
93f557cbe600 category changes
Claus Gittinger <cg@exept.de>
parents: 1754
diff changeset
   181
!XWDReader methodsFor:'writing'!
49
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   182
1814
2f204c2a957d image saving - use save:onStream:
Claus Gittinger <cg@exept.de>
parents: 1805
diff changeset
   183
save:image onStream:aStream
49
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   184
    "Save as a version 7 color X11 window dump file (xwd) to the file fileName.
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   185
     This produces a mapped color table with 16 bit color.  The xwd file can be 
204
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   186
     viewed by the xwud program and printed with xpr.  
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   187
     No compression is performed.
49
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   188
205
ddb3c0dfcc0d commentary
Claus Gittinger <cg@exept.de>
parents: 204
diff changeset
   189
     See the file ...include/X11/XWDFile.h for a definition of the format."
49
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   190
205
ddb3c0dfcc0d commentary
Claus Gittinger <cg@exept.de>
parents: 204
diff changeset
   191
    "
49
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   192
     Notice:
204
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   193
        this method was adapted from a goody in the uiuc archive 
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   194
        (Prime time freeware).
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   195
        The original files header is:
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   196
            NAME            imageToXWD
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   197
            AUTHOR          Brad Schoening <brad@boole.com>
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   198
            FUNCTION        Writes a Smalltalk image to an X11 xwd file
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   199
            ST-VERSION      PPST 4.0 or 4.1
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   200
            DISTRIBUTION    world
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   201
            VERSION         1.0
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   202
            DATE            July 1993
51
ac84315b8181 *** empty log message ***
claus
parents: 49
diff changeset
   203
204
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   204
        thanks to Brad for giving us the base for this mehtod.
49
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   205
    "
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   206
1814
2f204c2a957d image saving - use save:onStream:
Claus Gittinger <cg@exept.de>
parents: 1805
diff changeset
   207
    |rgbColor paletteColors ncolors dumpName headerSize|
49
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   208
51
ac84315b8181 *** empty log message ***
claus
parents: 49
diff changeset
   209
    (self class canRepresent:image) ifFalse:[
461
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 398
diff changeset
   210
        ^ Image cannotRepresentImageSignal 
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 398
diff changeset
   211
            raiseWith:image
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 398
diff changeset
   212
            errorString:('XWD format cannot represent this image').
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 398
diff changeset
   213
    ].
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 398
diff changeset
   214
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 398
diff changeset
   215
    image mask notNil ifTrue:[
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 398
diff changeset
   216
        Image informationLostQuerySignal
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 398
diff changeset
   217
            raiseWith:image
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 398
diff changeset
   218
            errorString:('XWD format does not support an imageMask').
49
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   219
    ].
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   220
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   221
    dumpName := 'stdin'.
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   222
    headerSize := 4 * (25 + (dumpName size / 4) ceiling).
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   223
    paletteColors := image palette "colors".
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   224
    ncolors := paletteColors size.
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   225
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   226
    "create the header (each item is 32 bits long)"
461
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 398
diff changeset
   227
49
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   228
    aStream binary.
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   229
    aStream nextLongPut: headerSize.                                "total header size in bytes"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   230
    aStream nextLongPut: 7.                                         "XWD file version"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   231
    aStream nextLongPut: 2.                                         "pixmap format : ZPixmap"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   232
    aStream nextLongPut: 8.                                         "pixmap depth"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   233
    aStream nextLongPut: image width.                               "pixmap cols"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   234
    aStream nextLongPut: image height.                              "pixmap rows"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   235
    aStream nextLongPut: 0.                                         "bitmap x offset"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   236
    aStream nextLongPut: 1.                                         "byte order: MSBFirst"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   237
    aStream nextLongPut: 8.                                         "bitmap unit"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   238
    aStream nextLongPut: 1.                                         "bitmap bit order: MSBFirst"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   239
    aStream nextLongPut: 8.                                         "bitmap scanline pad"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   240
    aStream nextLongPut: 8.                                         "bits per pixel"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   241
    aStream nextLongPut: image width.                               "bytes per scanline"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   242
    aStream nextLongPut: 3.                                         "colormap class : PseudoColor"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   243
    aStream nextLongPut: 0.                                         "Z red mask"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   244
    aStream nextLongPut: 0.                                         "Z green mask"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   245
    aStream nextLongPut: 0.                                         "Z blue mask"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   246
    aStream nextLongPut: 8.                                         "bits per rgb"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   247
    aStream nextLongPut: 256.                                       "number of color map entries"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   248
    aStream nextLongPut: ncolors.                                   "number of color structures"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   249
    aStream nextLongPut: image width.                               "window width"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   250
    aStream nextLongPut: image height.                              "window height"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   251
    aStream nextLongPut: 0.                                         "window upper left x coordinate"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   252
    aStream nextLongPut: 0.                                         "window upper left y coordinate"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   253
    aStream nextLongPut: 0.                                         "window border width"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   254
    aStream nextPutAll: dumpName asByteArray.       "name of dump"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   255
    "Pad the string to the next 32-bit boundary"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   256
    aStream nextPut: 0. "/ 6
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   257
    aStream nextPut: 0. "/ 7
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   258
    aStream nextPut: 0. "/ 8
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   259
1754
cc844dc3504e avoid position
Claus Gittinger <cg@exept.de>
parents: 1745
diff changeset
   260
"/    [(aStream position1Based rem: 4) == 0] whileFalse: [ aStream nextPut: 0 ].
49
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   261
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   262
    "Write out the color table.  Each color table entry is 12 bytes long composed of:
204
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   263
                    an index                (4 bytes)
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   264
                    red color value         (2 bytes)
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   265
                    green color value       (2 bytes)
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   266
                    blue color value        (2 bytes)
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   267
                    flag values             (1 byte)
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   268
                    pad                     (1 byte)
49
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   269
    "
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   270
    0 to: ncolors-1 do: [ :index |
204
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   271
        |r g b|
49
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   272
204
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   273
        aStream nextLongPut: index.
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   274
        rgbColor := paletteColors at: (1+index).
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   275
        (rgbColor isNil) ifTrue: [ rgbColor := ColorValue white ].
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   276
        r := (rgbColor red / 100.0 * 65535) rounded.
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   277
        g := (rgbColor green / 100.0 * 65535) rounded.
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   278
        b := (rgbColor blue / 100.0 * 65535) rounded.
49
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   279
204
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   280
        aStream nextWordPut:r.
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   281
        aStream nextWordPut:g.
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   282
        aStream nextWordPut:b.
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   283
        aStream nextPut: 7.                     "flags"
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   284
        aStream nextPut: 0.                     "pad"
49
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   285
    ].
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   286
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   287
    "Write out the pixels as index color values"
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   288
"/    Cursor write showWhile: [ 
99
claus
parents: 96
diff changeset
   289
"/            |cindex|
83
claus
parents: 51
diff changeset
   290
"/
49
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   291
"/            1 to: (image height) do: [ :row |
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   292
"/                    1 to: (image width) do: [ :col |
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   293
"/                            cindex := image atPoint: (col-1)@(row-1).
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   294
"/                            aStream nextPut: cindex.]]
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   295
"/    ].
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   296
    aStream nextPutAll:image bits.
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   297
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   298
    "
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   299
     XWDReader save:(Image fromUser) onFile: '/tmp/st.xwd' 
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   300
     (Image fromFile: '/tmp/st.xwd') inspect 
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   301
    "
461
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 398
diff changeset
   302
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 398
diff changeset
   303
    "Modified: 27.2.1997 / 12:45:15 / cg"
49
f7938135fb9a *** empty log message ***
claus
parents: 46
diff changeset
   304
! !
204
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   305
391
Claus Gittinger <cg@exept.de>
parents: 390
diff changeset
   306
!XWDReader class methodsFor:'documentation'!
204
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   307
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   308
version
4009
bece1481d314 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   309
    ^ '$Header$'
204
277d2523d8cb commentary
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   310
! !
1736
a8f1fcc3e6bc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   311
4009
bece1481d314 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   312
398
aef700d15416 new suffix-table
Claus Gittinger <cg@exept.de>
parents: 391
diff changeset
   313
XWDReader initialize!