introduce img.convert + example
update tga decoder
img.to_rgb and img.to_rgb2 are deprecated


git-svn-id: svn://kolibrios.org@3055 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
Ivan Baravy 2012-11-18 16:10:28 +00:00
parent 5a0f87bd4c
commit e9998d7f13
7 changed files with 407 additions and 163 deletions

View File

@ -75,8 +75,8 @@ START:
dec ecx dec ecx
jnz @b jnz @b
stdcall [img.encode], [image_to_rgb2], (LIBIMG_FORMAT_ID_PNM), 0 stdcall [img.encode], [image_to_rgb2], (LIBIMG_FORMAT_PNM), 0
; stdcall [img.encode], [image_initial], (LIBIMG_FORMAT_ID_PNM), 0 ; stdcall [img.encode], [image_initial], (LIBIMG_FORMAT_PNM), 0
test eax, eax test eax, eax
jz exit jz exit
mov [encoded_file], eax mov [encoded_file], eax

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View File

@ -0,0 +1,184 @@
use32
org 0x0
db 'MENUET01'
dd 0x01, START, I_END, E_END, E_END, 0, 0
;-----------------------------------------------------------------------------
include '../../../../../proc32.inc'
include '../../../../../macros.inc'
include '../../../../../dll.inc'
;include '../../../../../debug.inc'
include '../../libio/libio.inc'
include '../../libimg/libimg.inc'
;-----------------------------------------------------------------------------
START:
mcall 68, 11
stdcall dll.Load, @IMPORT
or eax, eax
jnz exit
invoke file.open, input_file, O_READ
or eax, eax
jz exit
mov [fh], eax
invoke file.size, input_file
mov [img_data_len], ebx
stdcall mem.Alloc, ebx
or eax, eax
jz exit
mov [img_data], eax
invoke file.read, [fh], eax, [img_data_len]
cmp eax, -1
je exit
cmp eax, [img_data_len]
jne exit
invoke file.close, [fh]
inc eax
jz exit
invoke img.decode, [img_data], [img_data_len], 0
or eax, eax
jz exit
mov [image_initial], eax
;pushfd
;pushad
;mov ebx, [image_initial]
;debug_print_dec dword[ebx + Image.Type]
;newline
;popad
;popfd
stdcall mem.Free, [img_data]
test eax, eax
jz exit
invoke img.convert, [image_initial], 0, Image.bpp8g, 0, 0
test eax, eax
jz exit
mov [image_converted], eax
invoke img.destroy, [image_initial]
mov ebx, [image_converted]
mov eax, [ebx + Image.Width]
add eax, 200*0x10000 + 5*2 - 1 ; window x position + 10 pixels width for skin borders
mov [window_width], eax
mcall 48, 4 ; get skin height
mov ebx, [image_converted]
add eax, [ebx + Image.Height]
add eax, 100*0x10000 + 5 - 1 ; window y position + 5 pixels height for skin bottom border
mov [window_height], eax
;-----------------------------------------------------------------------------
still:
mcall 10
cmp eax, 1
je .draw_window
cmp eax, 2
je .key
cmp eax, 3
je .button
jmp still
.draw_window:
mcall 12, 1
mcall 0, [window_width], [window_height], 0x74FFFFFF, 0x00000000, window_title
call draw_image
mcall 12, 2
jmp still
.key:
mcall 2
jmp still
.button:
mcall 17
shr eax, 8
cmp eax, 1
jne still
exit:
invoke img.destroy, [image_converted]
mcall -1
proc draw_image
mov ebx, [image_converted]
invoke img.draw, ebx, 0, 0, [ebx + Image.Width], [ebx + Image.Height], 0, 0
ret
endp
;-----------------------------------------------------------------------------
window_title db 'img.convert example',0
input_file:
db '/hd0/1/kolibri_logo.jpg',0
; db '/hd0/1/tga/CTC16.TGA',0
; db '/hd0/1/indexed_packbits_le_test_00.tiff',0
; db '/hd0/1/graya_123x123.tiff',0
; db '/hd0/1/bilevel_00.wbmp',0
; db '/hd0/1/rgb_af.jpg',0
; db '/hd0/1/gray_5x7.tiff',0
; db '/hd0/1/rgb_lzw_le_2x2.tiff',0
; db '/hd0/1/grayscale_123x123.tiff',0
; db '/hd0/1/grayscale_357x357.tiff',0
; db '/hd0/1/grayscale_620x620.tiff',0
; db '/hd0/1/rgb_220.jpg',0
; db '/hd0/1/rgba_217.tiff',0
; db '/hd0/1/rgb_7x9.tiff',0
; db '/hd0/1/rgba_7x9.tiff',0
; db '/hd0/1/gray_7x9.tiff',0
; db '/hd0/1/rgb_70x90.png',0
;-----------------------------------------------------------------------------
align 4
@IMPORT:
library \
libio , 'libio.obj' , \
libimg , 'libimg.obj'
import libio , \
libio.init , 'lib_init' , \
file.size , 'file_size' , \
file.open , 'file_open' , \
file.read , 'file_read' , \
file.close , 'file_close'
import libimg , \
libimg.init , 'lib_init' , \
img.decode , 'img_decode' , \
img.destroy , 'img_destroy' , \
img.draw , 'img_draw' , \
img.convert , 'img_convert' , \
img.types_table , 'img_types_table'
;-----------------------------------------------------------------------------
I_END:
fh dd ?
img_data_len dd ?
img_data dd ?
image_initial dd ?
image_converted dd ?
window_width dd ?
window_height dd ?
rd 0x1000 ; stack
E_END:

