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

0 Members and 1 Guest are viewing this topic.

Offline ViL

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

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

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\
includelib  \masm32\lib\gdiplus.lib


; 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>

; 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


NicePicture         db "NicePicture.png",0


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
    mov      edx,GDIplusBitmapData.dwWidth
    xor      ebx,ebx
    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

    invoke  GdipDisposeImage,pImage
    invoke  GdiplusShutdown,GdiplusToken
    mov      eax,ReturnMessage
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 »