• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1135
  • Last Modified:

How can I recover the jpeg files and create multiple jpeg files using c?

The signatures 0xff 0xd8 0xff 0xe0 or
0xff 0xd8 0xff 0xe1 should be in the first 4 bytes of every jpeg file.  The input file is card.raw.  I  tried to attach it, but it's too large.
/****************************************************************************
 * bmp.h
 *
  * BMP-related data types based on Microsoft's own.
 ***************************************************************************/
 
 
/* 
 * Common Data Types 
 *
 * The data types in this section are essentially aliases for C/C++ 
 * primitive data types.
 *
 * Adapted from http://msdn.microsoft.com/en-us/library/cc230309.aspx.
 */
 
typedef unsigned char   BYTE;
typedef unsigned long   DWORD;
typedef long            LONG;
typedef unsigned short  WORD;
 
 
/*
 * BITMAPFILEHEADER
 *
 * The BITMAPFILEHEADER structure contains information about the type, size,
 * and layout of a file that contains a DIB [device-independent bitmap].
 *
 * Adapted from http://msdn2.microsoft.com/en-us/library/ms532321.aspx.
 */
 
typedef struct 
{ 
    WORD   bfType; 
    DWORD  bfSize; 
    WORD   bfReserved1; 
    WORD   bfReserved2; 
    DWORD  bfOffBits; 
} __attribute__((__packed__)) 
BITMAPFILEHEADER; 
 
 
/*
 * BITMAPINFOHEADER
 *
 * The BITMAPINFOHEADER structure contains information about the 
 * dimensions and color format of a DIB [device-independent bitmap].
 *
 * Adapted from http://msdn2.microsoft.com/en-us/library/ms532290.aspx.
 */
       
typedef struct
{
    DWORD  biSize; 
    LONG   biWidth; 
    LONG   biHeight; 
    WORD   biPlanes; 
    WORD   biBitCount; 
    DWORD  biCompression; 
    DWORD  biSizeImage; 
    LONG   biXPelsPerMeter; 
    LONG   biYPelsPerMeter; 
    DWORD  biClrUsed; 
    DWORD  biClrImportant; 
} __attribute__((__packed__))
BITMAPINFOHEADER; 
 
 
/* 
 * RGBTRIPLE
 *
 * This structure describes a color consisting of relative intensities of
 * red, green, and blue.
 *
 * Adapted from http://msdn.microsoft.com/en-us/library/aa922590.aspx.
 */
 
typedef struct
{
    BYTE  rgbtBlue;
    BYTE  rgbtGreen;
    BYTE  rgbtRed;
} __attribute__((__packed__))
RGBTRIPLE;
 
 
 
/****************************************************************************
 * 
 *
 
 
 ***************************************************************************/
       
#include <stdio.h>
#include <stdlib.h>
 
