0

My raw file has 16 images to . I can only get 14 jpg files with my code. running in valgrind returns no leaks, gdb however repeats on the following lines for a long time so I am sure the problem is there.

However, outside of gdb it runs successfully and makes 14 files. I have a feeling I am not writing off of the necessary info to the output file but i can not figure out why.

while(fread(&buffer, 512, 1, inptr) != 0)
{
   if(inptr != NULL)
   {
      if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff &&
             buffer[3] == 0xe0 || buffer[3] == 0xe1)
      {Code to write to file here}
   }
}

I have even checked the hex dump for the output files and they start correctly for a jpg.

I am so lost and spend a long time on this, any help would be much appreciated.

Updated code below.

while(fread(&buffer, 512, 1, inptr) != 0)
{
    if(inptr != NULL)
    {   
      // Checks for start of jpg header

        if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff &&
             (buffer[3] == 0xe0 || buffer[3] == 0xe1))
        {   // Creates the new file
            sprintf(outfile, "%03d.jpg", fileNum++);

            // Gives the file a pointer, opens the file, writes to the file
            FILE* outptr;

            outptr = fopen(outfile, "a");

            fwrite(&buffer, 512, 1, outptr);

            // Checks that the buffer isnt empty again
            if(fread(buffer, 512, 1, inptr) == 0)
                break;

            // Checks the next bytes, if not header bytes write to outfile
            while (buffer[0] != 0xff && buffer[1] != 0xd8 && buffer[2] != 0xff &&
                     (buffer[3] != 0xe0 || buffer[3] != 0xe1))
            {
                if(fread(buffer, 512, 1, inptr) == 0)
                    break;

                //fread(buffer, 512, 1, inptr);

                fwrite(buffer, 512, 1, outptr);
            }
            fclose(outptr);
        }
    }
}
7
  • The if statement that you posted would generate a warning during compile, which would then be treated as an error by the -Werror parameter, unless you have suppressed it. Unless it was a copying typo, the two buffer[3] tests should be inside parantheses. if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && ( buffer[3] == 0xe0 || buffer[3] == 0xe1) ) Without this, any file with 0xe1 in that position would pass. However, this doesn't explain what's happening. There's not enough code to see what's going on.
    – Cliff B
    Jun 29 '15 at 19:15
  • Thank you for giving it a look. Here is more of the code. Thanks again. sprintf(outfile, "%03d.jpg", fileNum++); // Gives the file a pointer, opens the file, writes to the file FILE* outptr; outptr = fopen(outfile, "a"); fwrite(&buffer, 512, 1, outptr); // Checks that the buffer isnt empty again if(fread(buffer, 512, 1, inptr) == 0) break; For space reasons the rest is in the next comment.
    – MrNiceGuy
    Jun 30 '15 at 17:20
  • // Checks the next bytes, if not header bytes write to outfile while(buffer[0] != 0xff && buffer[1] != 0xd8 && buffer[2] != 0xff && (buffer[3] != 0xe0 || buffer[3] != 0xe1)) { if(fread(buffer, 512, 1, inptr) == 0) break; fread(buffer, 512, 1, inptr); fwrite(buffer, 512, 1, outptr); } fclose(outptr); } This ends the {Code to write to file here} part.
    – MrNiceGuy
    Jun 30 '15 at 17:24
  • Please edit the original question and put the code there.
    – Cliff B
    Jun 30 '15 at 17:32
  • updated. Also I changed the second while to and if and i got all 16 jpg files as i was expecting but I couldnt open them in the image viewer with error "Improper call to JPG library in state 201".
    – MrNiceGuy
    Jun 30 '15 at 17:40
1

I don't even know how your program is even able to get 14 jpgs.

In the code block below, you're reading a FAT block but you never write it to your file. Thus, every jpg file you're writing is missing its 2nd FAT block. // Checks that the buffer isnt empty again if(fread(buffer, 512, 1, inptr) == 0) break;

And for the // Checks the next bytes, if not header bytes write to outfile loop, imagine you're just done with the last FAT block for 001.jpg. Since its the last block, it'll still pass the while check and you'll then proceed to read the next block, which should be the first block of the 002.jpg, and write it to 001.jpg. After this, it finally fails the while check and returns to the top of the code, where it reads the 2nd block of the 002.jpg. It then checks this 2nd block for the starting bytes. This should fail every time but somehow, you still managed to get 14 jpgs with this.

Also, the if(inptr != NULL) is superfluous. if inptr == NULL I believe an error would have already been thrown earlier by fread(&buffer, 512, 1, inptr).

1
  • The line above is a write line, does this read line overwrite something? Also, I changed the second while to an if now I am getting 16 jpg files as I am expecting to get. Im still getting "Improper call to JPG library in state 201" when trying to open the jpgs in the image viewer
    – MrNiceGuy
    Jun 30 '15 at 19:47

You must log in to answer this question.

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