PCXReader.st
author Claus Gittinger <cg@exept.de>
Tue, 29 Aug 2017 02:19:30 +0200
changeset 3994 8ab8729d5dec
parent 3993 921798977590
child 3995 ab4741a8e8f5
permissions -rw-r--r--
#BUGFIX by cg class: PCXReader added: #readCompressedDepth24Data changed: #readCompressedData #readCompressedData_2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27
93da277c5ddd Initial revision
claus
parents:
diff changeset
     1
"
93da277c5ddd Initial revision
claus
parents:
diff changeset
     2
 COPYRIGHT (c) 1994 by Claus Gittinger
93da277c5ddd Initial revision
claus
parents:
diff changeset
     3
	      All Rights Reserved
93da277c5ddd Initial revision
claus
parents:
diff changeset
     4
93da277c5ddd Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
93da277c5ddd Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
93da277c5ddd Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
93da277c5ddd Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
93da277c5ddd Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
93da277c5ddd Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
93da277c5ddd Initial revision
claus
parents:
diff changeset
    11
"
1562
3b444efb61ee bug fix: palette is padded after RLE data.
Claus Gittinger <cg@exept.de>
parents: 812
diff changeset
    12
"{ Package: 'stx:libview2' }"
3b444efb61ee bug fix: palette is padded after RLE data.
Claus Gittinger <cg@exept.de>
parents: 812
diff changeset
    13
3990
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
    14
"{ NameSpace: Smalltalk }"
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
    15
27
93da277c5ddd Initial revision
claus
parents:
diff changeset
    16
ImageReader subclass:#PCXReader
3991
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
    17
	instanceVariableNames:'header buffer nBuffer bufferIndex sourceBytesPerRow depth nPlanes'
201
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
    18
	classVariableNames:''
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
    19
	poolDictionaries:''
1745
4fa0fad2a463 code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents: 1737
diff changeset
    20
	category:'Graphics-Images-Readers'
27
93da277c5ddd Initial revision
claus
parents:
diff changeset
    21
!
93da277c5ddd Initial revision
claus
parents:
diff changeset
    22
29
e04e1aceff6f *** empty log message ***
claus
parents: 27
diff changeset
    23
!PCXReader class methodsFor:'documentation'!
e04e1aceff6f *** empty log message ***
claus
parents: 27
diff changeset
    24
e04e1aceff6f *** empty log message ***
claus
parents: 27
diff changeset
    25
copyright
e04e1aceff6f *** empty log message ***
claus
parents: 27
diff changeset
    26
"
e04e1aceff6f *** empty log message ***
claus
parents: 27
diff changeset
    27
 COPYRIGHT (c) 1994 by Claus Gittinger
e04e1aceff6f *** empty log message ***
claus
parents: 27
diff changeset
    28
	      All Rights Reserved
e04e1aceff6f *** empty log message ***
claus
parents: 27
diff changeset
    29
e04e1aceff6f *** empty log message ***
claus
parents: 27
diff changeset
    30
 This software is furnished under a license and may be used
e04e1aceff6f *** empty log message ***
claus
parents: 27
diff changeset
    31
 only in accordance with the terms of that license and with the
e04e1aceff6f *** empty log message ***
claus
parents: 27
diff changeset
    32
 inclusion of the above copyright notice.   This software may not
e04e1aceff6f *** empty log message ***
claus
parents: 27
diff changeset
    33
 be provided or otherwise made available to, or used by, any
e04e1aceff6f *** empty log message ***
claus
parents: 27
diff changeset
    34
 other person.  No title to or ownership of the software is
e04e1aceff6f *** empty log message ***
claus
parents: 27
diff changeset
    35
 hereby transferred.
e04e1aceff6f *** empty log message ***
claus
parents: 27
diff changeset
    36
"
e04e1aceff6f *** empty log message ***
claus
parents: 27
diff changeset
    37
!
e04e1aceff6f *** empty log message ***
claus
parents: 27
diff changeset
    38
e04e1aceff6f *** empty log message ***
claus
parents: 27
diff changeset
    39
documentation
e04e1aceff6f *** empty log message ***
claus
parents: 27
diff changeset
    40
"
3992
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
    41
    this class provides methods to load PCX bitmap files.
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
    42
    PCX used to be a popular image format in the early PC times,
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
    43
    but became almost obsolete in the meantime.
3993
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
    44
    This reader is not tuned and performs slow on non-8bit images;
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
    45
    if at all, use it to convert old image files to newer formats,
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
    46
    such as png, tiff or jpg.
