Dark Bit Factory & Gravity

PROGRAMMING => Freebasic => Topic started by: Shockwave on October 15, 2007

Title: Resourcing 24 bit bitmaps into your programs.
Post by: Shockwave on October 15, 2007
I wanted to include windows 24 bit bitmaps into some things because the reduction in colour quality was not acceptable for some artwork.

So I researched the windows bitmap format which is quite straight forward, anyway I made this very simple little thing that enables you to include a 24 bit bitmap into your exe and display it.

The resources you need are below;

>HERE< (http://www.dbfinteractive.com/demos/24bitimage.zip)

You will see a folder containing 4 files.
1: Landscape.bmp this is a 24 bit bitmap  image.
2: 24BITTEST.exe is a compiled version for those without freebasic.
3: 24BITTEST.bas is the source code
4: bimage.bas is the converted picture.

So a quick tutorial.

Firstly this works only with uncompressed windows bitmaps and the width must divide by 8 with no remainder.

Onec you have your picture, you need to convert it.

Use bin2bas by rbraz.

Open a dos window and then type bin2bas picture.bmp bimage.bas

picture.bmp is the name of your unconverted picture.

then open the program 24bittest.bas

these lines;

Code: [Select]
DIM SHARED AS INTEGER  IMGX = 400:' Picture Width (must be divisible by 8)
 DIM SHARED AS INTEGER  IMGY = 300:' Picture Height

Need to be set as the width and height of your picture.

Then scroll down to ;

Code: [Select]
SUB LOADBMP()
   
    DIM AS INTEGER L,M,PSTART
    PSTART=53 + ( (IMGX*IMGY) * 3 )
    M=0   
    FOR L=PSTART TO 54 STEP-3
        PICTURE_BUFFER(M) = RGB(landscape.bmp(L),landscape.bmp(L-1),landscape.bmp(L-2))
        M=M+1
    NEXT
   
END SUB

and where it says landscape.bmp, change all of these to the original name of your picture before you converted it so in this example it would become;

Code: [Select]
SUB LOADBMP()
   
    DIM AS INTEGER L,M,PSTART
    PSTART=53 + ( (IMGX*IMGY) * 3 )
    M=0   
    FOR L=PSTART TO 54 STEP-3
        PICTURE_BUFFER(M) = RGB(picture.bmp(L),picture.bmp(L-1),picture.bmp(L-2))
        M=M+1
    NEXT
   
END SUB


And that's it.

If you want to add masking / clipping do it yourself it's quite easy, I hope this is of some use to peeps :)

Enjoy.
Title: Re: Resourcing 24 bit bitmaps into your programs.
Post by: Clyde on October 18, 2007
Neato, I'll give this a try.
Title: Re: Resourcing 24 bit bitmaps into your programs.
Post by: Shockwave on October 18, 2007
Let me know if you run into any problems.
Title: Re: Resourcing 24 bit bitmaps into your programs.
Post by: Clyde on October 19, 2007
Hiya Mate :)

Im having some trouble with getting the snippet bellow to convert from a single array Array( XRES*YRES ) to a bidimentional one ( XRES, YRES ). What happens is that the image is mirrored / reversed. You'll see from running it, btw it is using the same Landscape image as before.

Im using the (x,y) approach as a few effects use that technique.


Code: [Select]
'-------------------------------------------------------------------------------
'      PROGRAM TO DRAW A 24 BIT WINDOWS BITMAP BY SHOCKWAVE ^ S!P
'
'Feel free to use this in your own stuff :) A small credit would be nice.
'
'-------------------------------------------------------------------------------

'   Uses PTC_EXT by Rbraz, install it beforeusing this program.

 #INCLUDE "TINYPTC_EXT.BI"
 #INCLUDE "WINDOWS.BI"
   
'   The picture;

 #INCLUDE "bimage.bas"

'-------------------------------------------------------------------------------

 OPTION STATIC
 OPTION EXPLICIT

