flat assembler
Message board for the users of flat assembler.

Index > Tutorials and Examples > [help] Saving a bitmap in memory to a png on disk?

Goto page Previous  1, 2
Author
Thread Post new topic Reply to topic
nasm



Joined: 02 Nov 2021
Posts: 183
nasm 29 Sep 2024, 21:12
If there should be any point in doing PNG in assembly, it should be all assembly and therefore we need volunteers who can post their private PNG code here. Anyone?
Post 29 Sep 2024, 21:12
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4073
Location: vpcmpistri
bitRAKE 30 Sep 2024, 00:34
Do you have a github or something? Where can I see your work nasm?

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 30 Sep 2024, 00:34
View user's profile Send private message Visit poster's website Reply with quote
nasm



Joined: 02 Nov 2021
Posts: 183
nasm 30 Sep 2024, 06:36
bitRAKE, no github for me. I've never posted my code anywhere so far.

_________________
Do you think anyone will sell their soul over a 10 cent yoghurt with a bad after taste that lasts, a decade?
Post 30 Sep 2024, 06:36
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4073
Location: vpcmpistri
bitRAKE 03 Oct 2024, 14:52
Here is the brief way to load any WIC supported image format and write out as another supported format:
Code:
        .inputPath      dq ? ; wide-string
        .outputPath     dq ? ; wide-string

; needed COM objects:
        .pFactory       IWICImagingFactory
        .pDecoder       IWICBitmapDecoder
        .pFrame         IWICBitmapFrameDecode
        .pConverter     IWICFormatConverter
        .pStream        IWICStream
        .pEncoder       IWICBitmapEncoder
        .pFrameEncode   IWICBitmapFrameEncode

        CoCreateInstance & CLSID_WICImagingFactory2, NULL, CLSCTX_INPROC_SERVER, & IID_IWICImagingFactory, & .pFactory

        IWICImagingFactory__CreateDecoderFromFilename [.pFactory], [.inputPath], NULL, GENERIC_READ, WICDecodeMetadataCacheOnDemand, & .pDecoder
        IWICBitmapDecoder__GetFrame [.pDecoder], 0, & .pFrame

; create and configuring converter for desired output format:

        IWICImagingFactory__CreateFormatConverter [.pFactory], & .pConverter
        IWICFormatConverter__Initialize [.pConverter], [.pFrame], & GUID_WICPixelFormat32bppBGRA, WICBitmapDitherTypeNone, NULL, 0.0, WICBitmapPaletteTypeCustom

        IWICImagingFactory__CreateStream [.pFactory], & .pStream
        IWICStream__InitializeFromFilename [.pStream], [.outputPath], GENERIC_WRITE

        IWICImagingFactory__CreateEncoder [.pFactory], & GUID_ContainerFormatBmp, NULL, & .pEncoder
        IWICBitmapEncoder__Initialize [.pEncoder], [.pStream], WICBitmapEncoderNoCache

        IWICBitmapEncoder__CreateNewFrame [.pEncoder], & .pFrameEncode, NULL
        IWICBitmapFrameEncode__Initialize [.pFrameEncode], NULL

        {data:16} .format ddq ? ; format need to be readwrite
        movdqa xmm0, [GUID_WICPixelFormat32bppBGRA]
        movdqa [.format], xmm0
        IWICBitmapFrameEncode__SetPixelFormat [.pFrameEncode], & .format

; write the converted pixels to the frame

        IWICBitmapFrameEncode__WriteSource [.pFrameEncode], [.pConverter], NULL
        IWICBitmapFrameEncode__Commit [.pFrameEncode]
        IWICBitmapEncoder__Commit [.pEncoder]    
Use one of the GUID_ContainerFormat* values to change the encoder, or GUID_WICPixelFormat* to change the pixel data desired.

Note: no error checking - lots of HRESULTS to examine, and IUnknown__Release all the objects when done. The important thing is to watch what data types and access restriction each parameter has. I've tested the code and it does work - this is the minimal process needed.
Post 03 Oct 2024, 14:52
View user's profile Send private message Visit poster's website Reply with quote
nasm



Joined: 02 Nov 2021
Posts: 183
nasm 07 Oct 2024, 09:36
bitRAKE, I'm going to try it soon. You're one of the legends who still care about the assembly forum.

The truth is, we DO need more PNG code, this example will get us far, but we do need to have alternatives too. It's a great world, the needs are also great.

Do we have more legends? The fact of the matter is that it can be more than just PNG, if you have a custom image format that works well for video processing, that too is welcome.
Post 07 Oct 2024, 09:36
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4353
Location: Now
edfed 07 Oct 2024, 10:58
hem. an idea like this.. why don't develop a set of binaries that can be installed in ram and be called like any bios function? libpng can be one of these.
Post 07 Oct 2024, 10:58
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20445
Location: In your JS exploiting you and your system
revolution 07 Oct 2024, 11:33
Code:
~ find /lib* -iname "libpng*"
/lib/x86_64-linux-gnu/libpng12.so.0.54.0
/lib/x86_64-linux-gnu/libpng12.so.0
/lib/i386-linux-gnu/libpng12.so.0.54.0
/lib/i386-linux-gnu/libpng12.so.0
~    
Post 07 Oct 2024, 11:33
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4073
Location: vpcmpistri
bitRAKE 08 Oct 2024, 05:31
nasm wrote:
You're one of the legends who still care about the assembly forum.
Do we have more legends?
I appreciate your enthusiasm, but perhaps you go too far. People contribute what they can - myself included. My ability to contribute is my good fortune (and the generosity of others) and I wish for it to be yours as well.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 08 Oct 2024, 05:31
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4353
Location: Now
edfed 13 Oct 2024, 12:40
https://learn.microsoft.com/en-us/windows/win32/gdiplus/-gdiplus-converting-a-bmp-image-to-a-png-image-use