3992
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
    47
    
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
    48
    Due to not having too many examples for testing, 
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
    49
    this could fail to read some files. 
201
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
    50
    (especially, I have no uncompressed files for testing).
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
    51
3992
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
    52
    Only 1,2,4,8 and 24-bit PCX images are supported.
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
    53
    Image writing is not.
201
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
    54
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
    55
    [See also:]
3992
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
    56
        Image Form Icon
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
    57
        BlitImageReader FaceReader GIFReader JPEGReader PBMReader 
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
    58
        ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader 
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
    59
        XBMReader XPMReader XWDReader 
29
e04e1aceff6f *** empty log message ***
claus
parents: 27
diff changeset
    60
"
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
    61
!
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
    62
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
    63
examples
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
    64
"
3992
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
    65
                                                        [exBegin]
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
    66
    Image fromFile:'/usr/share/lilo/suse_640x480.pcx'
3992
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
    67
                                                        [exEnd]
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
    68
                                                        [exBegin]
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
    69
    Image fromFile:'../../goodies/bitmaps/pcxImages/lena_depth8_palette.pcx'
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
    70
                                                        [exEnd]
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
    71
"
29
e04e1aceff6f *** empty log message ***
claus
parents: 27
diff changeset
    72
! !
e04e1aceff6f *** empty log message ***
claus
parents: 27
diff changeset
    73
43
e85c7d392833 *** empty log message ***
claus
parents: 35
diff changeset
    74
!PCXReader class methodsFor:'initialization'!
e85c7d392833 *** empty log message ***
claus
parents: 35
diff changeset
    75
e85c7d392833 *** empty log message ***
claus
parents: 35
diff changeset
    76
initialize
201
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
    77
    "tell Image-class, that a new fileReader is present
398
aef700d15416 new suffix-table
Claus Gittinger <cg@exept.de>
parents: 259
diff changeset
    78
     for the '.pcx' extension."
43
e85c7d392833 *** empty log message ***
claus
parents: 35
diff changeset
    79
647
6f26c76aa0c9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 630
diff changeset
    80
    MIMETypes defineImageType:'image/x-pcx'  suffix:'pcx' reader:self.
201
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
    81
630
48657c687a8b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 566
diff changeset
    82
    "Modified: 27.6.1997 / 18:39:23 / cg"
43
e85c7d392833 *** empty log message ***
claus
parents: 35
diff changeset
    83
! !
e85c7d392833 *** empty log message ***
claus
parents: 35
diff changeset
    84
27
93da277c5ddd Initial revision
claus
parents:
diff changeset
    85
!PCXReader class methodsFor:'testing'!
93da277c5ddd Initial revision
claus
parents:
diff changeset
    86
93da277c5ddd Initial revision
claus
parents:
diff changeset
    87
isValidImageFile:aFilename
93da277c5ddd Initial revision
claus
parents:
diff changeset
    88
    "return true, if aFilename contains a PCX image"
93da277c5ddd Initial revision
claus
parents:
diff changeset
    89
29
e04e1aceff6f *** empty log message ***
claus
parents: 27
diff changeset
    90
    |count header inStream|
27
93da277c5ddd Initial revision
claus
parents:
diff changeset
    91
93da277c5ddd Initial revision
claus
parents:
diff changeset
    92
    inStream := self streamReadingFile:aFilename.
93da277c5ddd Initial revision
claus
parents:
diff changeset
    93
    inStream isNil ifTrue:[^ false].
93da277c5ddd Initial revision
claus
parents:
diff changeset
    94
    inStream binary.
93da277c5ddd Initial revision
claus
parents:
diff changeset
    95
93da277c5ddd Initial revision
claus
parents:
diff changeset
    96
    header := ByteArray uninitializedNew:128.
29
e04e1aceff6f *** empty log message ***
claus
parents: 27
diff changeset
    97
    count := inStream nextBytes:128 into:header.
27
93da277c5ddd Initial revision
claus
parents:
diff changeset
    98
    inStream close.
93da277c5ddd Initial revision
claus
parents:
diff changeset
    99
29
e04e1aceff6f *** empty log message ***
claus
parents: 27
diff changeset
   100
    ((count == 128) and:[self isValidPCXHeader:header]) ifFalse:[
27
93da277c5ddd Initial revision
claus
parents:
diff changeset
   101
	^ false
93da277c5ddd Initial revision
claus
parents:
diff changeset
   102
    ].