View File

@ -47,7 +47,7 @@ include 'pnm/pnm.asm'
include 'wbmp/wbmp.asm' include 'wbmp/wbmp.asm'
include 'scale.asm' include 'scale.asm'
;include 'convert.asm' include 'convert.asm'
;include 'transform.asm' ;include 'transform.asm'
;;================================================================================================;; ;;================================================================================================;;
@ -1929,6 +1929,18 @@ img.formats_table:
.z80 dd LIBIMG_FORMAT_Z80, img.is.z80, img.decode.z80, img.encode.z80, 0 ;this must be the last entry as there are no signatures in z80 screens at all .z80 dd LIBIMG_FORMAT_Z80, img.is.z80, img.decode.z80, img.encode.z80, 0 ;this must be the last entry as there are no signatures in z80 screens at all
dd 0 dd 0
align 4
img.types_table: ; entries order must correspond to type defnitions in libimg.inc
dd 0 ; there is no Image.bpp* = 0
.bpp8i dd (1 SHL Image.bpp24)
.bpp24 dd (1 SHL Image.bpp24) OR (1 SHL Image.bpp8g)
.bpp32 dd (1 SHL Image.bpp24)
.bpp15 dd (1 SHL Image.bpp24)
.bpp16 dd (1 SHL Image.bpp24)
.bpp1 dd (1 SHL Image.bpp24)
.bpp8g dd (1 SHL Image.bpp24) OR (1 SHL Image.bpp1 )
.bpp8a dd (1 SHL Image.bpp24)
;;================================================================================================;; ;;================================================================================================;;
;;////////////////////////////////////////////////////////////////////////////////////////////////;; ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
;;================================================================================================;; ;;================================================================================================;;
@ -2206,6 +2218,7 @@ export \
img.rotate.layer , 'img_rotate_layer' , \ img.rotate.layer , 'img_rotate_layer' , \
img.draw , 'img_draw' , \ img.draw , 'img_draw' , \
img.scale , 'img_scale' , \ img.scale , 'img_scale' , \
img.convert , 'img_convert' , \
img.formats_table, 'img_formats_table' img.formats_table, 'img_formats_table'
; import from deflate unpacker ; import from deflate unpacker

View File

@ -67,7 +67,7 @@ LIBIMG_ENCODE_DELETE_ALPHA = 0x08
LIBIMG_ENCODE_FLUSH_ALPHA = 0x10 LIBIMG_ENCODE_FLUSH_ALPHA = 0x10
; convert flags ; convert flags
LIBIMG_CONVERT_IN_PLACE = 0x01 ; do not create new image, store result in _src ; TBD
struct FormatsTableEntry struct FormatsTableEntry
Format_id dd ? Format_id dd ?

View File

