Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Loading a bitmap Image in C.

bushxeebushxee Posts: 16Member
Hello everyone. Im creating a jigsaw puzzle game in C and for that i need to load images into the code.. can anyone help so to which function can be used or what should i read.. Im not asking for any sort of coding just a help rest i'll manage by myself..
Thankyou
«1

Comments

  • bilderbikkelbilderbikkel Posts: 754Member
    : Hello everyone. Im creating a jigsaw puzzle game in C and for that i
    : need to load images into the code.. can anyone help so to which
    : function can be used or what should i read.. Im not asking for any
    : sort of coding just a help rest i'll manage by myself..
    : Thankyou
    Try Google 'load bitmap C'. My first hit was http://www.vbforums.com/archive/index.php/t-261522.html containing full code!


    bilderbikkel
  • LundinLundin Posts: 3,711Member
    : Hello everyone. Im creating a jigsaw puzzle game in C and for that i
    : need to load images into the code.. can anyone help so to which
    : function can be used or what should i read.. Im not asking for any
    : sort of coding just a help rest i'll manage by myself..
    : Thankyou


    If you search the forums you will find plenty of example code, assuming you are using Windows. Though, the easiest way is of course to use a fancy RAD-tool like Borland Builder or Visual Studio and let it do all the hard work.
  • pentaclepentacle Posts: 36Member
    : Hello everyone. Im creating a jigsaw puzzle game in C and for that i
    : need to load images into the code.. can anyone help so to which
    : function can be used or what should i read.. Im not asking for any
    : sort of coding just a help rest i'll manage by myself..
    : Thankyou
    http://writeka.com/emage/bmp16_format.html

    Note that this code is written in Turbo C. You have to include [b][/b]
  • bushxeebushxee Posts: 16Member
    : : Hello everyone. Im creating a jigsaw puzzle game in C and for that i
    : : need to load images into the code.. can anyone help so to which
    : : function can be used or what should i read.. Im not asking for any
    : : sort of coding just a help rest i'll manage by myself..
    : : Thankyou
    : http://writeka.com/emage/bmp16_format.html
    :
    : Note that this code is written in Turbo C. You have to include
    : [b][/b]

    thankyou everyone for your assistance but there is one more thing that i want to ask is that the following code is generating an error and the error is very few parameters to be called in int loadbitmap(char *);
    so can anyone help me resolve this error?
    [code]#include
    #include
    #include

    int loadbitmap(char*);
    int getcol(int);
    void main (void)
    {
    clrscr();
    printf("Loading bitmap demo");
    loadbitmap();
    getch();
    }
    int loadbitmap(char *filename)
    {
    FILE *ptr=NULL; //file handle to open bitmap file
    int width,height; //width and height of the bitmap
    unsigned long temp=0,i=0,j=0; //some variables i need
    unsigned long ww;
    ptr=fopen(filename,"rb"); //open the bitmap file
    if(!ptr) return 0; //if its not there return
    width=0;height=0; //initialise wisth and height to zero
    fseek(ptr,18,SEEK_SET); //got to offset 18 in file
    for(i=0x1;i<=0x10000;i*=0x100) //read the width
    {
    temp=fgetc(ptr);
    width+=(temp*i);
    }
    fseek(ptr,22,SEEK_SET); //go to offset 22 in file
    for(i=0x1;i<=0x10000;i*=0x100) //read the height
    {
    temp=fgetc(ptr);
    height+=(temp*i);
    }

    ww=width; //ww is the number of reads to make for each horizontal line
    if(ww%2) //ww has to be half of width - since each pixel is only 4 bits of information
    ww++;ww/=2; //just getting the correct value
    if(ww%4) //now - ww is stored as sets of 4 pixels each - i think !?! :so this is the adjustment made
    ww=(ww/4)*4+4; //if width is less than ww*2 we ignore what we read

    fseek(ptr,119,SEEK_SET); //Ok! offset 119 - lets read the pixels -
    //remember the bitmap is stroed up - side - down
    int ch,ch1,ch2;
    for(i=0;i<height;i++)
    for(j=0;j<ww;j++)
    {
    ch=fgetc(ptr); //each character read is 2 pixels - yes 4 bits per pixel - so 16 colors
    ch1=ch;ch2=ch; //find those colors using bitwise ops
    ch1=ch1&(0xf0);ch1=ch1>>4; // ~~
    ch2=ch2&(0x0f); // ~~
    if(j*2<width) //ok so put the first pixel read on screen
    putpixel((j)*2,(height-1-i),getcol(ch1));
    if(j*2+1<width) //put the second pixel read on screen
    putpixel((j)*2+1,(height-1-i),getcol(ch2));
    }
    fclose(ptr); //close the file handle
    return 1;
    }



    int getcol(int col)
    {
    switch(col)
    {
    case 0: return 0; //BLACK;
    case 1: return 4; //RED;
    case 2: return 2; //GREEN
    case 3: return 6; //BROWN
    case 4: return 1; //BLUE;
    case 5: return 5; //MAGENTA;
    case 6: return 3; //CYAN;
    case 7: return 7; //LIGHTGRAY;
    case 8: return 8; //DARKGRAY;
    case 9: return 12; //LIGHTRED;
    case 10:return 10; //LIGHTGREEN
    case 11:return 14; //YELLOW;
    case 12:return 9; //LIGHTBLUE;
    case 13:return 13; //LIGHTMAGENTA
    case 14:return 11; //LIGHTCYAN;
    case 15:return 15; //WHITE;
    }
    return col;
    }[/code]
  • bilderbikkelbilderbikkel Posts: 754Member
    Easy:

    : [code]: #include
    : #include
    : #include
    :
    : int loadbitmap(char*);
    : int getcol(int);
    : [red]int[/red] main (void)
    : {
    : clrscr();
    : printf("Loading bitmap demo");
    : loadbitmap([red]"MyBitmap.bmp"[/red]);
    : getch();
    : }

    : int loadbitmap(char *filename)
    : {
    : FILE *ptr=NULL; //file handle to open bitmap file
    : int width,height; //width and height of the bitmap
    : unsigned long temp=0,i=0,j=0; //some variables i need
    : unsigned long ww;
    : ptr=fopen(filename,"rb"); //open the bitmap file
    : if(!ptr) return 0; //if its not there return
    : width=0;height=0; //initialise wisth and height to zero
    : fseek(ptr,18,SEEK_SET); //got to offset 18 in file
    : for(i=0x1;i<=0x10000;i*=0x100) //read the width
    : {
    : temp=fgetc(ptr);
    : width+=(temp*i);
    : }
    : fseek(ptr,22,SEEK_SET); //go to offset 22 in file
    : for(i=0x1;i<=0x10000;i*=0x100) //read the height
    : {
    : temp=fgetc(ptr);
    : height+=(temp*i);
    : }
    :
    : ww=width; //ww is the number of reads to make for each horizontal line
    : if(ww%2) //ww has to be half of width - since each pixel is only 4 bits of information
    : ww++;ww/=2; //just getting the correct value
    : if(ww%4) //now - ww is stored as sets of 4 pixels each - i think !?! :so this is the adjustment made
    : ww=(ww/4)*4+4; //if width is less than ww*2 we ignore what we read
    :
    : fseek(ptr,119,SEEK_SET); //Ok! offset 119 - lets read the pixels -
    : //remember the bitmap is stroed up - side - down
    : int ch,ch1,ch2;
    : for(i=0;i<height;i++)
    : for(j=0;j<ww;j++)
    : {
    : ch=fgetc(ptr); //each character read is 2 pixels - yes 4 bits per pixel - so 16 colors
    : ch1=ch;ch2=ch; //find those colors using bitwise ops
    : ch1=ch1&(0xf0);ch1=ch1>>4; // ~~
    : ch2=ch2&(0x0f); // ~~
    : if(j*2<width) //ok so put the first pixel read on screen
    : putpixel((j)*2,(height-1-i),getcol(ch1));
    : if(j*2+1<width) //put the second pixel read on screen
    : putpixel((j)*2+1,(height-1-i),getcol(ch2));
    : }
    : fclose(ptr); //close the file handle
    : return 1;
    : }
    :
    :
    :
    : int getcol(int col)
    : {
    : switch(col)
    : {
    : case 0: return 0; //BLACK;
    : case 1: return 4; //RED;
    : case 2: return 2; //GREEN
    : case 3: return 6; //BROWN
    : case 4: return 1; //BLUE;
    : case 5: return 5; //MAGENTA;
    : case 6: return 3; //CYAN;
    : case 7: return 7; //LIGHTGRAY;
    : case 8: return 8; //DARKGRAY;
    : case 9: return 12; //LIGHTRED;
    : case 10:return 10; //LIGHTGREEN
    : case 11:return 14; //YELLOW;
    : case 12:return 9; //LIGHTBLUE;
    : case 13:return 13; //LIGHTMAGENTA
    : case 14:return 11; //LIGHTCYAN;
    : case 15:return 15; //WHITE;
    : }
    : return col;
    : }[/code]:
    Good luck,
    bilderbikkel
  • bushxeebushxee Posts: 16Member
    : Easy:
    :
    : : [code]: : #include
    : : #include
    : : #include
    : :
    : : int loadbitmap(char*);
    : : int getcol(int);
    : : [red]int[/red] main (void)
    : : {
    : : clrscr();
    : : printf("Loading bitmap demo");
    : : loadbitmap([red]"MyBitmap.bmp"[/red]);
    : : getch();
    : : }


    :
    : : int loadbitmap(char *filename)
    : : {
    : : FILE *ptr=NULL; //file handle to open bitmap file
    : : int width,height; //width and height of the bitmap
    : : unsigned long temp=0,i=0,j=0; //some variables i need
    : : unsigned long ww;
    : : ptr=fopen(filename,"rb"); //open the bitmap file
    : : if(!ptr) return 0; //if its not there return
    : : width=0;height=0; //initialise wisth and height to zero
    : : fseek(ptr,18,SEEK_SET); //got to offset 18 in file
    : : for(i=0x1;i<=0x10000;i*=0x100) //read the width
    : : {
    : : temp=fgetc(ptr);
    : : width+=(temp*i);
    : : }
    : : fseek(ptr,22,SEEK_SET); //go to offset 22 in file
    : : for(i=0x1;i<=0x10000;i*=0x100) //read the height
    : : {
    : : temp=fgetc(ptr);
    : : height+=(temp*i);
    : : }
    : :
    : : ww=width; //ww is the number of reads to make for each horizontal line
    : : if(ww%2) //ww has to be half of width - since each pixel is only 4 bits of information
    : : ww++;ww/=2; //just getting the correct value
    : : if(ww%4) //now - ww is stored as sets of 4 pixels each - i think !?! :so this is the adjustment made
    : : ww=(ww/4)*4+4; //if width is less than ww*2 we ignore what we read
    : :
    : : fseek(ptr,119,SEEK_SET); //Ok! offset 119 - lets read the pixels -
    : : //remember the bitmap is stroed up - side - down
    : : int ch,ch1,ch2;
    : : for(i=0;i<height;i++)
    : : for(j=0;j<ww;j++)
    : : {
    : : ch=fgetc(ptr); //each character read is 2 pixels - yes 4 bits per pixel - so 16 colors
    : : ch1=ch;ch2=ch; //find those colors using bitwise ops
    : : ch1=ch1&(0xf0);ch1=ch1>>4; // ~~
    : : ch2=ch2&(0x0f); // ~~
    : : if(j*2<width) //ok so put the first pixel read on screen
    : : putpixel((j)*2,(height-1-i),getcol(ch1));
    : : if(j*2+1<width) //put the second pixel read on screen
    : : putpixel((j)*2+1,(height-1-i),getcol(ch2));
    : : }
    : : fclose(ptr); //close the file handle
    : : return 1;
    : : }
    : :
    : :
    : :
    : : int getcol(int col)
    : : {
    : : switch(col)
    : : {
    : : case 0: return 0; //BLACK;
    : : case 1: return 4; //RED;
    : : case 2: return 2; //GREEN
    : : case 3: return 6; //BROWN
    : : case 4: return 1; //BLUE;
    : : case 5: return 5; //MAGENTA;
    : : case 6: return 3; //CYAN;
    : : case 7: return 7; //LIGHTGRAY;
    : : case 8: return 8; //DARKGRAY;
    : : case 9: return 12; //LIGHTRED;
    : : case 10:return 10; //LIGHTGREEN
    : : case 11:return 14; //YELLOW;
    : : case 12:return 9; //LIGHTBLUE;
    : : case 13:return 13; //LIGHTMAGENTA
    : : case 14:return 11; //LIGHTCYAN;
    : : case 15:return 15; //WHITE;
    : : }
    : : return col;
    : : }[/code]: :
    : Good luck,
    : bilderbikkel



    thanx but this code is still not giving the image on the screen.. whats the problem???
  • luckyayush02luckyayush02 Posts: 1Member
    use initgraph to intialize graph inside main
  • arronleearronlee Posts: 34Member

    You can also load images with the help of some 3rd party image loaders. I am also testing abaout the related projects these days. We can aommunicate about it later.

  • tienkhoanguyentienkhoanguyen houPosts: 158Member

    /* God, Jesus Christ, is number one! Only God is good!!!!!!!!!!!! / / Honour to my father, Nguyen Binh Thuy Honour to my mother, Huong Thi Vu

    Loving my brother, Carl Anh Cuong Cao Vu

    Created by: Nguyen Khoa Tien / / Thank you Jesus Christ. */

    include<stdio.h>

    include<stdlib.h>

    include<conio.h>

    include <graphics.h>

    int main() { int x = 1, y = 1, d = 1, yd = 1, p=12000; int ixstart, ixlength, ixcurrent, ixend; int iystart, iylength, iycurrent, iyend; int bxstart, bxlength, bxcurrent, bxend; int bystart, bylength, bycurrent, byend; int pixcolor; int bit128, bit64, bit32, bit16; int bit8, bit4, bit2, bit1; int labour;

    FILE *ifp; int data, data2;

    int i = 0, i2 = 0;

    int gd=DETECT, gm;

    initgraph(&gd,&gm,"");

    ifp = fopen("JC.bmp", "r");

    i = 0; preload:; i++; if (i < 14) { data = fgetc(ifp); goto preload; }

    i = 0; ScanIt:;

    if (i <= 102) { data = fgetc(ifp); /* data2 = fgetc(ifp); / / if (data == 0 && data2 == 0) { i = 0; i2--; goto ScanIt; } */

    bit128 = 0; bit64 = 0; bit32 = 0; bit16 = 0; bit8 = 0; bit4 = 0; bit2 = 0; bit1 = 0; labour = data; if (labour >= 128) { bit128 = 1; labour = labour - 128; } if (labour >= 64) { bit64 = 1; labour = labour - 64; } if (labour >= 32) { bit32 = 1; labour = labour - 32; } if (labour >= 16) { bit16 = 1; labour = labour - 16; } if (labour >= 8) { bit8 = 1; labour = labour - 8; } if (labour >= 4) { bit4 = 1; labour = labour - 4; } if (labour >= 2) { bit2 = 1; labour = labour - 2; } if (labour >= 1) { bit1 = 1; labour = labour - 1; }

    putpixel(350+i, 200+i2, bit18+bit24+bit42+bit81);

    putpixel(350+i+1, 200+i2, bit18+bit324+bit642+bit1281-1);

    /* printf(" %d", data); */ i = i + 2; goto ScanIt; }

    if (i2 >= -100) { i = 0; i2--; goto ScanIt; }

    /* cleardevice(); */ REPEAT:; if (d == 1 && x < 50) { x++; } if (x >= 50) { d = 0; } if (d == 0 && x > 1) { x--; } if (x <= 1) { d = 1; }

    if (yd == 1 && y < 55) { y++; } if (y >= 55) { yd = 0; } if (yd == 0 && y > 1) { y--; } if (y <= 1) { yd = 1; }

    settextstyle(1, 0, 12); setcolor(random(12)); outtextxy(100, 350, "Jesus!");

    p = 1; PauseIt:; p--; if (p > 1) { goto PauseIt;

    }

    MovePicToBuf:;

    ixstart = 349; ixlength = 105; ixend = ixstart + ixlength; ixcurrent = ixstart;

    iystart = 98; iylength = 102; iyend = iystart + iylength; iycurrent = iystart;

    bxstart = x; bxlength = ixlength; bxend = bxstart + bxlength; bxcurrent = bxstart;

    bystart = y; bylength = iylength; byend = bystart + bylength; bycurrent = bystart;

    MovRtn:;

    pixcolor = getpixel(ixcurrent, iycurrent); if (ixcurrent <= ixend) { ixcurrent++; goto PutIt; }

    ixcurrent = ixstart;

    if (iycurrent <= iyend) { iycurrent++; goto PutIt; } goto EnlesLop;

    PutIt:; putpixel(bxcurrent, bycurrent, pixcolor);

    if (bxcurrent <= bxend) { bxcurrent++; goto MovRtn; }

    if (bycurrent <= byend) { bxcurrent = bxstart; bycurrent++; goto MovRtn; }

    EnlesLop:; goto REPEAT;

    }

  • tienkhoanguyentienkhoanguyen houPosts: 158Member

    @tienkhoanguyen said: /* God, Jesus Christ, is number one! Only God is good!!!!!!!!!!!! / / Honour to my father, Nguyen Binh Thuy Honour to my mother, Huong Thi Vu

    Loving my brother, Carl Anh Cuong Cao Vu

    Created by: Nguyen Khoa Tien / / Thank you Jesus Christ. */

    include<stdio.h>

    include<stdlib.h>

    include<conio.h>

    include <graphics.h>

    int main() { int x = 1, y = 1, d = 1, yd = 1, p=12000; int ixstart, ixlength, ixcurrent, ixend; int iystart, iylength, iycurrent, iyend; int bxstart, bxlength, bxcurrent, bxend; int bystart, bylength, bycurrent, byend; int pixcolor; int bit128, bit64, bit32, bit16; int bit8, bit4, bit2, bit1; int labour;

    FILE *ifp; int data, data2;

    int i = 0, i2 = 0;

    int gd=DETECT, gm;

    initgraph(&gd,&gm,"");

    ifp = fopen("JC.bmp", "r");

    i = 0; preload:; i++; if (i < 14) { data = fgetc(ifp); goto preload; }

    i = 0; ScanIt:;

    if (i <= 102) { data = fgetc(ifp); /* data2 = fgetc(ifp); / / if (data == 0 && data2 == 0) { i = 0; i2--; goto ScanIt; } */

    bit128 = 0; bit64 = 0; bit32 = 0; bit16 = 0; bit8 = 0; bit4 = 0; bit2 = 0; bit1 = 0; labour = data; if (labour >= 128) { bit128 = 1; labour = labour - 128; } if (labour >= 64) { bit64 = 1; labour = labour - 64; } if (labour >= 32) { bit32 = 1; labour = labour - 32; } if (labour >= 16) { bit16 = 1; labour = labour - 16; } if (labour >= 8) { bit8 = 1; labour = labour - 8; } if (labour >= 4) { bit4 = 1; labour = labour - 4; } if (labour >= 2) { bit2 = 1; labour = labour - 2; } if (labour >= 1) { bit1 = 1; labour = labour - 1; }

    putpixel(350+i, 200+i2, bit18+bit24+bit42+bit81);

    putpixel(350+i+1, 200+i2, bit18+bit324+bit642+bit1281-1);

    /* printf(" %d", data); */ i = i + 2; goto ScanIt; }

    if (i2 >= -100) { i = 0; i2--; goto ScanIt; }

    /* cleardevice(); */ REPEAT:; if (d == 1 && x < 50) { x++; } if (x >= 50) { d = 0; } if (d == 0 && x > 1) { x--; } if (x <= 1) { d = 1; }

    if (yd == 1 && y < 55) { y++; } if (y >= 55) { yd = 0; } if (yd == 0 && y > 1) { y--; } if (y <= 1) { yd = 1; }

    settextstyle(1, 0, 12); setcolor(random(12)); outtextxy(100, 350, "Jesus!");

    p = 1; PauseIt:; p--; if (p > 1) { goto PauseIt;

    }

    MovePicToBuf:;

    ixstart = 349; ixlength = 105; ixend = ixstart + ixlength; ixcurrent = ixstart;

    iystart = 98; iylength = 102; iyend = iystart + iylength; iycurrent = iystart;

    bxstart = x; bxlength = ixlength; bxend = bxstart + bxlength; bxcurrent = bxstart;

    bystart = y; bylength = iylength; byend = bystart + bylength; bycurrent = bystart;

    MovRtn:;

    pixcolor = getpixel(ixcurrent, iycurrent); if (ixcurrent <= ixend) { ixcurrent++; goto PutIt; }

    ixcurrent = ixstart;

    if (iycurrent <= iyend) { iycurrent++; goto PutIt; } goto EnlesLop;

    PutIt:; putpixel(bxcurrent, bycurrent, pixcolor);

    if (bxcurrent <= bxend) { bxcurrent++; goto MovRtn; }

    if (bycurrent <= byend) { bxcurrent = bxstart; bycurrent++; goto MovRtn; }

    EnlesLop:; goto REPEAT;

    }

    I am the developer of this version. It took me working night and day to come up with my own routine. Anyways, the color may be off because my bit processing is done manually. It should work in Turbo C as well. You do need the library and other files that go with Turbo C. However, Turbo C is free and is only a few seconds to download. I hope you enjoy. I enjoy everyone helping me out too. Thank you for your help. I like the above app however it would not work for me. So I made my own and it has been tested on Microsoft Windows XP (32-bit).

    Please just change JC.bmp to your bitmap file you want to use. This app loads a bitmap file in this case JC.bmp and moves it around in a small section.

    -Tien Nguyen

«1
Sign In or Register to comment.