Pictures?

This is a discussion on Pictures? within the C Programming forums, part of the General Programming Boards category; hey i don;t know anything about this, but can i add pictures into my program or sumthin?...

  1. #1
    oompaloompa boy
    Guest

    Unhappy Pictures?

    hey i don;t know anything about this, but can i add pictures into my program or sumthin?

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    if you want to add an Icon, that's a different story.
    However, the CPROGRAMMING team added an entry to the FAQ about adding bitmaps to programs (console)
    now, you could go program in windows for good pictures but here is a brief explanation of how it works...


    Just a few notes: I'm not going to go into great detail here.
    If you want to do this in Win32 it is easy. Check out the tutorial at http://users.ox.ac.uk/~ball0597/Refe...wsProgramming/
    If you want to do this in Console mode: You can't. Console mode won't do graphics. The End.
    If you want to do this in DOS mode. This is more complicated. Check out www.wotsit.com for information regarding picture files of all kind. The actual code to do this is semi-long. I'll put it in anyhow. But just so you're warned, this example is quite limited. Its cut down to make it easier to understand. This will load "test.bmp" (This can be changed, obviously) from the running directory. It will first initialize mode 13 (320x200 screen with 256 col) and then load the BMP. It will only load an 8-bit bmp of a size less than the screen. The code can be adapted to read other types of BMPs quite easily. Here goes!
    Code:
    #include <dos.h>
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    
    char far *Screen;
    
    //Sets the display to VGA 320x200x256
    void VGAScreen (){
      union REGS r;
      r.h.ah = 0;
      r.h.al = 0x13;
      int86(0x10, &r, &r);
      return;
    }
    
    //Goes back to textmode
    void TextScreen (){
      union REGS r;
      r.h.ah = 0;
      r.h.al = 0x3;
      int86(0x10, &r, &r);
      return;
    }
    
    //This sets a DAC register to a specific Red Green Blue-value
    int SetDAC (unsigned char DAC, unsigned char R, unsigned char G, unsigned char B)
    {
      outportb (0x3C8, DAC);
      outportb (0x3C9, R);
      outportb (0x3C9, G);
      outportb (0x3C9, B);
      return 0;
    }
    
    int LoadBMP (){
      struct BMPHeader {
      unsigned short bfType;
      long bfSize,
      bfReserved,
      bfOffBits,
      biSize,
      biWidth,
      biHeight;
      unsigned short biPlanes,
      biBitCount;
      long biCompression,
      biSizeImage,
      biXPelsPerMeter,
      biYPelsPerMeter,
      biClrUsed,
      biClrImportant;
      } Header;
      FILE *BMPFile;
    
      unsigned char c, Palette[256][4];
      char *filename = "test.bmp";
    
      unsigned int offset, lines, paddedWidth;
    
      //This checks for the file
      BMPFile = fopen (filename, "rb");
      if (BMPFile == NULL) {
    	 printf ("Cant open file.");
    	 return 1;
      }
    
      //Read the headerinformation
      fread (&Header, 54, 1, BMPFile);
    
      if (Header.bfType != 19778 || Header.bfReserved != 0 || Header.biPlanes !=1) {
    	 //Not a valid bitmap file - don't display
    	 printf ("Not a valid bitmap.");
    	 fclose (BMPFile);
    	 return 1;
      }
      if (Header.biCompression != 0) {
    	 //Compressed file - don't display
    	 printf ("Compressed file.");
    	 fclose (BMPFile);
    	 return 1;
      }
      if (Header.biBitCount != 8) {
    	 //If the file is other than 8-bit dont read.
    	 printf ("Not an 8-bit bitmap.");
    	 fclose (BMPFile);
    	 return 1;
      }
      if (Header.biWidth > 320 || Header.biHeight > 200) {
    	 //If its larger than 320*200 dont load.
    	 printf ("Size too large.");
    	 fclose (BMPFile);
    	 return 1;
      }
    
      //Load the palette info
      fread (&Palette, 1024, 1, BMPFile);
      for (c = 0; c < 255; c++)
    	 SetDAC (c, Palette[c][2] >> 2, Palette[c][1] >> 2, Palette[c][0] >> 2);
    
      offset = (100 + (Header.biHeight >> 1)) * 320 + 160 - (Header.biWidth >> 1);
    
      lines = 0;
    
      paddedWidth = Header.biWidth & 0xFFFC;
      if (Header.biWidth != paddedWidth) paddedWidth += 4;
      //Loop for reading lines
      while (lines < Header.biHeight) {
    	 fread (Screen + offset, paddedWidth, 1, BMPFile);
    	 offset -= 320;
    	 lines++;
      }
      fclose (BMPFile);
      return 0;
    }
    
    
    
    int main (int argcount, char *argvalue[]){
      //Set up a pointer in vga memory
      Screen = (char far *)0xA0000000L;
    
      VGAScreen ();
      LoadBMP();
      getch ();
      TextScreen ();
      return 0;
    }
    -Luke
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Xlib/xcb and drawing pictures
    By Bill Cosby in forum Linux Programming
    Replies: 0
    Last Post: 01-25-2009, 02:45 PM
  2. Tagging Pictures
    By Tonto in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 03-17-2008, 02:44 PM
  3. reading pictures and array
    By sunoflight77 in forum C++ Programming
    Replies: 0
    Last Post: 05-09-2005, 03:16 PM
  4. pictures
    By TravisS in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 01-06-2003, 01:31 PM
  5. How do you get the little pictures under your name?
    By sean in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-09-2001, 10:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21