19
|
1 |
/*
|
|
2 |
* Copyright (c) 2010 Kanrikogaku Kenkyusho, Ltd.
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "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 |
* Kanrikogaku Kenkyusho, Ltd. - Initial contribution
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description: Contains the CDirectPrintRsImageParser class definition.
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
|
|
19 |
// DirectPrintRsImageParser.cpp: implementation of the CDirectPrintRsImageParser class.
|
|
20 |
//
|
|
21 |
//////////////////////////////////////////////////////////////////////
|
|
22 |
|
|
23 |
#include "directprintrsimageparser.h"
|
|
24 |
#include "clog.h"
|
|
25 |
|
|
26 |
//////////////////////////////////////////////////////////////////////
|
|
27 |
// Construction/Destruction
|
|
28 |
//////////////////////////////////////////////////////////////////////
|
|
29 |
|
|
30 |
CDirectPrintRsImageParser* CDirectPrintRsImageParser::NewLC()
|
|
31 |
{
|
|
32 |
CDirectPrintRsImageParser* self = new (ELeave) CDirectPrintRsImageParser();
|
|
33 |
CleanupStack::PushL(self);
|
|
34 |
self->ConstructL();
|
|
35 |
return self;
|
|
36 |
}
|
|
37 |
|
|
38 |
|
|
39 |
void CDirectPrintRsImageParser::ConstructL()
|
|
40 |
{
|
|
41 |
User::LeaveIfError(iFs.Connect());
|
|
42 |
}
|
|
43 |
|
|
44 |
|
|
45 |
CDirectPrintRsImageParser::~CDirectPrintRsImageParser()
|
|
46 |
{
|
|
47 |
iFs.Close();
|
|
48 |
}
|
|
49 |
|
|
50 |
CDirectPrintRsImageParser::CDirectPrintRsImageParser()
|
|
51 |
{
|
|
52 |
}
|
|
53 |
|
|
54 |
void CDirectPrintRsImageParser::ValidateL( const TDesC& aFileName, TBool& aValid, TInt& aErr )
|
|
55 |
{
|
|
56 |
LOG1("CDirectPrintRsImageParser::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("CDirectPrintRsImageParser::ValidateL file.Open (shareread mode): %d", aErr);
|
|
70 |
if ( aErr != KErrNone )
|
|
71 |
{
|
|
72 |
aErr = file.Open( iFs, aFileName, EFileRead | EFileShareAny );
|
|
73 |
LOG1("CDirectPrintRsImageParser::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("CDirectPrintRsImageParser::ValidateL aValid: %d", aValid);
|
|
139 |
LOG1("CDirectPrintRsImageParser::ValidateL aErr: %d", aErr);
|
|
140 |
LOG("CDirectPrintRsImageParser::ValidateL end");
|
|
141 |
}
|
|
142 |
|
|
143 |
TBool CDirectPrintRsImageParser::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 CDirectPrintRsImageParser::ReadData( RFile& aFile, TInt aStartByte, TDes8& aData )
|
|
156 |
{
|
|
157 |
LOG("CDirectPrintRsImageParser::ReadData begin");
|
|
158 |
TInt err = aFile.Read( aStartByte, aData );
|
|
159 |
LOG1("CDirectPrintRsImageParser::ReadData return: %d", err);
|
|
160 |
return err;
|
|
161 |
}
|
|
162 |
|
|
163 |
// End of File
|