Thread: Interesting problem with bmp drawing

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    23

    Interesting problem with bmp drawing

    HI!
    I've been programming my bmp loader recently but got stuck in drawing this bmp... Everything is right for sqare bitmaps or bitmaps with height < width.
    But when bitmap height > than it's width the function draws it in a wrong way... Here's the code:

    Code:
    #include "stdio.h"
    #include "stdlib.h"
    #include "SDL.h"
    #include "bmp_load.h"
    
    //open file
    BMP::BMP(char *filename, SDL_Surface *scr)
    {
            FILE *f = fopen(filename, "rb");
            if(!f)
            {
                    printf("Couldn't open file %s\n", filename);
                    exit(1);
            }
    
            fread(&bF, sizeof(BITMAPFILEHEADER), 1, f);
            fread(&bI, sizeof(BITMAPINFOHEADER), 1, f);
    
            if(bF.bfType != BITMAP_ID)      //it's not a bitmap file
            {
                    printf("Wrong file! %s\n", filename);
                    exit(1);
            }
    
            fseek(f, bF.bfOffBits, SEEK_SET);
    
            imgData = (unsigned char*)malloc(bF.bfSize);
            fread(imgData, bF.bfSize, 1, f);
    
            screen = scr;
    
            isT = 0;
            fclose(f);
    }
    
    BMP::BMP()
    {
    
    }
    
    BMP::~BMP()
    {
            delete imgData;
    }
    
    int BMP::load(char *filename, SDL_Surface *scr)
    {
            FILE *f = fopen(filename, "rb");
            if(!f)
            {
                    printf("Couldn't open file %s\n", filename);
                    return 0;
            }
    
            fread(&bF, sizeof(BITMAPFILEHEADER), 1, f);
            fread(&bI, sizeof(BITMAPINFOHEADER), 1, f);
    
            if(bF.bfType != BITMAP_ID)      //it's not a bitmap file
            {
                    printf("Wrong file! %s\n", filename);
                    return 0;
            }
    
            fseek(f, bF.bfOffBits, SEEK_SET);
    
            imgData = (unsigned char*)malloc(bF.bfSize);
            fread(imgData, bF.bfSize, 1, f);
    
            screen = scr;
    
    	printf("Width: %d Height: %d\n", bI.biWidth, bI.biHeight);
    	
            fclose(f);
            return 1;
    }
    
    inline void BMP::draw_px(int x, int y, int r, int g, int b)
    {
            color = SDL_MapRGB(screen->format, r, g, b);
            bufp = (int*)screen->pixels+y*screen->pitch/4+x;
            *bufp = color;
    }
    
    void BMP::draw(int posX, int posY)
    {
            if(!isT)
            {
                    posY += bI.biHeight;
                    for(int j = 0; j < bI.biHeight; j++)
                            for(int i = 0; i < bI.biWidth; i++)
                                    draw_px(posX+i, posY-j, imgData[j*bI.biWidth*3+i*3+2], imgData[j*bI.biWidth*3 + i*3+1], imgData[j*bI.biWidth*3 + i*3]);
            } else { 
                    posY += bI.biHeight;
                    for(int j = 0; j < bI.biHeight; j++)
                            for(int i = 0; i < bI.biWidth; i++)
                                    if((imgData[j*bI.biWidth*3+i*3+2] != R) || (imgData[j*bI.biWidth*3+i*3+1] != G) || (imgData[j*bI.biWidth*3+i*3] != B))
                                            draw_px(posX+i, posY-j, imgData[j*bI.biWidth*3+i*3+2], imgData[j*bI.biWidth*3 + i*3+1], imgData[j*bI.biWidth*3 + i*3]);
            }
    
    
    }
    
    void BMP::setTransparent(int cR, int cG, int cB)
    {
            isT = 1;
            R = cR;
            G = cG;
            B = cB;
    }
    In my opinion the drawing function is wrong, but it could be also the loading function...
    Could you help me?

  2. #2
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    The constructor and load () functions do pretty much the same thing. Why not have the constructor call load () and have the constructor-specific code (what little there is) just there?

    Not what you wanted to hear, but worth saying nontheless...
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Did you take into account that if the width is not a multiple of 4 there are empty bytes at the end of each scanline?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    23
    Quote Originally Posted by Magos
    Did you take into account that if the width is not a multiple of 4 there are empty bytes at the end of each scanline?
    No, I didn't know it before :/
    Thanks!


    Hm... you're right... I don't know why I've rewritten that code twice instead of just calling load()
    (these are bad sides of programming at 2 a.m. :> )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  3. Interesting Multithreading Problem :: MFC
    By kuphryn in forum Windows Programming
    Replies: 4
    Last Post: 04-22-2002, 02:28 PM
  4. Interesting problem with file I/O...
    By skillet in forum C++ Programming
    Replies: 12
    Last Post: 02-20-2002, 11:14 AM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM