October 29, 2025, 08:16:56 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Sprite to bmp file

Started by ckoehn, November 29, 2014, 07:54:05 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ckoehn

Is there a way to save a sprite buffer to a bmp file?

I'm loading a grayscale bitmap into a sprite buffer to process it for a cnc machine.  After I process it, I would like to save it back to a grayscale file.

Later,
Clint

LarryMc

LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

LarryMc

See if this will do it for you
Add the following as an inc file to you application
then call
SaveBitmap(filename:string,win:WINDOW,l:int,t:int,w:int,h:int)

TYPE BITMAP
DEF bmType:INT
DEF bmWidth:INT
DEF bmHeight:INT
DEF bmWidthCHARs:INT
DEF bmPlanes:WORD
DEF bmBitsPixel:WORD
DEF bmBits:INT
ENDTYPE

TYPE BITMAPINFOHEADER
DEF biSize:INT
DEF biWidth:INT
DEF biHeight:INT
DEF biPlanes:WORD
DEF biBitCount:WORD
DEF biCompression:INT
DEF biSizeImage:INT
DEF biXPelsPerMeter:INT
DEF biYPelsPerMeter:INT
DEF biClrUsed:INT
DEF biClrImportant:INT
ENDTYPE

TYPE BITMAPFILEHEADER,1
DEF bfType:WORD
DEF bfSize:INT
DEF bfReserved1:WORD
DEF bfReserved2:WORD
DEF bfOffBits:INT
ENDTYPE
CONST BI_RGB = 0
CONST BI_RLE8 = 1
CONST BI_RLE4 = 2
CONST BI_bitfields = 3

'API functions for dealing with the bitmaps
DECLARE "gdi32",BitBlt(HDCDest:uint,destx:int,desty:int,destw:int,desth:int,HDCSrc:int,srcx:int,srcy:int,rop:INT),int
DECLARE "gdi32",CreateCompatibleBitmap(HDC:uint,width:int,height:int),int
DECLARE "gdi32",CreateCompatibleDC(HDC:uint),int
DECLARE "gdi32",SelectObject(HDC:uint,Handle:int),int
DECLARE "gdi32",DeleteDC(HDC:uint),int
DECLARE "gdi32",DeleteObject(handle:uint),int
DECLARE "gdi32",GetObjectA(handle:uint,size:int,mem:POINTER),int
DECLARE "gdi32",GetDIBits(HDC:uint,hbitmap:uint,start:int,lines:int,bits:POINTER,info:POINTER,usage:INT),int
DECLARE IMPORT,ZeroMemory ALIAS RtlZeroMemory(pvoid as POINTER,length as INT),INT

DECLARE SaveBitmap(filename:string,win:WINDOW,l:int,t:int,w:int,h:int)
DECLARE CreateInfoStructure(win:WINDOW,hbitmap:int,info:BITMAPINFOHEADER),INT

'functions for saving bitmap files
SUB SaveBitmap(filename:string,win:WINDOW,l:int,t:int,w:int,h:int)
def hdcWin,hdcComp,hbitmap,hbitmapold,quadsize:int
def info:BITMAPINFOHEADER
def fileheader:BITMAPFILEHEADER
def lpbits,lpbminfo:MEMORY
def fileb=0:BFILE
ZeroMemory(info,len(info))
ZeroMemory(fileheader,len(fileheader))
hdcWin = GETHDC(win)
hdcComp = CreateCompatibleDC(hdcWin)
if hdcComp = 0
MESSAGEBOX win,"Couldn't create DC","Error"
RELEASEHDC win,hdcWin
return
endif
hbitmap = CreateCompatibleBitmap(hdcWin,w,h)
if hbitmap = 0
MESSAGEBOX win,"Couldn't create bitmap","Error"
RELEASEHDC win,hdcWin
endif
hbitmapold = SelectObject(hdcComp,hbitmap)
BitBlt(hdcComp,0,0,w,h,hdcWin,l,t,0x00CC0020)
SelectObject(hdcComp,hbitmapold)
DeleteDC(hdcComp)
REM at this point hbitmap contains a valid bitmap handle.
REM fill in the BITMAPINFOHEADER and compute the color data
quadsize = CreateInfoStructure(win,hbitmap,info)
REM allocate memory and get the 'bits' of the bitmap
ALLOCMEM lpbits,1,info.biSizeImage
ALLOCMEM lpbminfo,1,len(info) + quadsize
WRITEMEM lpbminfo,1,info
GetDIBits(hdcWin,hbitmap,0,info.biHeight,lpbits,lpbminfo,0)
REM open a binary file and write the header,color data and bitmap bits
if(OPENFILE(fileb,filename,"W") = 0)
fileheader.bfType = 0x4d42
fileheader.bfSize = len(fileheader) + info.biSize + quadsize + info.biSizeImage
fileheader.bfOffBits = len(fileheader) + info.biSize + quadsize
WRITE fileb,fileheader
WRITE fileb,lpbminfo
WRITE fileb,lpbits
CLOSEFILE fileb
endif
FREEMEM lpbminfo
FREEMEM lpbits
RELEASEHDC win,hdcWin
DeleteObject(hbitmap)
RETURN
ENDSUB

SUB CreateInfoStructure(win:WINDOW,hbitmap:int,info:BITMAPINFOHEADER)
def bmp:BITMAP
def mem:MEMORY
def cClrBits:WORD
def quadsize:int : quadsize = 0
ALLOCMEM mem,1,len(bmp)
GetObjectA(hbitmap,len(bmp),mem)
READMEM mem,1,bmp
FREEMEM mem
cClrBits = bmp.bmPlanes * bmp.bmBitsPixel
    if (cClrBits = 1)
        cClrBits = 1
    else
if (cClrBits <= 4)
      cClrBits = 4
    else
if (cClrBits <= 8)
        cClrBits = 8
    else
if (cClrBits <= 16)
        cClrBits = 16
    else
if (cClrBits <= 24)
        cClrBits = 24
    else
        cClrBits = 32
endif
endif
endif
endif
endif
    info.biSize = len(info)
    info.biWidth = bmp.bmWidth
    info.biHeight = bmp.bmHeight
    info.biPlanes = bmp.bmPlanes
    info.biBitCount = bmp.bmBitsPixel
    if (cClrBits < 24)
        info.biClrUsed = 2^cClrBits
endif
info.biCompression = 0
info.biSizeImage = (info.biWidth + 7.0) / 8.0 * info.biHeight * cClrBits
if(cClrBits < 24)
quadsize = 4 * (2^cClrBits)
endif
RETURN quadsize
ENDSUB


LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

ckoehn

Thanks very much LarryMc, will try it.

Later,
Clint

ckoehn

Doesn't this just copy the visible screen to a bmp file?  This could work with smaller sprites.

I would like to copy a sprite buffer to a bmp file.  The sprite buffer can be bigger than a screen.

I'll post some code of what I'm doing once it get it completed enough.

Later,
Clint