Like Tree2Likes
  • 2 Post By claudiu

program to display an image in dos screen

This is a discussion on program to display an image in dos screen within the C Programming forums, part of the General Programming Boards category; i tried the following program given below to load and display a bmp file in a dos screen but it ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    7

    program to display an image in dos screen

    i tried the following program given below to load and display a bmp file in a dos screen but it isn't working (image is not shown on screen) whats the problem???????
    Code:
    #include <dos.h>
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    #include <windows.h>
    
     char far  *Screen;
    
    /* Sets the display to VGA 320x200x256 */
     void VGAScreen(void)
      {
        union REGS r;
    
        r.h.ah = 0;
        r.h.al = 0x13;
        int86(0x10, &r, &r);
      return;
      }
    
    /* Goes back to textmode */
    void TextScreen(void)
    {
      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(void)
    {
      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 = "rck.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);
    }
    image file was a 8 bit image file

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,442
    So which windows based compiler did you use to compile this 20+ year old fossil code?

    If you want to compile this code, you need an actual DOS compiler, which generates DOS executable files, which will enable windows to load the DOS VM to at least give you a chance of running this.

    A windows compiler will generate win32 executables, and you just can't point at memory and go poking around with the likes of outport.

    On the other hand, why are you trying this anyway - use the win32 API to display an image and be done with the sorry mess that was DOS.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    7
    thanks 4 ur reply.
    my requirement is to load and display a 600*420 bmp file and program code has to be in c.
    where can i get a sample code . can u help??????????

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,442
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    7
    yes of course
    if u can please guide me

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,294
    Code:
    /* Sets the display to VGA 320x200x256 */
    display a 600*420 bmp file
    Can you not see an obvious problem there?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,442
    The poster refuses to say which compiler they're using (on any forum), and also refuses to be more specific than "I got some errors".
    This is just too much effort to be bothered with.

    > my requirement is to load and display a 600*420 bmp file and program code has to be in c.
    ...
    > /* Sets the display to VGA 320x200x256 */
    I see you've basically taken lightatdawn's post from 2005 and changed a couple of function names.

    First of all, BMP has a multitude of sub-formats (BMP file format - Wikipedia, the free encyclopedia), of which that code will only deal with one of those (and for small images). In the short term, you're going to be stuck with the 8-bit paletted BMP files.

    Second, if you really want to do this on DOS, then you're going to have to make some code to approximate your image.
    In this case, you're going to have to scale 600*420 down to 320x200
    Dividing both by 2 gives you 300x210 (if you don't mind losing a few rows of pixels top and bottom), and makes a first cut of the code rather easy.
    For each 2x2 pixel block in the image file, say
    AB
    CD
    you just display A
    That is, on an even row, you just take every other pixel.
    And each odd row, you ignore completely.

    Or you just throw that DOS crap in the bin, get a win32 compiler and use various APIs to just load and display ANY BMP file right off the bat. It's more work to display an image to begin with, but you'll have a lot more functionality when you're done (larger images, more BMP formats supported, better results).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    7

    steps to be followed for manualy loading and displaying a bmp file

    i want to display a bmp file of resolution 600*420 (using c compiler)
    by manual loading
    i tried to display the image by loading the pixel value. but this method has a limitation that it take up pixel value of image upto 64kb size.
    so now i am trying to load it manually.
    what are the steps that i has to follow, what are the header files that i has to use ?
    my system specification are windows xp professional , intel pentium 4 cpu.
    i don't know where to start ,does this method has any limitaton ,...etc
    somebody please help.


  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    7
    i want to display a bmp file of resolution 600*420 (using c compiler)
    by manual loading
    i tried to display the image by loading the pixel value. but this method has a limitation that it take up pixel value of image upto 64kb size.
    so now i am trying to load it manually.
    what are the steps that i has to follow, what are the header files that i has to use ?
    my system specification are windows xp professional , intel pentium 4 cpu.
    i don't know where to start ,does this method has any limitaton ,...etc
    compiler that we are using are code block c compiler
    somebody please help.
    Last edited by crazy4; 05-25-2012 at 02:27 AM.

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    7
    i want to display a bmp file of resolution 600*420 (using c compiler)
    by manual loading
    i tried to display the image by loading the pixel value. but this method has a limitation that it take up pixel value of image upto 64kb size.
    so now i am trying to load it manually.
    what are the steps that i has to follow, what are the header files that i has to use ?
    my system specification are windows xp professional , intel pentium 4 cpu.
    i don't know where to start ,does this method has any limitaton ,...etc
    compiler that i am using is turboc and codeblock
    somebody please help.

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    ...... psycho ?! .....
    GReaper and manasij7479 like this.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  12. #12
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,169
    O_o

    This guy seems stuck in a loop.
    This guy seems stuck in a loop.
    This guy seems stuck in a loop.

    Soma

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,442
    Go here -> Simple DirectMedia Layer
    Download SDL-devel-1.2.15-mingw32.tar.gz (Mingw32), since you're using code::blocks on windows

    Then read this -> Using SDL: Video (or Simple DirectMedia Layer and choose a language)
    Pretty soon, you'll come across how to load a BMP
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to display an image?
    By byebyebyezzz in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2011, 06:40 PM
  2. SDL wont display .png image
    By bijan311 in forum C++ Programming
    Replies: 5
    Last Post: 04-26-2010, 06:55 AM
  3. display image name
    By shamee_banu in forum Windows Programming
    Replies: 1
    Last Post: 06-29-2007, 12:13 PM
  4. 8-bit image display
    By rakesh09 in forum C Programming
    Replies: 1
    Last Post: 03-20-2003, 06:28 PM
  5. Display a bitmap image
    By MysticMizra in forum Windows Programming
    Replies: 7
    Last Post: 10-21-2002, 03:36 AM

Tags for this Thread


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