Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "Resources.h" | 8 #include "Resources.h" |
| 9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
| 10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
| (...skipping 734 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 745 } | 745 } |
| 746 { | 746 { |
| 747 SkAutoLockPixels alp(bitmap); | 747 SkAutoLockPixels alp(bitmap); |
| 748 REPORTER_ASSERT(r, bitmap.getColorTable() && "first pass"); | 748 REPORTER_ASSERT(r, bitmap.getColorTable() && "first pass"); |
| 749 } | 749 } |
| 750 { | 750 { |
| 751 SkAutoLockPixels alp(bitmap); | 751 SkAutoLockPixels alp(bitmap); |
| 752 REPORTER_ASSERT(r, bitmap.getColorTable() && "second pass"); | 752 REPORTER_ASSERT(r, bitmap.getColorTable() && "second pass"); |
| 753 } | 753 } |
| 754 } | 754 } |
| 755 | |
| 756 | |
| 757 //////////////////////////////////////////////////////////////////////////////// | |
| 758 namespace { | |
| 759 class SingleAllocator : public SkBitmap::Allocator { | |
| 760 public: | |
| 761 // If the pixels in fPixles are big enough, use them. | |
|
djsollen
2014/08/01 14:21:26
fPixels
| |
| 762 virtual bool allocPixelRef(SkBitmap* bm, SkColorTable* ct) SK_OVERRIDE { | |
| 763 SkASSERT(bm); | |
| 764 if (bm->info().getSafeSize(bm->rowBytes()) <= fSize) { | |
| 765 bm->setPixels(fPixels, ct); | |
| 766 fPixels = NULL; | |
| 767 fSize = 0; | |
| 768 return true; | |
| 769 } | |
| 770 return bm->allocPixels(NULL, ct); | |
| 771 } | |
| 772 bool ready() { return fPixels != NULL; } | |
| 773 SingleAllocator(void* p, size_t s) : fPixels(p), fSize(s) { } | |
|
djsollen
2014/08/01 14:21:26
put constructor/destructor at the top of class.
| |
| 774 ~SingleAllocator() {} | |
| 775 private: | |
| 776 void* fPixels; | |
| 777 size_t fSize; | |
| 778 }; | |
| 779 } // namespace | |
| 780 | |
| 781 /* This tests for a bug in libjpeg where INT32 is typedefed to long | |
| 782 and memory can be written to outside of the array. */ | |
| 783 DEF_TEST(ImageDecoding_JpegOverwrite, r) { | |
| 784 SkString resourceDir = GetResourcePath(); | |
| 785 SkString path = SkOSPath::Join(resourceDir.c_str(), "randPixels.jpg"); | |
| 786 SkAutoTUnref<SkStreamAsset> stream( | |
| 787 SkStream::NewFromFile(path.c_str())); | |
| 788 if (!stream.get()) { | |
| 789 SkDebugf("\nPath '%s' missing.\n", path.c_str()); | |
| 790 return; | |
| 791 } | |
| 792 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream)); | |
| 793 if (NULL == decoder.get()) { | |
| 794 ERRORF(r, "\nSkImageDecoder::Factory failed.\n"); | |
| 795 return; | |
| 796 } | |
| 797 SkAssertResult(stream->rewind()); | |
| 798 | |
| 799 static const uint16_t sentinal = 0xBEEF; | |
| 800 static const int pixelCount = 16; | |
| 801 SkAutoTMalloc<uint16_t> pixels(pixelCount + 1); | |
| 802 // pixels.get() should be 4-byte aligned. | |
| 803 // This is necessary to reproduce the bug. | |
| 804 | |
| 805 pixels[pixelCount] = sentinal; // This value should not be changed. | |
| 806 | |
| 807 SkAutoTUnref<SingleAllocator> allocator( | |
| 808 SkNEW_ARGS(SingleAllocator, | |
| 809 ((void*)pixels.get(), sizeof(uint16_t) * pixelCount))); | |
| 810 decoder->setAllocator(allocator); | |
| 811 decoder->setSampleSize(2); | |
| 812 SkBitmap bitmap; | |
| 813 bool success = decoder->decode(stream, &bitmap, kRGB_565_SkColorType, | |
| 814 SkImageDecoder::kDecodePixels_Mode); | |
| 815 REPORTER_ASSERT(r, success); | |
| 816 REPORTER_ASSERT(r, !allocator->ready()); // Decoder used correct memory | |
| 817 REPORTER_ASSERT(r, sentinal == pixels[pixelCount]); | |
| 818 } | |
| OLD | NEW |