TargaReader.st
author Claus Gittinger <cg@exept.de>
Fri, 01 Sep 2017 15:36:01 +0200
changeset 4009 bece1481d314
parent 4002 91e154c3580d
child 4016 47c4ea8134ec
permissions -rw-r--r--
#DOCUMENTATION by cg class: XWDReader changed: #readImage class: XWDReader class comment/format in: #documentation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37
aeed5856f457 Initial revision
claus
parents:
diff changeset
     1
"
aeed5856f457 Initial revision
claus
parents:
diff changeset
     2
 COPYRIGHT (c) 1994 by Claus Gittinger
aeed5856f457 Initial revision
claus
parents:
diff changeset
     3
	      All Rights Reserved
aeed5856f457 Initial revision
claus
parents:
diff changeset
     4
aeed5856f457 Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
aeed5856f457 Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
aeed5856f457 Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
aeed5856f457 Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
aeed5856f457 Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
aeed5856f457 Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
aeed5856f457 Initial revision
claus
parents:
diff changeset
    11
"
1577
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
    12
"{ Package: 'stx:libview2' }"
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
    13
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
    14
"{ NameSpace: Smalltalk }"
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
    15
37
aeed5856f457 Initial revision
claus
parents:
diff changeset
    16
ImageReader subclass:#TargaReader
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
    17
	instanceVariableNames:'orientation bytesPerRow bytesPerPixel imageType'
210
5405de794686 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    18
	classVariableNames:''
5405de794686 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    19
	poolDictionaries:''
1745
4fa0fad2a463 code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents: 1738
diff changeset
    20
	category:'Graphics-Images-Readers'
37
aeed5856f457 Initial revision
claus
parents:
diff changeset
    21
!
aeed5856f457 Initial revision
claus
parents:
diff changeset
    22
aeed5856f457 Initial revision
claus
parents:
diff changeset
    23
!TargaReader class methodsFor:'documentation'!
aeed5856f457 Initial revision
claus
parents:
diff changeset
    24
aeed5856f457 Initial revision
claus
parents:
diff changeset
    25
copyright
aeed5856f457 Initial revision
claus
parents:
diff changeset
    26
"
aeed5856f457 Initial revision
claus
parents:
diff changeset
    27
 COPYRIGHT (c) 1994 by Claus Gittinger
aeed5856f457 Initial revision
claus
parents:
diff changeset
    28
	      All Rights Reserved
aeed5856f457 Initial revision
claus
parents:
diff changeset
    29
aeed5856f457 Initial revision
claus
parents:
diff changeset
    30
 This software is furnished under a license and may be used
aeed5856f457 Initial revision
claus
parents:
diff changeset
    31
 only in accordance with the terms of that license and with the
aeed5856f457 Initial revision
claus
parents:
diff changeset
    32
 inclusion of the above copyright notice.   This software may not
aeed5856f457 Initial revision
claus
parents:
diff changeset
    33
 be provided or otherwise made available to, or used by, any
aeed5856f457 Initial revision
claus
parents:
diff changeset
    34
 other person.  No title to or ownership of the software is
aeed5856f457 Initial revision
claus
parents:
diff changeset
    35
 hereby transferred.
aeed5856f457 Initial revision
claus
parents:
diff changeset
    36
"
aeed5856f457 Initial revision
claus
parents:
diff changeset
    37
!
aeed5856f457 Initial revision
claus
parents:
diff changeset
    38
aeed5856f457 Initial revision
claus
parents:
diff changeset
    39
documentation
aeed5856f457 Initial revision
claus
parents:
diff changeset
    40
"
aeed5856f457 Initial revision
claus
parents:
diff changeset
    41
    this class provides methods for loading targa-file (tga) images.
556
a2d03470af59 documentation
Claus Gittinger <cg@exept.de>
parents: 555
diff changeset
    42
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
    43
    The Targa-File format is an image file format used by a wide variety of both
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
    44
    scanners and imaging software, and exists in many incarnations. 
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
    45
    For details, please refer to the 'Truevision Technical Guide':
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
    46
37
aeed5856f457 Initial revision
claus
parents:
diff changeset
    47
    Limitations: 
556
a2d03470af59 documentation
Claus Gittinger <cg@exept.de>
parents: 555
diff changeset
    48
        not fully tested (I only had a few targa files to check things)
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
    49
        only supports 8, 24 and 32 bits/pixel formats; 
