gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

OffTopic > Creating a bitmap

#172630 - Azenris - Sun Feb 21, 2010 9:54 pm

Im using VB for this. It was working till i changed to using 4bpp instead of 8bpp. Im just wondering if you can spot anything obviously wrong with this. It creates a new file 16x16 bitmap 4bpp by taking the data from larger bitmap, 256x384, 4bpp.

What Happens:

The file is created, but cannot be opened in windows preview, or windows paint, but Graphics Gale can still open it, when just clicking the save icon in graphics gale after opening it, it will work fine else where. In graphics gale the palette is there and the image is correct.

Code:

' ###########################################################
' constants
Public Const TILE_PIXELS_X = 16
Public Const TILE_PIXELS_Y = 16
Public Const TILE_PIXELS_S = (TILE_PIXELS_X / 2) * TILE_PIXELS_Y

Private Const PALSET_PREFIX = "pal_set_"
Private Const PALSET_LOCATION = ".\data\palsets\"
Public Const GRAPHIC_DATA_DIR = ".\data\program_files\tiles\graphics\"

' ###########################################################
Private Sub CreateLittleImage(tile As Integer, offset As Integer)
    Dim file As Byte
    file = FreeFile

    ' create/open the new image file
    Open GRAPHIC_DATA_DIR & "tile_ico_" & tile + offset & ".bmp" For Binary As #file
        Dim newImage_Header As TYPE_BITMAPFILEHEADER
        Dim newImage_InfoHeader As TYPE_BITMAPINFOHEADER
       
        ' fill out the header details
        newImage_Header.bfType = 19778      ' means "BM" as in bitmap
        newImage_Header.bfSize = 0          ' left 0 with no compression
        newImage_Header.bfReserved1 = 0     ' res
        newImage_Header.bfReserved2 = 0     ' res
        newImage_Header.bfOffBits = 1078    ' standard offset

        newImage_InfoHeader.biSize = 40                 ' standard size
        newImage_InfoHeader.biWidth = TILE_PIXELS_X     ' new image width (pixels)
        newImage_InfoHeader.biHeight = TILE_PIXELS_Y    ' new image height (pixels)
        newImage_InfoHeader.biPlanes = 1                ' 1 plane
        newImage_InfoHeader.biBitCount = 4              ' bits per pixel
        newImage_InfoHeader.biCompression = 0           ' no compression
        newImage_InfoHeader.biSizeImage = TILE_PIXELS_S ' image byte size
        newImage_InfoHeader.biXPelsPerMeter = 0
        newImage_InfoHeader.biYPelsPerMeter = 0
        newImage_InfoHeader.biClrUsed = 0
        newImage_InfoHeader.biClrImportant = 0
       
        Put #file, 1, newImage_Header                   ' write the header to the beginning of the file
        Put #file, , newImage_InfoHeader                ' write the info header straight after
        Put #file, , BMP_ColourTable                    ' then write the colour table
       
        Dim x As Integer, y As Integer
        Dim tilesPerRow As Integer, tilesPerCol As Integer
        Dim imageLocX As Long, imageLocY As Long
       
        ' calculate the offset required to location the correct
        ' location in the master image for finding the image
        ' being made
        tilesPerRow = (BMP_InfoHeader.biWidth / TILE_PIXELS_X)
        tilesPerCol = (BMP_InfoHeader.biHeight / TILE_PIXELS_Y)
       
        If (tile < tilesPerRow) Then
            imageLocX = tile
            imageLocY = tilesPerCol - 1
        Else
            imageLocX = tile Mod tilesPerRow
            imageLocY = (tilesPerCol - 1) - Int((tile / tilesPerRow))
        End If

        imageLocX = imageLocX * (TILE_PIXELS_X / 2)
        imageLocY = (imageLocY * (TILE_PIXELS_Y * (BMP_InfoHeader.biWidth / 2)))
       
        ' cycle through the master data and write the bytes to the new file
        For y = 0 To (TILE_PIXELS_Y - 1)
            For x = 0 To ((TILE_PIXELS_X / 2) - 1)
                Put #file, , BMP_Data(imageLocX + imageLocY + (x + (y * (BMP_InfoHeader.biWidth / 2))))
            Next x
        Next y
       
    ' close the new image
    Close #file
End Sub

_________________
My Homebrew Games

#172632 - sajiimori - Mon Feb 22, 2010 12:29 am

I have no idea, but how about using Paint or something to create a .bmp exactly like the one you're trying to write, and look for differences in the binary?

#172639 - Cearn - Mon Feb 22, 2010 11:21 am

It's the BITMAPFILEHEADER's bfOffBits. It's supposed to be the offset to where the image data starts (usually right after the palette). Your value of 1078 would be correct for an 8bpp image (14 (BMFH) + 40 (BMIH) + 1024 (palette) ), but for your 4bpp image it would place the start well outside the file. The windows preview apparently doesn't like it if the sizes don't match up; Graphics Gale seems a little more lenient. For correctness, it might be best to fill in the BITMAPFILEHEADER.bfSize field and BITMAPINFOHEADER.biClrUsed as well.

#172642 - Azenris - Mon Feb 22, 2010 5:26 pm

Doh :/ Thank you for the replies :)
_________________
My Homebrew Games