showbmp works but need help with it

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
depot1
Newbie
Posts: 7
Joined: Thu Aug 04, 2005 8:53 pm
Location: downey ca

showbmp works but need help with it

Post by depot1 »

this works but how do you get the data from the file
i want to read the pixles and palet
thank
**********code here****
'Show Bitmap 1.1 - by: Peter Swinkels, ***2005***
'These routines can be used to display RGB and RLE encoded Microsoft Windows bitmaps.
DEFINT A-Z
DECLARE SUB GeneratePalette ()
DECLARE SUB SetPalette (PaletteData AS STRING)
DECLARE SUB ShowBitmap (FileName AS STRING)

'Screen mode 12 can be used instead of mode 13 for 16 color (or monochrome) bitmaps.
SCREEN 13: CLS

'Insert the full path and filename for the bitmap to be displayed between the quotes.
ShowBitmap ""

SUB GeneratePalette
'This procedure generates a default palette.
DIM Blue AS INTEGER, Green AS INTEGER, Red AS INTEGER
DIM Brightness AS INTEGER, Entry AS INTEGER

Entry = 0
FOR Brightness = 0 TO 3
FOR Red = 0 TO 3
FOR Green = 0 TO 3
FOR Blue = 0 TO 3
PALETTE Entry, (CLNG((Blue + Brightness) * 10.5) * 65536) + (CLNG((Green + Brightness) * 10.5) * 256) + CLNG((Red + Brightness) * 10.5)
Entry = Entry + 1
NEXT Blue
NEXT Green
NEXT Red
NEXT Brightness
END SUB

SUB SetPalette (PaletteData AS STRING)
'This procedure sets the palette to match the bitmap's palette.
SHARED IndexCount
DIM Blue AS INTEGER, Green AS INTEGER, Red AS INTEGER
DIM Entry AS INTEGER, NewPalette(255) AS LONG

FOR Entry = 0 TO IndexCount - 1
Red = ASC(MID$(PaletteData, (Entry * 4) + 1, 1)) \ 4
Green = ASC(MID$(PaletteData, (Entry * 4) + 2, 1)) \ 4
Blue = ASC(MID$(PaletteData, (Entry * 4) + 3, 1)) \ 4
NewPalette(Entry) = (Red * 65536) + (Green * 256) + Blue
NEXT Entry

PALETTE USING NewPalette
END SUB

SUB ShowBitmap (FileName AS STRING)
'This procedure displays the specified bitmap.
SHARED IndexCount
DIM BitMask AS INTEGER, BitsPerPixel AS INTEGER, BytesPerPixel AS INTEGER, PixelsPerByte AS DOUBLE
DIM Blue AS INTEGER, Brightness AS INTEGER, Green AS INTEGER, Red AS INTEGER
DIM Byte AS LONG, Byte1 AS LONG, Byte2 AS LONG, Column AS LONG, Pixel AS INTEGER
DIM Compression AS INTEGER, Hght AS LONG, Padding AS LONG, PaletteData AS STRING, PixelData AS STRING, Wdth AS LONG
DIM x AS LONG, y AS LONG

'Checks if the specified file exists.
OPEN FileName FOR INPUT AS 1: CLOSE 1

'Loads and displays the specified bitmap.
OPEN "c:\5bmp.bmp" FOR BINARY AS 1
'Checks the file format.
SEEK #1, 1
IF INPUT$(2, 1) = "BM" THEN
'Gets the bitmap's compression type.
SEEK #1, 31
Compression = ASC(INPUT$(1, 1))

'Gets the width and height of the bitmap.
SEEK #1, 19
Wdth = CVL(INPUT$(4, 1))
Hght = CVL(INPUT$(4, 1))

'Gets the number of bits per pixel (color depth.)
SEEK #1, 29
BitsPerPixel = ASC(INPUT$(1, 1))

'Calculates the number of bytes per pixel.
BytesPerPixel = BitsPerPixel / 8
IF BytesPerPixel <1>= LOF(1)
Byte1 = ASC(INPUT$(1, 1))
Byte2 = ASC(INPUT$(1, 1))
IF Byte1 = 0 AND Byte2 <2> 2 THEN 'Checks whether absolute mode is used.
Byte2 = Byte2 / PixelsPerByte
PixelData = INPUT$(Byte2, 1)
FOR Byte = 1 TO Byte2 STEP PixelsPerByte
IF BitsPerPixel = 4 THEN BitMask = 240
IF BitsPerPixel = 8 THEN BitMask = 255

FOR Pixel = 0 TO PixelsPerByte - 1
IF Byte + Pixel = Byte2 THEN EXIT FOR
PSET ((x * PixelsPerByte) + Pixel, y), (ASC(MID$(PixelData, Byte + Pixel, 1)) AND BitMask) \ (BitMask \ (IndexCount - 1))
BitMask = BitMask \ IndexCount
NEXT Pixel
x = x + BytesPerPixel
NEXT Byte
''' x = x + 1
IF Byte2 MOD 2 > 0 THEN SEEK #1, SEEK(1) + 1
ELSEIF Byte1 > 0 THEN 'Checks whether encoded mode is used.
FOR Column = 1 TO Byte1 STEP PixelsPerByte
IF BitsPerPixel = 4 THEN BitMask = 240
IF BitsPerPixel = 8 THEN BitMask = 255

FOR Pixel = 0 TO PixelsPerByte - 1
IF Byte + Pixel = Byte1 THEN EXIT FOR
PSET ((x * PixelsPerByte) + Pixel, y), (Byte2 AND BitMask) \ (BitMask \ (IndexCount - 1))
BitMask = BitMask \ IndexCount
NEXT Pixel
x = x + BytesPerPixel
NEXT Column
END IF
LOOP
END IF
END IF
CLOSE 1
END SUB
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Try a TYPE to get the Bitmap header information

Post by burger2227 »

Code: Select all

TYPE BMPHeaderType         'BITMAP HEADER TYPE
     ID AS STRING * 2      'File ID is "BM"
     size AS LONG          'Size of the data file
     Res1 AS INTEGER       'Reserved 1 should be 0
     Res2 AS INTEGER       'Reserved 2 should be 0
     Offset AS LONG        'Position of start of pixel data
     Hsize AS LONG         'Information header size
     PWidth AS LONG        'Image width
     PDepth AS LONG        'Image height
     Planes AS INTEGER     'number of planes
     BPP AS INTEGER        'Bits per pixel, 8 for Screen 13, 256 color image
     Compress AS LONG      'Compression
     ImageBytes AS LONG    'Width * Height ,ImageSIZE
     Xres AS LONG          'Width in PELS per metre
     Yres AS LONG          'depth in PELS per metre
     NumColors AS LONG     'Number of COLORS
     SigColors AS LONG     'Significant COLORs
END TYPE

DIM SHARED BMPHead AS BMPHeaderType

GET #1, , BMPHead    'done all in one GET
Since you are opening the Bitmap in Binary mode, you can use one GET to retrieve all of the header information. Just use dot variables like:

Wide& = BMPHead.PWidth or you can use the dot variables themselves.

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Post Reply