PNGReader.st
author Claus Gittinger <cg@exept.de>
Thu, 08 May 2014 02:29:33 +0200
changeset 3331 443356a56b2f
parent 3327 4758853d1abe
child 3343 ca3446a105d2
permissions -rw-r--r--
class: LayoutOrigin changed: #=
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3327
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
     1
"{ Encoding: utf8 }"
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
     2
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     3
"
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
 COPYRIGHT (c) 1996 by Claus Gittinger
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
              All Rights Reserved
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
 This software is furnished under a license and may be used
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
 only in accordance with the terms of that license and with the
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
 inclusion of the above copyright notice.   This software may not
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
 be provided or otherwise made available to, or used by, any
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
 other person.  No title to or ownership of the software is
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
 hereby transferred.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
"
1671
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    14
"{ Package: 'stx:libview2' }"
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
    15
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
ImageReader subclass:#PNGReader
2697
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
    17
	instanceVariableNames:'colorType bitsPerChannel depth compressionMethod filterMethod
2725
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
    18
		interlaceMode bytesPerScanline globalDataChunk thisScanline
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
    19
		prevScanline'
2707
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
    20
	classVariableNames:'Verbose'
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
	poolDictionaries:''
1745
4fa0fad2a463 code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents: 1694
diff changeset
    22
	category:'Graphics-Images-Readers'
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
!
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
398
aef700d15416 new suffix-table
Claus Gittinger <cg@exept.de>
parents: 308
diff changeset
    25
!PNGReader class methodsFor:'documentation'!
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
copyright
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
"
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
 COPYRIGHT (c) 1996 by Claus Gittinger
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
              All Rights Reserved
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
 This software is furnished under a license and may be used
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
 only in accordance with the terms of that license and with the
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
 inclusion of the above copyright notice.   This software may not
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
 be provided or otherwise made available to, or used by, any
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
 other person.  No title to or ownership of the software is
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
 hereby transferred.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
"
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
!
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
documentation
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
"
2725
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
    45
    This class will provide methods for loading and saving PNG pictures.
2700
1fa3b493b74c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2699
diff changeset
    46
    It is currenty unfinished (interlaced image support is missing).
2725
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
    47
    In the meantime, use a pngtoXXX converter for interlaced images.
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
2725
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
    49
    [caveats:]
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
    50
        writer ignores any mask (for now).
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
    51
        writer only generates unfiltered rows or interlace data
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
    52
        
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
    [See also:]
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
        Image Form Icon
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
        BlitImageReader FaceReader GIFReader JPEGReader PBMReader PCXReader 
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
        ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader 
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
        XBMReader XPMReader XWDReader 
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
    [author:]
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
        Claus Gittinger
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
"
1694
3a77d9b69cf7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1671
diff changeset
    62
!
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
1694
3a77d9b69cf7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1671
diff changeset
    64
examples
3a77d9b69cf7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1671
diff changeset
    65
"
3a77d9b69cf7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1671
diff changeset
    66
    PNGReader fromFile:'/home/cg/AudioExplorer_51_files/use_small.png'
2509
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
    67
    PNGReader fromFile:'\\Exeptn\tmp\images\expeccoScreenshot4020_5526507.png'
2683
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
    68
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
    69
    PNGReader fromFile:'C:\Users\cg\Desktop\croquet\cobalt-base-current-build-20090210\cobalt-base-current-build-20090210\content\models\textures\checkerboard.png'
2695
6197b6af7f4f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2683
diff changeset
    70
    PNGReader fromFile:'C:\Dokumente und Einstellungen\cg\Desktop\misc\PNGs\Delete.png'
2697
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
    71
    PNGReader fromFile:'\\exeptn\unsaved\pd_stuff\PNGs\Delete.png'
2699
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
    72
    PNGReader fromFile:'\\exeptn\unsaved\pd_stuff\PNGs\Down.png'
2703
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
    73
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
    74
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
    75
    |img img2 outStream png|
2725
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
    76
2703
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
    77
    img := ToolbarIconLibrary error32x32Icon.
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
    78
    outStream := WriteStream on:(ByteArray new:100).
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
    79
    PNGReader save:img onStream:outStream.
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
    80
    png := outStream contents.
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
    81
    img2 := PNGReader fromStream:(png readStream).
2708
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
    82
    self assert:(img bits = img2 bits).
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
    83
    img inspect.
2703
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
    84
    img2 inspect.
1694
3a77d9b69cf7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1671
diff changeset
    85
"
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
! !
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
398
aef700d15416 new suffix-table
Claus Gittinger <cg@exept.de>
parents: 308
diff changeset
    88
!PNGReader class methodsFor:'initialization'!
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
initialize
2699
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
    91
    "install myself in the Image classes fileFormat table for the `.png' extension."
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
2709
4ecfb8ae5fab *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2708
diff changeset
    93
    MIMETypes defineImageType:'image/png'  suffix:'png' reader:self.
4ecfb8ae5fab *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2708
diff changeset
    94
    "/ backward compatibility from times when png was not yet so common...
4ecfb8ae5fab *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2708
diff changeset
    95
    MIMETypes defineImageType:'image/x-png' suffix:nil reader:self.
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
! !
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
2705
7ff2e44e44af *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2704
diff changeset
    98
!PNGReader class methodsFor:'queries'!
7ff2e44e44af *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2704
diff changeset
    99
7ff2e44e44af *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2704
diff changeset
   100
canRepresent:anImage
7ff2e44e44af *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2704
diff changeset
   101
    "return true, if anImage can be represented in my file format.
7ff2e44e44af *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2704
diff changeset
   102
     Any image is supported."
7ff2e44e44af *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2704
diff changeset
   103
7ff2e44e44af *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2704
diff changeset
   104
    ^ true
7ff2e44e44af *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2704
diff changeset
   105
! !
7ff2e44e44af *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2704
diff changeset
   106
398
aef700d15416 new suffix-table
Claus Gittinger <cg@exept.de>
parents: 308
diff changeset
   107
