|
1 // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include <s32file.h> |
|
17 #include <fbs.h> |
|
18 #include <bitmap.h> |
|
19 #include <graphics/bitmapuid.h> |
|
20 #include "fbsbitmapasyncstreamer.h" |
|
21 |
|
22 CFbsBitmapAsyncStreamer::CFbsBitmapAsyncStreamer(TMode aMode): |
|
23 CBase(), |
|
24 iFbs(RFbsSession::GetSession()), |
|
25 iMode(aMode) |
|
26 {} |
|
27 |
|
28 EXPORT_C CFbsBitmapAsyncStreamer::~CFbsBitmapAsyncStreamer() |
|
29 { |
|
30 iReadStream.Close(); |
|
31 iWriteStream.Close(); |
|
32 delete iStore; |
|
33 if(iMode==ELoad) delete iBitmap; |
|
34 } |
|
35 |
|
36 EXPORT_C CFbsBitmapAsyncStreamer* CFbsBitmapAsyncStreamer::NewL(TMode aMode) |
|
37 { |
|
38 CFbsBitmapAsyncStreamer* thisptr=new(ELeave) CFbsBitmapAsyncStreamer(aMode); |
|
39 CleanupStack::PushL(thisptr); |
|
40 thisptr->ConstructL(); |
|
41 CleanupStack::Pop(); |
|
42 return(thisptr); |
|
43 } |
|
44 |
|
45 void CFbsBitmapAsyncStreamer::ConstructL() |
|
46 { |
|
47 if(iMode==ELoad) iBitmap=new(ELeave) CFbsBitmap; |
|
48 } |
|
49 |
|
50 EXPORT_C TInt CFbsBitmapAsyncStreamer::Load(const TDesC& aFilename,TInt32 aId,TInt& aScanLines) |
|
51 { |
|
52 if(iMode!=ELoad) return(KErrGeneral); |
|
53 |
|
54 TRAPD(ret,DoLoadL(aFilename,aId)); |
|
55 if(ret!=KErrNone) return(ret); |
|
56 |
|
57 iDispMode = CBitwiseBitmap::DisplayMode(iHeader.iBitsPerPixel,iHeader.iColor); |
|
58 |
|
59 ret=iBitmap->Create(iHeader.iSizeInPixels,iDispMode); |
|
60 iBitmap->BeginDataAccess(); |
|
61 iBitmap->Address()->iHeader=iHeader; |
|
62 if(ret!=KErrNone) return(ret); |
|
63 |
|
64 aScanLines=iHeader.iSizeInPixels.iHeight; |
|
65 iScanLineBase=(TUint32*)iBitmap->DataAddress(); |
|
66 return(KErrNone); |
|
67 } |
|
68 |
|
69 void CFbsBitmapAsyncStreamer::DoLoadL(const TDesC& aFilename,TInt32 aId) |
|
70 { |
|
71 iStore=CDirectFileStore::OpenL(iFbs->FileServer(),aFilename,EFileShareReadersOnly); |
|
72 if (iStore->Type()[1]!=KMultiBitmapFileImageUid) |
|
73 User::LeaveIfError(KErrGeneral); |
|
74 TStreamId streamid=iStore->Root(); |
|
75 RStoreReadStream readstream; |
|
76 readstream.OpenL(*iStore,streamid); |
|
77 TInt numbitmaps=readstream.ReadInt32L(); |
|
78 if(numbitmaps<aId+1) |
|
79 User::Leave(KErrUnknown); |
|
80 TStreamId bmpstreamid; |
|
81 bmpstreamid.InternalizeL(readstream); |
|
82 for(TInt count=0;count<aId;count++) |
|
83 bmpstreamid.InternalizeL(readstream); |
|
84 readstream.Close(); |
|
85 iReadStream.OpenL(*iStore,bmpstreamid); |
|
86 CBitwiseBitmap::InternalizeHeaderL(iReadStream,iHeader); |
|
87 } |
|
88 |
|
89 EXPORT_C TBool CFbsBitmapAsyncStreamer::LoadScanLinesL(TInt aNumberOfScanLines,CFbsBitmap*& aBitmap) |
|
90 { |
|
91 if(iMode!=ELoad || iBitmap==NULL) User::Leave(KErrGeneral); |
|
92 if(iHeader.iCompression!=ENoBitmapCompression) |
|
93 { |
|
94 TRAP_IGNORE(iBitmap->Address()->DoInternalizeL(iReadStream,iHeader.iBitmapSize-iHeader.iStructSize,iBitmap->DataAddress())); |
|
95 iBitmap->EndDataAccess(EFalse); |
|
96 aBitmap=iBitmap; |
|
97 iBitmap=NULL; |
|
98 return(ETrue); |
|
99 } |
|
100 aNumberOfScanLines=Abs(aNumberOfScanLines); |
|
101 if(aNumberOfScanLines+iCurrentScanLine>iHeader.iSizeInPixels.iHeight) |
|
102 aNumberOfScanLines=iHeader.iSizeInPixels.iHeight-iCurrentScanLine; |
|
103 TInt slbytes = CBitwiseBitmap::ByteWidth(iHeader.iSizeInPixels.iWidth,iDispMode); |
|
104 TUint8* sladd=(TUint8*)iBitmap->Address()->ScanLineAddress(iScanLineBase,iCurrentScanLine); |
|
105 iReadStream.ReadL(sladd,aNumberOfScanLines*slbytes); |
|
106 iCurrentScanLine+=aNumberOfScanLines; |
|
107 if(iCurrentScanLine==iHeader.iSizeInPixels.iHeight) |
|
108 { |
|
109 iBitmap->EndDataAccess(EFalse); |
|
110 aBitmap=iBitmap; |
|
111 iBitmap=NULL; |
|
112 return(ETrue); |
|
113 } |
|
114 return(EFalse); |
|
115 } |
|
116 |
|
117 EXPORT_C TInt CFbsBitmapAsyncStreamer::Save(const TDesC& aFilename,CFbsBitmap* aBitmap,TInt32& aId,TInt& aScanLines) |
|
118 { |
|
119 if(iMode!=ESave || aBitmap==NULL || !aBitmap->iHandle) return(KErrGeneral); |
|
120 RFile file; |
|
121 TInt ret=file.Replace(iFbs->FileServer(),aFilename,EFileWrite); |
|
122 if(ret!=KErrNone) return(ret); |
|
123 iHeader=aBitmap->Header(); |
|
124 |
|
125 iDispMode = CBitwiseBitmap::DisplayMode(iHeader.iBitsPerPixel,iHeader.iColor); |
|
126 |
|
127 TRAP(ret,DoSaveL(file)); |
|
128 if(ret!=KErrNone) return(ret); |
|
129 aId=0; |
|
130 iBitmap=aBitmap; |
|
131 iBitmap->BeginDataAccess(); |
|
132 aScanLines=iHeader.iSizeInPixels.iHeight; |
|
133 iScanLineBase=(TUint32*)aBitmap->DataAddress(); |
|
134 return(KErrNone); |
|
135 } |
|
136 |
|
137 void CFbsBitmapAsyncStreamer::DoSaveL(RFile& aFile) |
|
138 { |
|
139 iStore=CDirectFileStore::NewL(aFile); |
|
140 TUidType uidtype(KDirectFileStoreLayoutUid,KMultiBitmapFileImageUid); |
|
141 iStore->SetTypeL(uidtype); |
|
142 iId=iWriteStream.CreateL(*iStore); |
|
143 iWriteStream.WriteInt32L(iHeader.iBitmapSize); |
|
144 iWriteStream.WriteInt32L(iHeader.iStructSize); |
|
145 iWriteStream.WriteInt32L(iHeader.iSizeInPixels.iWidth); |
|
146 iWriteStream.WriteInt32L(iHeader.iSizeInPixels.iHeight); |
|
147 iWriteStream.WriteInt32L(iHeader.iSizeInTwips.iWidth); |
|
148 iWriteStream.WriteInt32L(iHeader.iSizeInTwips.iHeight); |
|
149 iWriteStream.WriteInt32L(iHeader.iBitsPerPixel); |
|
150 iWriteStream.WriteUint32L(iHeader.iColor); |
|
151 iWriteStream.WriteInt32L(0); |
|
152 iWriteStream.WriteUint32L(iHeader.iCompression); |
|
153 } |
|
154 |
|
155 EXPORT_C TBool CFbsBitmapAsyncStreamer::SaveScanLinesL(TInt aNumberOfScanLines) |
|
156 { |
|
157 if(iMode!=ESave || iBitmap==NULL) User::Leave(KErrGeneral); |
|
158 aNumberOfScanLines=Abs(aNumberOfScanLines); |
|
159 if(aNumberOfScanLines+iCurrentScanLine>iHeader.iSizeInPixels.iHeight) |
|
160 aNumberOfScanLines=iHeader.iSizeInPixels.iHeight-iCurrentScanLine; |
|
161 TInt slbytes = CBitwiseBitmap::ByteWidth(iHeader.iSizeInPixels.iWidth,iDispMode); |
|
162 TUint8* sladd=(TUint8*)iBitmap->Address()->ScanLineAddress(iScanLineBase,iCurrentScanLine); |
|
163 iWriteStream.WriteL(sladd,aNumberOfScanLines*slbytes); |
|
164 iCurrentScanLine+=aNumberOfScanLines; |
|
165 if(iCurrentScanLine==iHeader.iSizeInPixels.iHeight) |
|
166 { |
|
167 iBitmap->EndDataAccess(ETrue); |
|
168 iWriteStream.Close(); |
|
169 TStreamId headstreamid=iWriteStream.CreateL(*iStore); |
|
170 iWriteStream.WriteInt32L(1); // number of bitmaps |
|
171 iId.ExternalizeL(iWriteStream); // stream id of bitmap |
|
172 iWriteStream.Close(); |
|
173 iStore->SetRootL(headstreamid); |
|
174 delete iStore; |
|
175 iStore=NULL; |
|
176 return(ETrue); |
|
177 } |
|
178 return(EFalse); |
|
179 } |
|
180 |