Solved

BMP resizing ANSI C code req.

Posted on 1998-12-12
9
610 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
 
LVL 5

Expert Comment

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

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Which version of C should I use when Perl is too slow? 13 173
Understanding the meaning of Portability in Embedded System Programming 8 128
why "." vs "->" 23 119
Computer slow / BSOD 10 42
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…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now