Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: third_party/WebKit/Source/platform/image-decoders/ImageDecoder.cpp

Issue 1812273003: Eliminate copies of encoded image data (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved. 2 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
3 * 3 *
4 * This library is free software; you can redistribute it and/or 4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public 5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either 6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version. 7 * version 2 of the License, or (at your option) any later version.
8 * 8 *
9 * This library is distributed in the hope that it will be useful, 9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 14 matching lines...) Expand all
25 #include "platform/image-decoders/bmp/BMPImageDecoder.h" 25 #include "platform/image-decoders/bmp/BMPImageDecoder.h"
26 #include "platform/image-decoders/gif/GIFImageDecoder.h" 26 #include "platform/image-decoders/gif/GIFImageDecoder.h"
27 #include "platform/image-decoders/ico/ICOImageDecoder.h" 27 #include "platform/image-decoders/ico/ICOImageDecoder.h"
28 #include "platform/image-decoders/jpeg/JPEGImageDecoder.h" 28 #include "platform/image-decoders/jpeg/JPEGImageDecoder.h"
29 #include "platform/image-decoders/png/PNGImageDecoder.h" 29 #include "platform/image-decoders/png/PNGImageDecoder.h"
30 #include "platform/image-decoders/webp/WEBPImageDecoder.h" 30 #include "platform/image-decoders/webp/WEBPImageDecoder.h"
31 #include "wtf/PassOwnPtr.h" 31 #include "wtf/PassOwnPtr.h"
32 32
33 namespace blink { 33 namespace blink {
34 34
35 static size_t copyFromSharedBuffer(char* buffer, size_t bufferLength, const Shar edBuffer& sharedBuffer, size_t offset) 35 inline bool matchesJPEGSignature(const char* contents)
36 {
37 size_t bytesExtracted = 0;
38 const char* moreData;
39 while (size_t moreDataLength = sharedBuffer.getSomeData(moreData, offset)) {
40 size_t bytesToCopy = std::min(bufferLength - bytesExtracted, moreDataLen gth);
41 memcpy(buffer + bytesExtracted, moreData, bytesToCopy);
42 bytesExtracted += bytesToCopy;
43 if (bytesExtracted == bufferLength)
44 break;
45 offset += bytesToCopy;
46 }
47 return bytesExtracted;
48 }
49
50 inline bool matchesJPEGSignature(char* contents)
51 { 36 {
52 return !memcmp(contents, "\xFF\xD8\xFF", 3); 37 return !memcmp(contents, "\xFF\xD8\xFF", 3);
53 } 38 }
54 39
55 inline bool matchesPNGSignature(char* contents) 40 inline bool matchesPNGSignature(const char* contents)
56 { 41 {
57 return !memcmp(contents, "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", 8); 42 return !memcmp(contents, "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", 8);
58 } 43 }
59 44
60 inline bool matchesGIFSignature(char* contents) 45 inline bool matchesGIFSignature(const char* contents)
61 { 46 {
62 return !memcmp(contents, "GIF87a", 6) || !memcmp(contents, "GIF89a", 6); 47 return !memcmp(contents, "GIF87a", 6) || !memcmp(contents, "GIF89a", 6);
63 } 48 }
64 49
65 inline bool matchesWebPSignature(char* contents) 50 inline bool matchesWebPSignature(const char* contents)
66 { 51 {
67 return !memcmp(contents, "RIFF", 4) && !memcmp(contents + 8, "WEBPVP", 6); 52 return !memcmp(contents, "RIFF", 4) && !memcmp(contents + 8, "WEBPVP", 6);
68 } 53 }
69 54
70 inline bool matchesICOSignature(char* contents) 55 inline bool matchesICOSignature(const char* contents)
71 { 56 {
72 return !memcmp(contents, "\x00\x00\x01\x00", 4); 57 return !memcmp(contents, "\x00\x00\x01\x00", 4);
73 } 58 }
74 59
75 inline bool matchesCURSignature(char* contents) 60 inline bool matchesCURSignature(const char* contents)
76 { 61 {
77 return !memcmp(contents, "\x00\x00\x02\x00", 4); 62 return !memcmp(contents, "\x00\x00\x02\x00", 4);
78 } 63 }
79 64
80 inline bool matchesBMPSignature(char* contents) 65 inline bool matchesBMPSignature(const char* contents)
81 { 66 {
82 return !memcmp(contents, "BM", 2); 67 return !memcmp(contents, "BM", 2);
83 } 68 }
84 69
85 PassOwnPtr<ImageDecoder> ImageDecoder::create(const SharedBuffer& data, AlphaOpt ion alphaOption, GammaAndColorProfileOption colorOptions) 70 PassOwnPtr<ImageDecoder> ImageDecoder::create(const char* contents, size_t lengt h, AlphaOption alphaOption, GammaAndColorProfileOption colorOptions)
86 { 71 {
87 const size_t longestSignatureLength = sizeof("RIFF????WEBPVP") - 1; 72 const size_t longestSignatureLength = sizeof("RIFF????WEBPVP") - 1;
88 ASSERT(longestSignatureLength == 14); 73 ASSERT(longestSignatureLength == 14);
89 74
75 if (length < longestSignatureLength)
76 return nullptr;
77
90 size_t maxDecodedBytes = Platform::current() ? Platform::current()->maxDecod edImageBytes() : noDecodedImageByteLimit; 78 size_t maxDecodedBytes = Platform::current() ? Platform::current()->maxDecod edImageBytes() : noDecodedImageByteLimit;
91 79
92 char contents[longestSignatureLength];
93 if (copyFromSharedBuffer(contents, longestSignatureLength, data, 0) < longes tSignatureLength)
94 return nullptr;
95
96 if (matchesJPEGSignature(contents)) 80 if (matchesJPEGSignature(contents))
97 return adoptPtr(new JPEGImageDecoder(alphaOption, colorOptions, maxDecod edBytes)); 81 return adoptPtr(new JPEGImageDecoder(alphaOption, colorOptions, maxDecod edBytes));
98 82
99 if (matchesPNGSignature(contents)) 83 if (matchesPNGSignature(contents))
100 return adoptPtr(new PNGImageDecoder(alphaOption, colorOptions, maxDecode dBytes)); 84 return adoptPtr(new PNGImageDecoder(alphaOption, colorOptions, maxDecode dBytes));
101 85
102 if (matchesGIFSignature(contents)) 86 if (matchesGIFSignature(contents))
103 return adoptPtr(new GIFImageDecoder(alphaOption, colorOptions, maxDecode dBytes)); 87 return adoptPtr(new GIFImageDecoder(alphaOption, colorOptions, maxDecode dBytes));
104 88
105 if (matchesWebPSignature(contents)) 89 if (matchesWebPSignature(contents))
106 return adoptPtr(new WEBPImageDecoder(alphaOption, colorOptions, maxDecod edBytes)); 90 return adoptPtr(new WEBPImageDecoder(alphaOption, colorOptions, maxDecod edBytes));
107 91
108 if (matchesICOSignature(contents) || matchesCURSignature(contents)) 92 if (matchesICOSignature(contents) || matchesCURSignature(contents))
109 return adoptPtr(new ICOImageDecoder(alphaOption, colorOptions, maxDecode dBytes)); 93 return adoptPtr(new ICOImageDecoder(alphaOption, colorOptions, maxDecode dBytes));
110 94
111 if (matchesBMPSignature(contents)) 95 if (matchesBMPSignature(contents))
112 return adoptPtr(new BMPImageDecoder(alphaOption, colorOptions, maxDecode dBytes)); 96 return adoptPtr(new BMPImageDecoder(alphaOption, colorOptions, maxDecode dBytes));
113 97
114 return nullptr; 98 return nullptr;
115 } 99 }
116 100
101 PassOwnPtr<ImageDecoder> ImageDecoder::create(const SharedBuffer& data, AlphaOpt ion alphaOption, GammaAndColorProfileOption colorOptions)
102 {
103 const char* contents;
104 const size_t length = data.getSomeData<size_t>(contents);
105 return create(contents, length, alphaOption, colorOptions);
106 }
107
108 PassOwnPtr<ImageDecoder> ImageDecoder::create(const SegmentReader& data, AlphaOp tion alphaOption, GammaAndColorProfileOption colorOptions)
109 {
110 const char* contents;
111 const size_t length = data.getSomeData(contents, 0);
112 return create(contents, length, alphaOption, colorOptions);
113 }
114
117 size_t ImageDecoder::frameCount() 115 size_t ImageDecoder::frameCount()
118 { 116 {
119 const size_t oldSize = m_frameBufferCache.size(); 117 const size_t oldSize = m_frameBufferCache.size();
120 const size_t newSize = decodeFrameCount(); 118 const size_t newSize = decodeFrameCount();
121 if (oldSize != newSize) { 119 if (oldSize != newSize) {
122 m_frameBufferCache.resize(newSize); 120 m_frameBufferCache.resize(newSize);
123 for (size_t i = oldSize; i < newSize; ++i) { 121 for (size_t i = oldSize; i < newSize; ++i) {
124 m_frameBufferCache[i].setPremultiplyAlpha(m_premultiplyAlpha); 122 m_frameBufferCache[i].setPremultiplyAlpha(m_premultiplyAlpha);
125 initializeNewFrame(i); 123 initializeNewFrame(i);
126 } 124 }
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 356
359 qcms_data_type dataFormat = hasAlpha ? QCMS_DATA_RGBA_8 : QCMS_DATA_RGB_8; 357 qcms_data_type dataFormat = hasAlpha ? QCMS_DATA_RGBA_8 : QCMS_DATA_RGB_8;
360 358
361 // FIXME: Don't force perceptual intent if the image profile contains an int ent. 359 // FIXME: Don't force perceptual intent if the image profile contains an int ent.
362 m_sourceToOutputDeviceColorTransform = adoptPtr(qcms_transform_create(inputP rofile.get(), dataFormat, gOutputDeviceProfile, QCMS_DATA_RGBA_8, QCMS_INTENT_PE RCEPTUAL)); 360 m_sourceToOutputDeviceColorTransform = adoptPtr(qcms_transform_create(inputP rofile.get(), dataFormat, gOutputDeviceProfile, QCMS_DATA_RGBA_8, QCMS_INTENT_PE RCEPTUAL));
363 } 361 }
364 362
365 #endif // USE(QCMSLIB) 363 #endif // USE(QCMSLIB)
366 364
367 } // namespace blink 365 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698