I'm trying to write a code to take an image as argv[1] and save it in a new file "argv[2]" with the image flipped upside down, but my output does not open with any image viewer
here's my full code. some one help me out please

#include <stdio.h>     
#include <string.h>
#include <fcntl.h>

int main(int argc, char *argv[]){
  char buffer[256];
  FILE *fp1, *fp2;
  long int fileSize, lineSize;

  if(argc != 3){
    printf("Call model: %s <inputFileName>  <outputFileName>\n", argv[0]);
    exit(0);
  }

  fp1 = fopen(argv[1], "r");
  fp2 = fopen(argv[2], "w");

  if(!fp1 || !fp2){
    printf("File oupening problem\n");
    exit(0);
  }

  fseek(fp1, 0L, SEEK_END);
  fileSize = ftell(fp1);
  rewind(fp1);

  fseek(fp2, fileSize, SEEK_SET);

  while(fgets(buffer, 256, fp1)){
    lineSize = strlen(buffer);
    fseek(fp2, -lineSize, SEEK_CUR);
    fputs(buffer, fp2);
    fseek(fp2, -lineSize, SEEK_CUR);
  }
  fclose(fp2);
  fclose(fp1);
}

are you shure you can use fgets ? why dont you use fread for start and try to copy image ?
You can also use some hex editor to compare values.

fgets is for text...

Maybe someone can help me understand how can i read metadata of the jpeg image on c as i understand there are some tags ?
Also is this data 2 byte not one byte to reverse >\?
I am using linux so maybe some library ?
Here is the code and i need to reverse image bot first determine metadata size not to reverse it..

Here is some code for image http://www.scs.ryerson.ca/~etabello/linux_icon.jpg

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


int iCreateTestFile();
int iReverseImage(unsigned char *pucImageData, long lSize)
{
   FILE * pfOutputImage = NULL;
   long i = 0;
   long j = 0;
   unsigned char *pucReversedImage = (unsigned char*) malloc(lSize);

   i = lSize - 1;
   j = 0;
   while(j < lSize)
   {
      pucReversedImage[i] = pucImageData[j];
      j++;
      i--;
   }

   pfOutputImage = fopen("linux_icon_reversed.jpg", "wb");

   fwrite(pucReversedImage, 1 , lSize, pfOutputImage);
   fclose(pfOutputImage);
   free(pucReversedImage);
   return 0;
}


int main()
{  
   iCreateTestFile();

   return 0;
}

int iCreateTestFile()
{
   FILE *pfInputImage = NULL;
   long lSize = 0;
   unsigned char *pucImageData = NULL;

   pfInputImage = fopen("linux_icon.jpg", "rb");
   fseek (pfInputImage , 0 , SEEK_END);
   lSize = ftell (pfInputImage);
   fseek (pfInputImage , 0 , SEEK_SET);
   pucImageData = (unsigned char*) malloc(lSize);
   fread(pucImageData, 1 , lSize, pfInputImage);

   if(pucImageData != NULL)
   {
      iReverseImage(pucImageData, lSize);
   }

   fclose(pfInputImage);
   free(pucImageData);
   return 0;
}

looks like i flipped bmp image with this code, bot it changed color ))
also still meta data unknown
http://www.cs.pitt.edu/~kirk/cs1501/Ramirez/Spring2008/assigs/compression/data/tux-large.bmp

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


int iCreateTestFile();
int iReverseImage(unsigned char *pucImageData, long lSize)
{
   FILE * pfOutputImage = NULL;
   long i = 0;
   long j = 0;
   unsigned char *pucReversedImage = (unsigned char*) malloc(lSize);

   i = lSize - 1;
   j = 100;
   while(j < lSize)
   {
      pucReversedImage[i] = pucImageData[j];
      j++;
      i--;
   }

   pfOutputImage = fopen("tux-large-reversed.bmp", "wb");
   fseek (pfOutputImage , 0 , SEEK_SET);
   fwrite(pucImageData, 1 , 100 , pfOutputImage);
   fseek (pfOutputImage , 100 , SEEK_SET);
   fwrite(pucReversedImage, 1 , lSize-100, pfOutputImage);
   fclose(pfOutputImage);
   free(pucReversedImage);
   return 0;
}


int main()
{  
   iCreateTestFile();

   return 0;
}

int iCreateTestFile()
{
   FILE *pfInputImage = NULL;
   long lSize = 0;
   unsigned char *pucImageData = NULL;

   pfInputImage = fopen("tux-large.bmp", "rb");
   fseek (pfInputImage , 0 , SEEK_END);
   lSize = ftell (pfInputImage);
   fseek (pfInputImage , 0 , SEEK_SET);
   pucImageData = (unsigned char*) malloc(lSize);
   fread(pucImageData, 1 , lSize, pfInputImage);

   if(pucImageData != NULL)
   {
      iReverseImage(pucImageData, lSize);
   }

   fclose(pfInputImage);
   free(pucImageData);
   return 0;
}

looks like i flipped bmp image with this code, bot it changed color ))
also still meta data unknown
http://www.cs.pitt.edu/~kirk/cs1501/Ramirez/Spring2008/assigs/compression/data/tux-large.bmp