93da277c5ddd Initial revision
claus
parents:
diff changeset
   103
    ^ true
102
claus
parents: 99
diff changeset
   104
claus
parents: 99
diff changeset
   105
    "Modified: 17.9.1995 / 17:32:07 / claus"
201
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
   106
!
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
   107
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
   108
isValidPCXHeader:aHeader
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
   109
    "return true, if aHeader looks like a PCX image header"
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
   110
530
749f21b7eff4 comment
Claus Gittinger <cg@exept.de>
parents: 499
diff changeset
   111
    "check magic number"
201
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
   112
    ((aHeader at:1) ~~ 16r0A) ifTrue:[
531
19c16bd5e0bf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   113
	^ false
201
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
   114
    ].
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
   115
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
   116
    "check version"
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
   117
    (#(0 2 3 5) includes:(aHeader at:2)) ifFalse:[
531
19c16bd5e0bf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   118
	^ false
201
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
   119
    ].
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
   120
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
   121
    ^ true
530
749f21b7eff4 comment
Claus Gittinger <cg@exept.de>
parents: 499
diff changeset
   122
749f21b7eff4 comment
Claus Gittinger <cg@exept.de>
parents: 499
diff changeset
   123
    "Modified: 16.4.1997 / 22:24:32 / cg"
27
93da277c5ddd Initial revision
claus
parents:
diff changeset
   124
! !
93da277c5ddd Initial revision
claus
parents:
diff changeset
   125
1848
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   126
!PCXReader methodsFor:'private-reading'!
27
93da277c5ddd Initial revision
claus
parents:
diff changeset
   127
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   128
extractColorMap16
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   129
    "extract the 16-entry colormap from the header"
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   130
1745
4fa0fad2a463 code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents: 1737
diff changeset
   131
    |rawMap|
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   132
1745
4fa0fad2a463 code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents: 1737
diff changeset
   133
    rawMap := header copyFrom:17 to:(17 + (16*3) - 1).
1759
f52382e5a9b8 slowly getting rid of the Colormap class
Claus Gittinger <cg@exept.de>
parents: 1756
diff changeset
   134
    ^ MappedPalette rgbBytesVector:rawMap 
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   135
!
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   136
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   137
nextByteFromBufferOrStream
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   138
    |byte|
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   139
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   140
    nBuffer ~~ 0 ifTrue:[
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   141
        byte := buffer at:bufferIndex.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   142
        bufferIndex := bufferIndex + 1. nBuffer := nBuffer - 1.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   143
    ] ifFalse:[
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   144
        byte := inStream next
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   145
    ].
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   146
    ^ byte
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   147
!
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   148
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   149
readColorMap256
1756
7c4f38c59b2a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1745
diff changeset
   150
    |rawMap mapSize|
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   151
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   152
    rawMap := ByteArray uninitializedNew:(256*3).
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   153
    nBuffer ~~ 0 ifTrue:[
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   154
        mapSize := buffer size - bufferIndex + 1.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   155
        mapSize := mapSize min:(256*3).
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   156
        rawMap replaceFrom:1 to:mapSize with:buffer startingAt:bufferIndex.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   157
        nBuffer < (256*3) ifTrue:[
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   158
            inStream nextBytes:((256*3)-nBuffer) into:rawMap startingAt:nBuffer+1
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   159
        ]
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   160
    ] ifFalse:[
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   161
        inStream nextBytes:(256*3) into:rawMap.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   162
    ].
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   163
1759
f52382e5a9b8 slowly getting rid of the Colormap class
Claus Gittinger <cg@exept.de>
parents: 1756
diff changeset
   164
    ^ MappedPalette rgbBytesVector:rawMap 
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   165
!
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   166
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   167
readCompressedData
3993
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   168
    |bendIndex rowStartIndex  endIndex byte nByte value idx2
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   169
     srcIndex dstIndex srcRowStartIndex dstRowStartIndex bytesPerPane planeData imageBytesPerRow|
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   170
3994
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   171
    (nPlanes > 1 and:[depth == 8]) ifTrue:[
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   172
        ^ self readCompressedDepth24Data.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   173
    ].
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   174
    
