How can I increase the image of bmp using c code only?

the command will be
resize n small.bmp large.bmp
where n is the multiple of original image
like 2, 4, 100 etc.
/****************************************************************************
 
 *
 * BMP-related data types based on Microsoft's own.
 ***************************************************************************/
 
 
/* 
 * Common Data Types 
 *
 * The data types in this section are essentially aliases for C/C++ 
 * primitive data types.
 *
 * Adapted from http://msdn.microsoft.com/en-us/library/cc230309.aspx.
 */
 
typedef unsigned char   BYTE;
typedef unsigned long   DWORD;
typedef long            LONG;
typedef unsigned short  WORD;
 
 
/*
 * BITMAPFILEHEADER
 *
 * The BITMAPFILEHEADER structure contains information about the type, size,
 * and layout of a file that contains a DIB [device-independent bitmap].
 *
 * Adapted from http://msdn2.microsoft.com/en-us/library/ms532321.aspx.
 */
 
typedef struct 
{ 
    WORD   bfType; 
    DWORD  bfSize; 
    WORD   bfReserved1; 
    WORD   bfReserved2; 
    DWORD  bfOffBits; 
} __attribute__((__packed__)) 
BITMAPFILEHEADER; 
 
 
/*
 * BITMAPINFOHEADER
 *
 * The BITMAPINFOHEADER structure contains information about the 
 * dimensions and color format of a DIB [device-independent bitmap].
 *
 * Adapted from http://msdn2.microsoft.com/en-us/library/ms532290.aspx.
 */
       
typedef struct
{
    DWORD  biSize; 
    LONG   biWidth; 
    LONG   biHeight; 
    WORD   biPlanes; 
    WORD   biBitCount; 
    DWORD  biCompression; 
    DWORD  biSizeImage; 
    LONG   biXPelsPerMeter; 
    LONG   biYPelsPerMeter; 
    DWORD  biClrUsed; 
    DWORD  biClrImportant; 
} __attribute__((__packed__))
BITMAPINFOHEADER; 
 
 
/* 
 * RGBTRIPLE
 *
 * This structure describes a color consisting of relative intensities of
 * red, green, and blue.
 *
 * Adapted from http://msdn.microsoft.com/en-us/library/aa922590.aspx.
 */
 
typedef struct
{
    BYTE  rgbtBlue;
    BYTE  rgbtGreen;
    BYTE  rgbtRed;
} __attribute__((__packed__))
RGBTRIPLE;
/****************************************************************************
 
 *
 
 * Copies a BMP piece by piece
 ***************************************************************************/
       
#include <stdio.h>
#include <stdlib.h>
 
#include "bmp.h"
 
 
int
main(int argc, char *argv[])
{
    // ensure proper usage
    if (argc != 4)
    {
        printf("Usage: resize number infile outfile\n");
        return 1;
    }
 
    // remember filenames
    int number = atoi(argv[1]);
    char *infile = argv[2];
    char *outfile = argv[3];
 
    // make sure it's postive number
    if (number < 0)
    {
        printf("Please enter a postive number\n");
        return 1;
    }
    // open input file 
    FILE *inptr = fopen(infile, "r");
    if (inptr == NULL)
    {
        printf("Could not open %s.\n", infile);
        return 2;
    }
 
    // open output file
    FILE *outptr = fopen(outfile, "w");
    if (outptr == NULL)
    {
        fprintf(stderr, "Could not create %s.\n", outfile);
        return 3;
    }
 
    // read infile's BITMAPFILEHEADER
    BITMAPFILEHEADER bf;
    fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
 
    // read infile's BITMAPINFOHEADER
    BITMAPINFOHEADER bi;
    fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
 
    // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
    if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 || 
        bi.biBitCount != 24 || bi.biCompression != 0)
    {
        fprintf(stderr, "Unsupported file format.\n");
        return 4;
    }
 
    // write outfile's BITMAPFILEHEADER
    fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
    printf("before bi.biWidth=%ld\n", bi.biWidth);
    bi.biWidth=bi.biWidth * number;     
    printf("after bi.biWidth=%ld\n", bi.biWidth);
    printf("before bi.biHeight=%ld\n", bi.biHeight);
    bi.biHeight=bi.biHeight * number; 
    printf("after bi.biHeight=%ld\n", bi.biHeight);
    printf("before bi.biSizeImage=%ld\n", bi.biSizeImage);
    bi.biSizeImage=bi.biSizeImage * number * number;
    printf("after bi.biSizeImage=%ld\n", bi.biSizeImage);
    // write outfile's BITMAPINFOHEADER
    fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
    
    unsigned char * buffer[1048576]={' '};
