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