//My program creates images of different sizes but without content (just black and white pixels). Someone can help me?
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;
bool JPEGstart(BYTE buffer[]);
int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 2)
{
fprintf(stderr, "Usage: ./recover image\n");
return 1;
}
// open input file
FILE *raw_file = fopen(argv[1], "r");
if (raw_file == NULL)
{
fprintf(stderr, "Could not open %s.\n", argv[1]);
return 2;
}
BYTE *buffer = malloc(512);
int COUNT = 0;
do
{
if (JPEGstart(buffer) == true)
{
COUNT++;
char *filename = malloc((7+1));
sprintf(filename, "%03i.jpg", COUNT);
FILE *img = fopen(filename, "w");
do
{
fwrite(buffer, 512, 1, img);
}
while (JPEGstart(buffer) == false);
// close outfile
fclose(img);
}
}
// read raw_file and checking if the end of card.raw was reached
while (fread(buffer, 512, 1, raw_file) == 1);
}
bool JPEGstart(BYTE buffer[])
{
if (buffer[0] == 0xff &&
buffer[1] == 0xd8 &&
buffer[2] == 0xff &&
(buffer[3] & 0xf0) == 0xe0)
{
return true;
}
else
{
return false;
}
}