210
5405de794686 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    50
        Image saving not supported
37
aeed5856f457 Initial revision
claus
parents:
diff changeset
    51
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
    52
    I had two tga files to test this code with; 
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
    53
    therefore it may pr may not work with other targa files 
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
    54
    (it certainly does not work with 1/16 bit images).
299
23d89531904f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 259
diff changeset
    55
37
aeed5856f457 Initial revision
claus
parents:
diff changeset
    56
    Suggestions: adapt & use the pbmplus library here.
210
5405de794686 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    57
5405de794686 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    58
    [See also:]
234
b6352d13e792 xrefs in documentation
Claus Gittinger <cg@exept.de>
parents: 210
diff changeset
    59
        Image Form Icon
210
5405de794686 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    60
        BlitImageReader FaceReader GIFReader JPEGReader PBMReader PCXReader 
5405de794686 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    61
        ST80FormReader SunRasterReader TIFFReader WindowsIconReader 
5405de794686 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
    62
        XBMReader XPMReader XWDReader
4001
153c0234371a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3998
diff changeset
    63
        
153c0234371a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3998
diff changeset
    64
        http://www.dca.fee.unicamp.br/~martino/disciplinas/ea978/tgaffs.pdf
4002
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
    65
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
    66
    [examples:]
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
    67
        Smalltalk loadPackage:'stx:goodies/communication'.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
    68
        Smalltalk loadPackage:'exept:libcrypt/ssl'.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
    69
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
    70
        |file|
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
    71
        file := (HTTPInterface getStreamFor:'https://samples.libav.org/image-samples/TGA/rgb32rle.tga') stream contents.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
    72
        (TargaReader fromStream:file readStream) inspect.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
    73
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
    74
        |file|
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
    75
        file := (HTTPInterface getStreamFor:'https://samples.libav.org/image-samples/TGA/rgb16rle.tga') stream contents.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
    76
        TargaReader fromStream:file readStream.
37
aeed5856f457 Initial revision
claus
parents:
diff changeset
    77
"
1738
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    78
!
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    79
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    80
format
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    81
"
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    82
    Shorts etc. are lsb.
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    83
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    84
    offset:
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    85
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    86
        1       lenID           byte
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    87
        2       hasColorMap     byte
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    88
        3       imageType       byte
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    89
                         1   MapRGB  
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    90
                         2   RawRGB   
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    91
                         3   RawMono   
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    92
                         9   MapEnCode 
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    93
                         10  RawEnCode 
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    94
        4, 5    cmapOffset      short
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    95
        6, 7    cmapLength      short
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    96
        8       cmapEntrySize   short
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    97
        9,10    xOrg            short
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    98
        11,12   yOrg            short
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
    99
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   100
        13,14   width 
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   101
        15,16   height 
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   102
        17      depth 
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   103
        18      flags
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   104
                        0000 xxxx  attribute-bits-per-pixel
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   105
                        0000 0001  greysc
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   106
                        0000 0010  colour
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   107
                        0000 0011  mapped
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   108
                        0000 0100  rleEncoded
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   109
                        0000 1000  interlaced
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   110
                        00xx 0000  origin (0 -> lower-left / 1 -> l-r / 2 -> u-l / 3 -> u-r)
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   111
                        xx00 0000  interleave (0 -> none / 1 -> odd/even / 2 ->4-fould / 3 reserved)
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   112
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   113
        19..    len             lenID-bytes
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   114
        ..      colorMap        cmapLength*3 bytes iff hasColorMap ~~ 0
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   115
"
37
aeed5856f457 Initial revision
claus
parents:
diff changeset
   116
! !
aeed5856f457 Initial revision
claus
parents:
diff changeset
   117
aeed5856f457 Initial revision
claus
parents:
diff changeset
   118
!TargaReader class methodsFor:'initialization'!
aeed5856f457 Initial revision
claus
parents:
diff changeset
   119
aeed5856f457 Initial revision
claus
parents:
diff changeset
   120
initialize
210
5405de794686 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   121
    "tell Image-class, that a new fileReader is present
398
aef700d15416 new suffix-table
Claus Gittinger <cg@exept.de>
parents: 299
diff changeset
   122
     for the '.tga' extension."
210
5405de794686 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   123
647
6f26c76aa0c9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 630
diff changeset
   124
    MIMETypes defineImageType:'image/x-targa' suffix:'tga' reader:self.
