TargaReader.st
changeset 4016 47c4ea8134ec
parent 4002 91e154c3580d
equal deleted inserted replaced
4015:7f7b255c15c2 4016:47c4ea8134ec
    13 
    13 
    14 "{ NameSpace: Smalltalk }"
    14 "{ NameSpace: Smalltalk }"
    15 
    15 
    16 ImageReader subclass:#TargaReader
    16 ImageReader subclass:#TargaReader
    17 	instanceVariableNames:'orientation bytesPerRow bytesPerPixel imageType'
    17 	instanceVariableNames:'orientation bytesPerRow bytesPerPixel imageType'
    18 	classVariableNames:''
    18 	classVariableNames:'Verbose'
    19 	poolDictionaries:''
    19 	poolDictionaries:''
    20 	category:'Graphics-Images-Readers'
    20 	category:'Graphics-Images-Readers'
    21 !
    21 !
    22 
    22 
    23 !TargaReader class methodsFor:'documentation'!
    23 !TargaReader class methodsFor:'documentation'!
   225 
   225 
   226     'TargaReader [warning]: unsupported orientation: ' errorPrint. orientation errorPrintCR.
   226     'TargaReader [warning]: unsupported orientation: ' errorPrint. orientation errorPrintCR.
   227 !
   227 !
   228 
   228 
   229 read16
   229 read16
   230     "read a 16 bit/pixel targa-image"
   230     "read a 16 bit/pixel targa-image.
       
   231      Notice, that the channels are in bgr order; not rgb"
   231 
   232 
   232     |totalBytes remainingBytes dstIndex b1 b2|
   233     |totalBytes remainingBytes dstIndex b1 b2|
   233 
   234 
   234     totalBytes := width * height * 2.
   235     totalBytes := width * height * 2.
   235     data := ByteArray new:totalBytes.
   236     data := ByteArray new:totalBytes.
   236 
   237 
   237     dstIndex := 1.
   238     dstIndex := 1.
   238     remainingBytes := totalBytes.
   239     remainingBytes := totalBytes.
   239     [remainingBytes > 0] whileTrue:[
   240     [remainingBytes > 0] whileTrue:[
       
   241         b2 := inStream nextByte.
   240         b1 := inStream nextByte.
   242         b1 := inStream nextByte.
   241         b2 := inStream nextByte.
       
   242         data at:dstIndex   put:b1.
   243         data at:dstIndex   put:b1.
   243         data at:dstIndex+1 put:b2.
   244         data at:dstIndex+1 put:b2.
       
   245         
   244         dstIndex := dstIndex + 2.
   246         dstIndex := dstIndex + 2.
   245         remainingBytes := remainingBytes - 2.
   247         remainingBytes := remainingBytes - 2.
   246     ].
   248     ].
   247 
   249 
   248     "Created: / 29-08-2017 / 23:38:30 / cg"
   250     "Created: / 29-08-2017 / 23:38:30 / cg"
       
   251     "Modified: / 17-09-2017 / 13:35:21 / cg"
   249 !
   252 !
   250 
   253 
   251 read16RLE
   254 read16RLE
   252     "read a 16 bit/pixel rle encoded targa-image"
   255     "read a 16 bit/pixel rle encoded targa-image.
       
   256      Notice, that the channels are in bgr order; not rgb"
   253 
   257 
   254     |total count dstIndex code n b1 b2|
   258     |total count dstIndex code n b1 b2|
   255 
   259 
   256     data := ByteArray new:((total := width * height * 2)).
   260     data := ByteArray new:((total := width * height * 2)).
   257     count := 0.
   261     count := 0.
   258     dstIndex := 1.
   262     dstIndex := 1.
   259     [count < total] whileTrue:[
   263     [count < total] whileTrue:[
   260         code := inStream nextByte.
   264         code := inStream nextByte.
   261         n := (code bitAnd:16r7F) + 1.
   265         n := (code bitAnd:16r7F) + 1.
   262         (code bitAnd:16r80) ~~ 0 ifTrue:[
   266         (code bitAnd:16r80) ~~ 0 ifTrue:[
       
   267             b2 := inStream nextByte.
   263             b1 := inStream nextByte.
   268             b1 := inStream nextByte.
   264             b2 := inStream nextByte.
       
   265             n timesRepeat:[
   269             n timesRepeat:[
   266                 data at:dstIndex put:b1.
   270                 data at:dstIndex put:b1.
   267                 data at:dstIndex+1 put:b2.
   271                 data at:dstIndex+1 put:b2.
   268                 dstIndex := dstIndex + 2
   272                 dstIndex := dstIndex + 2
   269             ].
   273             ].
   270         ] ifFalse:[
   274         ] ifFalse:[
   271             n timesRepeat:[
   275             n timesRepeat:[
       
   276                 b2 := inStream nextByte.
   272                 b1 := inStream nextByte.
   277                 b1 := inStream nextByte.
   273                 b2 := inStream nextByte.
       
   274                 data at:dstIndex put:b1.
   278                 data at:dstIndex put:b1.
   275                 data at:dstIndex+1 put:b2.
   279                 data at:dstIndex+1 put:b2.
   276                 dstIndex := dstIndex + 2
   280                 dstIndex := dstIndex + 2
   277             ]
   281             ]
   278         ].
   282         ].
   279         count := count + (n * 2).
   283         count := count + (n * 2).
   280     ].
   284     ].
   281 
   285 
   282     "Created: / 29-08-2017 / 23:39:19 / cg"
   286     "Created: / 29-08-2017 / 23:39:19 / cg"
       
   287     "Modified (comment): / 17-09-2017 / 13:35:59 / cg"
   283 !
   288 !
   284 
   289 
   285 read24
   290 read24
   286     "read a 24 bit/pixel targa-image"
   291     "read a 24 bit/pixel targa-image.
       
   292      Notice, that the channels are in bgr order; not rgb"
   287 
   293 
   288     |totalBytes|
   294     |totalBytes|
   289 
   295 
   290     totalBytes := width * height * 3.
   296     totalBytes := width * height * 3.
   291     data := ByteArray new:totalBytes.
   297     data := ByteArray new:totalBytes.
   292     inStream nextBytes:totalBytes into:data.
   298     inStream nextBytes:totalBytes into:data.
   293 
   299 
   294     "
   300     "
   295      mhmh - pixel-byte order is blue-green-red
   301      mhmh - pixel-byte order is blue-green-red; swap blue & red bytes
   296      swap blue & red bytes
       
   297     "
   302     "
   298     self class swap:totalBytes bytesFromRGB_to_BGR_in:data.
   303     self class swap:totalBytes bytesFromRGB_to_BGR_in:data.
   299 
   304 
   300     "Modified: / 29-08-2017 / 23:06:02 / cg"
   305     "Modified: / 29-08-2017 / 23:06:02 / cg"
       
   306     "Modified (comment): / 17-09-2017 / 13:37:53 / cg"
   301 !
   307 !
   302 
   308 
   303 read24RLE
   309 read24RLE
   304     "read an 8 bit/pixel rle encoded targa-image"
   310     "read an 8 bit/pixel rle encoded targa-image.
       
   311      Notice, that the channels are in bgr order; not rgb"
   305 
   312 
   306     |total count dstIndex code n r g b|
   313     |total count dstIndex code n r g b|
   307 
   314 
   308     data := ByteArray new:((total := width * height * 3)).
   315     data := ByteArray new:((total := width * height * 3)).
   309     count := 0.
   316     count := 0.
   335         count := count + (n * 3).
   342         count := count + (n * 3).
   336     ].
   343     ].
   337 
   344 
   338     "Created: / 21-04-1997 / 20:43:23 / cg"
   345     "Created: / 21-04-1997 / 20:43:23 / cg"
   339     "Modified: / 29-08-2017 / 23:06:35 / cg"
   346     "Modified: / 29-08-2017 / 23:06:35 / cg"
       
   347     "Modified (comment): / 17-09-2017 / 13:37:58 / cg"
   340 !
   348 !
   341 
   349 
   342 read32
   350 read32
   343     "read a 32 bit/pixel targa-image"
   351     "read a 32 bit/pixel targa-image.
       
   352      Notice, that the channels are in bgra order; not rgba"
   344 
   353 
   345     |totalBytes remainingBytes dstIndex a r g b|
   354     |totalBytes remainingBytes dstIndex a r g b|
   346 
   355 
   347     totalBytes := width * height * 4.
   356     totalBytes := width * height * 4.
   348     data := ByteArray new:totalBytes.
   357     data := ByteArray new:totalBytes.
   367      TargaReader fromFile:'/phys/exept/unsaved2/smalltalk/Squeak/croquet/Croquet/Content/Cisco/2520/Textures/2520caseback.tga'
   376      TargaReader fromFile:'/phys/exept/unsaved2/smalltalk/Squeak/croquet/Croquet/Content/Cisco/2520/Textures/2520caseback.tga'
   368      TargaReader fromFile:'/phys/exept/unsaved2/smalltalk/Squeak/croquet/Croquet/Content/Cisco/2520/Textures/2520dimm1front.tga'
   377      TargaReader fromFile:'/phys/exept/unsaved2/smalltalk/Squeak/croquet/Croquet/Content/Cisco/2520/Textures/2520dimm1front.tga'
   369     "
   378     "
   370 
   379 
   371     "Modified: / 29-08-2017 / 23:09:42 / cg"
   380     "Modified: / 29-08-2017 / 23:09:42 / cg"
       
   381     "Modified (comment): / 17-09-2017 / 13:37:26 / cg"
   372 !
   382 !
   373 
   383 
   374 read32RLE
   384 read32RLE
   375     "read a 32 bit/pixel rle encoded targa-image; skip alpha channel (for now)"
   385     "read a 32 bit/pixel rle encoded targa-image; skip alpha channel (for now).
       
   386      Notice, that the channels are in bgra order; not rgba"
   376 
   387 
   377     |total count dstIndex code n a r g b|
   388     |total count dstIndex code n a r g b|
   378 
   389 
   379     data := ByteArray new:((total := width * height * 4)).
   390     data := ByteArray new:((total := width * height * 4)).
   380     count := 0.
   391     count := 0.
   410         count := count + (n * 4).
   421         count := count + (n * 4).
   411     ].
   422     ].
   412 
   423 
   413     "Created: / 21-04-1997 / 20:43:54 / cg"
   424     "Created: / 21-04-1997 / 20:43:54 / cg"
   414     "Modified: / 29-08-2017 / 23:35:48 / cg"
   425     "Modified: / 29-08-2017 / 23:35:48 / cg"
       
   426     "Modified (comment): / 17-09-2017 / 13:37:37 / cg"
   415 !
   427 !
   416 
   428 
   417 read8
   429 read8
   418     "read an 8 bit/pixel targa-image"
   430     "read an 8 bit/pixel targa-image"
   419 
   431 
   490     depth := inStream next.
   502     depth := inStream next.
   491     (#(8 15 16 24 32) includes:depth) ifFalse:[
   503     (#(8 15 16 24 32) includes:depth) ifFalse:[
   492         ^ self fileFormatError:'unsupported depth: %1' with:depth printString.
   504         ^ self fileFormatError:'unsupported depth: %1' with:depth printString.
   493     ].
   505     ].
   494     depth == 32 ifTrue:[
   506     depth == 32 ifTrue:[
   495         'TargaReader [info]: alpha channel ignored' infoPrintCR.
   507         Logger warning: 'TargaReader [info]: alpha channel ignored'.
   496     ] ifFalse:[
   508     ] ifFalse:[
   497         'TargaReader [info]: depth: ' infoPrint. depth infoPrintCR.
   509         Verbose == true ifTrue:[ Logger info:'TargaReader [info]: depth: %1' with:depth ].
   498     ].
   510     ].
   499 
   511 
   500     "/ MapRGB == 1
   512     "/ MapRGB == 1
   501     "/ RawRGB == 2
   513     "/ RawRGB == 2
   502     "/ RawMono == 3
   514     "/ RawMono == 3
   505 
   517 
   506     (#(1 2 3 9 10) includes:imageType) ifFalse:[
   518     (#(1 2 3 9 10) includes:imageType) ifFalse:[
   507         "/ 'TargaReader [warning]: unsupported imageType: ' errorPrint. imageType errorPrintCR.
   519         "/ 'TargaReader [warning]: unsupported imageType: ' errorPrint. imageType errorPrintCR.
   508         ^ self fileFormatError:'unsupported imageType: %1' with:imageType printString.
   520         ^ self fileFormatError:'unsupported imageType: %1' with:imageType printString.
   509     ].
   521     ].
   510     'TargaReader [info]: imageType: ' infoPrint. imageType infoPrintCR.
   522     Verbose == true ifTrue:[ Logger info:'TargaReader [info]: imageType: %1' with:imageType].
   511 
   523 
   512     self reportDimension.
   524     self reportDimension.
   513 
   525 
   514     "/ flags:
   526     "/ flags:
   515     "/    0000 xxxx  attribute-bits-per-pixel
   527     "/    0000 xxxx  attribute-bits-per-pixel
   637     "
   649     "
   638      TargaReader fromFile:'bitmaps/test.tga' 
   650      TargaReader fromFile:'bitmaps/test.tga' 
   639     "
   651     "
   640 
   652 
   641     "Modified: / 13-10-1998 / 19:50:48 / ps"
   653     "Modified: / 13-10-1998 / 19:50:48 / ps"
   642     "Modified: / 29-08-2017 / 23:41:12 / cg"
   654     "Modified: / 18-09-2017 / 08:56:24 / cg"
   643 ! !
   655 ! !
   644 
   656 
   645 !TargaReader class methodsFor:'documentation'!
   657 !TargaReader class methodsFor:'documentation'!
   646 
   658 
   647 version
   659 version