#include "bmp.h"
 
 
int
main(int argc, char *argv[])
{
    // ensure proper usage
    if (argc != 1)
    {
        printf("Usage: recover\n");
        return 1;
    }
 
    // open input file 
    FILE *inptr = fopen("/home/cs50/pub/share/pset5/card.raw", "r");
    if (inptr == NULL)
    {
        printf("Could not open file.\n");
        return 2;
    }
    FILE *outptr = fopen("test.jpg","w");
 
    // read infile's BITMAPFILEHEADER
    BITMAPFILEHEADER bf;
    fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
 
    // read infile's BITMAPINFOHEADER
    BITMAPINFOHEADER bi;
    fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
 
//    char *buffer = malloc(bf* sizeof(BYTE));
    //if (buffer == NULL)
    //      return 4;
   // buffer=fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
    
    printf("bf.bfType=%x\n",bf.bfType);
    printf("bf.bfOffBits=%ld\n",bf.bfOffBits);
    printf("bi.biSize=%ld\n",bi.biSize);
    printf("bi.biBitCount=%x\n",bi.biBitCount);
    printf("bi.biCompression=%ld\n",bi.biCompression);
    // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
//    if (bf.bfType != 0x58eb || bf.bfOffBits != 134348852 
 //   || bi.biSize != 131104)
 //   {
  //      fprintf(stderr, "Unsupported file format.\n");
  //      return 4;
  //  }
    
    unsigned char * buffer[1048576]={' '};
    unsigned char found;
    
    found='n';
    fread(&buffer, sizeof(BYTE), 1048576, inptr);
    for (int i=0; i< 512; i++)
    {
       printf("buffer[]=%x\n",buffer[i]);
    
      //  if (buffer[0] == 0xff && buffer[1] == 0xd8  && buffer[2] == 0xff && 
      //  buffer[3] == 0xe0)
        if ((buffer[i] ==  0xff)
           &&
           (buffer[i+1] == 0xd8)
           &&
           (buffer[i+2] == 0xff)
           &&
           (buffer[i+3] == 0xe0))
        {
            printf("found\n");
            found='y';
            exit;
        }
        
        if ((buffer[i] == 0xff && buffer[i+1] == 0xd8  && buffer[i+2] == 0xff &&
        buffer[i+3] == 0xe1))
        {
            printf("found,too\n");
            found='y';
            exit;
        }
        
    }
    printf("found=%c\n",found);
    // 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;
    // define red 
    //char red = 0x00f;
    
    // iterate over infile's scanlines
  //  for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
  //  {
        // iterate over pixels in scanline
      //  for (int j = 0; j < bi.biWidth; j++)
    //    {
            // temporary storage
        //    RGBTRIPLE triple;
 
            // read RGB triple from infile
          //  fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
           // printf("biHeight=%d\n", biHeight);
           // printf("bi.biWidth=%ld\n", bi.biWidth);
//            printf("bf.bfOffBits=%ld\n",bf.bfOffBits);
//            if (triple.rgbtBlue == 0x00)
//            {
//            printf("triple.rgbtBlue=%x\n",triple.rgbtBlue);    
//            printf("triple.rgbtGreen=%x\n",triple.rgbtGreen);
//           }
           
           //  for (int i = 0; i < 4; i++)
            // {
          //   char buffer[512];
           //  fread(buffer, sizeof(BYTE), 1, inptr);
           //  printf("buffer=%c\n",buffer[0]);
           ///  if (buffer[j] == 0xff &&
            //    buffer[j+1] == 0xd8 &&
            //    buffer[j+2] == 0xff &&
            //    buffer[j+3] == 0xe1)
            //      return 0;
            // }
           //change red into white
            //if (triple.rgbtBlue == 0x00 && triple.rgbtGreen == 
           // 0x00 && triple.rgbtRed == 0xff)          
          //  {
            //  triple.rgbtBlue = 0xff;
             // triple.rgbtGreen = 0xff;
             // triple.rgbtRed = 0xff;
 
          //  }   
            // write RGB triple to outfile
            //fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
      //  }
 
        // skip over padding, if any
        //fseek(inptr, padding, SEEK_CUR);
       // fseek(inptr, rgbtRed, SEEK_CUR);
        
        // write padding to outfile
       // for (int k = 0; k < padding; k++)
         //   fputc(0x00, outptr);
    
 
    // close infile
    fclose(inptr);
 
    // close outfile
    fclose(outptr);
 
    // that's all folks
    return 0;
}

Open in new window

0
codedreamer
Asked:
codedreamer
  • 2
1 Solution
 
jgordosCommented:
Okay,

I'm not sure exactly what your question is...

The c code you posted looks like you intend to open a file called "card.raw", and it almost looks like you want to write that file as a jpg, because you've declared outptr as "test.jpg".

However, no where do you seem to convert the data you receive into the the JPG file format.

A JPG is not a bitmap; they're completely different data formats.

You'll need to convert the headers to the correct formats, and use some sort of image conversion library routine (or roll your own) to save the file as a JPG.

Also, you'll need to manipulate the filename that you're writing to if you intend to write more than one JPG at a time.

-john


0
 
codedreamerAuthor Commented:
Could you please give me some sample of code of how to do that?  I agree with your design approach.  Thank you.
0
 
jgordosCommented:
You can use this library of routines:

http://filters.sourceforge.net/

Among the routines there are JPG reader/writers...  you shouldn't have any trouble with it.

-john
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now