| OLD | NEW |
| 1 | 1 |
| 2 #include "SkPdfObject.h" | 2 #include "SkPdfObject.h" |
| 3 #include "SkPdfStreamCommonDictionary_autogen.h" | 3 #include "SkPdfStreamCommonDictionary_autogen.h" |
| 4 | 4 |
| 5 #include "SkFlate.h" | 5 #include "SkFlate.h" |
| 6 #include "SkStream.h" | 6 #include "SkStream.h" |
| 7 #include "SkPdfNativeTokenizer.h" | 7 #include "SkPdfNativeTokenizer.h" |
| 8 | 8 |
| 9 SkPdfObject SkPdfObject::kNull = SkPdfObject::makeNull(); | 9 SkPdfObject SkPdfObject::kNull = SkPdfObject::makeNull(); |
| 10 | 10 |
| 11 bool SkPdfObject::applyFlateDecodeFilter(SkPdfAllocator* allocator) { | 11 bool SkPdfObject::applyFlateDecodeFilter() { |
| 12 if (!SkFlate::HaveFlate()) { | 12 if (!SkFlate::HaveFlate()) { |
| 13 // TODO(edisonn): warn, make callers handle it | 13 // TODO(edisonn): warn, make callers handle it |
| 14 return false; | 14 return false; |
| 15 } | 15 } |
| 16 | 16 |
| 17 SkMemoryStream skstream(fStr.fBuffer, fStr.fBytes >> 1, false); | 17 const unsigned char* old = fStr.fBuffer; |
| 18 bool deleteOld = isStreamOwned(); |
| 19 |
| 20 SkMemoryStream skstream(fStr.fBuffer, fStr.fBytes >> 2, false); |
| 18 SkDynamicMemoryWStream uncompressedData; | 21 SkDynamicMemoryWStream uncompressedData; |
| 19 | 22 |
| 20 if (SkFlate::Inflate(&skstream, &uncompressedData)) { | 23 if (SkFlate::Inflate(&skstream, &uncompressedData)) { |
| 21 fStr.fBytes = (uncompressedData.bytesWritten() << 1) + kUnfilteredStream
Bit; | 24 fStr.fBytes = (uncompressedData.bytesWritten() << 2) + kOwnedStreamBit +
kUnfilteredStreamBit; |
| 22 fStr.fBuffer = (unsigned char*)allocator->alloc(uncompressedData.bytesWr
itten()); | 25 fStr.fBuffer = (const unsigned char*)new unsigned char[uncompressedData.
bytesWritten()]; |
| 23 uncompressedData.copyTo(fStr.fBuffer); | 26 uncompressedData.copyTo((void*)fStr.fBuffer); |
| 27 |
| 28 if (deleteOld) { |
| 29 delete[] old; |
| 30 } |
| 31 |
| 24 return true; | 32 return true; |
| 25 } else { | 33 } else { |
| 26 // TODO(edisonn): warn, make callers handle it | 34 // TODO(edisonn): warn, make callers handle it |
| 27 return false; | 35 return false; |
| 28 } | 36 } |
| 29 } | 37 } |
| 30 | 38 |
| 31 bool SkPdfObject::applyDCTDecodeFilter(SkPdfAllocator* allocator) { | 39 bool SkPdfObject::applyDCTDecodeFilter() { |
| 32 // this would fail, and it won't allow any more filters. | 40 // this would fail, and it won't allow any more filters. |
| 33 // technically, it would be possible, but not a real world scenario | 41 // technically, it would be possible, but not a real world scenario |
| 34 // TODO(edisonn): or get the image here and store it for fast retrieval? | 42 // TODO(edisonn): or get the image here and store it for fast retrieval? |
| 35 return false; | 43 return false; |
| 36 } | 44 } |
| 37 | 45 |
| 38 bool SkPdfObject::applyFilter(const char* name, SkPdfAllocator* allocator) { | 46 bool SkPdfObject::applyFilter(const char* name) { |
| 39 if (strcmp(name, "FlateDecode") == 0) { | 47 if (strcmp(name, "FlateDecode") == 0) { |
| 40 return applyFlateDecodeFilter(allocator); | 48 return applyFlateDecodeFilter(); |
| 41 } else if (strcmp(name, "DCTDecode") == 0) { | 49 } else if (strcmp(name, "DCTDecode") == 0) { |
| 42 return applyDCTDecodeFilter(allocator); | 50 return applyDCTDecodeFilter(); |
| 43 } | 51 } |
| 44 // TODO(edisonn): allert, not supported, but should be implemented asap | 52 // TODO(edisonn): allert, not supported, but should be implemented asap |
| 45 return false; | 53 return false; |
| 46 } | 54 } |
| 47 | 55 |
| 48 bool SkPdfObject::filterStream(SkPdfAllocator* allocator) { | 56 bool SkPdfObject::filterStream() { |
| 49 if (!hasStream()) { | 57 if (!hasStream()) { |
| 50 return false; | 58 return false; |
| 51 } | 59 } |
| 52 | 60 |
| 53 if (isStreamFiltered()) { | 61 if (isStreamFiltered()) { |
| 54 return true; | 62 return true; |
| 55 } | 63 } |
| 56 | 64 |
| 57 SkPdfStreamCommonDictionary* stream = (SkPdfStreamCommonDictionary*)this; | 65 SkPdfStreamCommonDictionary* stream = (SkPdfStreamCommonDictionary*)this; |
| 58 | 66 |
| 59 if (!stream->has_Filter()) { | 67 if (!stream->has_Filter()) { |
| 60 fStr.fBytes = ((fStr.fBytes >> 1) << 1) + kFilteredStreamBit; | 68 fStr.fBytes = ((fStr.fBytes >> 1) << 1) + kFilteredStreamBit; |
| 61 return true; | 69 } else if (stream->isFilterAName(NULL)) { |
| 62 } | |
| 63 | |
| 64 if (stream->isFilterAName(NULL)) { | |
| 65 std::string filterName = stream->getFilterAsName(NULL); | 70 std::string filterName = stream->getFilterAsName(NULL); |
| 66 applyFilter(filterName.c_str(), allocator); | 71 applyFilter(filterName.c_str()); |
| 67 } else if (stream->isFilterAArray(NULL)) { | 72 } else if (stream->isFilterAArray(NULL)) { |
| 68 const SkPdfArray* filters = stream->getFilterAsArray(NULL); | 73 const SkPdfArray* filters = stream->getFilterAsArray(NULL); |
| 69 int cnt = filters->size(); | 74 int cnt = filters->size(); |
| 70 for (int i = cnt - 1; i >= 0; i--) { | 75 for (int i = cnt - 1; i >= 0; i--) { |
| 71 const SkPdfObject* filterName = filters->objAtAIndex(i); | 76 const SkPdfObject* filterName = filters->objAtAIndex(i); |
| 72 if (filterName != NULL && filterName->isName()) { | 77 if (filterName != NULL && filterName->isName()) { |
| 73 if (!applyFilter(filterName->nameValue(), allocator)) { | 78 if (!applyFilter(filterName->nameValue())) { |
| 74 break; | 79 break; |
| 75 } | 80 } |
| 76 } else { | 81 } else { |
| 77 // TODO(edisonn): report warning | 82 // TODO(edisonn): report warning |
| 78 } | 83 } |
| 79 } | 84 } |
| 80 } | 85 } |
| 81 | 86 |
| 82 fStr.fBytes = ((fStr.fBytes >> 1) << 1) + kFilteredStreamBit; | |
| 83 | |
| 84 return true; | 87 return true; |
| 85 } | 88 } |
| OLD | NEW |