[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 643
  • Last Modified:

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
0
zuppy
Asked:
zuppy
  • 6
  • 3
1 Solution
 
scrapdogCommented:
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;
        }
}

scrappy
0
 
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;
   quanto=0;
   uguaglianza=0;
   end_of_file=false;
   d=(info_header.biWidth*info_header.biHeight);
   oldbitmapptr1=(char *)malloc(d*sizeof(char));
   d=(info_header2.biWidth*info_header2.biHeight);
   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++)
          {
          c=fgetc(file_pointer);
          if (c<127) c=0;
          if (c>=127) c=1;
          *(oldbitmapptr1+zz+z)=c;
          };
        };
    for (z=0;z<=info_header2.biWidth;z++)
        {
        for (zz=0;zz<=info_header2.biHeight;zz++)
          {
          c=fgetc(file_pointer2);
          if (c<127) c=0;
          if (c>=127) c=1;
          *(oldbitmapptr2+zz+z)=c;
          };
        };

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


Thanks  by now


Zuppy

0
 
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).

zuppy

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
scrapdogCommented:
Could you please clarify the question?  What is/isn't working?
0
 
zuppyAuthor Commented:
Ok....
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.

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

command file1 file2

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

zuppy

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.
0
 
zuppyAuthor Commented:
Hoping u can continue help me... :)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now