0

so my program is working correctly the only problem is with the downsizing i can't make it work correctly here is my code

#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <math.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;

  bi.biWidth *= key ;
  bi.biHeight *= key ;
  int padding2 = (4 - (bi.biWidth* sizeof(RGBTRIPLE)) % 4) % 4;
  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);

if(key>=1)
{

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

{

   for(int x=0; x<(key-1);x++)
   {
      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);
   }
  fseek(inptr,-((bi.biWidth/key) * sizeof(RGBTRIPLE) + padding),SEEK_CUR );
  }
}
 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);
  }
 }
}


 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);
  }


 }
}   


  // close infile
  fclose(inptr);

  // close outfile
  fclose(outptr);

  // success
 return 0;
  }
0

I'm pretty sure in the key < 1 case, you meant to use 1 / key - 1 instead of key - 1.

Don't forget to fix that indentation, the code is pretty hard to read. Also some comments would be nice.

And of course your code wouldn't work for certain scaling factors (probably for values like 1.5, 0.333, 0.2, factor times input size not integers) due to rounding issues, but I guess you might pass check50, as it only tests for the easy cases.

You must log in to answer this question.

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