Solved

BMP resizing ANSI C code req.

Posted on 1998-12-12
9
613 Views
Last Modified: 2008-03-03
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
Comment
Question by:zuppy
  • 6
  • 3
9 Comments
 
LVL 5

Accepted Solution

by:
scrapdog earned 600 total points
ID: 1255064
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
 

Author Comment

by:zuppy
ID: 1255065
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
 

Author Comment

by:zuppy
ID: 1255066
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 5

Expert Comment

by:scrapdog
ID: 1255067
Could you please clarify the question?  What is/isn't working?
0
 

Author Comment

by:zuppy
ID: 1255068
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
 

Author Comment

by:zuppy
ID: 1255069
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
 
LVL 5

Expert Comment

by:scrapdog
ID: 1255070
A BMP, as in a standard windows BMP file?  Or just a raw bitmap?
0
 

Author Comment

by:zuppy
ID: 1255071
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
 

Author Comment

by:zuppy
ID: 1255072
Hoping u can continue help me... :)
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question