37
aeed5856f457 Initial revision
claus
parents:
diff changeset
   125
630
48657c687a8b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 556
diff changeset
   126
    "Modified: 27.6.1997 / 18:39:43 / cg"
37
aeed5856f457 Initial revision
claus
parents:
diff changeset
   127
! !
aeed5856f457 Initial revision
claus
parents:
diff changeset
   128
aeed5856f457 Initial revision
claus
parents:
diff changeset
   129
!TargaReader class methodsFor:'testing'!
aeed5856f457 Initial revision
claus
parents:
diff changeset
   130
aeed5856f457 Initial revision
claus
parents:
diff changeset
   131
isValidImageFile:aFileName
aeed5856f457 Initial revision
claus
parents:
diff changeset
   132
    "return true, if aFileName contains a targa-file image"
aeed5856f457 Initial revision
claus
parents:
diff changeset
   133
1577
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
   134
    |aStream w h depth flags imageType ok|
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
   135
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
   136
    ok := true.
37
aeed5856f457 Initial revision
claus
parents:
diff changeset
   137
43
e85c7d392833 *** empty log message ***
claus
parents: 37
diff changeset
   138
    aStream := self streamReadingFile:aFileName.
e85c7d392833 *** empty log message ***
claus
parents: 37
diff changeset
   139
    aStream isNil ifTrue:[^ false].
e85c7d392833 *** empty log message ***
claus
parents: 37
diff changeset
   140
    aStream binary.
1577
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
   141
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
   142
    aStream next.   "lenID"
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
   143
    aStream next.   "hasColorMap"
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
   144
    imageType := aStream next.  
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
   145
    aStream skip:2. "cmapOffset"
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
   146
    aStream skip:2. "cmapLength"
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
   147
    aStream next.   "cmapEntrySize"
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
   148
    aStream skip:2. "xOrg"
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
   149
    aStream skip:2. "yOrg"
37
aeed5856f457 Initial revision
claus
parents:
diff changeset
   150
43
e85c7d392833 *** empty log message ***
claus
parents: 37
diff changeset
   151
    w := aStream nextShortMSB:false.
e85c7d392833 *** empty log message ***
claus
parents: 37
diff changeset
   152
    h := aStream nextShortMSB:false.
37
aeed5856f457 Initial revision
claus
parents:
diff changeset
   153
43
e85c7d392833 *** empty log message ***
claus
parents: 37
diff changeset
   154
    depth := aStream next.
e85c7d392833 *** empty log message ***
claus
parents: 37
diff changeset
   155
    flags := aStream next.
37
aeed5856f457 Initial revision
claus
parents:
diff changeset
   156
1577
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
   157
    "/ MapRGB == 1
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
   158
    "/ RawRGB == 2
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
   159
    "/ RawMono == 3
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
   160
    "/ MapEnCode == 9
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
   161
    "/ RawEnCode == 10
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
   162
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
   163
    (#(1 2 3 9 10) includes:imageType) ifFalse:[
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
   164
        "/ 'TargaReader [warning]: unsupported imageType: ' errorPrint. imageType errorPrintCR.
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
   165
        ok := false
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
   166
    ] ifTrue:[
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
   167
        (#(8 "16" 24 32) includes:depth) ifFalse:[
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
   168
            ok := false
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
   169
        ].
37
aeed5856f457 Initial revision
claus
parents:
diff changeset
   170
    ].
43
e85c7d392833 *** empty log message ***
claus
parents: 37
diff changeset
   171
    aStream close. 
1577
a8851bce16c5 isValidImageFile improved
Claus Gittinger <cg@exept.de>
parents: 1079
diff changeset
   172
    ^ ok
37
aeed5856f457 Initial revision
claus
parents:
diff changeset
   173
aeed5856f457 Initial revision
claus
parents:
diff changeset
   174
    "
aeed5856f457 Initial revision
claus
parents:
diff changeset
   175
     TargaReader isValidImageFile:'bitmaps/test.tga'    
aeed5856f457 Initial revision
claus
parents:
diff changeset
   176
     TargaReader isValidImageFile:'bitmaps/garfield.gif'  
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   177
    "
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   178
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   179
    "Modified: 21.4.1997 / 20:46:52 / cg"
37
aeed5856f457 Initial revision
claus
parents:
diff changeset
   180
