0

so im trying to resize an image of green square that has a smaller white square inside it and when i do the resizing the green square resize but the white one disappears i'dont know where the problem is i think their is something wrong with the horizontal and vertical resizeing

#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>

#include "bmp.h"

int main(int argc , char *argv[])
{
// ensure proper usage
if (argc != 4)
{
    fprintf(stderr, "Usage: ./resize f infile outfile\n");
    return 1;
}

// remember filenames
char *infile = argv[2];
char *outfile = argv[3];
char *f        = argv[1];
double  change = atof (f); 
float key = (float) change ;

// open input file
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
    fprintf(stderr, "Could not open %s.\n", infile);
    return 2;
}

// open output file
FILE *outptr = fopen(outfile, "w");
if (outptr == NULL)
{
    fclose(inptr);
    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)
{
    fclose(outptr);
    fclose(inptr);
    fprintf(stderr, "Unsupported file format.\n");
    return 4;
}
 int keyp = (int) key;

    // determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int padding2 = (4 - (bi.biWidth *keyp * sizeof(RGBTRIPLE)) % 4) % 4;

bi.biWidth *= key ;
bi.biHeight *= key ;
bi.biSizeImage =((bi.biWidth * sizeof(RGBTRIPLE))+(padding2))* 
(abs(bi.biHeight)) ;
bf.bfSize = bi.biSizeImage + sizeof(BITMAPINFOHEADER) + 
sizeof(BITMAPFILEHEADER);



// write outfile's BITMAPFILEHEADER


fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);

// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);


for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
 {
 RGBTRIPLE triple;

for (int o=0 ; o< bi.biWidth/key ; o++)
{


fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

}
// skip over padding, if any
    fseek(inptr, padding, SEEK_CUR);

    for(int r = 0; r< bi.biWidth ;r++)
    {
        // temporary storage

     fwrite(&triple, sizeof(RGBTRIPLE),1 , outptr);
     }

     // then add it back (to demonstrate how)
    for (int k = 0; k < padding2; k++)
    {
        fputc(0x00, outptr);
    }
 }  



// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;
}

so this is the code after trying to do what you said

for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{

  for (int o=0 ; o< bi.biWidth/key ; o++)
  { 
    RGBTRIPLE triple;

    fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

    for(int r = 0; r< key ;r++)
    {

    fwrite(&triple, sizeof(RGBTRIPLE),1 , outptr);

    }

  } 

    // skip over padding, if any
    fseek(inptr, padding, SEEK_CUR);

   // then add it back 
    for (int k = 0; k < padding2; k++)
    {
        fputc(0x00, outptr);
    }
 }  

so here is the downsizing section

else
{

for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i ++)
{
  for (int o=0 ; o< bi.biWidth/key ; o++)
  {
    RGBTRIPLE triple;

    fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

    fwrite(&triple, sizeof(RGBTRIPLE), 1 , outptr);

    for(int h=0 ; h<(key-1); h++)
 {
    fseek(inptr, sizeof(RGBTRIPLE),SEEK_CUR);
    o++ ;
   }
 }


 for(int l =0 ; l<(key-1);l++)
 {
   fseek(inptr,((bi.biWidth/key) * sizeof(RGBTRIPLE) + padding),SEEK_CUR );
    i++ ;
 }

// skip over padding, if any
 fseek(inptr, padding, SEEK_CUR);


// then add it back
for (int k = 0; k < padding2; k++)
{
    fputc(0x00, outptr);
}


  }
 }
1

Let's look at the code that actually processed the image data. (I've refined the style a little to clean it up, but the code is the same.)

for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    RGBTRIPLE triple;

    for (int o=0 ; o< bi.biWidth/key ; o++)
    {
        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
    }
    // skip over padding, if any
    fseek(inptr, padding, SEEK_CUR);

    for(int r = 0; r< bi.biWidth ;r++)
    {
        // temporary storage
        fwrite(&triple, sizeof(RGBTRIPLE),1 , outptr);
    }

    // then add it back (to demonstrate how)
    for (int k = 0; k < padding2; k++)
    {
        fputc(0x00, outptr);
    }
} 

Here's how it actually works. The outer i loop causes the inner code to be processed once for each line. OK so far.

The o-loop causes the input file to read a triple and store it in at the triple address, then repeats this over and over for the width of the input line. That means that after the loop finishes, it has read and discarded all but the last triple from the input line. Only the last triple remains to be processed. All the previous triples have been overwritten.

The remaining code skips over input padding, writes out r copies of the triple that was last read (same logic as the read loop), and writes padding.

The code needs to write out each triple before reading the next one. It also needs to make sure it writes it out enough times for EACH triple.

Looks like a partial rewrite is in order. ;-)

Programming notes:
While the code works, the handling of the scaling factor is not efficient.

char *f        = argv[1];
double  change = atof (f); 
float key = (float) change ;
...
int keyp = (int) key;

There are 4 variables to contain the same value. It'd be a lot more efficient to just use it once. It's standard practice to copy an argv value to a var with a meaningful name, but not to multiple vars - unless they will be modified and the original needs to be retained. In this case, argv[1] could be moved directly into an int.

float key = atof(argv[1]);

All the extra steps and vars can be eliminated. There's no need for them and they'll just cause confusion to anyone trying to read the code.

Finally, make use of style50 until you are consistently writing code that is stylistically correct, especially indenting standards.

If this answers your question, please click on the check mark. Let's keep up on forum maintenance. ;-)

5
  • Can't tell what's going on without seeing the entire block of code for processing the image data. You could edit the question and add the modified code. Please don't change the original code. It would confuse anyone that might read the question and try to understand the answer later. But, have you worked through exactly what it is doing now, and have you thought about why it is doing so, and how it is different from what it should be doing?
    – Cliff B
    Jul 8 '19 at 22:17
  • so i edited the question with the code that i wrote trying to do what you said but there is something wrong @Cliff B
    – rashad
    Jul 8 '19 at 22:47
  • 1
    Can you describe what's wrong? Have you looked at the raw image data? What test data have you used? What scaling factors have you tested? I already know the issue, but it's far more important that you discover the problem. You should at least be able to identify and describe the problem in detail. Hint: what scaling factor are you using? Have you verified that a scaling factor of 1 works correctly?
    – Cliff B
    Jul 8 '19 at 23:29
  • so i'm almost done with my resize pset but i have a problem with the downsizing i can't figure out where the problem is or how to fix it can you hepl me please thanks
    – rashad
    Jul 21 '19 at 20:54
  • so i edited the post to show you my downsizing coding when i try to run this code the image comes out with random colors @CliffB
    – rashad
    Jul 21 '19 at 21:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .