Insert image (what ever file type) into QBasic/Quick Basic

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
aan310
Newbie
Posts: 4
Joined: Mon Nov 05, 2007 6:07 pm

Insert image (what ever file type) into QBasic/Quick Basic

Post by aan310 »

does any one have code to do this? i just need to add one image, and i must use Qbasic or Quick Basic (no, they arent the same thing, but very similar)

thx
Nodtveidt
Veteran
Posts: 826
Joined: Sun Jul 25, 2004 4:24 am
Location: Quebradillas, PR
Contact:

Post by Nodtveidt »

There are literally hundreds of different image file types. If you could tell us the one you're using, it would help a lot.
aan310
Newbie
Posts: 4
Joined: Mon Nov 05, 2007 6:07 pm

Post by aan310 »

well, i suppose jpg right now, but i can convert it to what ever, so jpg, or bitmap is the best, but w/e
Nodtveidt
Veteran
Posts: 826
Joined: Sun Jul 25, 2004 4:24 am
Location: Quebradillas, PR
Contact:

Post by Nodtveidt »

BMP would be much better, JPG images aren't really suited for QB. JPG decompressors have been written but they are really slow and limited, plus QB doesn't have any native 24bpp graphics modes anyways. You're best off converting your image to a 256 color BMP file (RGB format, unless you need RLE format for some reason) and then using a BMP loader. The BMP format isn't hard to understand, wotsit.org has several documents on how the format works, and there have been several sources over the years that show how it's done. It's a really easy format to use.

You could try this source:

http://www.qbasic.com/classic/files/bmp.bas

as a basis for your own routine. It's not the best and uses several rookie mistakes in it but it gets the job done.
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

I don't think that BMP.BAS has any padder for the bitmap width and will skew some images. The following is the Bitmap Header Information from WOTSIT.org :

Code: Select all

TYPE BmpHeaderType                             
   Id AS STRING * 2     'Should be "BM"              
   Size AS LONG         'Size of the data            
   rr1 AS INTEGER                                   
   rr2 AS INTEGER                                   
   Offset AS LONG    'Position of start of pixel data    
   Horz AS LONG                                     
   Wid AS LONG         'Image width                  
   Hei AS LONG         'Image height                 
   Planes AS INTEGER '                               
   Bpp AS INTEGER      'Should read 8 for a 256 color 
   pakbyte AS LONG                                  
   Imagebytes AS LONG  'Width * Height             
   xres AS LONG                                     
   yres AS LONG                                     
   colch AS LONG                                    
   ic AS LONG                                       
   Pal AS STRING * 1024 'Stored as Blue, Green, Red for 256 colors
END TYPE     '1078 byte offset total shoud match Offset
DIM Header AS BmpHeaderType  
GET #1, , Header 'grabs all of the header information
Bpp should be 4(16 color) for SCREEN 12 and 8(256 color) or 4 for 13. Header.Pal is a string of 1024 or 64 ASCII characters that hold the BGR color information and a pad byte that is ignored. Pal holds the color settings of the bitmap and are set to the color port. If a bitmap is 4 bit, only read 64 characters of the string. The data after the header is for the image pixel data.

I have a BSAVER.ZIP for downloading at QbasicStation.com in member files.
I hope you have an image editing program to set the BPP(color palette) and sizes.
Ted
Last edited by burger2227 on Wed Nov 07, 2007 11:26 pm, edited 1 time in total.
aan310
Newbie
Posts: 4
Joined: Mon Nov 05, 2007 6:07 pm

Post by aan310 »

sorry, but can you explain step by step how to use one of these template thing, and what needs to be done to prepare the image, i'm a BASIC n00b with a big project due soon, Tetris.....
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

The process is a little hard to grasp at first. The Header.Pal string actually holds ASCII characters of the color values. If an asterisk (CHR$(42)) is in the string the color value = ASC("*") or 42. So you start reading the string with Blue = ASC(MID$(Header.pal, p, 1)) where p is a FOR loop variable. The 4th byte is ignored because it is a padder. The string holds the values Blue, Green, Red, Pad and repeats until you reach 64 characters for 4 BPP colors or 1024 characters for 8 BPP colors. The values are sent to the color port starting at 0:

Code: Select all

OUT &H3C8, 0  'done once at start sets attribute to 0
FOR p = 1 TO 48 '4 BPP SCREEN 12
 OUT &H3C9, Red 
 OUT &H3C9, Green
 OUT &H3C9, blue
NEXT
Some bitmap loaders use GET # and some use MID$ of the Pal string.
Every 3 values sent moves the color attribute up one starting from 0.

The pixel image data is also saved in ASCII characters and is usually read by GET #. The data tells the pixel color to use as set by Pal.

My Q-Basics.ZIP program describes the process at QbasicStation.com . Look in Member Demo Files. It has a lot of interesting stuff from beginner to advanced stuff. I made it so that I didn't have to type a lot LOL!
Post Reply