Unfortunately, modifying or "flipping" a bitmap image is not that simple. You just cannot transpose every single byte in the binary BMP file and expect it to work correctly. I've attached sample code that will input a 24 bit BMP file and write it out to a new file called Test.bmp. Please take note of all the structural "metadata" that is needed to properly form a BMP file. Also, note that this data is BYTE aligned which is indicated by the pragma pack(1) statement. There is a section of data within the binary file that contains the pixel data. It is this data that you must "flip" not the whole binary file.

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

#define byte unsigned char
#define dword unsigned int
#define word unsigned short int

#pragma pack (1)

//4 Bytes per each pallete color
typedef struct {
    unsigned char rgbBlue,   
                 rgbGreen,   
                   rgbRed,   
              rgbReserved;   
}BITMAP_PALETTE;

// BMP File header
typedef struct bitmap_file_header {
    byte bfType[2];   
    dword bfSize;
    word bfReserved1;   
    word bfReserved2;
    dword bfOffBits;
} BITMAP_FILE_HEADER;

// BMP Image header
typedef struct bitmap_info_header {
    dword biSize;
    dword biWidth;
    dword biHeight;
    word biPlanes;                  
    word biBitCount;
    dword biCompression;      
    dword biSizeImage;
    dword biXPelsPerMeter;    
    dword biYPelsPerMeter;
    dword biClrUsed;              
    dword biClrImportant;
} BITMAP_INFO_HEADER;

typedef struct image
{
    BITMAP_FILE_HEADER file_info;
    BITMAP_INFO_HEADER bitmap_info;
    BITMAP_PALETTE *bitmap_palette;
    byte *data;
} IMAGE;

int save_image(IMAGE *image, char name[])
{
    FILE *fp;
    int i, rowsize;
    byte temp;
    unsigned int row, col, colour;
    int nx, ny;

    if(image == NULL)
        return EXIT_FAILURE;
    fp=fopen(name, "wb");
    if(fp==NULL)
    {
        printf("cannot open the file %s\n", name);
        return EXIT_FAILURE;
    }
    fwrite(&image->file_info, sizeof(BITMAP_FILE_HEADER),1,fp);
    fwrite(&image->bitmap_info, sizeof(BITMAP_INFO_HEADER),1,fp);
    int itotalPalettbytes = (int) image->file_info.bfOffBits - sizeof(BITMAP_FILE_HEADER) - (int)sizeof(BITMAP_INFO_HEADER);
    fwrite((void *)(&image->bitmap_palette),4, itotalPalettbytes / 4, fp);
    if (((image->bitmap_info.biWidth * 3) % 4) == 0) {
        nx = image->bitmap_info.biWidth * 3;
    }
    else {
        nx = image->bitmap_info.biWidth * 3 + 4 -((image->bitmap_info.biWidth * 3) % 4);
    }
    ny = image->bitmap_info.biHeight;
    fwrite((void *)image->data, sizeof(byte),nx * ny ,  fp);
    fclose(fp);
    return EXIT_SUCCESS;
}

int main(int argc, char **argv)
{
    int iNx = 9, iNy;
    FILE *fp;
    int iTotalPaletteBytes;
    unsigned short int iType;     //holds the first two bytes of the file
    IMAGE *ImageInput = (IMAGE *) malloc(sizeof(IMAGE));
    if ((fp = fopen(argv[1], "rb")) == NULL)
    {
        printf("%s doesn't exist.\n",argv[1]);
    }
    else
    {
        fread((void *)&iType, 2, 1, fp);
        //The BM is a short integer... 19778
        if (iType == 19778)
        {
            fseek(fp,0,SEEK_SET);
            //dump the first 14 bytes
            fread((void *)&(ImageInput->file_info), sizeof(BITMAP_FILE_HEADER), 1, fp);
            //dump the next 40 bytes
            fread((void *)&(ImageInput->bitmap_info), sizeof(BITMAP_INFO_HEADER), 1, fp);
            iTotalPaletteBytes = (int)ImageInput->file_info.bfOffBits - sizeof(BITMAP_FILE_HEADER) - (int)sizeof(BITMAP_INFO_HEADER);
            //Dimention the array
            ImageInput->bitmap_palette = (BITMAP_PALETTE *) malloc(iTotalPaletteBytes);
            //dump the next 4*n bytes
            fread((void *)(ImageInput->bitmap_palette),4, iTotalPaletteBytes / 4, fp);    //works same way
            if (ImageInput->bitmap_info.biBitCount == 24) {
                //Go to the beginning of the pixel data in the file
                fseek(fp, ImageInput->file_info.bfOffBits, SEEK_SET);

                //Calculate the width of the bitmap taking into account padding bytes
                if (((ImageInput->bitmap_info.biWidth * 3) % 4) == 0) {
                    iNx = ImageInput->bitmap_info.biWidth * 3;
                }
                else {
                    iNx = ImageInput->bitmap_info.biWidth * 3 + 4 -((ImageInput->bitmap_info.biWidth * 3) % 4);
                }
                iNy = ImageInput->bitmap_info.biHeight;
                ImageInput->data = (unsigned char *) malloc(iNx * iNy * 3);   
                memset(ImageInput->data,0, sizeof ImageInput->data);
                //Retrive the data array from the file
                fread((void *)ImageInput->data, iNx * iNy * 3, 1, fp);
                save_image(ImageInput, "Test.bmp");
            }
        }
    }
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.