Author Topic: PNG library suited for tiny intros on masm?  (Read 316 times)

0 Members and 1 Guest are viewing this topic.

Offline ViL

  • Avocado lover 🥑
  • C= 64
  • **
  • Posts: 52
  • Karma: 1
    • View Profile
Hii!

i've stumbled on PNGlib like it's the only png decoder for masm, it is the only one out there? this is the site of PNGlib
http://www.madwizard.org/programming/projects/pnglib

while it's considerably small, uPNG on C output is even smaller, counting kb; but some pairs of kb makes the difference on tiny intros :)

may i ask if anyone knows another png decoder for masm? :D

any idea is very welcome! x3
« Last Edit: July 02, 2018 by ViL »

Offline Siekmanski

  • ZX 81
  • *
  • Posts: 8
  • Karma: 3
    • View Profile
Re: PNG library suited for tiny intros on masm?
« Reply #1 on: July 02, 2018 »
Hi Vil,

No need for bloated 3th party image libraries.
You can use GDIplus for that with a few lines of code.
It can load bmp, gif, jpeg, png, tiff and ico files from disk ( or from memory or from resource section ) and decompress it to memory as uncompressed raw ARGB data.

Here is an example to load a bitmap image from disk in Masm. ( and it's very very small.  ;D)

Code: [Select]
include     \masm32\include\gdiplus.inc
includelib  \masm32\lib\gdiplus.lib

.const

; GDI+ info
ImageLockModeRead               equ 1
PixelFormat32bppARGB            equ 26200Ah

GdiplusStartupInput struct
    GdiplusVersion              dd ?
    DebugEventCallback          dd ?
    SuppressBackgroundThread    dd ?
    SuppressExternalCodecs      dd ?
GdiplusStartupInput ends

BitmapData struct                         
    dwWidth                     dd ?   
    dwHeight                    dd ?   
    Stride                      dd ?   
    PixelFormat                 dd ?   
    Scan0                       dd ?   
    Reserved                    dd ?
BitmapData ends

GdiplusInput   GdiplusStartupInput <1,NULL,FALSE,FALSE>

.data?
; GDI+ data
align 4
pImage              dd ?
ReturnMessage       dd ?
GdiplusToken        dd ?
GDIplusBitmapData   BitmapData <?>
FilenameW           dw MAX_PATH dup (?)
ARGB_raw_buffer     dd 640 * 480 dup (?) ; memory to hold a raw 32bit bitmap of 640*480

.data

NicePicture         db "NicePicture.png",0

.code

align 4
GDIp_LoadBitmap proc uses ebx esi edi ImageName:DWORD,pBitmap:DWORD

    mov      ReturnMessage,FALSE

    invoke  GdiplusStartup,offset GdiplusToken,offset GdiplusInput,NULL
    test    eax,eax
    jnz      Exit_LoadBitmap
   
    invoke  MultiByteToWideChar,CP_ACP,0,ImageName,-1,offset FilenameW,MAX_PATH-1
    invoke  GdipCreateBitmapFromFile,offset FilenameW,addr pImage
    test    eax,eax
    jnz      ShutdownGDIplus

    invoke  GdipBitmapLockBits,pImage,NULL,ImageLockModeRead,PixelFormat32bppARGB,offset GDIplusBitmapData
    test    eax,eax
    jnz      Close_Image

    mov      esi,GDIplusBitmapData.Scan0     ; pointer to the bitmap data
    mov      edi,offset ARGB_raw_buffer      ; pointer to the output bitmap data
   
    mov      ecx,GDIplusBitmapData.dwHeight
Height_lp:
    mov      edx,GDIplusBitmapData.dwWidth
    xor      ebx,ebx
Width_lp:
    mov      eax,dword ptr [esi+ebx]   
    mov      dword ptr [edi],eax
    add         edi,4
    add      ebx,4
    dec      edx
    jnz      Width_lp
    add      esi,GDIplusBitmapData.Stride   
    dec      ecx
    jnz      Height_lp   

    mov      ReturnMessage,TRUE

    invoke  GdipBitmapUnlockBits,pImage,offset GDIplusBitmapData

Close_Image:
    invoke  GdipDisposeImage,pImage
ShutdownGDIplus:
    invoke  GdiplusShutdown,GdiplusToken
Exit_LoadBitmap:
    mov      eax,ReturnMessage
    ret
GDIp_LoadBitmap endp

; load bmp, gif, jpeg, png, tiff and ico files from disk and decompress to memory as uncompressed raw ARGB data.

    invoke      GDIp_LoadBitmap,offset NicePicture,offset ARGB_raw_buffer
« Last Edit: July 02, 2018 by Siekmanski »