• C

BMP resizing ANSI C code req.

I'd like 2 have the sourcecode ( in ANSI C) to handle a bmp and resize it.
For ex. I have a 500x234 bmp and I want to do a 80x80 bmp (the same that Lview do with the RESIZE OPTION)
Only ANSI C code, not exe files.

please mail to mauro@s715.ing.unibo.it
Who is Participating?

Improve company productivity with a Business Account.Sign Up

scrapdogConnect With a Mentor Commented:
Here is a function that will take any array and resize it up or down onto another.  It assumes that all your memory is allocated.  If you wanted to copy a 500x234 bitmap onto a 80x80 bitmap while resizing it, you would call it like this:

resize_bmp(oldbitmapptr, 500,234, newbitmapptr, 80,80);

where oldbitmapptr and newbitmapptr are pointers to arrays of characters.  Remember, the memory must have been previously allocated before calling this.

It is assumes that the data is stored in a two dimensional array like this:  

char bitmap[y_size][x_size]

i.e. a 500x234 bitmap would be stored as an array of 234 of arrays of 500 chars.  If you need to change this, you could easily alter the code.

void resize_bmp(char *bmpold, int oldx, int oldy,
                               char *bmpnew, int newx, int newy)
      float scalex = oldx/newx;
      float scaley = oldy/newy;
      char *newpixel, *oldpixel;
      int pixelx, pixely, oldpixelx, oldpixely, x, y;
      for (x=0; x<(newx-1); x++)
        for (y=0; y<(newy-1); y++) {
             oldpixelx = x * scalex;  pixelx = (int)oldpixelx;
             if ((pixelx-oldpixelx)>=.5) pixelx++;
             oldpixely = y * scaley;  pixely = (int)oldpixely;
             if ((pixely-oldpixely)>=.5) pixely++;
             if (pixelx>(oldx-1)) pixelx=oldx-1;
             if (pixely>(oldy-1)) pixely=oldy-1;
             newpixel=(char *)(bmpnew + y*newx + x);
             oldpixel=(char *)(bmpold + pixely*oldx + pixelx);
             *newpixel = *oldpixel;

zuppyAuthor Commented:
Here my part of code...(note: it handle 2 "256 grayscale" BMP at once and produce 2 "1 bit color" BMP )
After this I shoud have my new Immage (80x80) in the arrays (newbitmapptr1 & newbitmapptr2 )but it seems not.
Are U sure 4 Your code?
Assume info_header.xx are done.

char *oldbitmapptr1;
char *newbitmapptr1;
char *oldbitmapptr2;
char *newbitmapptr2;

unsigned i;
   int z;
   int zz;
   int c;  
   int d;
   int uguaglianza;
   int quanto;
   oldbitmapptr1=(char *)malloc(d*sizeof(char));
   oldbitmapptr2=(char *)malloc(d*sizeof(char));
   newbitmapptr1=(char *)malloc(6400*sizeof(char));
   newbitmapptr2=(char *)malloc(6400*sizeof(char));

    for (z=0;z<=info_header.biWidth;z++)
        for (zz=0;zz<=info_header.biHeight;zz++)
          if (c<127) c=0;
          if (c>=127) c=1;
    for (z=0;z<=info_header2.biWidth;z++)
        for (zz=0;zz<=info_header2.biHeight;zz++)
          if (c<127) c=0;
          if (c>=127) c=1;

resize_bmp(oldbitmapptr1,info_header.biWidth,info_header.biHeight, newbitmapptr1,80,80);

Thanks  by now


zuppyAuthor Commented:
Sorry I made a mistake:

I've forgot to say that the 2 "1 bit" BMP means 256Gray BMP using only 2 colors (Black and White).


A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

Could you please clarify the question?  What is/isn't working?
zuppyAuthor Commented:
Running manually the resize using Lview and using my program all goes well (the programm do that it must do) but if I skip this passage implementing it in my program using your function the output isn't that I  had before.
I think allocation memory is right so U function probably does not the right resize.
My program counts how many black and how many white pixel are in the 80x80 BMP.

zuppyAuthor Commented:
I increase point to double (600) if somebody write a procedure that read and write a BMP resizing it to 80x80.

command file1 file2

A BMP, as in a standard windows BMP file?  Or just a raw bitmap?
zuppyAuthor Commented:
a standard Windows BMP file (not OS/2).


ps.:please...add commento to code(and use"easy to understand what they do" variables's names)...so , if I need I can modify to my necessity.Thanks.
zuppyAuthor Commented:
Hoping u can continue help me... :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.