! !
aeed5856f457 Initial revision
claus
parents:
diff changeset
   181
1848
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   182
!TargaReader methodsFor:'private-reading'!
37
aeed5856f457 Initial revision
claus
parents:
diff changeset
   183
1738
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   184
handleImageOrientation
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   185
    |rowIdx startIdx endIdx t|
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   186
1738
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   187
    orientation == #topLeft ifTrue:[
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   188
        ^ self.
37
aeed5856f457 Initial revision
claus
parents:
diff changeset
   189
    ].
aeed5856f457 Initial revision
claus
parents:
diff changeset
   190
1738
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   191
    orientation == #topRight ifTrue:[
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   192
        "/ flip horizontal
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   193
        rowIdx := 1.
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   194
        1 to:height do:[:row |
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   195
            startIdx := rowIdx.
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   196
            endIdx := rowIdx + bytesPerRow - bytesPerPixel.
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   197
            1 to:width//2 do:[:x |
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   198
                0 to:bytesPerPixel-1 do:[:c |
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   199
                    t := data at:startIdx+c.
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   200
                    data at:startIdx+c put:(data at:endIdx+c).
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   201
                    data at:endIdx+c put:t.
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   202
                ].
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   203
                startIdx := startIdx + bytesPerPixel.
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   204
                endIdx := endIdx - bytesPerPixel.
1065
32a68620ddab care for EOF while reading colorMap.
Claus Gittinger <cg@exept.de>
parents: 918
diff changeset
   205
            ].
1738
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   206
            rowIdx := rowIdx + width.
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   207
        ].
1738
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   208
        ^ self.
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   209
    ].
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   210
1738
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   211
    orientation == #bottomLeft ifTrue:[
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   212
        "/ flip vertical
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   213
        startIdx := 1.
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   214
        endIdx := 1 + ((height - 1) * bytesPerRow).
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   215
        t := ByteArray new:bytesPerRow.
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   216
        1 to:height//2 do:[:row |
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   217
            t replaceFrom:1 to:bytesPerRow with:data startingAt:startIdx.
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   218
            data replaceFrom:startIdx to:startIdx+bytesPerRow-1 with:data startingAt:endIdx.
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   219
            data replaceFrom:endIdx to:endIdx+bytesPerRow-1 with:t startingAt:1.
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   220
            startIdx := startIdx + bytesPerRow.
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   221
            endIdx := endIdx - bytesPerRow
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   222
        ].
1738
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   223
        ^ self.
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   224
    ].
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   225
1738
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   226
    'TargaReader [warning]: unsupported orientation: ' errorPrint. orientation errorPrintCR.
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   227
!
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   228
4002
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   229
read16
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   230
    "read a 16 bit/pixel targa-image"
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   231
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   232
    |totalBytes remainingBytes dstIndex b1 b2|
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   233
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   234
    totalBytes := width * height * 2.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   235
    data := ByteArray new:totalBytes.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   236
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   237
    dstIndex := 1.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   238
    remainingBytes := totalBytes.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   239
    [remainingBytes > 0] whileTrue:[
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   240
        b1 := inStream nextByte.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   241
        b2 := inStream nextByte.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   242
        data at:dstIndex   put:b1.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   243
        data at:dstIndex+1 put:b2.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   244
        dstIndex := dstIndex + 2.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   245
        remainingBytes := remainingBytes - 2.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   246
    ].
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   247
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   248
    "Created: / 29-08-2017 / 23:38:30 / cg"
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   249
!
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   250
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   251
read16RLE
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   252
    "read a 16 bit/pixel rle encoded targa-image"
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   253
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   254
    |total count dstIndex code n b1 b2|
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   255
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   256
    data := ByteArray new:((total := width * height * 2)).
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   257
    count := 0.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   258
    dstIndex := 1.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   259
    [count < total] whileTrue:[
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   260
        code := inStream nextByte.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   261
        n := (code bitAnd:16r7F) + 1.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   262
        (code bitAnd:16r80) ~~ 0 ifTrue:[
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   263
            b1 := inStream nextByte.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   264
            b2 := inStream nextByte.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   265
            n timesRepeat:[
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   266
                data at:dstIndex put:b1.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   267
                data at:dstIndex+1 put:b2.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   268
                dstIndex := dstIndex + 2
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   269
            ].
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   270
        ] ifFalse:[
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   271
            n timesRepeat:[
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   272
                b1 := inStream nextByte.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   273
                b2 := inStream nextByte.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   274
                data at:dstIndex put:b1.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   275
                data at:dstIndex+1 put:b2.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   276
                dstIndex := dstIndex + 2
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   277
            ]
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   278
        ].
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   279
        count := count + (n * 2).
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   280
    ].
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   281
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   282
    "Created: / 29-08-2017 / 23:39:19 / cg"
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   283
!
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   284
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   285
read24
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   286
    "read a 24 bit/pixel targa-image"
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   287
1738
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   288
    |totalBytes|
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   289
1738
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   290
    totalBytes := width * height * 3.
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   291
    data := ByteArray new:totalBytes.
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   292
    inStream nextBytes:totalBytes into:data.
