Skip to main content
fix code formatting, again
Source Link
Yuri Laguardia
  • 2.6k
  • 1
  • 10
  • 17

/**

  • copy.c
  • Computer Science 50
  • Problem Set 4
  • Copies a BMP piece by piece, just because. */

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

#include "bmp.h"

int main(int argc, char* argv[]) { // ensure proper usage if (argc != 4) { printf("Usage Code after suggested changes: ./copy infile outfile\n"); return 1; }

/**
 * copy.c
 *
 * Computer Science 50
 * Problem Set 4
 *
 * Copies a BMP piece by piece, just because.
 */
       
#include <stdio.h>
#include <stdlib.h>

#include "bmp.h"

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

    // remember filenames
    char* infile = argv[2];
    char* outfile = argv[3];
   
    int n = atoi(argv[1]); 
        if(n<1 || n>100)
            {
                printf("invalid factor\n");
                return 6;
            }

    // 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)
    {
        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 origwidth = bi.biWidth;
     int origheight = bi.biHeight;
     bi.biWidth = n * bi.biWidth;
     bi.biHeight = n * bi.biHeight;
   
     // determine padding for scanlines
        int padding =  (4 - (origwidth * sizeof(RGBTRIPLE)) % 4) % 4;
        int padding1 =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
     bi.biSizeImage = (bi.biWidth * sizeof(RGBTRIPLE) + padding1) * abs(bi.biHeight);
     bf.bfSize = 54 + bi.biSizeImage;

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

    int seek=0;

    // iterate over infile's scanlines
    for (int i = 0; i < abs(origheight); i++)
    {
     for(int u = 0;u<n;u++)
     {
            fseek(inptr,seek,SEEK_SET);
        // iterate over pixels in scanline
        for (int j = 0; j < origwidth; j++)
        {
            // temporary storage
            RGBTRIPLE triple;

            // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

            // write RGB triple to outfile
            
       
          for (int r=0;r<n;r++)
            {
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
               
        }  
       
         // skip over padding, if any
        fseek(inptr, padding, SEEK_CUR);

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


    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // that's all folks
    return 0;
}

}

/**

  • copy.c
  • Computer Science 50
  • Problem Set 4
  • Copies a BMP piece by piece, just because. */

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

#include "bmp.h"

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

// remember filenames
char* infile = argv[2];
char* outfile = argv[3];

int n = atoi(argv[1]); 
    if(n<1 || n>100)
        {
            printf("invalid factor\n");
            return 6;
        }

// 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)
{
    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 origwidth = bi.biWidth;
 int origheight = bi.biHeight;
 bi.biWidth = n * bi.biWidth;
 bi.biHeight = n * bi.biHeight;

 // determine padding for scanlines
    int padding =  (4 - (origwidth * sizeof(RGBTRIPLE)) % 4) % 4;
    int padding1 =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
 bi.biSizeImage = (bi.biWidth * sizeof(RGBTRIPLE) + padding1) * abs(bi.biHeight);
 bf.bfSize = 54 + bi.biSizeImage;

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

int seek=0;

// iterate over infile's scanlines
for (int i = 0; i < abs(origheight); i++)
{
 for(int u = 0;u<n;u++)
 {
        fseek(inptr,seek,SEEK_SET);
    // iterate over pixels in scanline
    for (int j = 0; j < origwidth; j++)
    {
        // temporary storage
        RGBTRIPLE triple;

        // read RGB triple from infile
        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

        // write RGB triple to outfile
        
   
      for (int r=0;r<n;r++)
        {
        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
        }
           
    }  
   
     // skip over padding, if any
    fseek(inptr, padding, SEEK_CUR);

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


// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;

}

Code after suggested changes:

/**
 * copy.c
 *
 * Computer Science 50
 * Problem Set 4
 *
 * Copies a BMP piece by piece, just because.
 */
       
#include <stdio.h>
#include <stdlib.h>

#include "bmp.h"

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

    // remember filenames
    char* infile = argv[2];
    char* outfile = argv[3];
   
    int n = atoi(argv[1]); 
        if(n<1 || n>100)
            {
                printf("invalid factor\n");
                return 6;
            }

    // 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)
    {
        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 origwidth = bi.biWidth;
     int origheight = bi.biHeight;
     bi.biWidth = n * bi.biWidth;
     bi.biHeight = n * bi.biHeight;
   
     // determine padding for scanlines
        int padding =  (4 - (origwidth * sizeof(RGBTRIPLE)) % 4) % 4;
        int padding1 =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
     bi.biSizeImage = (bi.biWidth * sizeof(RGBTRIPLE) + padding1) * abs(bi.biHeight);
     bf.bfSize = 54 + bi.biSizeImage;

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

    int seek=0;

    // iterate over infile's scanlines
    for (int i = 0; i < abs(origheight); i++)
    {
     for(int u = 0;u<n;u++)
     {
            fseek(inptr,seek,SEEK_SET);
        // iterate over pixels in scanline
        for (int j = 0; j < origwidth; j++)
        {
            // temporary storage
            RGBTRIPLE triple;

            // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

            // write RGB triple to outfile
            
       
          for (int r=0;r<n;r++)
            {
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
               
        }  
       
         // skip over padding, if any
        fseek(inptr, padding, SEEK_CUR);

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


    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // that's all folks
    return 0;
}
deleted 386 characters in body
Source Link

/**

  • copy.c
  • Computer Science 50
  • Problem Set 4
  • Copies a BMP piece by piece, just because. */

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

#include "bmp.h"

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

/**
 * copy.c
 *
 * Computer Science 50
 * Problem Set 4
 *
 * Copies a BMP piece by piece, just because.
 */
       
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>

#include "bmp.h"

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

    // remember filenames
    char* infile = argv[2];
    char* outfile = argv[3];
    if(!(isdigit(argv[1])))
        return 5;
    int n = atoi(argv[1]); 
        if(n<1 &&|| n>100)
            {
                printf("invalid factor\n");
                return 6;
            }

    // 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)
    {
        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 origwidth = bi.biWidth;
 int origheight = bi.biHeight;
 bi.biWidth = n * bi.biWidth;
     bi.biHeight = n * bi.biHeight;
     
     // determine padding for scanlines
    int padding =  (4 - (bi.biWidthorigwidth * sizeof(RGBTRIPLE)) % 4) % 4;
    int 
 padding1 =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
 bi.biSizeImage = (bi.biWidth * sizeof(RGBTRIPLE) + paddingpadding1) * abs(bi.biHeight;biHeight);
     bf.bfSize = 54 + bi.biSizeImage;

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

   int seek=0;

    // iterate over infile's scanlines
    for (int i = 0,0; biHeii =< abs(bi.biHeightorigheight)/n; i < biHei;; i++)
    {
      for (int u=0;u<n;n++)
 u = 0;u<n;u++)
 {
  {  
     fseek(inptr,seek,SEEK_SET);
    // iterate over pixels in scanline
        for (int j = 0; j < (bi.biWidth)/n;origwidth; j++)
        {
            // temporary storage
            RGBTRIPLE triple;

            // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

            // write RGB triple to outfile
          
 for (int r=0;r<n;r++)
      for (int r=0;r<n;r++)
    {
    {
        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
                
      }  }

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

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

     
// close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // that's all folks
    return 0;
}

i am always getting seg fault on compiling}

/**
 * copy.c
 *
 * Computer Science 50
 * Problem Set 4
 *
 * Copies a BMP piece by piece, just because.
 */
       
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>

#include "bmp.h"

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

    // remember filenames
    char* infile = argv[2];
    char* outfile = argv[3];
    if(!(isdigit(argv[1])))
        return 5;
    int n = atoi(argv[1]); 
        if(n<1 && n>100)
            {
                printf("invalid factor\n");
                return 6;
            }

    // 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)
    {
        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;
    }
    
     bi.biWidth = n * bi.biWidth;
     bi.biHeight = n * bi.biHeight;
     
     // determine padding for scanlines
    int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
     
      bi.biSizeImage = (bi.biWidth * sizeof(RGBTRIPLE) + padding) * bi.biHeight;
     bf.bfSize = 54 + bi.biSizeImage;

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

    

    // iterate over infile's scanlines
    for (int i = 0, biHei = abs(bi.biHeight)/n; i < biHei; i++)
    {
      for (int u=0;u<n;n++)
      {  
         // iterate over pixels in scanline
        for (int j = 0; j < (bi.biWidth)/n; j++)
        {
            // temporary storage
            RGBTRIPLE triple;

            // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

            // write RGB triple to outfile
          for (int r=0;r<n;r++)
            {
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
                
        }

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

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

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // that's all folks
    return 0;
}

i am always getting seg fault on compiling

/**

  • copy.c
  • Computer Science 50
  • Problem Set 4
  • Copies a BMP piece by piece, just because. */

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

#include "bmp.h"

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

// remember filenames
char* infile = argv[2];
char* outfile = argv[3];

int n = atoi(argv[1]); 
    if(n<1 || n>100)
        {
            printf("invalid factor\n");
            return 6;
        }

// 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)
{
    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 origwidth = bi.biWidth;
 int origheight = bi.biHeight;
 bi.biWidth = n * bi.biWidth;
 bi.biHeight = n * bi.biHeight;

 // determine padding for scanlines
    int padding =  (4 - (origwidth * sizeof(RGBTRIPLE)) % 4) % 4;
    int padding1 =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
 bi.biSizeImage = (bi.biWidth * sizeof(RGBTRIPLE) + padding1) * abs(bi.biHeight);
 bf.bfSize = 54 + bi.biSizeImage;

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

int seek=0;

// iterate over infile's scanlines
for (int i = 0; i < abs(origheight); i++)
{
 for(int u = 0;u<n;u++)
 {
        fseek(inptr,seek,SEEK_SET);
    // iterate over pixels in scanline
    for (int j = 0; j < origwidth; j++)
    {
        // temporary storage
        RGBTRIPLE triple;

        // read RGB triple from infile
        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

        // write RGB triple to outfile
         
   
      for (int r=0;r<n;r++)
        {
        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
        }
           
    }  
    
     // skip over padding, if any
    fseek(inptr, padding, SEEK_CUR);

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

 
// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;

}

code format fix
Source Link
Yuri Laguardia
  • 2.6k
  • 1
  • 10
  • 17

/**

  • copy.c
  • Computer Science 50
  • Problem Set 4
  • Copies a BMP piece by piece, just because. */

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

#include "bmp.h"

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

/**
 * copy.c
 *
 * Computer Science 50
 * Problem Set 4
 *
 * Copies a BMP piece by piece, just because.
 */
       
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>

#include "bmp.h"

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

    // remember filenames
    char* infile = argv[2];
    char* outfile = argv[3];
    if(!(isdigit(argv[1])))
        return 5;
    int n = atoi(argv[1]); 
        if(n<1 && n>100)
            {
                printf("invalid factor\n");
                return 6;
            }

    // 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)
    {
        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;
    }
    
     bi.biWidth = n * bi.biWidth;
     bi.biHeight = n * bi.biHeight;
     
     // determine padding for scanlines
    int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
     
     bi.biSizeImage = (bi.biWidth * sizeof(RGBTRIPLE) + padding) * bi.biHeight;
     bf.bfSize = 54 + bi.biSizeImage;

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

    

    // iterate over infile's scanlines
    for (int i = 0, biHei = abs(bi.biHeight)/n; i < biHei; i++)
    {
      for (int u=0;u<n;n++)
      {  
        // iterate over pixels in scanline
        for (int j = 0; j < (bi.biWidth)/n; j++)
        {
            // temporary storage
            RGBTRIPLE triple;

            // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

            // write RGB triple to outfile
          for (int r=0;r<n;r++)
            {
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
                
        }

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

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

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // that's all folks
    return 0;
}

}

/**

  • copy.c
  • Computer Science 50
  • Problem Set 4
  • Copies a BMP piece by piece, just because. */

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

#include "bmp.h"

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

// remember filenames
char* infile = argv[2];
char* outfile = argv[3];
if(!(isdigit(argv[1])))
    return 5;
int n = atoi(argv[1]); 
    if(n<1 && n>100)
        {
            printf("invalid factor\n");
            return 6;
        }

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

 bi.biWidth = n * bi.biWidth;
 bi.biHeight = n * bi.biHeight;
 
 // determine padding for scanlines
int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
 
 bi.biSizeImage = (bi.biWidth * sizeof(RGBTRIPLE) + padding) * bi.biHeight;
 bf.bfSize = 54 + bi.biSizeImage;

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



// iterate over infile's scanlines
for (int i = 0, biHei = abs(bi.biHeight)/n; i < biHei; i++)
{
  for (int u=0;u<n;n++)
  {  
    // iterate over pixels in scanline
    for (int j = 0; j < (bi.biWidth)/n; j++)
    {
        // temporary storage
        RGBTRIPLE triple;

        // read RGB triple from infile
        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

        // write RGB triple to outfile
      for (int r=0;r<n;r++)
        {
        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
        }
            
    }

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

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

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;

}

/**
 * copy.c
 *
 * Computer Science 50
 * Problem Set 4
 *
 * Copies a BMP piece by piece, just because.
 */
       
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>

#include "bmp.h"

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

    // remember filenames
    char* infile = argv[2];
    char* outfile = argv[3];
    if(!(isdigit(argv[1])))
        return 5;
    int n = atoi(argv[1]); 
        if(n<1 && n>100)
            {
                printf("invalid factor\n");
                return 6;
            }

    // 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)
    {
        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;
    }
    
     bi.biWidth = n * bi.biWidth;
     bi.biHeight = n * bi.biHeight;
     
     // determine padding for scanlines
    int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
     
     bi.biSizeImage = (bi.biWidth * sizeof(RGBTRIPLE) + padding) * bi.biHeight;
     bf.bfSize = 54 + bi.biSizeImage;

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

    

    // iterate over infile's scanlines
    for (int i = 0, biHei = abs(bi.biHeight)/n; i < biHei; i++)
    {
      for (int u=0;u<n;n++)
      {  
        // iterate over pixels in scanline
        for (int j = 0; j < (bi.biWidth)/n; j++)
        {
            // temporary storage
            RGBTRIPLE triple;

            // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

            // write RGB triple to outfile
          for (int r=0;r<n;r++)
            {
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
                
        }

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

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

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // that's all folks
    return 0;
}
Source Link
Loading