as in the screenshot example, but with png mimetype. it looks ok in win10

Code:
section '.data' data readable writeable

  file_name du 'test.png',0

  encoder_mimetype du 'image/png',0
    


Code:
format PE GUI 4.0

include 'win32w.inc'

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

struct ImageCodecInfo
  Clsid             db 16 dup ?
  FormatID          db 16 dup ?
  CodecName         dd ?
  DllName           dd ?
  FormatDescription dd ?
  FilenameExtension dd ?
  MimeType          dd ?
  Flags             dd ?
  Version           dd ?
  SigCount          dd ?
  SizeSize          dd ?
  SigPattern        dd ?
  SigMask           dd ?
ends

section '.text' code readable executable

entry $

        invoke  GdiplusStartup,token,input,NULL
        test    eax,eax
        jnz     exit

        invoke  GdipGetImageEncodersSize,encoders_count,encoders_size
        test    eax,eax
        jnz     gdiplus_shutdown
        invoke  VirtualAlloc,0,[encoders_size],MEM_COMMIT,PAGE_READWRITE
        test    eax,eax
        jz      gdiplus_shutdown
        mov     ebx,eax
        invoke  GdipGetImageEncoders,[encoders_count],[encoders_size],ebx
        test    eax,eax
        jnz     gdiplus_shutdown
    scan_encoders:
        mov     esi,[ebx+ImageCodecInfo.MimeType]
        mov     edi,encoder_mimetype
        mov     ecx,(encoder_clsid-encoder_mimetype) shr 1
        repe    cmpsw
        je      encoder_found
        add     ebx,sizeof.ImageCodecInfo
        dec     [encoders_count]
        jnz     scan_encoders
        ; no encoder found
        jmp     gdiplus_shutdown
     encoder_found:
        lea     esi,[ebx+ImageCodecInfo.Clsid]
        mov     edi,encoder_clsid
        mov     ecx,4
        rep     movsd
        invoke  VirtualFree,ebx,0,MEM_RELEASE

        invoke  GetDC,HWND_DESKTOP
        test    eax,eax
        jz      gdiplus_shutdown
        mov     esi,eax
        invoke  GetSystemMetrics,SM_CYSCREEN
        mov     [screen_height],eax
        invoke  GetSystemMetrics,SM_CXSCREEN
        mov     [screen_width],eax
        invoke  CreateCompatibleBitmap,esi,[screen_width],[screen_height]
        test    eax,eax
        jz      release_desktop_dc
        mov     ebx,eax
        invoke  CreateCompatibleDC,esi
        test    eax,eax
        jz      delete_bitmap
        mov     edi,eax
        invoke  SelectObject,edi,ebx
        test    eax,eax
        jz      delete_dc
        invoke  BitBlt,edi,0,0,[screen_width],[screen_height],esi,0,0,SRCCOPY
        test    eax,eax
        jz      delete_dc

        invoke  GdipCreateBitmapFromHBITMAP,ebx,NULL,gdip_bitmap
        test    eax,eax
        jnz     delete_dc

        invoke  GdipSaveImageToFile,[gdip_bitmap],file_name,encoder_clsid,NULL

        invoke  GdipDisposeImage,[gdip_bitmap]
  delete_dc:
        invoke  DeleteObject,edi
  delete_bitmap:
        invoke  DeleteObject,ebx
  release_desktop_dc:
        invoke  ReleaseDC,HWND_DESKTOP,esi
  gdiplus_shutdown:
        invoke  GdiplusShutdown,[token]
  exit:
        invoke  ExitProcess,0


section '.data' data readable writeable

  file_name du 'test.png',0

  encoder_mimetype du 'image/png',0
           .length = $ - encoder_mimetype
  encoder_clsid db 16 dup ?

  input GdiplusStartupInput 1
  token dd ?
  memdc dd ?
  gdip_bitmap dd ?

  encoders_count dd ?
  encoders_size dd ?

  screen_width dd ?
  screen_height dd ?

section '.rdata' data readable

data import

  library kernel32,'KERNEL32.DLL',\
          user32,'USER32.DLL',\
          gdi32,'GDI32.DLL',\
          gdiplus, 'GDIPLUS.DLL'

  include 'api\kernel32.inc'
  include 'api\user32.inc'
  include 'api\gdi32.inc'

  import  gdiplus,\
          GdiplusStartup,'GdiplusStartup',\
          GdiplusShutdown,'GdiplusShutdown',\
          GdipGetImageEncodersSize,'GdipGetImageEncodersSize',\
          GdipGetImageEncoders,'GdipGetImageEncoders',\
          GdipSaveImageToFile,'GdipSaveImageToFile',\
          GdipDisposeImage,'GdipDisposeImage',\
          GdipCreateBitmapFromHBITMAP,'GdipCreateBitmapFromHBITMAP'

end data

    
Post 13 Oct 2024, 12:40
View user's profile Send private message Visit poster's website Reply with quote
nasm



Joined: 02 Nov 2021
Posts: 183
nasm 15 Oct 2024, 11:06
edfed, thats a nice example. Gdi+ is a very nice have, MS never drops support for their API's, so it's a nice have.

We have many good examples here now, the only thing missing is a private PNG library which I think Ali.Z said he had, if he could post it here then the thread would be complete.

We will not scrutinize it, so don't be afraid to post it. If you feel that the work is too much and that nobody deserves the code, then you don't have to do it.

_________________
Do you think anyone will sell their soul over a 10 cent yoghurt with a bad after taste that lasts, a decade?
Post 15 Oct 2024, 11:06
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.