Thread: Move file postion indicator to next line

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    21

    Move file postion indicator to next line

    I am trying to resize a bmp image by FACTOR times. I got the width resize working correctly but for the height I need to print each row of pixels or 'RGBTRIPLE' FACTOR number of times. I am pretty sure I have the fseek line in the right spot (marked here) I just don't know how many bytes to move it by I tried 1 I though 1 byte past the end would move the pointer to the next line but that did not work. This is for a class so please no code. Here is my code. Thanks for any help

    Code:
           
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #include "bmp.h"
    
    
    int main(int argc, char* argv[])
    {
        // ensure proper usage
        if (argc != 4)
        {
            printf("Usage: ./copy n infile outfile\n");
            return 1;
        }
    
    
        // resize by
        int FACTOR = atoi(argv[1]);
        
        // remember filenames
        char* infile = argv[2];
        char* outfile = argv[3];
    
    
        // 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);
    
    
        bi.biSizeImage *= FACTOR;
        bi.biWidth *= FACTOR;
        bi.biHeight *= FACTOR;
    
    
        // 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;
        }
    
    
        // write outfile's BITMAPFILEHEADER
        fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
    
    
        // write outfile's BITMAPINFOHEADER
        fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
    
    
        // determine padding for scanlines
        int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    
    
        // iterate over infile's scanlines
        for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
        {
          //  for (int j = 0; j < FACTOR; j++)
           // {
                // iterate over pixels in scanline
                for (int k = 0; k < bi.biWidth; k++)
                {
                    // temporary storage
                    RGBTRIPLE triple;
    
    
                    // read RGB triple from infile
                    fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
    
    
                    for (int k = 0; k < FACTOR; k++)
                    {
                        // write RGB triple to outfile
                        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                    }
                }
    
    
                // skip over padding, if any
                fseek(inptr, padding, SEEK_CUR);
    
    
                // then add it back 
                for (int l = 0; l < padding; l++)
                {
                    fputc(0x00, outptr);
                }
             //   fseek (outptr, 1, SEEK_CUR); //here
           // }
        }
    
    
        // close infile
        fclose(inptr);
    
    
        // close outfile
        fclose(outptr);
    
    
        return 0;
    }
    Last edited by arortell; 07-27-2014 at 03:24 PM.

  2. #2
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    I've never done this before, but think I understand what you are trying to do.

    My question is, what does a .bmp file use as a delimiter for pixel rows? Are there any present in the output file?

    As I understand it, you have the width resize working correctly, but the height resize isn't working, because you need FACTOR * number_of_delimiters_in_input_file, to get to the correct line spacing?

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I just don't know how many bytes to move it by I tried 1 I though 1 byte past the end would move the pointer to the next line but that did not work.
    You should be opening your file in binary mode, and in binary mode there are no "next lines", there are just bytes.

    Jim

  4. #4
    Registered User
    Join Date
    Apr 2014
    Posts
    21
    I found some threads on another forum they talked about resetting the input file pointer after reading that row I think that might be easier so I will try that when I get home. As far as a delimiter I thought it was just in bytes like 'jimblumberg' said the only positions there are is SEEK_CUR which is the current position, SEEK_SET this is the beginning of the file and SEEK_END which is obviously the end of the file so I think I will have to figure out the width in bytes of each row and reset the inptr from the beginning of the file after reading each row. I know I could just store the row in a buffer but I think using the file position indicator is the whole point of this exercise. Thanks for any suggestions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Move to specified line in CSV file
    By strokebow in forum C++ Programming
    Replies: 26
    Last Post: 07-03-2012, 09:35 AM
  2. Move pointer to next line in .txt file
    By motarded in forum C++ Programming
    Replies: 6
    Last Post: 03-01-2006, 10:18 AM
  3. Move the Caret to a line
    By TheDan in forum Windows Programming
    Replies: 3
    Last Post: 08-07-2005, 12:59 PM
  4. How do you move up a line?
    By epb613 in forum C Programming
    Replies: 3
    Last Post: 05-31-2005, 01:44 PM
  5. progress indicator for file copying
    By Shadow in forum C Programming
    Replies: 2
    Last Post: 05-24-2002, 02:30 PM