! Copyright (C) 2009 Keith Lazuka.
! See http://factorcode.org/license.txt for BSD license.
-USING: accessors fry images images.loader images.normalization
-images.viewer io io.backend io.directories io.encodings.binary
-io.files io.pathnames io.streams.byte-array kernel locals
+USING: accessors assocs fry images images.loader images.normalization
+images.loader.private images.viewer io io.backend io.directories
+io.encodings.binary io.files io.pathnames io.streams.byte-array kernel locals
namespaces quotations random sequences serialize tools.test ;
IN: images.testing
f verbose-tests? [
path load-image dup clone normalize-image 1quotation swap
'[
- binary [ _ image-class image>stream ] with-byte-writer
- image-class load-image* normalize-image
+ binary [
+ _ image-class [ types get value-at ] keep image>stream
+ ] with-byte-writer image-class load-image* normalize-image
] unit-test
] with-variable ;
[ '[ _ load-reference-image ] ] bi
unit-test
] with-variable ;
-
+
: <rgb-image> ( -- image )
<image>
RGB >>component-order
: read-color-map-type ( -- byte )
1 read le> dup
{ 0 1 } member? [ bad-tga-header ] unless ;
-
+
: read-image-type ( -- byte )
1 read le> dup
{ 0 1 2 3 9 10 11 } member? [ bad-tga-header ] unless ; inline
[ first ]
[ dup third second seek-absolute seek-input read ] bi 2array
] map >hashtable ; inline
-
+
:: read-tga ( -- image )
#! Read header
read-id-length :> id-length
id-length read-image-id :> image-id
map-type map-length map-entry-size read-color-map :> color-map-data
image-width image-height pixel-depth read-image-data :> image-data
-
+
[
#! Read optional footer
26 seek-end seek-input
read-key-color :> key-color
read-pixel-aspect-ratio :> aspect-ratio
read-gamma-value :> gamma-value
- read-color-correction-offset :> color-correction-offset
+ read-color-correction-offset :> color-correction-offset
read-postage-stamp-offset :> postage-stamp-offset
read-scan-line-offset :> scan-line-offset
read-premultiplied-alpha :> premultiplied-alpha
-
+
color-correction-offset 0 =
[
color-correction-offset seek-absolute seek-input
postage-stamp-offset seek-absolute seek-input
pixel-depth read-postage-stamp-image :> postage-data
] unless
-
+
scan-line-offset seek-absolute seek-input
image-height read-scan-line-table :> scan-offsets
-
+
#! Read optional developer section
directory-offset 0 =
[ f ]
#! Only 24-bit uncompressed BGR and 32-bit uncompressed BGRA are supported.
#! Other formats would need to be converted to work within the image class.
- map-type 0 = [ bad-tga-unsupported ] unless
+ map-type 0 = [ bad-tga-unsupported ] unless
image-type 2 = [ bad-tga-unsupported ] unless
pixel-depth { 24 32 } member? [ bad-tga-unsupported ] unless
pixel-order { 0 2 } member? [ bad-tga-unsupported ] unless
-
+
#! Create image instance
image new
alpha-bits 0 = [ BGR ] [ BGRA ] if >>component-order
pixel-order 0 = >>upside-down?
image-data >>bitmap
ubyte-components >>component-type ;
-
+
M: tga-image stream>image*
drop [ [ read-tga ] throw-on-eof ] with-input-stream ;
M: tga-image image>stream
- drop
+ 2drop
[
component-order>> { BGRA BGRA } member? [ bad-tga-unsupported ] unless
] keep
]
[ bitmap>> write ]
} cleave ;
-