3991
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   175
    imageBytesPerRow := (((width * depth) + 7) // 8).
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   176
    
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   177
    bytesPerPane := height * (imageBytesPerRow max:sourceBytesPerRow).
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   178
    planeData := 1 to:nPlanes collect:[:planeNr | ByteArray uninitializedNew:bytesPerPane].
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   179
    data := planeData at:1.
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   180
    
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   181
    buffer := ByteArray uninitializedNew:4096.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   182
    bufferIndex := 1.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   183
    bendIndex := 1.
201
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
   184
3993
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   185
    rowStartIndex := 1.
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   186
    1 to:height do:[:row |
3991
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   187
        1 to:nPlanes do:[:planeNr |
3993
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   188
            |planeBytes|
3991
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   189
3993
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   190
            planeBytes := planeData at:planeNr.
3991
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   191
            
3993
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   192
            dstIndex := rowStartIndex.
3991
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   193
            endIndex := dstIndex + sourceBytesPerRow.
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   194
            
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   195
            [dstIndex < endIndex] whileTrue:[
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   196
                bufferIndex == bendIndex ifTrue:[
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   197
                    nBuffer := inStream nextBytes:4096 into:buffer.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   198
                    bufferIndex := 1.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   199
                    bendIndex := nBuffer + 1.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   200
                ].
3991
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   201
                byte := buffer at:bufferIndex.
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   202
                bufferIndex := bufferIndex + 1.
3991
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   203
                ((byte bitAnd:2r11000000) ~~ 2r11000000) ifTrue:[
3993
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   204
                    planeBytes at:dstIndex put:byte.
3991
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   205
                    dstIndex := dstIndex + 1.
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   206
                ] ifFalse:[
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   207
                    nByte := byte bitAnd:2r00111111.
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   208
                    bufferIndex == bendIndex ifTrue:[
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   209
                        nBuffer := inStream nextBytes:4096 into:buffer.
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   210
                        bufferIndex := 1.
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   211
                        bendIndex := nBuffer + 1.
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   212
                    ].
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   213
                    value := buffer at:bufferIndex.
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   214
                    bufferIndex := bufferIndex + 1.
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   215
                    idx2 := ((dstIndex + nByte) min:endIndex) - 1.
3993
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   216
                    planeBytes from:dstIndex to:idx2 put:value.
3991
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   217
                    dstIndex := dstIndex + nByte.
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   218
                ].
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   219
            ].
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   220
        ].
3993
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   221
        "/ rowStartIndex := endIndex.
3994
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   222
        rowStartIndex := rowStartIndex + (imageBytesPerRow * nPlanes).
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   223
    ].
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   224
    nBuffer := endIndex - bufferIndex.
3990
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   225
3992
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   226
    "/ now merge the planes
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   227
    nPlanes > 1 ifTrue:[
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   228
        depth == 8 ifTrue:[
3993
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   229
            (nPlanes >= 3) ifTrue:[
3992
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   230
                "/ a simple rgb image
3993
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   231
                nPlanes := nPlanes min:4.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   232
3992
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   233
                data := ByteArray uninitializedNew:(nPlanes*width*height).
3993
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   234
                srcRowStartIndex := dstRowStartIndex := 1.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   235
                1 to:height do:[:y |
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   236
                    1 to:nPlanes do:[:p |
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   237
                        |planeBytes|
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   238
                        
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   239
                        dstIndex := dstRowStartIndex + (p - 1).
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   240
                        srcIndex := srcRowStartIndex.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   241
                        planeBytes := planeData at:p.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   242
                        1 to:width do:[:x |
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   243
                            data at:dstIndex put:(planeBytes at:srcIndex).
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   244
                            dstIndex := dstIndex + nPlanes.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   245
                            srcIndex := srcIndex + 1.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   246
                        ].    
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   247
                    ].
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   248
                    srcRowStartIndex := srcRowStartIndex + imageBytesPerRow.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   249
                    dstRowStartIndex := dstRowStartIndex + (width * nPlanes).
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   250
                ].
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   251
            ].
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   252
            depth := nPlanes * 8.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   253
            photometric := nPlanes==3 ifTrue:[#rgb] ifFalse:[#rgba].
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   254
        ] ifFalse:[
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   255
            (depth == 1) ifTrue:[
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   256
                |newDepth nPlanesUsed|
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   257
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   258
                nPlanesUsed := nPlanes min:4.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   259
                newDepth := (nPlanesUsed * depth) nextPowerOf2.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   260
                
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   261
                data := ByteArray new:((width*height*newDepth)+7)//8.
3992
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   262
                srcIndex := dstIndex := 1.
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   263
                1 to:height do:[:y |
3993
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   264
                    |inMask outBitCount outBits|
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   265
                    
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   266
                    inMask := 16r80.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   267
                    outBitCount := 0.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   268
                    outBits := 0.
3992
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   269
                    1 to:width do:[:x |
3993
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   270
                        1 to:nPlanesUsed do:[:p |
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   271
                            outBits := (outBits bitShift:1).
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   272
                            (((planeData at:p) at:srcIndex) bitAnd:inMask) ~~ 0 ifTrue:[
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   273
                                outBits := outBits bitOr:1.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   274
                            ].
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   275
                        ].
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   276
                        outBits := outBits bitShift:(newDepth-nPlanesUsed).
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   277
                        outBitCount := outBitCount + newDepth.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   278
                        
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   279
                        outBitCount >= 8 ifTrue:[
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   280
                            data at:dstIndex put:((data at:dstIndex) bitOr:outBits).
3992
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   281
                            dstIndex := dstIndex + 1.
3993
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   282
                            outBitCount := 0.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   283
                            outBits := 0.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   284
                        ].
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   285
                        inMask := inMask rightShift:1.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   286
                        inMask == 0 ifTrue:[
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   287
                            inMask := 16r80.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   288
                            srcIndex := srcIndex + 1.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   289
                        ].
3992
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   290
                    ].
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   291
                ].
3993
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   292
                depth := newDepth.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   293
                "/ rgbi colormap.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   294
self halt.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   295
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   296
            ] ifFalse:[
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   297
                self halt.
3992
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   298
            ].
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   299
        ].    
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   300
    ].
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   301
3991
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   302
"/    sourceBytesPerRow ~~ (((width * depth) + 7) // 8) ifTrue:[
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   303
"/        "/ have to compress - above code reads sourceBytesPerRow
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   304
"/        "/ (to keep in sync with RLE); but we want width bytesPerRow in the image data.
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   305
"/        "/ Can compress in the data-area; leftover pixels are simply ignored
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   306
"/        "/ by other image processing code
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   307
"/        "/
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   308
"/        1 to:nPlanes do:[:planeNr |
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   309
"/            |dst|
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   310
"/
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   311
"/            dst := planeData at:planeNr.
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   312
"/
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   313
"/            dstIndex := width + 1.
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   314
"/            srcIndex := sourceBytesPerRow + 1.
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   315
"/            2 to:height do:[:row |
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   316
"/                dst replaceFrom:dstIndex to:dstIndex+width-1 with:dst startingAt:srcIndex.
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   317
"/                dstIndex := dstIndex + width.
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   318
"/                srcIndex := srcIndex + sourceBytesPerRow
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   319
"/            ]
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   320
"/        ].
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   321
"/    ].
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   322
3994
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   323
    "Modified: / 29-08-2017 / 02:13:12 / cg"
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   324
!
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   325
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   326
readCompressedDepth24Data
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   327
    |bEndIndex rowStartIndex rowBytes endIndex byte nByte value idx2
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   328
     srcIndex dstIndex imageBytesPerRow|
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   329
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   330
    imageBytesPerRow := (((width * depth * nPlanes) + 7) // 8).
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   331
    
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   332
    data := ByteArray new:(nPlanes*width*height).
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   333
    
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   334
    buffer := ByteArray uninitializedNew:4096.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   335
    bufferIndex := 1.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   336
    bEndIndex := 1.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   337
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   338
    rowBytes := ByteArray new:(sourceBytesPerRow * nPlanes).
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   339
    
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   340
    rowStartIndex := 1.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   341
    1 to:height do:[:row |
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   342
        dstIndex := 1.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   343
        endIndex := 1 + (sourceBytesPerRow * nPlanes).
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   344
        [dstIndex < endIndex] whileTrue:[
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   345
            bufferIndex == bEndIndex ifTrue:[
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   346
                nBuffer := inStream nextBytes:4096 into:buffer.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   347
                bufferIndex := 1.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   348
                bEndIndex := nBuffer + 1.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   349
            ].
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   350
            byte := buffer at:bufferIndex.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   351
            bufferIndex := bufferIndex + 1.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   352
            ((byte bitAnd:2r11000000) ~~ 2r11000000) ifTrue:[
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   353
                rowBytes at:dstIndex put:byte.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   354
                dstIndex := dstIndex + 1.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   355
            ] ifFalse:[
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   356
                nByte := byte bitAnd:2r00111111.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   357
                bufferIndex == bEndIndex ifTrue:[
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   358
                    nBuffer := inStream nextBytes:4096 into:buffer.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   359
                    bufferIndex := 1.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   360
                    bEndIndex := nBuffer + 1.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   361
                ].
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   362
                value := buffer at:bufferIndex.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   363
                bufferIndex := bufferIndex + 1.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   364
                idx2 := ((dstIndex + nByte) min:endIndex) - 1.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   365
                rowBytes from:dstIndex to:idx2 put:value.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   366
                dstIndex := dstIndex + nByte.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   367
            ].
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   368
        ].
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   369
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   370
        dstIndex := rowStartIndex.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   371
        srcIndex := 1.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   372
        1 to:width do:[:x |
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   373
            data at:dstIndex put:(rowBytes at:x).
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   374
            data at:dstIndex+1 put:(rowBytes at:x+sourceBytesPerRow).
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   375
            data at:dstIndex+2 put:(rowBytes at:x+sourceBytesPerRow+sourceBytesPerRow).
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   376
            dstIndex := dstIndex + 3.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   377
        ].
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   378
        "/ data replaceFrom:rowStartIndex to:rowStartIndex+(sourceBytesPerRow*nPlanes)-1 with:rowBytes startingAt:1.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   379
        
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   380
        "/ rowStartIndex := endIndex.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   381
        rowStartIndex := rowStartIndex + imageBytesPerRow.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   382
    ].
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   383
    nBuffer := endIndex - bufferIndex.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   384
    depth := depth * nPlanes.
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   385
8ab8729d5dec #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3993
diff changeset
   386
    "Created: / 29-08-2017 / 02:13:07 / cg"
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   387
!
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   388
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   389
readRestAfterHeader
201
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
   390
    "read an raw image in pcx format from aStream.
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
   391
     The header has already been read into the header argument."
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
   392
3991
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   393
    | version compression xmin ymin xmax ymax
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   394
      paletteType 
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   395
      byte        "{Class: SmallInteger }" 
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   396
      nMaxPad|
27
93da277c5ddd Initial revision
claus
parents:
diff changeset
   397
531
19c16bd5e0bf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   398
    "/ typedef struct {                         /*header for PCX bitmap files*/
3990
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   399
    "/    unsigned char       signature;          /*1 PCX file identifier */
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   400
    "/    unsigned char       version;            /*2 version compatibility level */
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   401
    "/    unsigned char       encoding;           /*3 compression method */
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   402
    "/    unsigned char       bitsperpix;         /*4 bits per pixel, or depth */
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   403
    "/    unsigned short      Xleft;              /*5 X position of left edge */
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   404
    "/    unsigned short      Ytop;               /*7 Y position of top edge */
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   405
    "/    unsigned short      Xright;             /*9 X position of right edge */
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   406
    "/    unsigned short      Ybottom;            /*11 Y position of bottom edge */
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   407
    "/    unsigned short      Xscreensize;        /*13 X screen res of source image */
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   408
    "/    unsigned short      Yscreensize;        /*15 Y screen res of source image */
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   409
    "/    unsigned char       PCXpalette[16][3];  /*17 PCX color map */
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   410
    "/    unsigned char       reserved1;          /*17+48 should be 0, 1 if std res fax */
566
f3cbbba715d9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   411
    "/    unsigned char       planes;             /*66 bit planes in image*/
f3cbbba715d9 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 561
diff changeset
   412
    "/    unsigned short      linesize;           /*67 byte delta between scanlines */
3990
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   413
    "/    unsigned char       paletteinfo;        /*69 paletteType */
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   414
    "/                                                /*0 == undef
531
19c16bd5e0bf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   415
    "/                                                  1 == color
19c16bd5e0bf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   416
    "/                                                  2 == grayscale*/
3990
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   417
    "/    unsigned char reserved2[58];            /*70 fill to struct size of 128*/
531
19c16bd5e0bf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   418
    "/ } PCX_HEADER;
19c16bd5e0bf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   419
27
93da277c5ddd Initial revision
claus
parents:
diff changeset
   420
    version := header at:2.
3990
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   421
    "/    'version=' print. version printNL.
27
93da277c5ddd Initial revision
claus
parents:
diff changeset
   422
    compression := header at:3.
3990
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   423
    "/    'compression=' print. compression printNL.
27
93da277c5ddd Initial revision
claus
parents:
diff changeset
   424
    (#(0 1) includes:compression) ifFalse:[
812
01d6b05a1276 use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   425
        ^ self fileFormatError:'unknown compression'.
27
93da277c5ddd Initial revision
claus
parents:
diff changeset
   426
    ].
530
749f21b7eff4 comment
Claus Gittinger <cg@exept.de>
parents: 499
diff changeset
   427
3990
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   428
    depth := header at:4.
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   429
    "/    'depth=' print. depth printNL.
27
93da277c5ddd Initial revision
claus
parents:
diff changeset
   430
    nPlanes := header at:66.
3990
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   431
    "/    'planes=' print. nPlanes printNL.
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   432
    sourceBytesPerRow := header wordAt:67 MSB:false.
3990
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   433
    "/    'srcBytesPerRow=' print. srcBytesPerRow printNL.
27
93da277c5ddd Initial revision
claus
parents:
diff changeset
   434
    paletteType := header at:69.
93da277c5ddd Initial revision
claus
parents:
diff changeset
   435
83
claus
parents: 43
diff changeset
   436
    xmin := header wordAt:5 MSB:false. 
claus
parents: 43
diff changeset
   437
    ymin := header wordAt:7 MSB:false.
claus
parents: 43
diff changeset
   438
    xmax := header wordAt:9 MSB:false. 
claus
parents: 43
diff changeset
   439
    ymax := header wordAt:11 MSB:false.
27
93da277c5ddd Initial revision
claus
parents:
diff changeset
   440
93da277c5ddd Initial revision
claus
parents:
diff changeset
   441
    width := (xmax - xmin + 1).
93da277c5ddd Initial revision
claus
parents:
diff changeset
   442
    height := (ymax - ymin + 1).
3990
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   443
    "/    'width=' print. width printNL.
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   444
    "/    'height=' print. width printNL.
27
93da277c5ddd Initial revision
claus
parents:
diff changeset
   445
3991
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   446
    "
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   447
     although it would be easy to implement ...
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   448
     I have no test pictures for other formats.
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   449
     So its not (yet) implemented
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   450
    "
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   451
    ((#(1 2 4 8) includes:depth) "and:[nPlanes == 1]") ifFalse:[
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   452
        "/        'PCXReader: depth: ' errorPrint. depth errorPrint. 
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   453
        "/        ' planes:' errorPrint. nPlanes errorPrintNL.
3992
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   454
        ^ self fileFormatError:'can only handle depth''s 1,2,4 or 8'.
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   455
    ].
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   456
    (nPlanes between:1 and:4) ifFalse:[
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   457
        ^ self fileFormatError:'can only handle 1 to 4 planes'.
3991
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   458
    ].
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   459
1846
d29322944b05 dimensionReport
Claus Gittinger <cg@exept.de>
parents: 1805
diff changeset
   460
    self reportDimension.
d29322944b05 dimensionReport
Claus Gittinger <cg@exept.de>
parents: 1805
diff changeset
   461
3992
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   462
    "/ precompute a first guess at the photometric;
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   463
    "/ warning: might be changed by readCompressedData
3990
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   464
    paletteType == 2 ifTrue:[
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   465
        photometric := #blackIs0.
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   466
    ] ifFalse:[
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   467
        depth == 1 ifTrue:[
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   468
            photometric := #blackIs0.
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   469
        ] ifFalse:[    
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   470
            photometric := #palette.
561
bb8103acb292 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
   471
        ].
27
93da277c5ddd Initial revision
claus
parents:
diff changeset
   472
    ].
93da277c5ddd Initial revision
claus
parents:
diff changeset
   473
3992
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   474
    compression == 1 ifTrue:[
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   475
        self readCompressedData
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   476
    ] ifFalse:[
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   477
        self readUncompressedData
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   478
    ].
b7c4e0a78b6b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3991
diff changeset
   479
3993
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   480
    "/ warning: depth might be changed by readCompressedData
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   481
    depth == 24 ifTrue:[
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   482
        samplesPerPixel := 3.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   483
        bitsPerSample := #( 8 8 8 ).
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   484
        photometric := #rgb.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   485
    ] ifFalse:[    
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   486
        depth == 32 ifTrue:[
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   487
            samplesPerPixel := 4.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   488
            bitsPerSample := #( 8 8 8 8).
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   489
            photometric := #rgba.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   490
        ] ifFalse:[    
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   491
            samplesPerPixel := 1.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   492
            bitsPerSample := { depth }.
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   493
        ].
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   494
    ].
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   495
3991
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   496
    photometric == #palette ifTrue:[ 
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   497
       (version == 5) ifTrue:[
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   498
            true "depth == 8" ifTrue:[
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   499
                inStream isPositionable ifTrue:[
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   500
                    "/ seek to the end, minus 3*256-1 bytes and check there
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   501
                    inStream position:(inStream fileSize - (3*256)-1).
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   502
                    nBuffer := 0.
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   503
                    byte := inStream next.
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   504
                ] ifFalse:[    
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   505
                    "/ RLE data is padded - skip over zeros for the 0C-byte                    
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   506
                    nMaxPad := 15.
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   507
                    byte := self nextByteFromBufferOrStream.
3990
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   508
3991
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   509
                    [(byte ~~ 16r0C) and:[nMaxPad > 0]] whileTrue:[
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   510
                        byte := self nextByteFromBufferOrStream.
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   511
                        nMaxPad := nMaxPad - 1.
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   512
                    ].
3990
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   513
                ].
3991
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   514
                (byte == 16r0C) ifTrue:[
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   515
                    colorMap := self readColorMap256.
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   516
                ] ifFalse:[
3990
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   517
                    'PCXREADER: no valid 256-entry palette (got' errorPrint. 
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   518
                    byte errorPrint. '; expected ' errorPrint. 16rC0 errorPrint. ')' errorPrintCR.
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   519
                ].
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   520
            ].
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   521
        ].
3991
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   522
        ((version == 2) or:[ colorMap isNil and:[ (depth <= 4) ] ]) ifTrue:[
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   523
            "/ take palette from header
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   524
            colorMap := self extractColorMap16.
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   525
        ].    
3990
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   526
    ].
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   527
        
27
93da277c5ddd Initial revision
claus
parents:
diff changeset
   528
    "
93da277c5ddd Initial revision
claus
parents:
diff changeset
   529
     |i f|
29
e04e1aceff6f *** empty log message ***
claus
parents: 27
diff changeset
   530
     i := Image fromFile:'somefile.pcx'.
e04e1aceff6f *** empty log message ***
claus
parents: 27
diff changeset
   531
     i inspect.
27
93da277c5ddd Initial revision
claus
parents:
diff changeset
   532
    "
499
58852715e10d handle partial colorMap
Claus Gittinger <cg@exept.de>
parents: 398
diff changeset
   533
3993
921798977590 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 3992
diff changeset
   534
    "Modified: / 28-08-2017 / 15:53:11 / cg"
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   535
!
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   536
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   537
readUncompressedData
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   538
    |dstIndex|
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   539
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   540
    "
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   541
     actually untested ...
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   542
    "
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   543
    data := ByteArray uninitializedNew:(height * width).
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   544
    sourceBytesPerRow ~~ width ifTrue:[
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   545
        dstIndex := 1.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   546
        1 to:height do:[:row |
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   547
            inStream nextBytes:width into:data startingAt:dstIndex.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   548
            dstIndex := dstIndex + width.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   549
            inStream skip:(sourceBytesPerRow - width).
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   550
        ]
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   551
    ] ifFalse:[
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   552
        inStream nextBytes:(height * width) into:data.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   553
    ].
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   554
    nBuffer := 0.
27
93da277c5ddd Initial revision
claus
parents:
diff changeset
   555
! !
93da277c5ddd Initial revision
claus
parents:
diff changeset
   556
1848
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   557
!PCXReader methodsFor:'reading'!
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   558
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   559
readImage
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   560
    "read an image in pcx format from inStream"
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   561
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   562
    inStream binary.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   563
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   564
    header := ByteArray uninitializedNew:128.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   565
    (inStream nextBytes:128 into:header) == 128 ifFalse:[
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   566
        ^ self fileFormatError:'short file'.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   567
    ].
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   568
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   569
    (self class isValidPCXHeader:header) ifFalse:[
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   570
        ^ self fileFormatError:'wrong header'.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   571
    ].
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   572
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   573
    self readRestAfterHeader.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   574
! !
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   575
201
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
   576
!PCXReader class methodsFor:'documentation'!
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
   577
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
   578
version
3990
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   579
    ^ '$Header$'
3991
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   580
!
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   581
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   582
version_CVS
a0cea747ba0b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 3990
diff changeset
   583
    ^ '$Header$'
201
c707ed0db1d0 commentary
Claus Gittinger <cg@exept.de>
parents: 182
diff changeset
   584
! !
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1562
diff changeset
   585
3990
1edec82fe2fa #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   586
27
93da277c5ddd Initial revision
claus
parents:
diff changeset
   587
PCXReader initialize!