|
1 // Copyright (c) 1997-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 // Licensed under US Patent No 4,558,302 and foreign counterparts |
|
15 // |
|
16 // |
|
17 |
|
18 #include <imageconversion.h> |
|
19 #include "ImageClientMain.h" |
|
20 #include "ImageUtils.h" |
|
21 #include <barsc.h> |
|
22 #include <barsread.h> |
|
23 #include <bautils.h> |
|
24 #include <101F45B1_extra.rsg> |
|
25 #include "GIFConvert.h" |
|
26 #include "icl/ICL_UIDS.hrh" |
|
27 #include <gifscaler.h> |
|
28 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS |
|
29 #include <icl/icl_uids_const.hrh> |
|
30 #include <icl/icl_uids_def.hrh> |
|
31 #include <icl/imagecodecdef.h> |
|
32 #endif |
|
33 |
|
34 _LIT(KGIFPanicCategory, "GIFConvertPlugin"); |
|
35 |
|
36 // Global panic function |
|
37 GLDEF_C void Panic(TIclPanic aError) |
|
38 { |
|
39 User::Panic(KGIFPanicCategory, aError); |
|
40 } |
|
41 |
|
42 CGifDecoder* CGifDecoder::NewL() |
|
43 { |
|
44 return new(ELeave) CGifDecoder; |
|
45 } |
|
46 |
|
47 CGifDecoder::CGifDecoder() |
|
48 { |
|
49 } |
|
50 |
|
51 CGifDecoder::~CGifDecoder() |
|
52 { |
|
53 CImageDecoderPlugin::Cleanup(); |
|
54 } |
|
55 |
|
56 void CGifDecoder::ImageType(TInt aFrameNumber, TUid& aImageType, TUid& aImageSubType) const |
|
57 { |
|
58 __ASSERT_ALWAYS((aFrameNumber >= 0) && (aFrameNumber < NumberOfFrames()), Panic(EFrameNumberOutOfRange)); |
|
59 aImageType = KImageTypeGIFUid; |
|
60 aImageSubType = KNullUid; |
|
61 } |
|
62 |
|
63 TInt CGifDecoder::NumberOfImageComments() const |
|
64 { |
|
65 __ASSERT_ALWAYS(IsImageHeaderProcessingComplete(), Panic(EHeaderProcessingNotComplete)); |
|
66 const CFrameImageData& frameImageData = FrameData(0); |
|
67 TInt imageCommentCount = 0; |
|
68 const TInt imageDataCount = frameImageData.ImageDataCount(); |
|
69 for (TInt count = 0; count < imageDataCount; count++) |
|
70 { |
|
71 const TImageDataBlock* imageData = frameImageData.GetImageData(count); |
|
72 if (imageData->DataType() == KGIFCommentUid) |
|
73 imageCommentCount++; |
|
74 } |
|
75 |
|
76 return imageCommentCount; |
|
77 } |
|
78 |
|
79 HBufC* CGifDecoder::ImageCommentL(TInt aCommentNumber) const |
|
80 { |
|
81 __ASSERT_ALWAYS(IsImageHeaderProcessingComplete(), Panic(EHeaderProcessingNotComplete)); |
|
82 __ASSERT_ALWAYS((aCommentNumber >= 0) && (aCommentNumber < NumberOfImageComments()), Panic(ECommentNumberOutOfRange)); |
|
83 |
|
84 const CFrameImageData& frameImageData = FrameData(0); |
|
85 TInt commentCount = 0; |
|
86 TInt imageDataCount = frameImageData.ImageDataCount(); |
|
87 const TImageDataBlock* imageData = NULL; |
|
88 for (TInt count = 0; count < imageDataCount; count++) |
|
89 { |
|
90 imageData = frameImageData.GetImageData(count); |
|
91 if (imageData->DataType() == KGIFCommentUid) |
|
92 { |
|
93 if (commentCount == aCommentNumber) |
|
94 { |
|
95 break; |
|
96 } |
|
97 commentCount++; |
|
98 } |
|
99 } |
|
100 |
|
101 const TGifComment* gifComment = STATIC_CAST(const TGifComment*,imageData); |
|
102 HBufC* comment = NULL; |
|
103 if (gifComment) |
|
104 { |
|
105 comment = HBufC::NewL(gifComment->iComment->Length()); |
|
106 comment->Des().Copy(*(gifComment->iComment)); // Create a 16 bit copy of the 8 bit original |
|
107 } |
|
108 |
|
109 return comment; |
|
110 } |
|
111 |
|
112 void CGifDecoder::ScanDataL() |
|
113 { |
|
114 ReadFormatL(); |
|
115 |
|
116 ASSERT(ImageReadCodec()==NULL); |
|
117 CGifReadCodec* imageReadCodec = CGifReadCodec::NewL(iGlobalPalette, iFileInfo.iBackgroundColorIndex, iFileInfo.iScreenSize, |
|
118 ( (DecoderOptions() & CImageDecoder::EPreferFastDecode) == CImageDecoder::EPreferFastDecode) |
|
119 ); |
|
120 |
|
121 SetImageReadCodec(imageReadCodec); |
|
122 |
|
123 if((DecoderOptions()&CImageDecoder::EOptionUseFrameSizeInPixels) == CImageDecoder::EOptionUseFrameSizeInPixels) |
|
124 { |
|
125 imageReadCodec->SetUseFrameSizeInPixels(ETrue); |
|
126 } |
|
127 |
|
128 ReadFrameHeadersL(); |
|
129 } |
|
130 |
|
131 void CGifDecoder::ReadFormatL() |
|
132 { |
|
133 TPtrC8 bufferDes; |
|
134 |
|
135 // Read the header (+ 1 extra byte, so that we can check the first block id is valid) |
|
136 ReadDataL(0, bufferDes, (KGifFileInformationSize + (KGifColorTableMaxEntries * KGifPaletteEntrySize) + 1)); |
|
137 |
|
138 // Validate the header. |
|
139 if (bufferDes.Length() < KGifFileInformationSize) |
|
140 User::Leave(KErrUnderflow); |
|
141 |
|
142 iFileInfo.iSignature = bufferDes.Left(KGifSignatureLength); |
|
143 if (iFileInfo.iSignature != KGif87aFileSignature && |
|
144 iFileInfo.iSignature != KGif89aFileSignature) |
|
145 User::Leave(KErrCorrupt); |
|
146 |
|
147 const TUint8* ptr = &bufferDes[KGifSignatureLength]; |
|
148 iFileInfo.iScreenSize.iWidth = PtrReadUtil::ReadUint16(ptr); |
|
149 ptr+=2; |
|
150 |
|
151 iFileInfo.iScreenSize.iHeight = PtrReadUtil::ReadUint16(ptr); |
|
152 ptr+=2; |
|
153 |
|
154 TUint8 flags = *ptr++; |
|
155 iFileInfo.iBitsPerPixel = (flags & 0x07) + 1; |
|
156 iFileInfo.iColorResolutionBits = ((flags & 0x70) >> 4) + 1; |
|
157 iFileInfo.iGlobalColorMap = flags & 0x80; |
|
158 if (iFileInfo.iSignature == KGif89aFileSignature) |
|
159 iFileInfo.iSortedGlobalMap = flags & 0x08; |
|
160 |
|
161 if (iFileInfo.iGlobalColorMap) |
|
162 { |
|
163 iFileInfo.iBackgroundColorIndex = *ptr; |
|
164 iGlobalPaletteEntries = 1 << iFileInfo.iBitsPerPixel; |
|
165 } |
|
166 else |
|
167 { |
|
168 iFileInfo.iBackgroundColorIndex = KErrNotFound; |
|
169 iGlobalPaletteEntries = 0; |
|
170 } |
|
171 |
|
172 ptr++; |
|
173 TUint8 pixelAspectRatio = *ptr; |
|
174 if (iFileInfo.iSignature == KGif87aFileSignature) |
|
175 { |
|
176 iFileInfo.iPixelAspectRatio = pixelAspectRatio & 0x7f; |
|
177 iFileInfo.iSortedGlobalMap = pixelAspectRatio & 0x80; |
|
178 } |
|
179 else |
|
180 iFileInfo.iPixelAspectRatio = pixelAspectRatio; |
|
181 |
|
182 if (iFileInfo.iGlobalColorMap) |
|
183 { |
|
184 if (bufferDes.Length() < (KGifFileInformationSize + iGlobalPaletteEntries * KGifPaletteEntrySize)) |
|
185 User::Leave(KErrUnderflow); |
|
186 |
|
187 const TUint8* ptr = &bufferDes[KGifFileInformationSize]; |
|
188 TRgb* palettePtr = iGlobalPalette; |
|
189 TRgb* palettePtrLimit = iGlobalPalette + iGlobalPaletteEntries; |
|
190 |
|
191 while (palettePtr < palettePtrLimit) |
|
192 { |
|
193 *palettePtr++ = TRgb(ptr[0],ptr[1],ptr[2]); |
|
194 ptr += KGifPaletteEntrySize; |
|
195 } |
|
196 |
|
197 palettePtrLimit = iGlobalPalette + KGifColorTableMaxEntries; |
|
198 while (palettePtr < palettePtrLimit) |
|
199 *palettePtr++ = KRgbWhite; |
|
200 |
|
201 TGifBackgroundColor* gifBackgroundColor = new(ELeave) TGifBackgroundColor; |
|
202 gifBackgroundColor->iBackgroundColorIndex = iFileInfo.iBackgroundColorIndex; |
|
203 gifBackgroundColor->iBackgroundColor = iGlobalPalette[iFileInfo.iBackgroundColorIndex]; |
|
204 CleanupStack::PushL(gifBackgroundColor); |
|
205 |
|
206 User::LeaveIfError(AppendImageData(gifBackgroundColor)); |
|
207 CleanupStack::Pop(); // gifBackgroundColor |
|
208 } |
|
209 |
|
210 if (bufferDes.Length() < (KGifFileInformationSize + (iGlobalPaletteEntries * KGifPaletteEntrySize)) + 1) |
|
211 User::Leave(KErrUnderflow); |
|
212 |
|
213 // Check that first byte after the global color table is a valid block id |
|
214 TUint8 nextBlockId = bufferDes[KGifFileInformationSize + (iGlobalPaletteEntries * KGifPaletteEntrySize)]; |
|
215 switch (nextBlockId) |
|
216 { |
|
217 case KGifExtensionId: |
|
218 case KGifImageDescriptorId: |
|
219 case KGifPlainTextExtensionId: |
|
220 case KGifGraphicControlExtensionId: |
|
221 case KGifCommentExtensionId: |
|
222 case KGifApplicationExtensionId: |
|
223 break; |
|
224 |
|
225 default: |
|
226 User::Leave(KErrCorrupt); |
|
227 } |
|
228 |
|
229 TInt startPosition = KGifFileInformationSize; |
|
230 if (iFileInfo.iGlobalColorMap) |
|
231 startPosition += iGlobalPaletteEntries * KGifPaletteEntrySize; |
|
232 SetStartPosition(startPosition); |
|
233 |
|
234 TFrameInfo imageInfo; |
|
235 imageInfo = ImageInfo(); |
|
236 imageInfo.iOverallSizeInPixels = iFileInfo.iScreenSize; |
|
237 imageInfo.iBitsPerPixel = iFileInfo.iBitsPerPixel; |
|
238 imageInfo.iFrameSizeInTwips.SetSize(0,0); |
|
239 if (iFileInfo.iGlobalColorMap) |
|
240 { |
|
241 imageInfo.iBackgroundColor = iGlobalPalette[iFileInfo.iBackgroundColorIndex]; |
|
242 if (iFileInfo.iColorResolutionBits<=4) |
|
243 imageInfo.iFrameDisplayMode = EColor4K; |
|
244 else |
|
245 imageInfo.iFrameDisplayMode = EColor16M; |
|
246 } |
|
247 else |
|
248 imageInfo.iFrameDisplayMode = EColor256; |
|
249 imageInfo.iDelay = KErrNotFound; |
|
250 |
|
251 SetImageInfo(imageInfo); |
|
252 SetDataLength(KMaxTInt); // Set default data length in case format header doesn't contain this information |
|
253 } |
|
254 |
|
255 CFrameInfoStrings* CGifDecoder::FrameInfoStringsL(RFs& aFs, TInt aFrameNumber) |
|
256 { |
|
257 const TUid KGifCodecDllUid = {KGIFCodecDllUidValue}; |
|
258 |
|
259 RResourceFile resourceFile; |
|
260 OpenExtraResourceFileLC(aFs,KGifCodecDllUid,resourceFile); |
|
261 |
|
262 HBufC8* resourceInfo = resourceFile.AllocReadLC(THEDECODERINFO); |
|
263 TResourceReader resourceReader; |
|
264 resourceReader.SetBuffer(resourceInfo); |
|
265 |
|
266 TBuf<KCodecResourceStringMax> info; |
|
267 TBuf<KCodecResourceStringMax> templte; |
|
268 |
|
269 const TFrameInfo& frameInfo = FrameInfo(aFrameNumber); |
|
270 CFrameInfoStrings* frameInfoStrings = CFrameInfoStrings::NewLC(); |
|
271 |
|
272 info = resourceReader.ReadTPtrC(); |
|
273 frameInfoStrings->SetDecoderL(info); |
|
274 |
|
275 CDesCArrayFlat* resourceArray = resourceReader.ReadDesCArrayL(); |
|
276 CleanupStack::PushL(resourceArray); |
|
277 TUint formatIndex = (iFileInfo.iSignature == KGif87aFileSignature) ? 0 : 1; |
|
278 info = (*resourceArray)[formatIndex]; |
|
279 CleanupStack::PopAndDestroy(resourceArray); |
|
280 frameInfoStrings->SetFormatL(info); |
|
281 |
|
282 TInt width = frameInfo.iOverallSizeInPixels.iWidth; |
|
283 TInt height = frameInfo.iOverallSizeInPixels.iHeight; |
|
284 TInt depth = frameInfo.iBitsPerPixel; |
|
285 |
|
286 templte = resourceReader.ReadTPtrC(); |
|
287 info.Format(templte, width, height); |
|
288 frameInfoStrings->SetDimensionsL(info); |
|
289 |
|
290 templte = resourceReader.ReadTPtrC(); |
|
291 info.Format(templte, depth); |
|
292 frameInfoStrings->SetDepthL(info); |
|
293 |
|
294 // leave details blank |
|
295 |
|
296 CleanupStack::Pop(frameInfoStrings); |
|
297 CleanupStack::PopAndDestroy(2); // resourceInfo + resourceFile |
|
298 return frameInfoStrings; |
|
299 } |
|
300 |
|
301 // Gif encoder. |
|
302 CGifEncoder* CGifEncoder::NewL() |
|
303 { |
|
304 CGifEncoder* self = new(ELeave) CGifEncoder; |
|
305 CleanupStack::PushL(self); |
|
306 self->ConstructL(); |
|
307 CleanupStack::Pop(self); |
|
308 return self; |
|
309 |
|
310 } |
|
311 |
|
312 CGifEncoder::CGifEncoder() |
|
313 { |
|
314 } |
|
315 |
|
316 void CGifEncoder::ConstructL() |
|
317 { |
|
318 iPalette = CPalette::NewDefaultL(EColor256); |
|
319 } |
|
320 |
|
321 CGifEncoder::~CGifEncoder() |
|
322 { |
|
323 if (iGifScaler) |
|
324 { |
|
325 iGifScaler->Cancel(); |
|
326 delete iGifScaler; |
|
327 } |
|
328 delete iPalette; |
|
329 delete iDest; |
|
330 CImageEncoderPlugin::Cleanup(); |
|
331 } |
|
332 |
|
333 const CPalette* CGifEncoder::Palette() const |
|
334 { |
|
335 return iPalette; |
|
336 } |
|
337 |
|
338 void CGifEncoder::WriteExifDataL(TRequestStatus*& aScaleCompletionStatus) |
|
339 { |
|
340 if ((EncoderOptions() & CImageEncoder::EOptionGenerateAdaptivePalette) == CImageEncoder::EOptionGenerateAdaptivePalette) |
|
341 { |
|
342 if (!iPalette) |
|
343 { |
|
344 iPalette = CPalette::NewL(KGifColorTableMaxEntries); |
|
345 } |
|
346 iGifScaler->Scale(aScaleCompletionStatus, *iDest, *iPalette); |
|
347 } |
|
348 else |
|
349 { |
|
350 // the framework waits for aScaleCompletionStatus to complete |
|
351 SelfComplete(KErrNone); |
|
352 } |
|
353 } |
|
354 |
|
355 void CGifEncoder::PrepareEncoderL(const CFrameImageData* aFrameImageData) |
|
356 { |
|
357 // Create the codec. |
|
358 StartPosition() = KGifFileInformationSize; |
|
359 StartPosition() += KGifColorTableMaxEntries * KGifPaletteEntrySize; |
|
360 ASSERT(ImageWriteCodec() == NULL); |
|
361 |
|
362 // ensure iPalette is NULL to start with since CGifWriteCodec::FillBufferL() behaviour depends on it |
|
363 delete iPalette; |
|
364 iPalette = NULL; |
|
365 |
|
366 // check to see if client has supplied a palette |
|
367 if (aFrameImageData) |
|
368 { |
|
369 TInt frameDataCount = aFrameImageData->FrameDataCount(); |
|
370 TGifColorTable* gifColorTable = NULL; |
|
371 for (TInt frameDataIndex = 0; frameDataIndex < frameDataCount; frameDataIndex++) |
|
372 { |
|
373 TFrameDataBlock* frameData = const_cast<TFrameDataBlock*>(aFrameImageData->GetFrameData(frameDataIndex)); |
|
374 if (frameData->DataType() == KGIFColorTableUid) |
|
375 { |
|
376 gifColorTable = static_cast<TGifColorTable*>(frameData); |
|
377 } |
|
378 } |
|
379 |
|
380 if (gifColorTable) |
|
381 { |
|
382 if (!iPalette) |
|
383 { |
|
384 iPalette = CPalette::NewL(KGifColorTableMaxEntries); |
|
385 } |
|
386 for (TInt i = 0; i < KGifColorTableMaxEntries; i++) |
|
387 { |
|
388 iPalette->SetEntry(i, gifColorTable->iPalette[i]); |
|
389 } |
|
390 } |
|
391 } |
|
392 |
|
393 if ((EncoderOptions() & CImageEncoder::EOptionGenerateAdaptivePalette) == CImageEncoder::EOptionGenerateAdaptivePalette) |
|
394 { |
|
395 CFbsBitmap* sourceBitmap = const_cast<CFbsBitmap*>(&Source()); |
|
396 delete iGifScaler; |
|
397 iGifScaler = NULL; |
|
398 iGifScaler = CGifScaler::NewL(*sourceBitmap); |
|
399 |
|
400 delete iDest; |
|
401 iDest = NULL; |
|
402 iDest = new (ELeave) CFbsBitmap; |
|
403 User::LeaveIfError(iDest->Create(sourceBitmap->SizeInPixels(), EColor256)); |
|
404 } |
|
405 |
|
406 CGifWriteCodec* imageWriteCodec = CGifWriteCodec::NewL(*this); |
|
407 |
|
408 SetImageWriteCodec(imageWriteCodec); |
|
409 |
|
410 } |
|
411 |
|
412 void CGifEncoder::UpdateHeaderL() |
|
413 { |
|
414 TInt headerSize = KGifFileInformationSize + (KGifColorTableMaxEntries * KGifPaletteEntrySize); |
|
415 |
|
416 HBufC8* gifHeaderPtr = HBufC8::NewMaxLC(headerSize); |
|
417 TUint8* headerPtr = &gifHeaderPtr->Des()[0]; |
|
418 |
|
419 Mem::Copy(headerPtr,&KGif87aFileSignature()[0],KGifSignatureLength); headerPtr += KGifSignatureLength; |
|
420 |
|
421 TInt width = Source().SizeInPixels().iWidth; |
|
422 TInt height = Source().SizeInPixels().iHeight; |
|
423 PtrWriteUtil::WriteInt16(headerPtr, width); |
|
424 headerPtr+=2; |
|
425 PtrWriteUtil::WriteInt16(headerPtr, height); |
|
426 headerPtr+=2; |
|
427 TUint8 resolutionFlag = 0; |
|
428 resolutionFlag |= 8 - 1; // Bpp - 1 |
|
429 resolutionFlag |= (8 - 1) << 4; // Color Resolution |
|
430 resolutionFlag |= 0x80; // Global Color Table Flag |
|
431 *headerPtr++ = resolutionFlag; |
|
432 *headerPtr++ = 255; // Background Color Index |
|
433 *headerPtr++ = 0; // Pixel Aspect Ratio |
|
434 |
|
435 for (TInt paletteIndex = 0; paletteIndex < KGifColorTableMaxEntries; paletteIndex++) |
|
436 { |
|
437 TRgb entry; |
|
438 if (iPalette) |
|
439 { |
|
440 entry = iPalette->GetEntry(paletteIndex); |
|
441 } |
|
442 else |
|
443 { |
|
444 entry = TRgb::Color256(paletteIndex); |
|
445 } |
|
446 |
|
447 headerPtr[0] = (TUint8)entry.Red(); |
|
448 headerPtr[1] = (TUint8)entry.Green(); |
|
449 headerPtr[2] = (TUint8)entry.Blue(); |
|
450 headerPtr += 3; |
|
451 } |
|
452 |
|
453 TPtr8 bufferDes(gifHeaderPtr->Des()); |
|
454 WriteDataL(0,bufferDes); |
|
455 |
|
456 CleanupStack::PopAndDestroy(); // gifHeaderPtr |
|
457 } |
|
458 |