@ -1,5 +1,5 @@
;;================================================================================================;; ;;================================================================================================;;
;;//// tga.asm //// (c) Nable, 2007-2008 /////////////////////////////////////////////////////////;; ;;//// tga.asm //// (c) Nable, 2007-2008, (c) dunkaist, 2012 /////////////////////////////////////;;
;;================================================================================================;; ;;================================================================================================;;
;; ;; ;; ;;
;; This file is part of Common development libraries (Libs-Dev). ;; ;; This file is part of Common development libraries (Libs-Dev). ;;
@ -19,6 +19,8 @@
;; ;; ;; ;;
;; References: ;; ;; References: ;;
;; 1. Hiview 1.2 by Mohammad A. REZAEI ;; ;; 1. Hiview 1.2 by Mohammad A. REZAEI ;;
;; 2. Truevision TGA FILE FORMAT SPECIFICATION Version 2.0 ;;
;; Technical Manual Version 2.2 January, 1991 ;;
;; ;; ;; ;;
;;================================================================================================;; ;;================================================================================================;;
@ -37,40 +39,43 @@ proc img.is.tga _data, _length ;////////////////////////////////////////////////
push ebx push ebx
cmp [_length], 18 cmp [_length], 18
jbe .nope jbe .nope
mov eax, [_data] mov ebx, [_data]
mov ebx,[eax+1] ;bl=cmatype,bh=subtype mov eax, dword[ebx + tga_header.colormap_type]
cmp bl,1 ;cmatype is in [0..1] cmp al, 1
ja .nope ja .nope
cmp bh,11 ;subtype is in [1..3] (non-rle) or in [9..11] (rle) cmp ah, 11
ja .nope ja .nope
cmp bh,9 cmp ah, 9
jae .cont1 jae .cont1
cmp bh,3 cmp ah, 3
ja .nope ja .nope
.cont1: ;continue testing .cont1:
mov ebx,[eax+16] ;bl=bpp, bh=flags //image descriptor mov eax, dword[ebx + tga_header.image_spec.depth]
test ebx,111b ;bpp must be 8, 15, 16, 24 or 32 test eax, 111b ; bpp must be 8, 15, 16, 24 or 32
jnz .maybe15 jnz .maybe15
shr bl,3 shr al, 3
cmp bl,4 cmp al, 4
ja .nope ja .nope
jmp .cont2 jmp .cont2
.maybe15: .maybe15:
cmp bl,15 cmp al, 15
jne .nope jne .nope
.cont2: ;continue testing .cont2: ; continue testing
test bh,tga.flags.interlace_type ;deinterlacing is not supported yet movzx eax, byte[ebx + tga_header.colormap_spec.entry_size] ; palette bpp
jnz .nope cmp eax, 0
cmp byte[eax+7],24 ;test palette bpp - only 24 and 32 are supported
je .yep je .yep
cmp byte[eax+7],32 ;test palette bpp - only 24 and 32 are supported cmp eax, 16
je .yep je .yep
.nope: cmp eax, 24
je .yep
cmp eax, 32
je .yep
.nope:
xor eax, eax xor eax, eax
pop ebx pop ebx
ret ret
.yep: .yep:
xor eax, eax xor eax, eax
inc eax inc eax
pop ebx pop ebx
@ -88,131 +93,167 @@ proc img.decode.tga _data, _length, _options ;//////////////////////////////////
;< eax = 0 (error) or pointer to image ;; ;< eax = 0 (error) or pointer to image ;;
;;================================================================================================;; ;;================================================================================================;;
locals locals
IMGwidth dd ? width dd ?
IMGheight dd ? height dd ?
IMGbpp dd ? bytes_per_pixel dd ?
DupPixelCount dd ? retvalue dd ?
TgaBlockCount dd ?
endl endl
pushad push ebx esi edi
cld ;paranoia mov ebx, [_data]
and [DupPixelCount],0 ;prepare variables movzx esi, byte[ebx]
and [TgaBlockCount],0 ;prepare variables lea esi, [esi + ebx + sizeof.tga_header] ; skip comment and header
mov eax,[_data] mov edx, dword[ebx + tga_header.image_spec.width]
movzx esi,byte[eax] movzx ecx, dx ; ecx = width
lea esi,[esi+eax+18] ;skip comment and header shr edx, 16 ; edx = height
mov ebx,[eax+12] mov [width], ecx
movzx ecx,bx ;ecx=width mov [height], edx
shr ebx,16 ;ebx=height movzx eax, byte[ebx + tga_header.image_spec.depth]
mov [IMGwidth],ecx add eax, 7
mov [IMGheight],ebx shr eax, 3
movzx edx,byte[eax+16] mov [bytes_per_pixel], eax
cmp edx,16 movzx eax, byte[ebx + tga_header.image_spec.depth]
jnz @f
dec edx ;16bpp tga images are really 15bpp ARGB cmp eax, 8
@@: jne @f
sub edx, 16 - Image.bpp16 ; 15 -> Image.bpp15, 16 -> Image.bpp16 mov eax, Image.bpp8i
mov [IMGbpp],edx jmp .type_defined
stdcall img.create,ecx,ebx,edx @@:
mov [esp+28],eax ;save return value cmp eax, 15
test eax,eax ;failed to allocate? jne @f
jz .locret ;then exit mov eax, Image.bpp15
cmp edx,8 jmp .type_defined
@@:
cmp eax, 16
jne @f
mov eax, Image.bpp15 ; 16bpp tga images are really 15bpp ARGB
jmp .type_defined
@@:
cmp eax, 24
jne @f
mov eax, Image.bpp24
jmp .type_defined
@@:
cmp eax, 32
jne @f
mov eax, Image.bpp32
jmp .type_defined
@@:
.type_defined:
stdcall img.create, ecx, edx, eax
mov [retvalue], eax
test eax, eax ; failed to allocate?
jz .done ; then exit
mov ebx, eax
cmp dword[ebx + Image.Type], Image.bpp8i
jne .palette_parsed jne .palette_parsed
mov edi,[eax+Image.Palette] mov edi, [ebx + Image.Palette]
mov ecx,[_data] mov ecx, [_data]
cmp byte[ecx+2],3 ;we also have grayscale subtype cmp byte[ecx + tga_header.image_type], 3 ; we also have grayscale subtype
jz .write_grayscale_palette ;that don't hold palette in file jz .write_grayscale_palette ; that don't hold palette in file
cmp byte[ecx+2],11 cmp byte[ecx + tga_header.image_type], 11
jz .write_grayscale_palette jz .write_grayscale_palette
mov dh,[ecx+7] ;size of colormap entries in bits movzx eax, byte[ecx + tga_header.colormap_spec.entry_size] ; size of colormap entries in bits
movzx ecx,word[ecx+5] ;number of colormap entries movzx ecx, word[ecx + tga_header.colormap_spec.colormap_length] ; number of colormap entries
cmp dh,24 cmp eax, 24
jz .skip_24bpp_palette ;test if colormap entries are 24bpp je .24bpp_palette
rep movsd ;else they are 32 bpp cmp eax, 16
je .16bpp_palette
rep movsd ; else they are 32 bpp
jmp .palette_parsed jmp .palette_parsed
.write_grayscale_palette: .write_grayscale_palette:
push eax mov ecx, 0x100
mov ecx,0x100 xor eax, eax
xor eax,eax @@:
@@:
stosd stosd
add eax,0x010101 add eax, 0x010101
loop @b loop @b
pop eax
jmp .palette_parsed jmp .palette_parsed
.skip_24bpp_palette: .16bpp_palette: ; FIXME: code copypasted from img.do_rgb, should use img.convert
push eax push ebx edx ebp
@@: @@:
movzx eax, word[esi]
mov ebx, eax
add esi, 2
and eax, (0x1F) or (0x1F shl 10)
and ebx, 0x1F shl 5
lea edx, [eax + eax]
shr al, 2
mov ebp, ebx
shr ebx, 2
shr ah, 4
shl dl, 2
shr ebp, 7
add eax, edx
add ebx, ebp
mov [edi], al
mov [edi + 1], bl
mov [edi + 2], ah
add edi, 4
loop @b
pop ebp edx ebx
jmp .palette_parsed
.24bpp_palette:
@@:
lodsd lodsd
dec esi dec esi
and eax,0xFFFFFF and eax, 0xffffff
; bswap eax
; shr eax,8
stosd stosd
loop @b loop @b
pop eax .palette_parsed:
.palette_parsed: mov edi, [ebx + Image.Data]
mov edi,[eax+Image.Data] mov ebx, [width]
imul ebx,[IMGwidth] ;ebx=width*height imul ebx, [height]
mov edx, [bytes_per_pixel]
mov edx,[IMGbpp] mov eax, [_data]
add edx,7 test byte[eax + tga_header.image_type], 0x08
shr edx,3 ;edx=bytes per pixel jz .uncompressed
mov dh,dl ;dh=dl=bytes per pixel .next_rle_packet:
xor eax, eax
mov eax,[_data]
cmp byte[eax+2],9
jb .not_an_rle
.tga_read_rle_pixel:
cmp [DupPixelCount],0 ;Duplicate previously read pixel?
jg .duplicate_previously_read_pixel
dec [TgaBlockCount] ;Decrement pixels remaining in block
jns .read_non_rle_pixel
xor eax,eax
lodsb lodsb
test al,al ;Start of duplicate-pixel block? btr ax, 7 ; Run-length packet?
jns .2 jnc .raw_packet
and al,0x7f add eax, 1
mov [DupPixelCount],eax ;Number of duplications after this one sub ebx, eax
and [TgaBlockCount],0 ;Then read new block header @@:
jmp .read_non_rle_pixel mov ecx, edx
.2: rep movsb
mov dword[TgaBlockCount],eax sub esi, edx
.read_non_rle_pixel: sub eax, 1
xor eax,eax
mov dl,dh
@@:
shl eax,8
lodsb
dec dl
jnz @b jnz @b
cmp dh,3 add esi, edx
jne .put_pixel test ebx, ebx
bswap eax jnz .next_rle_packet
shr eax,8 jmp .done
jmp .put_pixel .raw_packet:
.duplicate_previously_read_pixel: mov ecx, eax
dec [DupPixelCount] add ecx, 1
.put_pixel: sub ebx, ecx
mov dl,dh imul ecx, edx
push eax rep movsb
@@: test ebx, ebx
stosb jnz .next_rle_packet
shr eax,8 .uncompressed:
dec dl imul edx, ebx
jnz @b mov ecx, edx
pop eax rep movsb
dec ebx .done:
jnz .tga_read_rle_pixel xor ebx, ebx
jmp .locret mov esi, [_data]
.not_an_rle: test byte[esi + tga_header.image_spec.descriptor], TGA_START_TOP
movzx edx,dl ;dh contains bpp too (for decoding needs) jnz @f
imul edx,ebx or ebx, FLIP_VERTICAL
mov ecx,edx @@:
rep movsb ;just copy the image test byte[esi + tga_header.image_spec.descriptor], TGA_START_RIGHT
.locret: jz @f
popad or ebx, FLIP_HORIZONTAL
@@:
test ebx, ebx
jz @f
stdcall img.flip, [retvalue], ebx
@@:
pop edi esi ebx
mov eax, [retvalue]
ret ret
endp endp
@ -230,7 +271,6 @@ proc img.encode.tga _img, _p_length, _options ;/////////////////////////////////
ret ret
endp endp
;;================================================================================================;; ;;================================================================================================;;
;;////////////////////////////////////////////////////////////////////////////////////////////////;; ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
;;================================================================================================;; ;;================================================================================================;;
@ -246,5 +286,3 @@ endp
;;================================================================================================;; ;;================================================================================================;;
;;////////////////////////////////////////////////////////////////////////////////////////////////;; ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
;;================================================================================================;; ;;================================================================================================;;
;