299
23d89531904f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 259
diff changeset
   293
37
aeed5856f457 Initial revision
claus
parents:
diff changeset
   294
    "
299
23d89531904f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 259
diff changeset
   295
     mhmh - pixel-byte order is blue-green-red
23d89531904f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 259
diff changeset
   296
     swap blue & red bytes
37
aeed5856f457 Initial revision
claus
parents:
diff changeset
   297
    "
1738
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   298
    self class swap:totalBytes bytesFromRGB_to_BGR_in:data.
37
aeed5856f457 Initial revision
claus
parents:
diff changeset
   299
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   300
    "Modified: / 29-08-2017 / 23:06:02 / cg"
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   301
!
37
aeed5856f457 Initial revision
claus
parents:
diff changeset
   302
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   303
read24RLE
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   304
    "read an 8 bit/pixel rle encoded targa-image"
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   305
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   306
    |total count dstIndex code n r g b|
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   307
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   308
    data := ByteArray new:((total := width * height * 3)).
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   309
    count := 0.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   310
    dstIndex := 1.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   311
    [count < total] whileTrue:[
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   312
        code := inStream nextByte.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   313
        n := (code bitAnd:16r7F) + 1.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   314
        (code bitAnd:16r80) ~~ 0 ifTrue:[
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   315
            b := inStream nextByte.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   316
            g := inStream nextByte.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   317
            r := inStream nextByte.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   318
            n timesRepeat:[
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   319
                data at:dstIndex put:r.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   320
                data at:dstIndex+1 put:g.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   321
                data at:dstIndex+2 put:b.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   322
                dstIndex := dstIndex + 3
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   323
            ].
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   324
        ] ifFalse:[
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   325
            n timesRepeat:[
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   326
                b := inStream nextByte.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   327
                g := inStream nextByte.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   328
                r := inStream nextByte.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   329
                data at:dstIndex put:r.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   330
                data at:dstIndex+1 put:g.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   331
                data at:dstIndex+2 put:b.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   332
                dstIndex := dstIndex + 3
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   333
            ]
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   334
        ].
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   335
        count := count + (n * 3).
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   336
    ].
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   337
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   338
    "Created: / 21-04-1997 / 20:43:23 / cg"
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   339
    "Modified: / 29-08-2017 / 23:06:35 / cg"
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   340
!
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   341
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   342
read32
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   343
    "read a 32 bit/pixel targa-image"
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   344
1738
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   345
    |totalBytes remainingBytes dstIndex a r g b|
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   346
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   347
    totalBytes := width * height * 4.
1738
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   348
    data := ByteArray new:totalBytes.
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   349
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   350
    dstIndex := 1.
