?
Solved

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

Posted on 2008-11-10
5
Medium Priority
?
1,129 Views
Last Modified: 2012-05-05
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
Comment
Question by:codedreamer
  • 2
3 Comments
 
LVL 11

Expert Comment

by:jgordos
ID: 22925941
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
 

Author Comment

by:codedreamer
ID: 22926158
Could you please give me some sample of code of how to do that?  I agree with your design approach.  Thank you.
0
 
LVL 11

Accepted Solution

by:
jgordos earned 2000 total points
ID: 22931043
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month17 days, 10 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question