; _______________________________________________________________________________________
;|                                                                                       |
;| ..::FreshLib::..  Free, open source. Licensed under "BSD 2-clause" license."          |
;|_______________________________________________________________________________________|
;
;  Description: Memory based images manipulation library.
;
;  Target OS: Win32
;
;  Dependencies: memory.asm
;
;  Notes:
;_________________________________________________________________________________________

uses msimg32, gdi32


struct TImage
  .width   dd ?  ; width in pixels.
  .height  dd ?  ; height in pixels.
  .pPixels dd ?  ; pointer to the pixel memory.

; os dependent data
  .lock    TMutex
  .hBitmap dd ?
ends



body CreateImage
  .bmi BITMAPV4HEADER
begin
        pushad

        stdcall GetMem, sizeof.TImage
        jc      .finish

        mov     esi, eax

        xor     eax, eax
        lea     edi, [.bmi]
        mov     ecx, sizeof.BITMAPV4HEADER/4
        rep stosd

        inc     eax

        mov     ecx, [.width]
        mov     edx, [.height]

        cmp     ecx, 0
        cmovle  ecx, eax

        cmp     edx, 0
        cmovle  edx, eax

        mov     [esi+TImage.width], ecx
        mov     [esi+TImage.height], edx

        mov     [.bmi.biSize], sizeof.BITMAPV4HEADER
        mov     [.bmi.biPlanes], 1

; it is already 0
;        mov     [.bmi.biCompression], BI_RGB
;        mov     [.bmi.biSizeImage], 0
;        mov     [.bmi.biClrUsed], 0
;        mov     [.bmi.biClrImportant], 0

        mov     [.bmi.BlueMask],  $000000ff
        mov     [.bmi.GreenMask], $0000ff00
        mov     [.bmi.RedMask],   $00ff0000
        mov     [.bmi.AlphaMask], $ff000000

        mov     [.bmi.biBitCount], 32
        neg     edx
        mov     [.bmi.biWidth], ecx
        mov     [.bmi.biHeight], edx

        lea     eax, [.bmi]
        lea     ecx, [esi+TImage.pPixels]
        xor     edx, edx
        invoke  CreateDIBSection, edx, eax, DIB_RGB_COLORS, ecx, edx, edx
        mov     [esi+TImage.hBitmap], eax

        mov     [esp+4*regEAX], esi

        lea     esi, [esi+TImage.lock]
        stdcall MutexCreate, 0, esi
        stdcall MutexRelease, esi

        clc

.finish:
        popad
        return
endp





body DestroyImage
begin
        pushad

        mov     esi, [.ptrImage]
        test    esi, esi
        jz      .finish

.lock_wait:
        stdcall LockImg, esi
        jc      .lock_wait


        invoke  GdiFlush
        invoke  DeleteObject, [esi+TImage.hBitmap]

        lea     edi, [esi+TImage.lock]
        stdcall MutexDestroy, edi

        stdcall FreeMem, esi

.finish:
        popad
        return
endp



;AC_SRC_OVER =  $00000000
;AC_SRC_ALPHA = $01000000
;
;struct BLENDFUNCTION
;  .BlendOp             db ?
;  .BlendFlags          db ?
;  .SourceConstantAlpha db ?
;  .AlphaFormat         db ?
;ends

body DrawImageRect
begin
        push    eax ecx edx esi edi

        mov     esi, [.pImage]
        test    esi, esi
        jz      .exit

        stdcall LockImg, esi
        jc      .exit

        invoke  CreateCompatibleDC, 0
        mov     ebx, eax

        invoke  SelectObject, ebx, [esi+TImage.hBitmap]
        push    eax

        invoke  BitBlt, [.where], [.xDst], [.yDst], [.width], [.height], ebx, [.xSrc], [.ySrc], SRCCOPY

        invoke  SelectObject, ebx ; from the stack
        invoke  DeleteDC, ebx

        stdcall UnlockImg, esi

.exit:
        pop     edi esi edx ecx eax
        return
endp




;body DrawText
;.pwc dd ?
;.pwl dd ?
;.buffer  rb 256
;.charlen dd ?
;begin
;        push    eax ebx ecx edx
;
;; first convert the string:
;        lea     eax, [.buffer]
;        mov     ecx, [.len]
;        shl     ecx, 3
;        cmp     ecx, $100
;        jbe     @f
;
;        stdcall GetMem, ecx
;
;@@:
;        mov     [.pwl], ecx
;        mov     [.pwc], eax
;
;        invoke  MultiByteToWideChar, CP_UTF8, 0, [.text], [.len], [.pwc], ecx
;        mov     [.charlen], eax
;
;
;        mov     ebx, [.pImage]
;        stdcall AllocateContext, [ebx+TImage.hBitmap]
;        mov     ebx, eax
;
;        cmp     [.font], 0
;        jne     @f
;
;        invoke  GetStockObject, DEFAULT_GUI_FONT
;        mov     [.font], eax
;
;@@:
;        invoke  SelectObject, [ebx+TContext.handle], [.font]
;        push    eax
;
;        invoke  SetBkMode, [ebx+TContext.handle], TRANSPARENT
;        invoke  SetTextAlign, [ebx+TContext.handle], TA_NOUPDATECP or TA_LEFT or TA_BASELINE
;
;        mov     eax, [.color]
;        bswap   eax
;        ror     eax, 8
;        invoke  SetTextColor, [ebx+TContext.handle], eax
;        invoke  TextOutW, [ebx+TContext.handle], [.x], [.y], [.pwc], [.charlen]
;
;        invoke  SelectObject, [ebx+TContext.handle] ; old font from the stack.
;
;        cmp     [.pwl], $100
;        jbe     @f
;        stdcall FreeMem, [.pwc]
;@@:
;        stdcall ReleaseContext, ebx
;
;        pop     edx ecx ebx eax
;        return
;endp