1738
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   351
    remainingBytes := totalBytes.
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   352
    [remainingBytes > 0] whileTrue:[
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   353
        b := inStream nextByte.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   354
        g := inStream nextByte.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   355
        r := inStream nextByte.
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   356
        a := inStream nextByte.  
1738
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   357
        data at:dstIndex   put:r.
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   358
        data at:dstIndex+1 put:g.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   359
        data at:dstIndex+2 put:b.
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   360
        data at:dstIndex+3 put:a.
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   361
        dstIndex := dstIndex + 4.
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   362
        remainingBytes := remainingBytes - 4.
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   363
    ].
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   364
1738
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   365
    "
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   366
     TargaReader fromFile:'/phys/exept//unsaved1/pd_stuff/graphic/3d_engines/crystal_space/CS/plugins/video/canvas/sdl/img/move.tga'
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   367
     TargaReader fromFile:'/phys/exept/unsaved2/smalltalk/Squeak/croquet/Croquet/Content/Cisco/2520/Textures/2520caseback.tga'
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   368
     TargaReader fromFile:'/phys/exept/unsaved2/smalltalk/Squeak/croquet/Croquet/Content/Cisco/2520/Textures/2520dimm1front.tga'
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   369
    "
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   370
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   371
    "Modified: / 29-08-2017 / 23:09:42 / cg"
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   372
!
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   373
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   374
read32RLE
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   375
    "read a 32 bit/pixel rle encoded targa-image; skip alpha channel (for now)"
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   376
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   377
    |total count dstIndex code n a r g b|
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   378
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   379
    data := ByteArray new:((total := width * height * 4)).
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   380
    count := 0.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   381
    dstIndex := 1.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   382
    [count < total] whileTrue:[
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   383
        code := inStream nextByte.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   384
        n := (code bitAnd:16r7F) + 1.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   385
        (code bitAnd:16r80) ~~ 0 ifTrue:[
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   386
            b := inStream nextByte.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   387
            g := inStream nextByte.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   388
            r := inStream nextByte.
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   389
            a := inStream nextByte.  
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   390
            n timesRepeat:[
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   391
                data at:dstIndex put:r.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   392
                data at:dstIndex+1 put:g.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   393
                data at:dstIndex+2 put:b.
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   394
                data at:dstIndex+3 put:a.
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   395
                dstIndex := dstIndex + 4
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   396
            ].
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   397
        ] ifFalse:[
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   398
            n timesRepeat:[
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   399
                b := inStream nextByte.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   400
                g := inStream nextByte.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   401
                r := inStream nextByte.
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   402
                a := inStream nextByte.        
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   403
                data at:dstIndex put:r.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   404
                data at:dstIndex+1 put:g.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   405
                data at:dstIndex+2 put:b.
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   406
                data at:dstIndex+3 put:a.
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   407
                dstIndex := dstIndex + 4
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   408
            ]
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   409
        ].
4002
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   410
        count := count + (n * 4).
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   411
    ].
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   412
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   413
    "Created: / 21-04-1997 / 20:43:54 / cg"
4002
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   414
    "Modified: / 29-08-2017 / 23:35:48 / cg"
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   415
!
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   416
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   417
read8
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   418
    "read an 8 bit/pixel targa-image"
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   419
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   420
    data := ByteArray new:(width * height).
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   421
    inStream nextBytes:(data size) into:data.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   422
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   423
    "Created: / 21-04-1997 / 20:12:35 / cg"
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   424
    "Modified: / 29-08-2017 / 23:10:32 / cg"
555
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   425
!
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   426
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   427
read8RLE
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   428
    "read an 8 bit/pixel rle encoded targa-image"
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   429
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   430
    |total count dstIndex code n byte|
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   431
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   432
    data := ByteArray new:((total := width * height)).
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   433
    count := 0.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   434
    dstIndex := 1.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   435
    [count < total] whileTrue:[
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   436
        code := inStream nextByte.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   437
        n := (code bitAnd:16r7F) + 1.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   438
        (code bitAnd:16r80) ~~ 0 ifTrue:[
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   439
            byte := inStream nextByte.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   440
            data from:dstIndex to:dstIndex+n-1 put:byte.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   441
        ] ifFalse:[
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   442
            inStream nextBytes:n into:data startingAt:dstIndex.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   443
        ].
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   444
        count := count + n.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   445
        dstIndex := dstIndex + n.
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   446
    ].
f51ec2cc4482 support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
   447
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   448
    "Created: / 21-04-1997 / 20:19:46 / cg"
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   449
    "Modified: / 29-08-2017 / 23:10:46 / cg"
1738
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   450
!
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   451
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   452
readColorMap:cmapLength
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   453
    colorMap := Array new:cmapLength.
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   454
    1 to:cmapLength do:[:index |
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   455
        |r g b|
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   456
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   457
        b := inStream nextByte.
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   458
        g := inStream nextByte.
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   459
        r := inStream nextByte.
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   460
        (r isNil or:[g isNil or:[b isNil]]) ifTrue:[
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   461
            ^ self fileFormatError:('short file - probably not targa format').
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   462
        ].
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   463
        colorMap at:index put:(Color redByte:r greenByte:g blueByte:b).
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   464
    ].
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   465
    ^ colorMap
37
aeed5856f457 Initial revision
claus
parents:
diff changeset
   466
