Advertisement

11.10.2008 at 12:16PM PST, ID: 23892245
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.8

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

Asked by codedreamer in Programming Languages, Game Programming, Multimedia Programming

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.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
/****************************************************************************
 * 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;
}
[+][-]11.10.2008 at 01:50PM PST, ID: 22925941

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.10.2008 at 02:13PM PST, ID: 22926158

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.11.2008 at 06:59AM PST, ID: 22931043

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Programming Languages, Game Programming, Multimedia Programming
Sign Up Now!
Solution Provided By: jgordos
Participating Experts: 1
Solution Grade: A
 
 
[+][-]12.01.2008 at 11:15PM PST, ID: 23074967

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]12.06.2008 at 08:33AM PST, ID: 23112663

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 - Hierarchy / EE_QW_3_20080625