View File

@ -1,5 +1,5 @@
;;================================================================================================;; ;;================================================================================================;;
;;//// tga.inc //// (c) Nable, 2007-2008 /////////////////////////////////////////////////////////;; ;;//// tga.inc //// (c) Nable, 2007-2008, (c) dunkaist, 2012 /////////////////////////////////////;;
;;================================================================================================;; ;;================================================================================================;;
;; ;; ;; ;;
;; This file is part of Common development libraries (Libs-Dev). ;; ;; This file is part of Common development libraries (Libs-Dev). ;;
@ -17,19 +17,28 @@
;; ;; ;; ;;
;;================================================================================================;; ;;================================================================================================;;
struct tga.FileHeader struct tga_colormap_spec
CommentLength db ? first_entry_index dw ?
ColormapType db ? colormap_length dw ?
SubType db ? entry_size db ?
DontKnow1 dw ?
ColormapSize dw ?
ColormapBpp db ?
DontKnow2 dd ?
Width dw ?
Height dw ?
BitPerPixel db ?
DontKnow3 db ?
ends ends
tga.flags.top_down_row_order equ 32 ;bit5 struct tga_image_spec
tga.flags.interlace_type equ 192;bits6/7 x_origin dw ?
y_origin dw ?
width dw ?
height dw ?
depth db ?
descriptor db ?
ends
struct tga_header
id_length db ?
colormap_type db ?
image_type db ?
colormap_spec tga_colormap_spec
image_spec tga_image_spec
ends
TGA_START_TOP = 0x20
TGA_START_RIGHT = 0x10