! !
aeed5856f457 Initial revision
claus
parents:
diff changeset
   467
1848
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   468
!TargaReader methodsFor:'reading'!
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   469
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   470
readImage
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   471
    "read a targa-image from aFileName. return the receiver (with all
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   472
     relevant instance variables set for the image) or nil on error"
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   473
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   474
    |depth flags lenID hasColorMap 
1848
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   475
     cmapOffset cmapLength cmapEntrySize xOrg yOrg rle|
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   476
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   477
    inStream binary.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   478
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   479
    lenID := inStream next.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   480
    hasColorMap := inStream next.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   481
    imageType := inStream next.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   482
    cmapOffset := inStream nextShortMSB:false.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   483
    cmapLength := inStream nextShortMSB:false.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   484
    cmapEntrySize := inStream next.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   485
    xOrg := inStream nextShortMSB:false.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   486
    yOrg := inStream nextShortMSB:false.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   487
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   488
    width := inStream nextShortMSB:false.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   489
    height := inStream nextShortMSB:false.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   490
    depth := inStream next.
4002
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   491
    (#(8 15 16 24 32) includes:depth) ifFalse:[
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   492
        ^ self fileFormatError:'unsupported depth: %1' with:depth printString.
1848
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   493
    ].
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   494
    depth == 32 ifTrue:[
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   495
        'TargaReader [info]: alpha channel ignored' infoPrintCR.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   496
    ] ifFalse:[
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   497
        'TargaReader [info]: depth: ' infoPrint. depth infoPrintCR.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   498
    ].
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   499
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   500
    "/ MapRGB == 1
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   501
    "/ RawRGB == 2
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   502
    "/ RawMono == 3
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   503
    "/ MapEnCode == 9
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   504
    "/ RawEnCode == 10
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   505
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   506
    (#(1 2 3 9 10) includes:imageType) ifFalse:[
1848
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   507
        "/ 'TargaReader [warning]: unsupported imageType: ' errorPrint. imageType errorPrintCR.
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   508
        ^ self fileFormatError:'unsupported imageType: %1' with:imageType printString.
1848
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   509
    ].
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   510
    'TargaReader [info]: imageType: ' infoPrint. imageType infoPrintCR.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   511
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   512
    self reportDimension.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   513
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   514
    "/ flags:
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   515
    "/    0000 xxxx  attribute-bits-per-pixel
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   516
    "/    0000 0001  greysc
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   517
    "/    0000 0010  colour
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   518
    "/    0000 0011  mapped
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   519
    "/    0000 0100  rleEncoded
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   520
    "/    0000 1000  interlaced
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   521
    "/    00xx 0000  origin (0 -> lower-left / 1 -> l-r / 2 -> u-l / 3 -> u-r)
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   522
    "/    xx00 0000  interleave (0 -> none / 1 -> odd/even / 2 ->4-fould / 3 reserved)
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   523
    "/
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   524
    flags := inStream next.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   525
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   526
    (flags bitAnd:2r11000000) ~~ 0 ifTrue:[
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   527
        ^ self fileFormatError:'unsupported interlace: %1' with:flags printString.
1848
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   528
    ].
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   529
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   530
    rle := flags bitTest:2r000001000.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   531
    flags := flags bitAnd:2r111110111.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   532
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   533
    (flags bitAnd:2r00001111) ~~ 0 ifTrue:[
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   534
        ^ self fileFormatError:'unsupported flags: %1' with:flags printString.
1848
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   535
    ].
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   536
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   537
    (flags bitAnd:2r00110000) == 16r20 ifTrue:[
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   538
        orientation := #topLeft
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   539
    ] ifFalse:[
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   540
        (flags bitAnd:2r00110000) == 16r30 ifTrue:[
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   541
            orientation := #topRight
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   542
        ] ifFalse:[
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   543
            (flags bitAnd:2r00110000) == 16r10 ifTrue:[
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   544
                orientation := #bottomRight
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   545
            ] ifFalse:[
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   546
                (flags bitAnd:2r00110000) == 0 ifTrue:[
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   547
                    orientation := #bottomLeft
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   548
                ]
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   549
            ]
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   550
        ]
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   551
    ].
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   552
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   553
    lenID ~~ 0 ifTrue:[
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   554
        inStream skip:lenID
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   555
    ].
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   556
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   557
    hasColorMap ~~ 0 ifTrue:[
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   558
        "/ read the colorMap
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   559
        colorMap := self readColorMap:cmapLength.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   560
        'TargaReader [info]: has colorMap' infoPrintCR.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   561
    ].
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   562
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   563
    depth == 32 ifTrue:[
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   564
        imageType == 2 ifTrue:[
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   565
            "/ rle ifTrue:[self halt:'oops - should not happen'].
1848
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   566
            self read32.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   567
        ] ifFalse:[
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   568
            "/ rle ifFalse:[self halt:'oops - should not happen'].
1848
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   569
            self read32RLE.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   570
        ].
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   571
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   572
        photometric := #rgba.
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   573
        samplesPerPixel := 4.
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   574
        bitsPerSample := #(8 8 8 8).
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   575
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   576
        bytesPerRow := width*4.
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   577
        bytesPerPixel := 4.
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   578
    ].
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   579
    
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   580
    depth == 24 ifTrue:[
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   581
        imageType == 2 ifTrue:[
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   582
            "/ rle ifTrue:[self halt:'oops - should not happen'].
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   583
            self read24.
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   584
        ] ifFalse:[
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   585
            "/ rle ifFalse:[self halt:'oops - should not happen'].
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   586
            self read24RLE.
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   587
        ].
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   588
        
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   589
        photometric := #rgb.
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   590
        samplesPerPixel := 3.
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   591
        bitsPerSample := #(8 8 8).
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   592
1848
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   593
        bytesPerRow := width*3.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   594
        bytesPerPixel := 3.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   595
    ].
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   596
    