'-------------------------------------------------------------------------------

 CONST   XRES    =   640:' Screen X
 CONST   YRES    =   480:' Screen Y
   
 DIM SHARED AS INTEGER  IMGX = 400:' Picture Width (must be divisible by 8)
 DIM SHARED AS INTEGER  IMGY = 300:' Picture Height
   
 DIM SHARED AS UINTEGER PICTURE_BUFFER (IMGX * IMGY)
DIM SHARED AS UINTEGER BUFFER         (XRES * YRES)

Dim Shared ImageBuffer(XRES,YRES)
Dim Shared ScreenBuffer(XRES*YRES)

'-------------------------------------------------------------------------------

 DECLARE SUB LOADBMP()
 DECLARE SUB DRAWBMP(BYVAL XP AS INTEGER , BYVAL YP AS INTEGER )   
 
 LOADBMP()

'-------------------------------------------------------------------------------

 PTC_ALLOWCLOSE(0)
 PTC_SETDIALOG(0,"",0,0)   
       
 IF (PTC_OPEN("24 BIT BITMAP",XRES,YRES)=0) THEN
 END-1
 END IF   

'-------------------------------------------------------------------------------

WHILE(GETASYNCKEYSTATE(VK_ESCAPE)<>-32767) 
   
        DRAWBMP(100,80)
        PTC_UPDATE@ ScreenBuffer(0)
       
        ERASE ScreenBUFFER
WEND
   
SUB LOADBMP()
   
    DIM AS INTEGER L,M,PSTART
    PSTART=53 + ( (IMGX*IMGY) * 3 )
    M=0   
    FOR L=PSTART TO 54 STEP-3
        PICTURE_BUFFER(M) = RGB(landscape.bmp(L),landscape.bmp(L-1),landscape.bmp(L-2))
        M=M+1
    NEXT
   
    '
    ' Convert to Bidimentional arrays
    '
    Dim x,y
   
    For y=0 to ImgY-1
        for x=0 to ImgX-1
           
            ImageBuffer( x,y )=Picture_Buffer( X + ( Y * Imgx ))

        Next
    Next
   
   
END SUB

SUB DrawImage(BYVAL XP AS INTEGER , BYVAL YP AS INTEGER )   
   
   Dim x,y
   
    For y=0 To ImgY-1
        For x=0 to ImgX-1
           
            ScreenBuffer(( y+YP )  * XRES + ( x+XP ))=ImageBuffer(x,y)

        Next
    Next
   
End Sub

Cheers and many thanks,
Clyde.
Title: Re: Resourcing 24 bit bitmaps into your programs.
Post by: Shockwave on October 19, 2007
Because of the bitmap format Clyde, images are stored upside down and that's why it went backwards when you drew it.

Tbh though I cant advocaet cleaning up your code unless you have a really good reason to use a 2 dimensional array.
It's a slow method now and you would be better off using the example I gave you which uses a 1 dimensional array and pointers.

Think about it, tinyptc uses a sequential buffer and what you are doing there is adding extra calculations and slowdown.

You'd be better off using the example I gave you in the first place.


but....


If you would really like to continue the slower method for some unknown reason, you'll need another variable set that you can set at the limits of the picture and decrease instead of increase so that the picture ends up in your array the right way around.

Title: Re: Resourcing 24 bit bitmaps into your programs.
Post by: Jim on October 20, 2007
If you really need it, changing
Code: [Select]
ImageBuffer( x,y )=Picture_Buffer( X + ( Y * Imgx ))
to
Code: [Select]
ImageBuffer( x,(Imgy-1)-y )=Picture_Buffer( X + ( Y * Imgx ))
will flip it vertically.
I think Nick's right about keeping it in 1D arrays though.

Jim
Title: Re: Resourcing 24 bit bitmaps into your programs.
Post by: .:] Druid [:. on November 07, 2007
I was thinking about it..you made it!  Thanks a lot shockie, this will be used...one day :)
Title: Re: Resourcing 24 bit bitmaps into your programs.
Post by: Shockwave on November 08, 2007
I will be really looking forward to seeing the result :)

Btw, Widowmaker used it in the last Skid Row intro he did because he didn't want to destroy the nice picture by Kenet and Irokos so it's been used once already :)