!PNGReader class methodsFor:'testing'!
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
isValidImageFile:aFileName
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
    "return true, if aFileName contains a PNG image"
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
    |inStream magic|
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
    inStream := self streamReadingFile:aFileName.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
    inStream isNil ifTrue:[^ false].
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   117
    inStream binary.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
    magic := ByteArray new:8.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
    inStream nextBytes:8 into:magic.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
    inStream close.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
    ^ (magic = #[137 80 78 71 13 10 26 10])
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
1694
3a77d9b69cf7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1671
diff changeset
   125
    "
3a77d9b69cf7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1671
diff changeset
   126
     self isValidImageFile:'/home/cg/AudioExplorer_51_files/use_small.png'
2683
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   127
     self isValidImageFile:'C:\Users\cg\Desktop\croquet\cobalt-base-current-build-20090210\cobalt-base-current-build-20090210\content\models\textures\checkerboard.png'
2695
6197b6af7f4f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2683
diff changeset
   128
     self isValidImageFile:'C:\Dokumente und Einstellungen\cg\Desktop\misc\PNGs\Delete.png'
1694
3a77d9b69cf7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1671
diff changeset
   129
    "
3a77d9b69cf7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1671
diff changeset
   130
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
    "Modified: 21.6.1996 / 20:38:46 / cg"
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   132
! !
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   133
2696
99339f07db68 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2695
diff changeset
   134
!PNGReader methodsFor:'private-chunks'!
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   135
2699
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   136
doPass: pass
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   137
    "Certain interlace passes are skipped with certain small image dimensions"
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   138
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   139
    pass = 1 ifTrue: [ ^ true ].
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   140
    ((width = 1) and: [height = 1]) ifTrue: [ ^ false ].
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   141
    pass = 2 ifTrue: [ ^ width >= 5 ].
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   142
    pass = 3 ifTrue: [ ^ height >= 5 ].
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   143
    pass = 4 ifTrue: [ ^ (width >=3 ) or: [height >= 5] ].
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   144
    pass = 5 ifTrue: [ ^ height >=3 ].
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   145
    pass = 6 ifTrue: [ ^ width >=2 ].
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   146
    pass = 7 ifTrue: [ ^ height >=2 ].
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   147
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   148
    self error:'invalid argument'.
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   149
    ^ true
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   150
!
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   151
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   152
processBKGDChunkLen:len
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   153
    inStream skip:len.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   154
    ^ true
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   155
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   156
    "Created: 21.6.1996 / 21:15:49 / cg"
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   157
!
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   158
2707
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   159
processCHRMChunkLen:len
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   160
    "cHRM Primary chromaticities chunk - currently unhandled"
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   161
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   162
    |whitePointX whitePointY redX redY greenX greenY blueX blueY|
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   163
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   164
    whitePointX := (inStream nextLongMSB:true) / 100000.0.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   165
    whitePointY := (inStream nextLongMSB:true) / 100000.0.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   166
    redX := (inStream nextLongMSB:true) / 100000.0.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   167
    redY := (inStream nextLongMSB:true) / 100000.0.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   168
    greenX := (inStream nextLongMSB:true) / 100000.0.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   169
    greenY := (inStream nextLongMSB:true) / 100000.0.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   170
    blueX := (inStream nextLongMSB:true) / 100000.0.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   171
    blueY := (inStream nextLongMSB:true) / 100000.0.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   172
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   173
    Verbose == true ifTrue:[
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   174
        'PNGReader: CHRM chunk ignored:' infoPrintCR.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   175
        'PNGReader:   whitePointX:' infoPrint. whitePointX infoPrintCR.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   176
        'PNGReader:   whitePointY:' infoPrint. whitePointX infoPrintCR.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   177
        'PNGReader:   redX:' infoPrint. redX infoPrintCR.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   178
        'PNGReader:   redY:' infoPrint. redY infoPrintCR.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   179
        'PNGReader:   greenX:' infoPrint. greenX infoPrintCR.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   180
        'PNGReader:   greenY:' infoPrint. greenY infoPrintCR.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   181
        'PNGReader:   blueX:' infoPrint. blueX infoPrintCR.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   182
        'PNGReader:   blueY:' infoPrint. blueY infoPrintCR.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   183
    ].
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   184
    ^ true
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   185
!
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   186
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   187
processChunk:type len:len
2509
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   188
    |chunk|
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   189
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   190
    type = 'IDAT' ifTrue:[
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   191
        "---since the compressed data can span multiple
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   192
        chunks, stitch them all together first. later,
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   193
        if memory is an issue, we need to figure out how
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   194
        to do this on the fly---"
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   195
        chunk := inStream next:len.
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   196
        globalDataChunk := globalDataChunk isNil 
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   197
                                ifTrue: [chunk] 
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   198
                                ifFalse:[globalDataChunk,chunk].
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   199
        ^ true
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   200
"/        ^ self processIDATChunkLen:len
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   201
    ].
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   202
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   203
    type = 'gAMA' ifTrue:[^ self processGAMAChunkLen:len].
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   204
    type = 'sBIT' ifTrue:[^ self processSBITChunkLen:len].
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   205
    type = 'tEXt' ifTrue:[^ self processTEXTChunkLen:len].
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   206
    type = 'tIME' ifTrue:[^ self processTIMEChunkLen:len].
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   207
    type = 'bKGD' ifTrue:[^ self processBKGDChunkLen:len].
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   208
    type = 'zTXt' ifTrue:[^ self processZTXTChunkLen:len].
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   209
    type = 'PLTE' ifTrue:[^ self processPLTEChunkLen:len].
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   210
1671
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   211
    type = 'pHYs' ifTrue:[^ self processPHYSChunkLen:len].
2707
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   212
    type = 'cHRM' ifTrue:[^ self processCHRMChunkLen:len].
1671
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   213
    type = 'tRNS' ifTrue:[^ self processTRNSChunkLen:len].
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   214
    type = 'IHDR' ifTrue:[^ self processIHDRChunkLen:len].
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   215
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   216
    ('PNGReader: unrecognized chunk: ' , type , ' ignored.') infoPrintCR.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   217
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   218
    inStream skip:len.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   219
    ^ true.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   220
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   221
    "Created: 21.6.1996 / 21:10:37 / cg"
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   222
    "Modified: 21.6.1996 / 21:22:37 / cg"
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   223
!
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   224
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   225
processGAMAChunkLen:len
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   226
    inStream skip:len.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   227
    ^ true
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   228
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   229
    "Created: 21.6.1996 / 21:10:52 / cg"
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   230
!
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   231
2509
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   232
processGlobalIDATChunk
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   233
    interlaceMode == 0 ifTrue: [
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   234
        ^ self processNonInterlacedGlobalDATA
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   235
    ]. 
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   236
    ^ self processInterlacedGlobalDATA
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   237
!
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   238
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   239
processIDATChunkLen:len
1671
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   240
    interlaceMode == 0 ifTrue: [
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   241
        ^ self processNonInterlacedDATA:len
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   242
    ]. 
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   243
    ^ self processInterlacedDATA:len
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   244
!
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   245
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   246
processInterlacedDATA:len
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   247
    inStream skip:len.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   248
    ^ true
1671
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   249
!
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   250
2699
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   251
processInterlacedGlobalDATA
3327
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   252
    "adam7 interlace method"
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   253
2699
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   254
    | zlibReader filter bytesPerPass startingCol colIncrement rowIncrement 
3327
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   255
      startingRow cx sc temp "filtersSeen" bitsPerPixel cy|
2699
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   256
3327
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   257
    interlaceMode == 1 ifFalse: [
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   258
        self error:'unsupported interlaced mode'.
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   259
        ^ self
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   260
    ].
2699
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   261
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   262
    "/ filtersSeen := Set new.
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   263
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   264
    startingCol := #(0 4 0 2 0 1 0 ).
3327
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   265
    startingRow := #(0 0 4 0 2 0 1 ).
2699
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   266
    colIncrement := #(8 8 4 4 2 2 1 ).
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   267
    rowIncrement := #(8 8 8 4 4 2 2 ).
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   268
2701
8ca05242522e adapt to zlib changes
Claus Gittinger <cg@exept.de>
parents: 2700
diff changeset
   269
    zlibReader := ZipStream readOpenAsZipStreamOn:(globalDataChunk readStream) suppressHeaderAndChecksum:false.
2699
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   270
    zlibReader binary.
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   271
    bytesPerScanline := self bytesPerRow.
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   272
    bitsPerPixel := self bitsPerPixel.
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   273
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   274
    1 to: 7 do: [:pass |
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   275
        (self doPass: pass) ifTrue:[
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   276
            cx := colIncrement at: pass.
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   277
            sc := startingCol at: pass.
3327
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   278
            cy := rowIncrement at: pass.
2699
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   279
            bytesPerPass := width - sc + cx - 1 // cx * bitsPerPixel + 7 // 8.
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   280
            prevScanline := ByteArray new: bytesPerPass.
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   281
            thisScanline := ByteArray new: bytesPerScanline.
3327
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   282
            (startingRow at: pass) to: height - 1 by: cy do: [:y |
2699
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   283
                filter := zlibReader next.
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   284
                "/ filtersSeen add: filter.
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   285
                (filter isNil or: [(filter between: 0 and: 4) not])
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   286
                    ifTrue: [^ self].
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   287
                zlibReader next: bytesPerPass into: thisScanline startingAt: 1.
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   288
                filter ~~ 0 ifTrue:[ self filterScanline: filter count: bytesPerPass ].
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   289
                self copyPixels:y at:sc by:cx.
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   290
                temp := prevScanline.
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   291
                prevScanline := thisScanline.
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   292
                thisScanline := temp.
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   293
            ]
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   294
        ]
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   295
    ].
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   296
    zlibReader atEnd ifFalse:[self error:'Unexpected data'].
2909
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   297
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   298
    "Modified: / 03-05-2011 / 12:03:33 / cg"
2699
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   299
!
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   300
1671
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   301
processNonInterlacedDATA:len
2701
8ca05242522e adapt to zlib changes
Claus Gittinger <cg@exept.de>
parents: 2700
diff changeset
   302
    self halt:'not used'
8ca05242522e adapt to zlib changes
Claus Gittinger <cg@exept.de>
parents: 2700
diff changeset
   303
"/
8ca05242522e adapt to zlib changes
Claus Gittinger <cg@exept.de>
parents: 2700
diff changeset
   304
"/    | zlibReader filter temp prevScanline thisScanline bytesPerScanline filtersSeen|
8ca05242522e adapt to zlib changes
Claus Gittinger <cg@exept.de>
parents: 2700
diff changeset
   305
"/
8ca05242522e adapt to zlib changes
Claus Gittinger <cg@exept.de>
parents: 2700
diff changeset
   306
"/    zlibReader := ZipStream readOpenAsZipStreamOn:inStream suppressHeaderAndChecksum:false. 
8ca05242522e adapt to zlib changes
Claus Gittinger <cg@exept.de>
parents: 2700
diff changeset
   307
"/    "/ zlibReader := ZLibReadStream on:inStream from: 1 to:len.
8ca05242522e adapt to zlib changes
Claus Gittinger <cg@exept.de>
parents: 2700
diff changeset
   308
"/
8ca05242522e adapt to zlib changes
Claus Gittinger <cg@exept.de>
parents: 2700
diff changeset
   309
"/    prevScanline := ByteArray new: self bytesPerRow.
8ca05242522e adapt to zlib changes
Claus Gittinger <cg@exept.de>
parents: 2700
diff changeset
   310
"/    thisScanline := ByteArray new: self bytesPerRow.
8ca05242522e adapt to zlib changes
Claus Gittinger <cg@exept.de>
parents: 2700
diff changeset
   311
"/    0 to: height - 1 do:[:index | 
8ca05242522e adapt to zlib changes
Claus Gittinger <cg@exept.de>
parents: 2700
diff changeset
   312
"/            filter := (zlibReader next: 1) first.
8ca05242522e adapt to zlib changes
Claus Gittinger <cg@exept.de>
parents: 2700
diff changeset
   313
"/            filtersSeen add: filter.
8ca05242522e adapt to zlib changes
Claus Gittinger <cg@exept.de>
parents: 2700
diff changeset
   314
"/            (filter isNil or: [(filter between: 0 and: 4) not]) ifTrue: [^self].
8ca05242522e adapt to zlib changes
Claus Gittinger <cg@exept.de>
parents: 2700
diff changeset
   315
"/            thisScanline := zlibReader next: bytesPerScanline into: thisScanline startingAt: 1.
8ca05242522e adapt to zlib changes
Claus Gittinger <cg@exept.de>
parents: 2700
diff changeset
   316
"/            self filterScanline: filter count: bytesPerScanline.
8ca05242522e adapt to zlib changes
Claus Gittinger <cg@exept.de>
parents: 2700
diff changeset
   317
"/            self copyPixels: index.
8ca05242522e adapt to zlib changes
Claus Gittinger <cg@exept.de>
parents: 2700
diff changeset
   318
"/            temp := prevScanline.
8ca05242522e adapt to zlib changes
Claus Gittinger <cg@exept.de>
parents: 2700
diff changeset
   319
"/            prevScanline := thisScanline.
8ca05242522e adapt to zlib changes
Claus Gittinger <cg@exept.de>
parents: 2700
diff changeset
   320
"/            thisScanline := temp
8ca05242522e adapt to zlib changes
Claus Gittinger <cg@exept.de>
parents: 2700
diff changeset
   321
"/    ]
2509
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   322
!
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   323
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   324
processNonInterlacedGlobalDATA
2725
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
   325
    | "data n" zlibReader filter temp bytesPerScanline "filtersSeen" i|
2696
99339f07db68 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2695
diff changeset
   326
99339f07db68 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2695
diff changeset
   327
    "/ filtersSeen := Set new.
2695
6197b6af7f4f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2683
diff changeset
   328
6197b6af7f4f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2683
diff changeset
   329
"/    data := ByteArray new:(self bytesPerRow * height)+1000.
6197b6af7f4f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2683
diff changeset
   330
"/    n := ZipStream uncompress: globalDataChunk into: data.
6197b6af7f4f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2683
diff changeset
   331
"/    self halt.
2509
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   332
2701
8ca05242522e adapt to zlib changes
Claus Gittinger <cg@exept.de>
parents: 2700
diff changeset
   333
    zlibReader := ZipStream readOpenAsZipStreamOn:(globalDataChunk readStream) suppressHeaderAndChecksum:false.
2696
99339f07db68 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2695
diff changeset
   334
    zlibReader binary.
2509
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   335
    bytesPerScanline := self bytesPerRow.
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   336
1671
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   337
    prevScanline := ByteArray new: bytesPerScanline.
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   338
    thisScanline := ByteArray new: bytesPerScanline.
2725
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
   339
    0 to: height - 1 do:[:y | 
2695
6197b6af7f4f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2683
diff changeset
   340
        filter := (zlibReader next: 1) first.
2696
99339f07db68 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2695
diff changeset
   341
        "/ filtersSeen add: filter.
2725
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
   342
        (filter notNil and: [filter between: 0 and: 4]) ifFalse: [
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
   343
            'PNGReader: unsupported filter' infoPrintCR.
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
   344
            ^ self
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
   345
        ].
2696
99339f07db68 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2695
diff changeset
   346
        zlibReader next: bytesPerScanline into: thisScanline startingAt: 1.
99339f07db68 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2695
diff changeset
   347
        filter ~~ 0 ifTrue:[ self filterScanline: filter count: bytesPerScanline ].
2725
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
   348
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
   349
        i := y * bytesPerScanline.
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
   350
        data replaceFrom:i+1 to:(i+bytesPerScanline) with:thisScanline startingAt:1.
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
   351
2695
6197b6af7f4f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2683
diff changeset
   352
        temp := prevScanline.
6197b6af7f4f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2683
diff changeset
   353
        prevScanline := thisScanline.
6197b6af7f4f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2683
diff changeset
   354
        thisScanline := temp
6197b6af7f4f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2683
diff changeset
   355
    ]
1671
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   356
!
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   357
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   358
processPHYSChunkLen:len
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   359
    "physical pixel chunk - currently unhandled"
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   360
2705
7ff2e44e44af *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2704
diff changeset
   361
    |pixelPerUnitX pixelPerUnitY unit|
7ff2e44e44af *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2704
diff changeset
   362
7ff2e44e44af *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2704
diff changeset
   363
    pixelPerUnitX := inStream nextLongMSB:true.  
7ff2e44e44af *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2704
diff changeset
   364
    pixelPerUnitY := inStream nextLongMSB:true.
7ff2e44e44af *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2704
diff changeset
   365
    unit := inStream nextByte.
7ff2e44e44af *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2704
diff changeset
   366
2707
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   367
    Verbose == true ifTrue:[
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   368
        'PNGReader: PHYS chunk ignored:' infoPrintCR.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   369
        'PNGReader:   ppuX:' infoPrint. pixelPerUnitX infoPrintCR.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   370
        'PNGReader:   ppuY:' infoPrint. pixelPerUnitY infoPrintCR.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   371
        'PNGReader:   unit:' infoPrint. unit infoPrintCR.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   372
    ].
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   373
2705
7ff2e44e44af *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2704
diff changeset
   374
    ^ true
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   375
!
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   376
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   377
processPLTEChunkLen:len
1671
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   378
    "read a color palette"
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   379
2725
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
   380
    |n "{ Class: SmallInteger }"
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
   381
     redBytes greenBytes blueBytes|
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   382
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   383
    (len \\ 3) ~~ 0 ifTrue:[
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   384
        'PNGReader: invalid size of PLTE chunk' infoPrintCR.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   385
        ^ false
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   386
    ].
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   387
    n := len // 3.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   388
    redBytes := ByteArray new:n.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   389
    greenBytes := ByteArray new:n.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   390
    blueBytes := ByteArray new:n.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   391
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   392
    1 to:n do:[:i |
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   393
        redBytes at:i put:(inStream nextByte).
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   394
        greenBytes at:i put:(inStream nextByte).
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   395
        blueBytes at:i put:(inStream nextByte)
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   396
    ].
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   397
2725
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
   398
    colorMap := MappedPalette 
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
   399
                    redVector:redBytes 
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
   400
                    greenVector:greenBytes 
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
   401
                    blueVector:blueBytes.
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
   402
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   403
    ^ true
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   404
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   405
    "Created: 21.6.1996 / 21:22:28 / cg"
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   406
    "Modified: 21.6.1996 / 21:43:01 / cg"
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   407
!
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   408
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   409
processSBITChunkLen:len
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   410
    inStream skip:len.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   411
    ^ true
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   412
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   413
    "Created: 21.6.1996 / 21:13:09 / cg"
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   414
!
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   415
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   416
processTEXTChunkLen:len
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   417
    inStream skip:len.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   418
    ^ true
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   419
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   420
    "Modified: 21.6.1996 / 21:15:27 / cg"
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   421
!
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   422
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   423
processTIMEChunkLen:len
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   424
    inStream skip:len.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   425
    ^ true
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   426
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   427
    "Created: 21.6.1996 / 21:15:43 / cg"
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   428
    "Modified: 21.6.1996 / 21:20:42 / cg"
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   429
!
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   430
2707
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   431
processTRNSChunkLen:len
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   432
    inStream skip:len.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   433
    ^ true
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   434
!
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   435
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   436
processZTXTChunkLen:len
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   437
    inStream skip:len.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   438
    ^ true
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   439
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   440
    "Created: 21.6.1996 / 21:15:58 / cg"
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   441
! !
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   442
2696
99339f07db68 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2695
diff changeset
   443
!PNGReader methodsFor:'private-filtering'!
99339f07db68 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2695
diff changeset
   444
2909
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   445
filterAverage:count 
2699
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   446
    "Use the average of the pixel to the left and the pixel above as a predictor"
2909
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   447
    
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   448
    |delta|
2699
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   449
2909
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   450
    delta := self bitsPerPixel // 8 max:1.
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   451
    1 to:delta do:[:i | 
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   452
        thisScanline at:i
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   453
            put:((thisScanline at:i) + ((prevScanline at:i) // 2) bitAnd:255)
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   454
    ].
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   455
    delta + 1 to:count do:[:i | 
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   456
        thisScanline at:i
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   457
            put:((thisScanline at:i) 
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   458
                    + (((prevScanline at:i) + (thisScanline at:i - delta)) // 2) bitAnd:255)
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   459
    ]
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   460
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   461
    "Modified: / 03-05-2011 / 12:14:01 / cg"
2699
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   462
!
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   463
2696
99339f07db68 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2695
diff changeset
   464
filterHorizontal:count
99339f07db68 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2695
diff changeset
   465
    "use the pixel to the left as a predictor"
99339f07db68 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2695
diff changeset
   466
99339f07db68 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2695
diff changeset
   467
    |delta|
99339f07db68 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2695
diff changeset
   468
99339f07db68 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2695
diff changeset
   469
    delta := self bitsPerPixel // 8 max:1.
99339f07db68 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2695
diff changeset
   470
    delta+1 to:count do:[:i|
99339f07db68 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2695
diff changeset
   471
        thisScanline at:i put:(((thisScanline at:i)+ (thisScanline at:i-delta)) bitAnd:255) 
99339f07db68 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2695
diff changeset
   472
    ]
99339f07db68 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2695
diff changeset
   473
!
99339f07db68 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2695
diff changeset
   474
2699
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   475
filterNone: count
2909
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   476
    "/ no filter - scanline is as is
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   477
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   478
    "Modified: / 03-05-2011 / 12:14:20 / cg"
2699
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   479
!
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   480
2909
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   481
filterPaeth:count 
2697
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   482
    "Select one of (the pixel to the left, the pixel above and the pixel to above left) to
2909
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   483
     predict the value of this pixel"
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   484
    
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   485
    |delta|
2697
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   486
2909
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   487
    delta := self bitsPerPixel // 8 max:1.
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   488
    1 to:delta do:[:i | 
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   489
        thisScanline 
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   490
            at:i
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   491
            put:(((thisScanline at:i) + (prevScanline at:i)) bitAnd:255)
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   492
    ].
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   493
    delta + 1 to:count do:[:i | 
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   494
        thisScanline 
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   495
            at:i
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   496
            put:(((thisScanline at:i) + (self 
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   497
                            paethPredictLeft:(thisScanline at:i - delta)
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   498
                            above:(prevScanline at:i)
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   499
                            aboveLeft:(prevScanline at:i - delta))) 
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   500
                    bitAnd:255)
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   501
    ]
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   502
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   503
    "Modified: / 03-05-2011 / 12:14:43 / cg"
2697
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   504
!
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   505
2696
99339f07db68 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2695
diff changeset
   506
filterScanline:filterType count:count
99339f07db68 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2695
diff changeset
   507
    self 
2909
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   508
        perform:(#(filterNone: filterHorizontal: filterVertical: filterAverage: filterPaeth:) at:filterType+1)
2696
99339f07db68 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2695
diff changeset
   509
        with:count
2909
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   510
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   511
    "Modified: / 03-05-2011 / 12:13:31 / cg"
2697
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   512
!
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   513
2909
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   514
filterVertical:count 
2697
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   515
    "Use the pixel above as a predictor"
2909
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   516
    
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   517
    1 to:count do:[:i | 
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   518
        thisScanline 
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   519
            at:i
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   520
            put:(((thisScanline at:i) + (prevScanline at:i)) bitAnd:255)
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   521
    ]
2697
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   522
2909
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   523
    "Modified: / 03-05-2011 / 12:14:34 / cg"
2697
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   524
!
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   525
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   526
paethPredictLeft: a above: b aboveLeft: c
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   527
    "Predicts the value of a pixel based on nearby pixels, based on Paeth (GG II, 1991)"
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   528
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   529
    | pa pb pc |
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   530
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   531
    pa := b > c ifTrue: [b - c] ifFalse: [c - b].
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   532
    pb := a > c ifTrue: [a - c] ifFalse: [c - a].
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   533
    pc := a + b - c - c.
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   534
    pc < 0 ifTrue: [
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   535
        pc := pc * -1
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   536
    ].
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   537
    ((pa <= pb) and: [pa <= pc]) ifTrue: [^ a].
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   538
    (pb <= pc) ifTrue: [^ b].
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   539
    ^ c
2696
99339f07db68 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2695
diff changeset
   540
! !
99339f07db68 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2695
diff changeset
   541
2699
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   542
!PNGReader methodsFor:'private-pixel copy'!
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   543
2909
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   544
copyPixels:y at:startX by:incX 
2704
bbcc771de65c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2703
diff changeset
   545
    "Handle interlaced pixels of supported colorTypes"
2909
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   546
    
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   547
    |s|
2699
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   548
2909
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   549
    s := #( #copyPixelsGray:at:by:
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   550
             nil
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   551
             #copyPixelsRGB:at:by: 
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   552
             #copyPixelsIndexed:at:by:
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   553
             #copyPixelsGrayAlpha:at:by: 
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   554
             nil 
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   555
             #copyPixelsRGBA:at:by: 
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   556
        ) at:colorType + 1.
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   557
    self perform:(s asSymbol) with:y with:startX with:incX
1848
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   558
2909
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   559
    "Modified: / 03-05-2011 / 12:02:45 / cg"
3327
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   560
!
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   561
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   562
copyPixelsRGBA:y at:startX by:incX 
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   563
    "Handle interlaced pixels of supported colorTypes.
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   564
     Untested code - please verify"
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   565
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   566
    |srcIndex nPixels dstIndex n r g b a|
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   567
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   568
    srcIndex := 1.
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   569
    dstIndex := y * bytesPerScanline.
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   570
    n := width // incX.
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   571
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   572
    nPixels := thisScanline size.
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   573
    [srcIndex < nPixels] whileTrue:[
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   574
        r := thisScanline at:srcIndex.
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   575
        g := thisScanline at:srcIndex+1.
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   576
        b := thisScanline at:srcIndex+2.
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   577
        a := thisScanline at:srcIndex+3.
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   578
        data replaceFrom:dstIndex+1 to:(dstIndex+3) with:thisScanline startingAt:srcIndex.
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   579
        srcIndex := srcIndex + 4.
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   580
        dstIndex := dstIndex + (incX * 4)
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   581
    ].
2699
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   582
! !
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   583
84b52c63e242 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2698
diff changeset
   584
!PNGReader methodsFor:'private-reading'!
2697
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   585
2683
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   586
getChunk
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   587
    |len type crc|
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   588
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   589
    inStream atEnd ifTrue:[^ false].
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   590
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   591
    len := inStream nextLongMSB:true.
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   592
    type := String new:4.
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   593
    (inStream nextBytes:4 into:type) ~~ 4 ifTrue:[^ false].
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   594
2707
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   595
    Verbose == true ifTrue:[
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   596
        'len: ' infoPrint. len infoPrint. ' type: ' infoPrint. type infoPrintCR.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   597
    ].
2683
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   598
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   599
    type = 'IEND' ifTrue:[^ false].
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   600
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   601
    (self processChunk:type len:len) ifFalse:[^ false].
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   602
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   603
    crc := inStream nextLongMSB:true.  "/ ignored - for now
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   604
    ^ true
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   605
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   606
    "Created: 21.6.1996 / 21:09:36 / cg"
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   607
    "Modified: 21.6.1996 / 21:20:26 / cg"
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   608
!
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   609
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   610
getIHDRChunk
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   611
    |len type crc|
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   612
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   613
    len := inStream nextLongMSB:true.
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   614
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   615
    type := String new:4.
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   616
    inStream nextBytes:4 into:type.
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   617
3183
434c79bbe0bf class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3100
diff changeset
   618
    type = 'IHDR' ifFalse:[self halt:'expected IHDR magic'. ^ false].
434c79bbe0bf class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3100
diff changeset
   619
    len == 13 ifFalse:[self halt:'unexpected IHDR length'. ^ false].
2683
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   620
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   621
    width := inStream nextLongMSB:true.        
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   622
    height := inStream nextLongMSB:true.        
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   623
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   624
    (width <= 0 or:[height <= 0]) ifTrue:[
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   625
        'PNGReader: invalid dimension(s)' infoPrintCR.
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   626
        ^ false.
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   627
    ].
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   628
    self reportDimension.
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   629
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   630
    depth := inStream nextByte.            "/ bits-per-channel
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   631
    colorType := inStream nextByte.
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   632
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   633
    compressionMethod := inStream nextByte.
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   634
    filterMethod := inStream nextByte.
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   635
    interlaceMode := inStream nextByte.
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   636
2707
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   637
    Verbose == true ifTrue:[
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   638
        'PNGReader: IHDR:' infoPrintCR.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   639
        'PNGReader:   width: ' infoPrint. width infoPrintCR.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   640
        'PNGReader:   height: ' infoPrint. height infoPrintCR.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   641
        'PNGReader:   depth: ' infoPrint. depth infoPrintCR.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   642
        'PNGReader:   colorType: ' infoPrint. colorType infoPrintCR.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   643
        'PNGReader:   compressionMethod: ' infoPrint. compressionMethod infoPrintCR.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   644
        'PNGReader:   filterMethod: ' infoPrint. filterMethod infoPrintCR.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   645
        'PNGReader:   interlaceMode: ' infoPrint. interlaceMode infoPrintCR.
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   646
    ].
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   647
e9c5c3ab8c18 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2706
diff changeset
   648
    crc := inStream nextLongMSB:true.
2683
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   649
    ^ true
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   650
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   651
    "Modified: 21.6.1996 / 21:38:35 / cg"
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   652
!
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   653
1848
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   654
setColorType:colorType
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   655
    colorType == 0 ifTrue:[
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   656
        photometric := #blackIs0.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   657
        samplesPerPixel := 1.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   658
        bitsPerSample := Array with:depth.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   659
        ^ true.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   660
    ].
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   661
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   662
    colorType == 2 ifTrue:[
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   663
        depth < 8 ifTrue:[
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   664
            'PNGReader: invalid colorType/depth combination' infoPrintCR.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   665
            ^ false.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   666
        ].
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   667
        photometric := #rgb.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   668
        samplesPerPixel := 3.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   669
        bitsPerSample := Array with:depth with:depth with:depth.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   670
        ^ true.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   671
    ].
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   672
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   673
    colorType == 3 ifTrue:[
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   674
        depth == 16 ifTrue:[
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   675
            'PNGReader: invalid colorType/depth combination' infoPrintCR.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   676
            ^ false.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   677
        ].
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   678
        photometric := #palette.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   679
        samplesPerPixel := 1.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   680
        bitsPerSample := Array with:depth.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   681
        ^ true.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   682
    ].
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   683
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   684
    colorType == 4 ifTrue:[
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   685
        depth < 8 ifTrue:[
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   686
            'PNGReader: invalid colorType/depth combination' infoPrintCR.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   687
            ^ false.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   688
        ].
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   689
        photometric := #blackIs0.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   690
        samplesPerPixel := 2.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   691
        bitsPerSample := Array with:depth with:depth.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   692
        ^ true.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   693
    ].
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   694
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   695
    colorType == 6 ifTrue:[
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   696
        depth < 8 ifTrue:[
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   697
            'PNGReader: invalid colorType/depth combination' infoPrintCR.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   698
            ^ false.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   699
        ].
2702
30fb9cf6affa changed #setColorType:
Claus Gittinger <cg@exept.de>
parents: 2701
diff changeset
   700
        photometric := #rgb.
1848
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   701
        samplesPerPixel := 4.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   702
        bitsPerSample := Array with:depth with:depth with:depth with:depth.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   703
        ^ true.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   704
    ].
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   705
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   706
    ('PNGReader: invalid colorType: ' , colorType printString , '.') infoPrintCR.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   707
    ^ false
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   708
! !
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   709
1805
93f557cbe600 category changes
Claus Gittinger <cg@exept.de>
parents: 1759
diff changeset
   710
!PNGReader methodsFor:'reading'!
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   711
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   712
fromStream:aStream
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   713
    "read a stream containing a PNG image.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   714
     Leave image description in instance variables."
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   715
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   716
    |header|
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   717
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   718
    inStream := aStream.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   719
    aStream binary.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   720
1671
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   721
    "PNG-files are always msb (network-world)"
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   722
    byteOrder := #msb.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   723
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   724
    header := ByteArray new:8.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   725
    aStream nextBytes:8 into:header.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   726
2683
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   727
    header ~= (self pngHeader) ifTrue:[
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   728
        'PNGReader: not a png file.' infoPrintCR.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   729
        ^ nil
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   730
    ].
2695
6197b6af7f4f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2683
diff changeset
   731
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   732
    (self getIHDRChunk) ifFalse:[
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   733
        'PNGReader: required IHDR chunk missing.' infoPrintCR.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   734
        ^ nil
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   735
    ].
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   736
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   737
    compressionMethod ~~ 0 ifTrue:[
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   738
        ('PNGReader: compressionMethod ' , compressionMethod printString , ' not supported.') infoPrintCR.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   739
        ^ nil
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   740
    ].
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   741
    filterMethod > 0 ifTrue:[
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   742
        'PNGReader: invalid filterMethod' infoPrintCR.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   743
        ^ nil.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   744
    ].
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   745
    interlaceMode > 1 ifTrue:[
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   746
        'PNGReader: invalid interlaceMode' infoPrintCR.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   747
        ^ nil.
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   748
    ].
1671
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   749
    (self setColorType:colorType) ifFalse:[
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   750
        ^ nil
1671
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   751
    ].
2706
3c2c631f9d16 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2705
diff changeset
   752
1671
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   753
    [self getChunk] whileTrue.
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   754
2697
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   755
    data := ByteArray new:(self bytesPerRow * height).
8e8223bf25af more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents: 2696
diff changeset
   756
2509
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   757
    globalDataChunk notNil ifTrue:[
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   758
        self processGlobalIDATChunk
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   759
    ].
866b56cabe7e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   760
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   761
    photometric == #palette ifTrue:[
2725
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
   762
        colorMap isNil ifTrue:[
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   763
            'PNGReader: missing palette chunk.' infoPrintCR.
2725
a480f3671588 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2709
diff changeset
   764
            "/ ^ nil
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   765
        ].
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   766
    ].
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   767
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   768
    "
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   769
     PNGReader fromFile:'/home/cg/libpng-0.89c/pngtest.png'
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   770
    "
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   771
2910
d5171b9245e6 comment/format in: #fromStream:
Claus Gittinger <cg@exept.de>
parents: 2909
diff changeset
   772
    "Modified: / 03-05-2011 / 12:17:34 / cg"
1805
93f557cbe600 category changes
Claus Gittinger <cg@exept.de>
parents: 1759
diff changeset
   773
! !
93f557cbe600 category changes
Claus Gittinger <cg@exept.de>
parents: 1759
diff changeset
   774
2683
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   775
!PNGReader methodsFor:'writing to file'!
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   776
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   777
pngHeader
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   778
    ^ #[137 80 78 71 13 10 26 10]
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   779
!
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   780
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   781
save:image onStream:aStream
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   782
    "save image in PNG-file-format onto aStream"
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   783
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   784
    outStream := aStream.
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   785
    outStream binary.
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   786
2703
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   787
    byteOrder := #msb.
2683
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   788
    width := image width.
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   789
    height := image height.
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   790
    photometric := image photometric.
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   791
    samplesPerPixel := image samplesPerPixel.
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   792
    bitsPerSample := image bitsPerSample.
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   793
    colorMap := image colorMap.
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   794
    data := image bits.
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   795
2703
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   796
    outStream nextPutAll:(self pngHeader).
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   797
    self writeIHDRChunk.
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   798
    photometric == #palette ifTrue: [self writePaletteChunk].
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   799
    self writeImageDataChunk.
2683
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   800
    self writeEndChunk
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   801
!
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   802
2703
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   803
writeChunk:chunkTypeChars size:len with:aBlock
2708
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   804
    |crc realOutStream chunkBytes|
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   805
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   806
    realOutStream := outStream.
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   807
    outStream := WriteStream on:(ByteArray new:len).
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   808
2703
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   809
    outStream nextPutBytes:4 from:chunkTypeChars startingAt:1.
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   810
    aBlock value.
2708
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   811
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   812
    chunkBytes := outStream contents.
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   813
    crc := CRC32Stream hashValueOf:chunkBytes.
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   814
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   815
    outStream := realOutStream.
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   816
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   817
    realOutStream nextPutLong:len MSB:true.
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   818
    realOutStream nextPutAll:chunkBytes.
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   819
    realOutStream nextPutLong:crc MSB:true.
2703
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   820
!
2683
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   821
2703
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   822
writeEndChunk
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   823
    self 
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   824
        writeChunk: 'IEND'
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   825
        size: 0
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   826
        with:[ ]
2683
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   827
!
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   828
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   829
writeFileHeader
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   830
    outStream nextPutAll:(self pngHeader)
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   831
!
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   832
2703
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   833
writeIHDRChunk
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   834
    |colorType|
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   835
3100
0c82f117bb45 class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 2910
diff changeset
   836
    ((photometric == #whiteIs0) or:[ (photometric == #blackIs0)]) ifTrue:[
2703
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   837
        colorType := 0.
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   838
        samplesPerPixel size > 1 ifTrue:[ colorType := colorType + 4].  "/ +alpha
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   839
    ].
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   840
    (photometric == #rgb) ifTrue:[
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   841
        colorType := 2.
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   842
        samplesPerPixel size > 3 ifTrue:[ colorType := colorType + 4].  "/ +alpha
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   843
    ].
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   844
    (photometric == #palette) ifTrue:[
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   845
        colorType := 3.
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   846
        samplesPerPixel size > 1 ifTrue:[ colorType := colorType + 4].  "/ +alpha
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   847
    ].
3100
0c82f117bb45 class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 2910
diff changeset
   848
    colorType isNil ifTrue:[
0c82f117bb45 class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 2910
diff changeset
   849
        self error:'unjandled photometric'
0c82f117bb45 class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 2910
diff changeset
   850
    ].
2703
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   851
2683
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   852
    self 
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   853
        writeChunk: 'IHDR'
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   854
        size: 13
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   855
        with:[ 
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   856
            self writeLong:width.
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   857
            self writeLong:height.
2703
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   858
            outStream nextPut: 8.  "Assume that all colors are 24bit"     "/ depth
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   859
            outStream nextPut: colorType.  
2683
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   860
            outStream nextPut: 0.  "Compression"
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   861
            outStream nextPut: 0.  "Filter method"
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   862
            outStream nextPut: 0   "Non-interlaced"
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   863
        ]
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   864
!
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   865
2703
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   866
writeImageDataChunk
2708
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   867
    |compressedByteStream compressedBytes zlibWriter bytesPerScanline idx|
2703
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   868
2708
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   869
    compressedByteStream := WriteStream on:(ByteArray new:100).
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   870
    zlibWriter := ZipStream writeOpenAsZipStreamOn:compressedByteStream suppressHeaderAndChecksum:false.
2703
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   871
    zlibWriter binary.
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   872
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   873
    bytesPerScanline := self bytesPerRow.
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   874
2708
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   875
    idx := 1.
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   876
    0 to:height-1 do:[:y |
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   877
        |row|
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   878
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   879
        row := data copyFrom:idx to:(idx+bytesPerScanline-1).
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   880
        zlibWriter nextPutAll:#[0].       "/ no filter
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   881
        zlibWriter nextPutAll:row.
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   882
        idx := idx + bytesPerScanline.
2703
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   883
    ].
2708
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   884
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   885
    zlibWriter close.
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   886
    compressedBytes := compressedByteStream contents.
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   887
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   888
    self 
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   889
        writeChunk: 'IDAT'
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   890
        size:(compressedBytes size)
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   891
        with:[ 
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   892
            outStream nextPutAll:compressedBytes
c206c9e9bd61 more for writing
Claus Gittinger <cg@exept.de>
parents: 2707
diff changeset
   893
        ]
2703
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   894
!
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   895
2683
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   896
writePaletteChunk
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   897
    self 
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   898
        writeChunk: 'PLTE'
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   899
        size: 3 * colorMap size
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   900
        with:[
2703
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   901
            colorMap do:[:color | 
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   902
                outStream
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   903
                    nextPut: color redByte;
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   904
                    nextPut: color greenByte;
ebf1067a3289 unfinished writing
Claus Gittinger <cg@exept.de>
parents: 2702
diff changeset
   905
                    nextPut: color blueByte
2683
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   906
            ]
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   907
        ]
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   908
! !
8c7880d9cedb preps for writing
Claus Gittinger <cg@exept.de>
parents: 2509
diff changeset
   909
398
aef700d15416 new suffix-table
Claus Gittinger <cg@exept.de>
parents: 308
diff changeset
   910
!PNGReader class methodsFor:'documentation'!
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   911
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   912
version
3327
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   913
    ^ '$Header: /cvs/stx/stx/libview2/PNGReader.st,v 1.33 2014-05-06 14:34:49 cg Exp $'
2909
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   914
!
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   915
3f9856162142 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 2725
diff changeset
   916
version_CVS
3327
4758853d1abe class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 3183
diff changeset
   917
    ^ '$Header: /cvs/stx/stx/libview2/PNGReader.st,v 1.33 2014-05-06 14:34:49 cg Exp $'
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   918
! !
1671
6fbb20a4af3a more but still unfinished
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   919
3100
0c82f117bb45 class: PNGReader
Claus Gittinger <cg@exept.de>
parents: 2910
diff changeset
   920
308
ba8b8191d8b6 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   921
PNGReader initialize!