halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkMultiPictureDocumentPriv.h" |
| 9 | #include "SkMultiPictureDocumentReader.h" |
| 10 | #include "SkPicture.h" |
| 11 | #include "SkStream.h" |
| 12 | |
| 13 | bool SkMultiPictureDocumentReader::init(SkStreamSeekable* stream) { |
| 14 | if (!stream) { |
| 15 | return false; |
| 16 | } |
| 17 | stream->seek(0); |
| 18 | const size_t size = sizeof(SkMultiPictureDocumentProtocol::kMagic) - 1; |
| 19 | char buffer[size]; |
| 20 | if (size != stream->read(buffer, size) || |
| 21 | 0 != memcmp(SkMultiPictureDocumentProtocol::kMagic, buffer, size)) { |
| 22 | stream = nullptr; |
| 23 | return false; |
| 24 | } |
| 25 | bool good = true; |
| 26 | uint32_t versionNumber = stream->readU32(); |
| 27 | if (versionNumber != 1) { |
| 28 | return false; |
| 29 | } |
| 30 | uint32_t pageCount = stream->readU32(); |
| 31 | fSizes.reset(pageCount); |
| 32 | fOffsets.reset(pageCount); |
| 33 | for (uint32_t i = 0; i < pageCount; ++i) { |
| 34 | SkMultiPictureDocumentProtocol::Entry entry; |
| 35 | good &= sizeof(entry) == stream->read(&entry, sizeof(entry)); |
| 36 | fSizes[i] = SkSize::Make(entry.sizeX, entry.sizeY); |
| 37 | good &= SkTFitsIn<size_t>(entry.offset); |
| 38 | fOffsets[i] = static_cast<size_t>(entry.offset); |
| 39 | } |
| 40 | return good; |
| 41 | } |
| 42 | |
| 43 | sk_sp<SkPicture> SkMultiPictureDocumentReader::readPage(SkStreamSeekable* stream, |
| 44 | int pageNumber) const { |
| 45 | SkASSERT(pageNumber >= 0); |
| 46 | SkASSERT(pageNumber < fOffsets.count()); |
| 47 | SkAssertResult(stream->seek(fOffsets[pageNumber])); |
| 48 | return SkPicture::MakeFromStream(stream); |
| 49 | } |