//    fread(&buffer, sizeof(RGBTRIPLE), 1, inptr);
   
 
         
    // determine padding for scanlines
    int padding =  ((4  - (bi.biWidth * sizeof(RGBTRIPLE)/number) % 4) * 
    (4  - (bi.biHeight * sizeof(RGBTRIPLE)/number) % 4)) % 4 ;
    printf("padding=%d\n", padding);
 
    printf("before bi.biHeight=%ld\n", bi.biHeight);
    //    bi.biHeight=bi.biHeight * number;
    //        printf("after bi.biHeight=%ld\n", bi.biHeight);
    
    // iterate over infile's scanlines
    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight/number; i++)
    {
        RGBTRIPLE triple;
 
        printf("i=%d\n",i);
//        printf("j=%d\n",j);
        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
        for (int m=0; m < number; m++)
        {
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            printf("outside triple.rgbtBlue=%x\n",triple.rgbtBlue);
            printf("outside triple.rgbtGreen=%x\n",triple.rgbtGreen);
            printf("outside triple.rgbtRed=%x\n",triple.rgbtRed);
        }
        // iterate over pixels in scanline
        for (int j = 0; j < bi.biWidth/number; j++)
        {
        //    printf("i=%d\n",i);
            printf("j=%d\n",j);
            // temporary storage
          //  RGBTRIPLE triple;
 
            // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
    //        buffer=triple;
      //      printf("buffer=%x\n",buffer);
            // write RGB triple to outfile
            for (int l=0; l < number; l++)
            {
               fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
               printf("inside triple.rgbtBlue=%x\n",triple.rgbtBlue);
               printf("inside triple.rgbtGreen=%x\n",triple.rgbtGreen);
               printf("inside triple.rgbtRed=%x\n",triple.rgbtRed);
            }
        }
      //  for (int m=0; m < number; m++)
      //  {
      //      fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
       //     printf("outside triple.rgbtBlue=%x\n",triple.rgbtBlue);
       //     printf("outside triple.rgbtGreen=%x\n",triple.rgbtGreen);
       //     printf("outside triple.rgbtRed=%x\n",triple.rgbtRed);
      //  }
        // skip over padding, if any
        fseek(inptr, padding, SEEK_CUR);
 
        // write padding to outfile
        for (int k = 0; k < padding; k++)
            fputc(0x00, outptr);
    }
 
    // close infile
    fclose(inptr);
 
    // close outfile
    fclose(outptr);
 
    // that's all folks
    return 0;
}

Open in new window

small.bmp
codedreamerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jgordosCommented:
This looks like a homework problem, so I'm not allowed to answer directly....

Your code looks pretty decent... ie, it looks to me like you can read the bitmap file, and then write what you read in...

So, now, you need to be thinking about what "some multiple of n" means, and how you might accomplish your goal...

So, a bitmap file that looks like

1234
4568
90AB
CDEF

that gets doubled in size, really means that you double the horizontal pixels, and you double the vertical scanlines, too...

11223344
11223344
44556688
44556688
9900AABB
9900AABB
CCDDEEFF
CCDDEEFF

So, think about how you would accomplish that, using techniques you already know... things like arrays maybe..

-john



12
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
codedreamerAuthor Commented:
I agree with your desing approach.  However, I am stuck on the code that I wrote.  I am not sure how come the vertical does not come out correctly.  Could you please debug the code for me?  Thank you.
0
jgordosCommented:
Dude, I can't debug your code for you.  This is YOUR homework, not mine.

I can offer the following hint...  Start with an extremely small bitmap, say a 1x1, and step thru your code line by line.

-john
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

codedreamerAuthor Commented:
I already turned in the homework.
I was wondering if you can walk through the code and see what's wrong with it so I can improve on it next time.
I am new to this website.  
If there is no code walk through, I am not sure why I need to join this website for support.
0
jgordosCommented:
I can help you in lots of ways without actually writing the code, or debugging your program.

For instance, in your scan line stuff....

I don't see anywhere where you store the scan line you just read, into an array, or a buffer anywhere.

How will you repeat that line 'n' times without such a structure?

-john
0
jgordosCommented:
This shouldn't be deleted. It should be closed with points assigned.
0
jgordosCommented:

Was this comment helpful? Yes No
jgordos:
This shouldn't be deleted. It should be closed with points assigned.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Game Programming

From novice to tech pro — start learning today.

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.