4002
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   597
    ((depth == 15) or:[depth == 16]) ifTrue:[
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   598
        imageType == 2 ifTrue:[
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   599
            "/ rle ifTrue:[self halt:'oops - should not happen'].
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   600
            self read16.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   601
        ] ifFalse:[
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   602
            "/ rle ifFalse:[self halt:'oops - should not happen'].
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   603
            self read16RLE.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   604
        ].
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   605
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   606
        photometric := #rgb.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   607
        samplesPerPixel := 3.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   608
        bitsPerSample := #(5 5 5).
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   609
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   610
        bytesPerRow := width*2.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   611
        bytesPerPixel := 2.
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   612
    ].
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   613
1848
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   614
    depth == 8 ifTrue:[
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   615
        (imageType == 1 or:[imageType == 3]) ifTrue:[
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   616
            "/ rle ifTrue:[self halt:'oops - should not happen'].
1848
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   617
            self read8.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   618
        ] ifFalse:[
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   619
            "/ rle ifFalse:[self halt:'oops - should not happen'].
1848
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   620
            self read8RLE
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   621
        ].
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   622
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   623
        (imageType == 3 or:[colorMap isNil]) ifTrue:[
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   624
            photometric := #blackIs0.
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   625
        ] ifFalse:[
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   626
            photometric := #palette.
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   627
        ].
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   628
        samplesPerPixel := 1.
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   629
        bitsPerSample := #(8).
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   630
1848
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   631
        bytesPerRow := width.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   632
        bytesPerPixel := 1.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   633
    ].
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   634
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   635
    self handleImageOrientation.
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   636
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   637
    "
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   638
     TargaReader fromFile:'bitmaps/test.tga' 
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   639
    "
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   640
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   641
    "Modified: / 13-10-1998 / 19:50:48 / ps"
4002
91e154c3580d #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4001
diff changeset
   642
    "Modified: / 29-08-2017 / 23:41:12 / cg"
1848
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   643
! !
864ca2cd4e71 category
Claus Gittinger <cg@exept.de>
parents: 1846
diff changeset
   644
210
5405de794686 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   645
!TargaReader class methodsFor:'documentation'!
5405de794686 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   646
5405de794686 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   647
version
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   648
    ^ '$Header$'
4001
153c0234371a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3998
diff changeset
   649
!
153c0234371a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3998
diff changeset
   650
153c0234371a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3998
diff changeset
   651
version_CVS
153c0234371a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3998
diff changeset
   652
    ^ '$Header$'
210
5405de794686 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 114
diff changeset
   653
! !
1738
9c85e9d8326b refactorings
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   654
3998
f38061cec746 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 1848
diff changeset
   655
37
aeed5856f457 Initial revision
claus
parents:
diff changeset
   656
TargaReader initialize!