Jump to content
Tuts 4 You

24 bit BMP writing code


enhzflep

Recommended Posts

Posted

Just finished some code for another site I visit. It's just been hacked together very quickly, though perhaps somebody will get some use from it. With error checking on the 4 calls to CreateFile or WriteFile you'd have an okay function.

Shown below is the output from the code.

Tootles...

.586
.MODEL flat, stdcall OPTION CASEMAP:NONE ;Case sensitive Include windows.inc
Include kernel32.inc
Include masm32.inc IncludeLib kernel32.lib
IncludeLib masm32.lib myMain PROTO
writeBmp24 PROTO :DWORD,:DWORD,:DWORD,:DWORD .DATA?
pixelData dd ? ; pointer to the pixel buffer
x dd ? ; used to create the pattern
y dd ? myBmpInfo BITMAPINFO <>
myInfoSize dd ?
fileHeader BITMAPFILEHEADER <>
outputFileHandle dd ? fileHandle dd ?
retVal dd ? .DATA
filenameStr DB "enhzflep.bmp", 0
bmpWidth equ 128
bmpHeight equ 128 .CODE Start:
Invoke myMain
Invoke ExitProcess,0
myMain Proc
; allocate mem for our pixel buffer
invoke GlobalAlloc, GMEM_FIXED, bmpWidth*bmpHeight*3
mov pixelData, eax ; make sure it's been cleared first
invoke RtlZeroMemory, eax,bmpWidth*bmpHeight*3 ; go through, make the array a simple XOR pattern.
mov ebx, pixelData
mov y, 0
.while y < bmpHeight
mov x, 0
.while x < bmpWidth
mov eax, x
xor eax, y
mov byte ptr [ebx], 0
inc ebx
mov byte ptr [ebx], 0
inc ebx
mov [ebx], al
inc ebx
inc x
.endw
inc y
.endw ; write the bugger to disk
Invoke writeBmp24, bmpWidth, bmpHeight, addr filenameStr, pixelData ; give back the memory
invoke GlobalFree, pixelData Ret
myMain endp
; writes a 24 bit bmp file
; call with width, height, pointer to filename, pointer to width*height*3 bytes of pixelData
writeBmp24 proc, myWidth:DWORD, myHeight:DWORD, myFilename:DWORD, pixBuffer:DWORD
; size of header for 24 bit images
mov myInfoSize, sizeof BITMAPINFOHEADER ; magic file signature
mov fileHeader.bfType, 04d42h ; unused, must be zero
mov fileHeader.bfReserved1, 0
mov fileHeader.bfReserved2, 0 ; offset in file of start of pixel data
mov eax, sizeof BITMAPFILEHEADER
add eax, myInfoSize
mov fileHeader.bfOffBits, eax ; total size of file
mov ebx, sizeof BITMAPFILEHEADER
add ebx, myInfoSize
mov edx, myWidth
imul edx, myHeight
push edx
add ebx, edx
shl edx, 1
add ebx, edx
mov fileHeader.bfSize, ebx ; size of header
mov myBmpInfo.bmiHeader.biSize, sizeof myBmpInfo.bmiHeader ; biSizeImage = width*height*3
mov myBmpInfo.bmiHeader.biSizeImage, edx
pop edx
add myBmpInfo.bmiHeader.biSizeImage, edx ; image dimensions
mov eax, myWidth
mov myBmpInfo.bmiHeader.biWidth, eax
mov eax, myHeight
mov myBmpInfo.bmiHeader.biHeight, eax ; pixels per meter
mov myBmpInfo.bmiHeader.biXPelsPerMeter, 2835
mov myBmpInfo.bmiHeader.biYPelsPerMeter, 2835 ; 1 color plane, 24 bits per pixel
mov myBmpInfo.bmiHeader.biPlanes, 1
mov myBmpInfo.bmiHeader.biBitCount, 24 ; uncompressed, 24 bit image
mov myBmpInfo.bmiHeader.biCompression, 0 ; unused for true color images - only used with images that have a palette
mov myBmpInfo.bmiHeader.biClrImportant, 0
mov myBmpInfo.bmiHeader.biClrUsed, 0 ; open file for writing - truncate to zero length if it already exists
invoke CreateFile, myFilename, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_ARCHIVE, 0
mov fileHandle, eax ; write the file header
invoke WriteFile, fileHandle, addr fileHeader, sizeof BITMAPFILEHEADER, addr retVal, 0
; write the info header
invoke WriteFile, fileHandle, addr myBmpInfo, myInfoSize, addr retVal, 0
; write the pixel data
invoke WriteFile, fileHandle, pixBuffer, myBmpInfo.bmiHeader.biSizeImage, addr retVal, 0 ; close the file
invoke CloseHandle, fileHandle Ret
writeBmp24 endp End Start

post-36039-1222108607_thumb.png

Posted

nice piece of code. could be usefull one day :)

  • 2 months later...
Posted

I just read trough this because I'd like to learn ASM, and I must say that it's nice commented ;) (not too much, but clear:))

Thanks!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...