1 /* |
|
2 * Copyright (c) 2004-2007 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Contains the CRsImageParser class definition. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // RsImageParser.cpp: implementation of the CRsImageParser class. |
|
20 // |
|
21 ////////////////////////////////////////////////////////////////////// |
|
22 |
|
23 #include "crsimageparser.h" |
|
24 #include "clog.h" |
|
25 |
|
26 ////////////////////////////////////////////////////////////////////// |
|
27 // Construction/Destruction |
|
28 ////////////////////////////////////////////////////////////////////// |
|
29 |
|
30 CRsImageParser* CRsImageParser::NewLC() |
|
31 { |
|
32 CRsImageParser* self = new (ELeave) CRsImageParser(); |
|
33 CleanupStack::PushL(self); |
|
34 self->ConstructL(); |
|
35 return self; |
|
36 } |
|
37 |
|
38 |
|
39 void CRsImageParser::ConstructL() |
|
40 { |
|
41 User::LeaveIfError(iFs.Connect()); |
|
42 } |
|
43 |
|
44 |
|
45 CRsImageParser::~CRsImageParser() |
|
46 { |
|
47 iFs.Close(); |
|
48 } |
|
49 |
|
50 CRsImageParser::CRsImageParser() |
|
51 { |
|
52 } |
|
53 |
|
54 void CRsImageParser::ValidateL( const TDesC& aFileName, TBool& aValid, TInt& aErr ) |
|
55 { |
|
56 LOG1("CRsImageParser::ValidateL aFileName: %S", &aFileName); |
|
57 aValid = EFalse; |
|
58 TInt dataPos( 0 ); |
|
59 TBuf8<9> imgData; |
|
60 TBool bAllDone( EFalse ); |
|
61 TInt height( 0 ); |
|
62 TInt width( 0 ); |
|
63 TInt startOfDataOffset( 0 ); |
|
64 TInt encoding( EUnknown ); |
|
65 TInt size( 0 ); |
|
66 |
|
67 RFile file; |
|
68 aErr = file.Open( iFs, aFileName, EFileRead | EFileShareReadersOnly ); |
|
69 LOG1("CRsImageParser::ValidateL file.Open (shareread mode): %d", aErr); |
|
70 if ( aErr != KErrNone ) |
|
71 { |
|
72 aErr = file.Open( iFs, aFileName, EFileRead | EFileShareAny ); |
|
73 LOG1("CRsImageParser::ValidateL file.Open (shareany mode): %d", aErr); |
|
74 } |
|
75 |
|
76 // magic numbers used here are JPEG format related |
|
77 else // if ( aErr == KErrNone ) // 1 IF |
|
78 { |
|
79 CleanupClosePushL( file ); |
|
80 aErr = file.Size( size ); |
|
81 if( size && aErr == KErrNone ) // 2 IF |
|
82 { |
|
83 aErr = ReadData( file, dataPos, imgData ); |
|
84 if( aErr == KErrNone && imgData.Length() == 9 ) // 3 IF |
|
85 { |
|
86 // First check for the JPEG header "FF D8" |
|
87 if( imgData[0] == 0xff && imgData[1] == 0xd8 ) // 4 IF |
|
88 { |
|
89 // Start skipping headers until we find the one we're looking for |
|
90 dataPos = (imgData[4] << 8) + imgData[5] + 4; |
|
91 |
|
92 aErr = ReadData( file, dataPos, imgData ); |
|
93 |
|
94 while( aErr == KErrNone && imgData.Length() == 9 && imgData[0] == 0xff ) |
|
95 { |
|
96 switch( imgData[1] ) |
|
97 { |
|
98 case 0xda: |
|
99 // Start of data |
|
100 startOfDataOffset = dataPos + ((imgData[2] << 8) + imgData[3] + 2); |
|
101 bAllDone = ETrue; |
|
102 break; |
|
103 |
|
104 case EDiffArithLossless: |
|
105 encoding = imgData[1]; |
|
106 height = (imgData[5] << 8) + imgData[6]; |
|
107 width = (imgData[7] << 8) + imgData[8]; |
|
108 break; |
|
109 |
|
110 default: |
|
111 break; |
|
112 } // SWITCH |
|
113 |
|
114 if( bAllDone ) |
|
115 { |
|
116 break; |
|
117 } |
|
118 else |
|
119 { |
|
120 dataPos += (imgData[2] << 8) + imgData[3] + 2; |
|
121 aErr = ReadData( file, dataPos, imgData ); |
|
122 } |
|
123 } // WHILE |
|
124 } // 4 IF |
|
125 else |
|
126 { |
|
127 // Not a JPG |
|
128 aErr = KErrCorrupt; |
|
129 } |
|
130 } // 3 IF |
|
131 } // 2 IF |
|
132 |
|
133 CleanupStack::PopAndDestroy( &file ); |
|
134 } // 1 IF |
|
135 |
|
136 aValid = Validate( startOfDataOffset, width, height, size, encoding, aErr ); |
|
137 |
|
138 LOG1("CRsImageParser::ValidateL aValid: %d", aValid); |
|
139 LOG1("CRsImageParser::ValidateL aErr: %d", aErr); |
|
140 LOG("CRsImageParser::ValidateL end"); |
|
141 } |
|
142 |
|
143 TBool CRsImageParser::Validate( TInt aStartOfDataOffset, TInt aWidth, TInt aHeight, TInt aSize, TInt aEncoding, TInt aErr ) |
|
144 { |
|
145 if ( aStartOfDataOffset && aWidth && aHeight && aSize && aEncoding && !aErr ) |
|
146 { |
|
147 return ETrue; |
|
148 } |
|
149 else |
|
150 { |
|
151 return EFalse; |
|
152 } |
|
153 } |
|
154 |
|
155 TInt CRsImageParser::ReadData( RFile& aFile, TInt aStartByte, TDes8& aData ) |
|
156 { |
|
157 LOG("CRsImageParser::ReadData begin"); |
|
158 TInt err = aFile.Read( aStartByte, aData ); |
|
159 LOG1("CRsImageParser::ReadData return: %d", err); |
|
160 return err; |
|
161 } |
|
162 |
|
163 // End of File |
|