From 6cf659fe71a36a4e51952eec2684d1df7997e906 Mon Sep 17 00:00:00 2001 From: Ivan Baravy Date: Tue, 31 Oct 2017 21:33:52 +0000 Subject: [PATCH] libimg: add img.from_file, refactor img.convert. Add new function, img.from_file: gets file name and returns decoded Image. Make img.convert code less spaghetti: use jump table, not a chain of jmp's. git-svn-id: svn://kolibrios.org@7105 a494cfbc-eb01-0410-851d-a64ba20cac60 --- .../libraries/libs-dev/libimg/convert.asm | 1037 +++++++++-------- .../libraries/libs-dev/libimg/libimg.asm | 86 +- .../libraries/libs-dev/libimg/libimg.inc | 10 +- 3 files changed, 593 insertions(+), 540 deletions(-) diff --git a/programs/develop/libraries/libs-dev/libimg/convert.asm b/programs/develop/libraries/libs-dev/libimg/convert.asm index c91d639fc..34b1a4c4b 100644 --- a/programs/develop/libraries/libs-dev/libimg/convert.asm +++ b/programs/develop/libraries/libs-dev/libimg/convert.asm @@ -33,7 +33,13 @@ proc img.convert _src, _dst, _dst_type, _flags, _param ;< eax = 0 / pointer to converted image ;; ;< ecx = error code / undefined ;; ;;================================================================================================;; - push ebx esi edi 0 0 +locals + img dd ? + prev dd ? +endl + push ebx esi edi + mov [img], 0 + mov [prev], 0 mov ebx, [_src] @@: mov eax, [ebx + Image.Previous] @@ -45,28 +51,26 @@ proc img.convert _src, _dst, _dst_type, _flags, _param stdcall img.convert.layer, ebx, [_dst], [_dst_type], [_flags], [_param] test eax, eax jz .error - cmp dword[esp + 4], 0 + cmp [img], 0 jnz @f - mov [esp + 4], eax + mov [img], eax @@: - mov ecx, [esp] + mov ecx, [prev] jecxz @f mov [ecx + Image.Next], eax - @@: - push [ebx + Image.Flags] - pop [eax + Image.Flags] - push [ebx + Image.Delay] - pop [eax + Image.Delay] mov [eax + Image.Previous], ecx - mov [esp], eax + @@: + mov [prev], eax + push [ebx + Image.Flags] + pop [eax + Image.Flags] + push [ebx + Image.Delay] + pop [eax + Image.Delay] mov ebx, [ebx + Image.Next] test ebx, ebx jnz .loop - .quit: - pop eax eax edi esi ebx - ret + mov eax, [img] .error: - pop eax eax edi esi ebx + pop edi esi ebx ret endp @@ -86,186 +90,173 @@ proc img.convert.layer _src, _dst, _dst_type, _flags, _param ;< ecx = error code / undefined ;; ;;================================================================================================;; locals - width rd 1 - height rd 1 + fun rd 1 endl push ebx esi edi - mov ebx, [_src] - mov eax, 1 - mov ecx, [_dst_type] - shl eax, cl - mov ecx, [ebx + Image.Type] - test eax, [img.types_table + 4*ecx] - jnz @f - mov ecx, LIBIMG_ERROR_BIT_DEPTH - jmp .error + mov ebx, [_src] + mov eax, [ebx + Image.Type] + mov esi, [img.convert.table + 4*eax] + .next: + lodsd + test eax, eax + jnz @f + mov ecx, LIBIMG_ERROR_BIT_DEPTH + jmp .exit @@: - mov eax, [_dst] - test eax, eax - jnz @f - stdcall img.create, [ebx + Image.Width], [ebx + Image.Height], [_dst_type] - test eax, eax - jz .error - mov [_dst], eax + cmp eax, [_dst_type] + lodsd + jnz .next + mov [fun], eax + + mov eax, [_dst] + test eax, eax + jnz @f + stdcall img.create, [ebx + Image.Width], [ebx + Image.Height], [_dst_type] + test eax, eax + jz .exit + mov [_dst], eax @@: - mov edi, [eax + Image.Data] - mov esi, [ebx + Image.Data] - mov eax, [ebx + Image.Type] - cmp eax, Image.bpp8i - je .bpp8i - cmp eax, Image.bpp8g - je .bpp8g - cmp eax, Image.bpp24 - je .bpp24 - cmp eax, Image.bpp32 - je .bpp32 - cmp eax, Image.bpp15 - je .bpp15 - cmp eax, Image.bpp16 - je .bpp16 - cmp eax, Image.bpp1 - je .bpp1 - cmp eax, Image.bpp8a - je .bpp8a - mov ecx, LIBIMG_ERROR_BIT_DEPTH - jmp .error + mov edi, [eax + Image.Data] + mov esi, [ebx + Image.Data] + mov eax, [ebx + Image.Type] + stdcall [fun], [_src], [_dst] + mov eax, [_dst] + .exit: + pop edi esi ebx + ret +endp - .find_in_table_and_jump: - mov ecx, [_dst_type] +proc img._.convert.bpp8i_to_bpp24 _src, _dst + mov ecx, [ebx + Image.Width] + imul ecx, [ebx + Image.Height] + + mov ebx, [ebx + Image.Palette] + sub ecx, 1 + jz .bpp8i.last @@: - mov eax, [edx] - add edx, 8 - cmp eax, ecx - jne @b - jmp dword[edx - 4] - - - .bpp8i: - mov edx, img.convert.bpp8i.table - jmp .find_in_table_and_jump - .bpp8i_to_bpp24: - mov ecx, [ebx + Image.Width] - imul ecx, [ebx + Image.Height] - - mov ebx, [ebx + Image.Palette] - sub ecx, 1 - jz .bpp8i.last - @@: - movzx eax, byte[esi] - add esi, 1 - mov eax, [ebx + eax*4] - mov [edi], eax - add edi, 3 - sub ecx, 1 - jnz @b + movzx eax, byte[esi] + add esi, 1 + mov eax, [ebx + eax*4] + mov [edi], eax + add edi, 3 + sub ecx, 1 + jnz @b .bpp8i.last: - movzx eax, byte[esi] - mov eax, [ebx + eax*4] - mov [edi], ax - shr eax, 16 - mov [edi + 2], al - mov eax, [_dst] - jmp .quit + movzx eax, byte[esi] + mov eax, [ebx + eax*4] + mov [edi], ax + shr eax, 16 + mov [edi + 2], al + ret +endp +proc img._.convert.bpp8i_to_bpp32 _src, _dst + mov ecx, [ebx + Image.Width] + imul ecx, [ebx + Image.Height] - .bpp8g: - mov edx, img.convert.bpp8g.table - jmp .find_in_table_and_jump - .bpp8g_to_bpp1: - mov eax, [_dst] - mov eax, [eax + Image.Palette] - mov dword[eax], 0x00000000 - mov dword[eax + 4], 0x00ffffff - mov edx, [ebx + Image.Height] + mov ebx, [ebx + Image.Palette] + @@: + movzx eax, byte[esi] + add esi, 1 + mov eax, [ebx + eax*4] + mov [edi], eax + add edi, 4 + dec ecx + jnz @b + ret +endp + + +proc img._.convert.bpp8g_to_bpp1 _src, _dst + mov eax, [_dst] + mov eax, [eax + Image.Palette] + mov dword[eax], 0x00000000 + mov dword[eax + 4], 0x00ffffff + mov edx, [ebx + Image.Height] .bpp8g_to_bpp1.line: - mov ax, 0x0800 - mov ecx, [ebx + Image.Width] + mov ax, 0x0800 + mov ecx, [ebx + Image.Width] .bpp8g_to_bpp1.pixel: - shl al, 1 - cmp byte[esi], 0x7f - cmc - adc eax, 0 - add esi, 1 - dec ah - jnz @f - mov byte[edi], al - add edi, 1 - mov ax, 0x0800 + shl al, 1 + cmp byte[esi], 0x7f + cmc + adc eax, 0 + add esi, 1 + dec ah + jnz @f + mov byte[edi], al + add edi, 1 + mov ax, 0x0800 @@: - dec ecx - jnz .bpp8g_to_bpp1.pixel - cmp ah, 8 - je @f - mov cl, ah - shl al, cl - mov byte[edi], al - add edi, 1 + dec ecx + jnz .bpp8g_to_bpp1.pixel + cmp ah, 8 + je @f + mov cl, ah + shl al, cl + mov byte[edi], al + add edi, 1 @@: - dec edx - jnz .bpp8g_to_bpp1.line - mov eax, [_dst] - jmp .quit + dec edx + jnz .bpp8g_to_bpp1.line + ret +endp - .bpp8g_to_bpp8g: - mov ecx, [ebx + Image.Width] - imul ecx, [ebx + Image.Height] - rep movsb - mov eax, [_dst] - jmp .quit - - .bpp8g_to_bpp24: - mov ecx, [ebx + Image.Width] - imul ecx, [ebx + Image.Height] +proc img._.convert.bpp8g_to_bpp24 _src, _dst + mov ecx, [ebx + Image.Width] + imul ecx, [ebx + Image.Height] @@: - mov al, byte[esi] - mov byte[edi + 0], al - mov byte[edi + 1], al - mov byte[edi + 2], al - add esi, 1 - add edi, 3 - sub ecx, 1 - jnz @b - mov eax, [_dst] - jmp .quit + mov al, byte[esi] + mov byte[edi + 0], al + mov byte[edi + 1], al + mov byte[edi + 2], al + add esi, 1 + add edi, 3 + sub ecx, 1 + jnz @b + ret +endp - .bpp24: - mov edx, img.convert.bpp24.table - jmp .find_in_table_and_jump - .bpp24_to_bpp24: - mov ecx, [ebx + Image.Width] - imul ecx, [ebx + Image.Height] - lea ecx, [ecx*3] - mov edx, ecx - shr ecx, 2 - rep movsd - mov ecx, edx - and ecx, 3 - rep movsb - mov eax, [_dst] - jmp .quit - .bpp24_to_bpp8g: - mov ecx, [ebx + Image.Width] - imul ecx, [ebx + Image.Height] + +proc img._.convert.bpp24_to_bpp24 _src, _dst + mov ecx, [ebx + Image.Width] + imul ecx, [ebx + Image.Height] + lea ecx, [ecx*3] + mov edx, ecx + shr ecx, 2 + rep movsd + mov ecx, edx + and ecx, 3 + rep movsb + ret +endp + + +proc img._.convert.bpp24_to_bpp8g _src, _dst + mov ecx, [ebx + Image.Width] + imul ecx, [ebx + Image.Height] @@: - movzx ebx, byte[esi + 0] - movzx eax, byte[esi + 1] - add ebx, eax - movzx eax, byte[esi + 2] - add eax, ebx - mov ebx, 3 - add esi, 3 - div bl - mov byte[edi], al - add edi, 1 - sub ecx, 1 - jnz @b - mov eax, [_dst] - jmp .quit - .bpp24_to_bpp32: - mov ecx, [ebx + Image.Width] - imul ecx, [ebx + Image.Height] + movzx ebx, byte[esi + 0] + movzx eax, byte[esi + 1] + add ebx, eax + movzx eax, byte[esi + 2] + add eax, ebx + mov ebx, 3 + add esi, 3 + div bl + mov byte[edi], al + add edi, 1 + sub ecx, 1 + jnz @b + ret +endp + + +proc img._.convert.bpp24_to_bpp32 _src, _dst + mov ecx, [ebx + Image.Width] + imul ecx, [ebx + Image.Height] @@: lodsw ror eax, 16 @@ -274,392 +265,416 @@ endl stosd dec ecx jnz @b - mov eax, [_dst] - jmp .quit + ret +endp - .bpp32: - mov edx, img.convert.bpp32.table - jmp .find_in_table_and_jump - .bpp32_to_bpp24: - mov ecx, [ebx + Image.Width] - imul ecx, [ebx + Image.Height] +proc img._.convert.bpp32_to_bpp24 _src, _dst + mov ecx, [ebx + Image.Width] + imul ecx, [ebx + Image.Height] @@: - mov eax, [esi] - mov [edi], ax - shr eax, 16 - mov [edi + 2], al - add esi, 4 - add edi, 3 - sub ecx, 1 - jnz @b - mov eax, [_dst] - jmp .quit + mov eax, [esi] + mov [edi], ax + shr eax, 16 + mov [edi + 2], al + add esi, 4 + add edi, 3 + sub ecx, 1 + jnz @b + ret +endp - .bpp15: - mov edx, img.convert.bpp15.table - jmp .find_in_table_and_jump - .bpp15_to_bpp24: - mov ecx, [ebx + Image.Width] - imul ecx, [ebx + Image.Height] +proc img._.convert.bpp32_to_bpp32 _src, _dst + mov ecx, [ebx + Image.Width] + imul ecx, [ebx + Image.Height] + rep movsd + ret +endp - .bpp15.intel: ; copypasted from do_rgb - push ebx ebp - sub ecx, 4 - jb .bpp15.tail + +proc img._.convert.bpp15_to_bpp24 _src, _dst + mov ecx, [ebx + Image.Width] + imul ecx, [ebx + Image.Height] + + .bpp15.intel: ; copypasted from do_rgb + push ebx ebp + sub ecx, 4 + jb .bpp15.tail align 16 .bpp15.intel.loop: repeat 2 - mov ebx, [esi] - mov al, [esi] - mov ah, [esi + 1] - add esi, 4 - and al, 0x1F - and ah, 0x1F shl 2 - mov ebp, ebx - mov dl, al - mov dh, ah - shr al, 2 - shr ah, 4 - shl dl, 3 - shl dh, 1 - and ebp, 0x1F shl 5 - add al, dl - add ah, dh - shr ebp, 2 - mov [edi], al - mov [edi + 2], ah - mov eax, ebx - mov ebx, ebp - shr eax, 16 - shr ebx, 5 - add ebx, ebp - mov ebp, eax - mov [edi + 1], bl - and eax, (0x1F) or (0x1F shl 10) - and ebp, 0x1F shl 5 - lea edx, [eax + eax] - shr al, 2 - mov ebx, ebp - shr ah, 4 - shl dl, 2 - shr ebx, 2 - shr ebp, 7 - add al, dl - add ah, dh - mov [edi + 3], al - add ebx, ebp - mov [edi + 5], ah - mov [edi + 4], bl - add edi, 6 + mov ebx, [esi] + mov al, [esi] + mov ah, [esi + 1] + add esi, 4 + and al, 0x1F + and ah, 0x1F shl 2 + mov ebp, ebx + mov dl, al + mov dh, ah + shr al, 2 + shr ah, 4 + shl dl, 3 + shl dh, 1 + and ebp, 0x1F shl 5 + add al, dl + add ah, dh + shr ebp, 2 + mov [edi], al + mov [edi + 2], ah + mov eax, ebx + mov ebx, ebp + shr eax, 16 + shr ebx, 5 + add ebx, ebp + mov ebp, eax + mov [edi + 1], bl + and eax, (0x1F) or (0x1F shl 10) + and ebp, 0x1F shl 5 + lea edx, [eax + eax] + shr al, 2 + mov ebx, ebp + shr ah, 4 + shl dl, 2 + shr ebx, 2 + shr ebp, 7 + add al, dl + add ah, dh + mov [edi + 3], al + add ebx, ebp + mov [edi + 5], ah + mov [edi + 4], bl + add edi, 6 end repeat - sub ecx, 4 - jnb .bpp15.intel.loop + sub ecx, 4 + jnb .bpp15.intel.loop .bpp15.tail: - add ecx, 4 - jz .bpp15.done + add ecx, 4 + jz .bpp15.done @@: - 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, 3 - sub ecx, 1 - jnz @b + 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, 3 + sub ecx, 1 + jnz @b .bpp15.done: - pop ebp ebx - mov eax, [_dst] - jmp .quit + pop ebp ebx + mov eax, [_dst] + jmp .quit .bpp15.amd: - push ebx ebp - sub ecx, 4 - jb .bpp15.tail + push ebx ebp + sub ecx, 4 + jb .bpp15.tail align 16 .bpp15.amd.loop: repeat 4 if (% mod 2) = 1 - mov eax, dword[esi] - mov ebx, dword[esi] + mov eax, dword[esi] + mov ebx, dword[esi] else - movzx eax, word[esi] - mov ebx, eax + movzx eax, word[esi] + mov ebx, eax end if - 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, 3 + 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, 3 end repeat - sub ecx, 4 - jnb .bpp15.amd.loop - jmp .bpp15.tail + sub ecx, 4 + jnb .bpp15.amd.loop + jmp .bpp15.tail + + .quit: + ret +endp - .bpp16: - mov edx, img.convert.bpp16.table - jmp .find_in_table_and_jump - .bpp16_to_bpp24: - mov ecx, [ebx + Image.Width] - imul ecx, [ebx + Image.Height] +proc img._.convert.bpp16_to_bpp24 _src, _dst + mov ecx, [ebx + Image.Width] + imul ecx, [ebx + Image.Height] .bpp16.intel: - push ebx ebp - sub ecx, 4 - jb .bpp16.tail + push ebx ebp + sub ecx, 4 + jb .bpp16.tail align 16 .bpp16.intel.loop: repeat 2 - mov ebx, [esi] - mov al, [esi] - mov ah, [esi + 1] - add esi, 4 - and al, 0x1F - and ah, 0x1F shl 3 - mov ebp, ebx - mov dl, al - mov dh, ah - shr al, 2 - shr ah, 5 - shl dl, 3 - and ebp, 0x3F shl 5 - add al, dl - add ah, dh - shr ebp, 3 - mov [edi], al - mov [edi + 2], ah - mov eax, ebx - mov ebx, ebp - shr eax, 16 - shr ebx, 6 - add ebx, ebp - mov ebp, eax - mov [edi + 1], bl - and eax, (0x1F) or (0x1F shl 11) - and ebp, 0x3F shl 5 - mov edx, eax - shr al, 2 - mov ebx, ebp - shr ah, 5 - shl dl, 3 - shr ebx, 3 - shr ebp, 9 - add al, dl - add ah, dh - mov [edi + 3], al - add ebx, ebp - mov [edi + 5], ah - mov [edi + 4], bl - add edi, 6 + mov ebx, [esi] + mov al, [esi] + mov ah, [esi + 1] + add esi, 4 + and al, 0x1F + and ah, 0x1F shl 3 + mov ebp, ebx + mov dl, al + mov dh, ah + shr al, 2 + shr ah, 5 + shl dl, 3 + and ebp, 0x3F shl 5 + add al, dl + add ah, dh + shr ebp, 3 + mov [edi], al + mov [edi + 2], ah + mov eax, ebx + mov ebx, ebp + shr eax, 16 + shr ebx, 6 + add ebx, ebp + mov ebp, eax + mov [edi + 1], bl + and eax, (0x1F) or (0x1F shl 11) + and ebp, 0x3F shl 5 + mov edx, eax + shr al, 2 + mov ebx, ebp + shr ah, 5 + shl dl, 3 + shr ebx, 3 + shr ebp, 9 + add al, dl + add ah, dh + mov [edi + 3], al + add ebx, ebp + mov [edi + 5], ah + mov [edi + 4], bl + add edi, 6 end repeat - sub ecx, 4 - jnb .bpp16.intel.loop + sub ecx, 4 + jnb .bpp16.intel.loop .bpp16.tail: - add ecx, 4 - jz .bpp16.done + add ecx, 4 + jz .bpp16.done @@: - movzx eax, word[esi] - mov ebx, eax - add esi, 2 - and eax, (0x1F) or (0x1F shl 11) - and ebx, 0x3F shl 5 - mov edx, eax - shr al, 2 - mov ebp, ebx - shr ebx, 3 - shr ah, 5 - shl dl, 3 - shr ebp, 9 - add eax, edx - add ebx, ebp - mov [edi], al - mov [edi + 1], bl - mov [edi + 2], ah - add edi, 3 - sub ecx, 1 - jnz @b + movzx eax, word[esi] + mov ebx, eax + add esi, 2 + and eax, (0x1F) or (0x1F shl 11) + and ebx, 0x3F shl 5 + mov edx, eax + shr al, 2 + mov ebp, ebx + shr ebx, 3 + shr ah, 5 + shl dl, 3 + shr ebp, 9 + add eax, edx + add ebx, ebp + mov [edi], al + mov [edi + 1], bl + mov [edi + 2], ah + add edi, 3 + sub ecx, 1 + jnz @b .bpp16.done: - pop ebp ebx - mov eax, [_dst] - jmp .quit + pop ebp ebx + mov eax, [_dst] + jmp .quit .bpp16.amd: - push ebx ebp - sub ecx, 4 - jb .bpp16.tail + push ebx ebp + sub ecx, 4 + jb .bpp16.tail align 16 .bpp16.amd.loop: repeat 4 if (% mod 2) = 1 - mov eax, dword[esi] - mov ebx, dword[esi] + mov eax, dword[esi] + mov ebx, dword[esi] else - movzx eax, word[esi] - mov ebx, eax + movzx eax, word[esi] + mov ebx, eax end if - add esi, 2 - and eax, (0x1F) or (0x1F shl 11) - and ebx, 0x3F shl 5 - mov edx, eax - shr al, 2 - mov ebp, ebx - shr ebx, 3 - shr ah, 5 - shl dl, 3 - shr ebp, 9 - add eax, edx - add ebx, ebp - mov [edi], al - mov [edi + 1], bl - mov [edi + 2], ah - add edi, 3 + add esi, 2 + and eax, (0x1F) or (0x1F shl 11) + and ebx, 0x3F shl 5 + mov edx, eax + shr al, 2 + mov ebp, ebx + shr ebx, 3 + shr ah, 5 + shl dl, 3 + shr ebp, 9 + add eax, edx + add ebx, ebp + mov [edi], al + mov [edi + 1], bl + mov [edi + 2], ah + add edi, 3 end repeat - sub ecx, 4 - jnb .bpp16.amd.loop - jmp .bpp16.tail + sub ecx, 4 + jnb .bpp16.amd.loop + jmp .bpp16.tail - - .bpp1: - mov edx, img.convert.bpp1.table - jmp .find_in_table_and_jump - .bpp1_to_bpp24: - push [ebx + Image.Width] - pop [width] - push [ebx + Image.Height] - pop [height] - mov edx, [ebx + Image.Palette] - .bpp1_to_bpp24.line: - mov ebx, [width] - .bpp1_to_bpp24.byte: - mov ah, 8 - mov al, byte[esi] - add esi, 1 - .bpp1_to_bpp24.bit: - xor ecx, ecx - shl al, 1 - adc ecx, 0 - mov ecx, [edx + 4*ecx] - mov word[edi], cx - shr ecx, 8 - mov byte[edi + 2], ch - add edi, 3 - sub ebx, 1 - jnz @f - sub [height], 1 - jnz .bpp1_to_bpp24.line - jmp .bpp1.done - @@: - sub ah, 1 - jnz .bpp1_to_bpp24.bit - jmp .bpp1_to_bpp24.byte - .bpp1.done: - mov eax, [_dst] - jmp .quit - - - .bpp8a: - mov edx, img.convert.bpp8a.table - jmp .find_in_table_and_jump - .bpp8a_to_bpp1: - mov eax, [_dst] - mov eax, [eax + Image.Palette] - mov dword[eax], 0x00000000 - mov dword[eax + 4], 0x00ffffff - mov edx, [ebx + Image.Height] - .bpp8a_to_bpp1.line: - mov ax, 0x0800 - mov ecx, [ebx + Image.Width] - .bpp8a_to_bpp1.pixel: - shl al, 1 - cmp byte[esi], 0x7f - cmc - adc eax, 0 - add esi, 2 - dec ah - jnz @f - mov byte[edi], al - add edi, 1 - mov ax, 0x0800 - @@: - dec ecx - jnz .bpp8a_to_bpp1.pixel - cmp ah, 8 - je @f - mov cl, ah - shl al, cl - mov byte[edi], al - add edi, 1 - @@: - dec edx - jnz .bpp8a_to_bpp1.line - mov eax, [_dst] - jmp .quit - - .bpp8a_to_bpp24: - mov ecx, [ebx + Image.Width] - imul ecx, [ebx + Image.Height] - @@: - mov al, byte[esi] - mov byte[edi + 0], al - mov byte[edi + 1], al - mov byte[edi + 2], al - add esi, 2 - add edi, 3 - sub ecx, 1 - jnz @b - mov eax, [_dst] - jmp .quit - - - .error: - xor eax, eax .quit: - pop edi esi ebx - ret + ret +endp + + +proc img._.convert.bpp1_to_bpp24 _src, _dst +locals + width rd 1 + height rd 1 +endl + push [ebx + Image.Width] + pop [width] + push [ebx + Image.Height] + pop [height] + mov edx, [ebx + Image.Palette] + .bpp1_to_bpp24.line: + mov ebx, [width] + .bpp1_to_bpp24.byte: + mov ah, 8 + mov al, byte[esi] + add esi, 1 + .bpp1_to_bpp24.bit: + xor ecx, ecx + shl al, 1 + adc ecx, 0 + mov ecx, [edx + 4*ecx] + mov word[edi], cx + shr ecx, 8 + mov byte[edi + 2], ch + add edi, 3 + sub ebx, 1 + jnz @f + sub [height], 1 + jnz .bpp1_to_bpp24.line + jmp .bpp1.done + @@: + sub ah, 1 + jnz .bpp1_to_bpp24.bit + jmp .bpp1_to_bpp24.byte + .bpp1.done: + ret +endp + + +proc img._.convert.bpp8a_to_bpp1 _src, _dst + mov eax, [_dst] + mov eax, [eax + Image.Palette] + mov dword[eax], 0x00000000 + mov dword[eax + 4], 0x00ffffff + mov edx, [ebx + Image.Height] + .bpp8a_to_bpp1.line: + mov ax, 0x0800 + mov ecx, [ebx + Image.Width] + .bpp8a_to_bpp1.pixel: + shl al, 1 + cmp byte[esi], 0x7f + cmc + adc eax, 0 + add esi, 2 + dec ah + jnz @f + mov byte[edi], al + add edi, 1 + mov ax, 0x0800 + @@: + dec ecx + jnz .bpp8a_to_bpp1.pixel + cmp ah, 8 + je @f + mov cl, ah + shl al, cl + mov byte[edi], al + add edi, 1 + @@: + dec edx + jnz .bpp8a_to_bpp1.line + ret +endp + + +proc img._.convert.bpp8a_to_bpp24 _src, _dst + mov ecx, [ebx + Image.Width] + imul ecx, [ebx + Image.Height] + @@: + mov al, byte[esi] + mov byte[edi + 0], al + mov byte[edi + 1], al + mov byte[edi + 2], al + add esi, 2 + add edi, 3 + sub ecx, 1 + jnz @b + ret endp img.convert.bpp8i.table: - dd Image.bpp24, img.convert.layer.bpp8i_to_bpp24 -img.convert.bpp8g.table: - dd Image.bpp24, img.convert.layer.bpp8g_to_bpp24 - dd Image.bpp8g, img.convert.layer.bpp8g_to_bpp8g - dd Image.bpp1, img.convert.layer.bpp8g_to_bpp1 + dd Image.bpp24, img._.convert.bpp8i_to_bpp24 + dd Image.bpp32, img._.convert.bpp8i_to_bpp32 + dd 0 img.convert.bpp24.table: - dd Image.bpp24, img.convert.layer.bpp24_to_bpp24 - dd Image.bpp8g, img.convert.layer.bpp24_to_bpp8g - dd Image.bpp32, img.convert.layer.bpp24_to_bpp32 + dd Image.bpp24, img._.convert.bpp24_to_bpp24 + dd Image.bpp8g, img._.convert.bpp24_to_bpp8g + dd Image.bpp32, img._.convert.bpp24_to_bpp32 + dd 0 img.convert.bpp32.table: - dd Image.bpp24, img.convert.layer.bpp32_to_bpp24 + dd Image.bpp24, img._.convert.bpp32_to_bpp24 + dd Image.bpp32, img._.convert.bpp32_to_bpp32 + dd 0 img.convert.bpp15.table: - dd Image.bpp24, img.convert.layer.bpp15_to_bpp24 + dd Image.bpp24, img._.convert.bpp15_to_bpp24 + dd 0 img.convert.bpp16.table: - dd Image.bpp24, img.convert.layer.bpp16_to_bpp24 + dd Image.bpp24, img._.convert.bpp16_to_bpp24 + dd 0 img.convert.bpp1.table: - dd Image.bpp24, img.convert.layer.bpp1_to_bpp24 + dd Image.bpp24, img._.convert.bpp1_to_bpp24 + dd 0 +img.convert.bpp8g.table: + dd Image.bpp24, img._.convert.bpp8g_to_bpp24 + dd Image.bpp1, img._.convert.bpp8g_to_bpp1 + dd 0 +img.convert.bpp2i.table: + dd 0 +img.convert.bpp4i.table: + dd 0 img.convert.bpp8a.table: - dd Image.bpp24, img.convert.layer.bpp8a_to_bpp24 + dd Image.bpp24, img._.convert.bpp8a_to_bpp24 + dd 0 + +img.convert.table: + dd 0 ; no image type zero + dd img.convert.bpp8i.table + dd img.convert.bpp24.table + dd img.convert.bpp32.table + dd img.convert.bpp15.table + dd img.convert.bpp16.table + dd img.convert.bpp1.table + dd img.convert.bpp8g.table + dd img.convert.bpp2i.table + dd img.convert.bpp4i.table + dd img.convert.bpp8a.table diff --git a/programs/develop/libraries/libs-dev/libimg/libimg.asm b/programs/develop/libraries/libs-dev/libimg/libimg.asm index 556fbc75f..59eeed31e 100644 --- a/programs/develop/libraries/libs-dev/libimg/libimg.asm +++ b/programs/develop/libraries/libs-dev/libimg/libimg.asm @@ -26,7 +26,10 @@ include '../../../../struct.inc' include '../../../../proc32.inc' include '../../../../macros.inc' include '../../../../config.inc' -;include '../../../../debug.inc' +include '../../../../debug-fdo.inc' +__DEBUG__ = 0 +__DEBUG_LEVEL__ = 1 +include '../../../../develop/libraries/libs-dev/libio/libio.inc' purge section,mov,add,sub include 'libimg.inc' @@ -124,14 +127,50 @@ endp ;;================================================================================================;; proc img.from_file _filename ;////////////////////////////////////////////////////////////////////;; ;;------------------------------------------------------------------------------------------------;; -;? --- TBD --- ;; +;? load file from disk and decode it ;; ;;------------------------------------------------------------------------------------------------;; -;> --- TBD --- ;; +;> [_filename] = file name as passed to libio ;; ;;------------------------------------------------------------------------------------------------;; ;< eax = 0 / pointer to image ;; ;;================================================================================================;; - xor eax, eax - ret +locals + fd dd ? + img_data_len dd ? + img_data dd ? ; raw bytes + img dd ? ; Image pointer +endl +DEBUGF 2, 'img.from_file: %s\n', [_filename] + push ebx + mov [img], 0 + invoke file.open, [_filename], O_READ + mov [fd], eax + test eax, eax + jz .exit + invoke file.size, [_filename] + test eax, eax + jnz .exit_close + mov [img_data_len], ebx + invoke mem.alloc, ebx + test eax, eax + jz .exit_close + mov [img_data], eax + invoke file.read, [fd], eax, [img_data_len] + cmp eax, -1 + jz .exit_free_close + cmp eax, [img_data_len] + jnz .exit_free_close + stdcall img.decode, [img_data], [img_data_len], 0 + test eax, eax + jz .exit_free_close + mov [img], eax + .exit_free_close: + invoke mem.free, [img_data] + .exit_close: + invoke file.close, [fd] + mov eax, [img] + .exit: + pop ebx + ret endp ;;================================================================================================;; @@ -2439,18 +2478,6 @@ 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 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 ) OR (1 SHL Image.bpp8g) - .bpp8a dd (1 SHL Image.bpp24) - ;;================================================================================================;; ;;////////////////////////////////////////////////////////////////////////////////////////////////;; ;;================================================================================================;; @@ -2717,6 +2744,8 @@ img._.get_scanline_len: ;/////////////////////////////////////////////////////// ;;////////////////////////////////////////////////////////////////////////////////////////////////;; ;;================================================================================================;; +include_debug_strings + align 4 type2bpp dd 8, 24, 32, 15, 16, 1, 9, 2, 4 img._.do_rgb.handlers: @@ -2786,14 +2815,23 @@ export \ align 16 @IMPORT: -library archiver, 'archiver.obj' +library \ + archiver, 'archiver.obj', \ + libio , 'libio.obj' + import archiver, \ - deflate_unpack2, 'deflate_unpack2',\ - deflateInit2, 'deflateInit2',\ - deflateReset, 'deflateReset',\ - deflate, 'deflate',\ - deflateEnd, 'deflateEnd',\ - calc_crc32, 'calc_crc32' + deflate_unpack2, 'deflate_unpack2',\ + deflateInit2, 'deflateInit2',\ + deflateReset, 'deflateReset',\ + deflate, 'deflate',\ + deflateEnd, 'deflateEnd',\ + calc_crc32, 'calc_crc32' + +import libio , \ + file.size , 'file_size' , \ + file.open , 'file_open' , \ + file.read , 'file_read' , \ + file.close, 'file_close' align 4 ; mutex for unpacker loading diff --git a/programs/develop/libraries/libs-dev/libimg/libimg.inc b/programs/develop/libraries/libs-dev/libimg/libimg.inc index 23f92b4ff..635bf5341 100644 --- a/programs/develop/libraries/libs-dev/libimg/libimg.inc +++ b/programs/develop/libraries/libs-dev/libimg/libimg.inc @@ -30,7 +30,7 @@ LIBIMG_FORMAT_XCF = 9 LIBIMG_FORMAT_TIFF = 10 LIBIMG_FORMAT_PNM = 11 LIBIMG_FORMAT_WBMP = 12 -LIBIMG_FORMAT_XBM = 13 +LIBIMG_FORMAT_XBM = 13 LIBIMG_FORMAT_Z80 = 14 ; scale type ; corresponding img.scale params @@ -65,10 +65,10 @@ LIBIMG_ERROR_NOT_INPLEMENTED = 9 LIBIMG_ERROR_INVALID_INPUT = 10 ; encode flags (byte 0x02 of _common option) -;LIBIMG_ENCODE_STRICT_SPECIFIC = 0x01 -LIBIMG_ENCODE_STRICT_BIT_DEPTH = 0x02 -;LIBIMG_ENCODE_DELETE_ALPHA = 0x08 -;LIBIMG_ENCODE_FLUSH_ALPHA = 0x10 +;LIBIMG_ENCODE_STRICT_SPECIFIC = 0x01 +LIBIMG_ENCODE_STRICT_BIT_DEPTH = 0x02 +;LIBIMG_ENCODE_DELETE_ALPHA = 0x08 +;LIBIMG_ENCODE_FLUSH_ALPHA = 0x10 ; convert flags ; none so far