Chromium Code Reviews| Index: gm/etc1bitmap.cpp |
| diff --git a/gm/etc1bitmap.cpp b/gm/etc1bitmap.cpp |
| index ec71d8daa56843344d4d95469f09ac018190e3fb..0de3e7c4d3b9f0689416009cf51e3f32206ca05e 100644 |
| --- a/gm/etc1bitmap.cpp |
| +++ b/gm/etc1bitmap.cpp |
| @@ -12,6 +12,58 @@ |
| #include "SkImageDecoder.h" |
| #include "SkOSFile.h" |
| +#include "etc1.h" |
| + |
| +/** |
| + * Remove the last row and column of ETC1 blocks, effectively |
| + * making a texture that started as power of two into a texture |
| + * that is no longer power of two... |
| + */ |
| +bool slice_etc1_data(void *data, int* width, int* height) { |
| + |
| + // First, parse the data and get to it... |
|
robertphillips
2014/06/04 14:36:50
etc1data -> origData ?
krajcevski
2014/06/04 14:52:17
Done.
|
| + etc1_byte *etc1data = reinterpret_cast<etc1_byte *>(data); |
| + if (!etc1_pkm_is_valid(etc1data)) { |
| + return false; |
| + } |
| + |
|
robertphillips
2014/06/04 14:36:50
origW, origH ?
krajcevski
2014/06/04 14:52:17
Done.
|
| + int encW = etc1_pkm_get_width(etc1data); |
| + int encH = etc1_pkm_get_height(etc1data); |
| + |
| + int blockWidth = (encW + 3) >> 2; |
| + int blockHeight = (encH + 3) >> 2; |
| + |
| + // Make sure that we have blocks to trim off.. |
| + if (blockWidth < 2 || blockHeight < 2) { |
| + return false; |
| + } |
| + |
| + int newWidth = (blockWidth - 1) << 2; |
| + int newHeight = (blockHeight - 1) << 2; |
| + |
| + size_t newDataSz = etc1_get_encoded_data_size(newWidth, newHeight) + ETC_PKM_HEADER_SIZE; |
| + SkAutoMalloc am(newDataSz); |
| + |
|
robertphillips
2014/06/04 14:36:50
Maybe amData -> newData ?
krajcevski
2014/06/04 14:52:17
Done.
|
| + etc1_byte *amData = reinterpret_cast<etc1_byte *>(am.get()); |
| + |
| + etc1_pkm_format_header(amData, newWidth, newHeight); |
| + amData += ETC_PKM_HEADER_SIZE; |
| + etc1data += ETC_PKM_HEADER_SIZE; |
| + |
| + for (int j = 0; j < blockHeight - 1; ++j) { |
|
robertphillips
2014/06/04 14:36:50
static const kETC1BlockBytes = 8; somewhere ?
krajcevski
2014/06/04 14:52:17
This is defined in etc1.h
|
| + memcpy(amData, etc1data, (blockWidth - 1)*8); |
| + etc1data += blockWidth*8; |
| + amData += (blockWidth - 1)*8; |
| + } |
| + |
| + // Stick the data back whence it came |
| + memcpy(data, am.get(), newDataSz); |
| + *width = newWidth; |
| + *height = newHeight; |
| + |
| + return true; |
| +} |
| + |
| namespace skiagm { |
| /** |
| @@ -90,9 +142,71 @@ private: |
| typedef ETC1BitmapGM INHERITED; |
| }; |
| +/** |
| + * Test decoding an image from a PKM file and then |
| + * from non-power-of-two compressed ETC1 data. First slice |
| + * off a row and column of blocks in order to make it non-power |
| + * of two. |
| + */ |
| +class ETC1Bitmap_NPOT_GM : public GM { |
| +public: |
| + ETC1Bitmap_NPOT_GM() { } |
| + virtual ~ETC1Bitmap_NPOT_GM() { } |
| + |
| +protected: |
| + virtual SkString onShortName() SK_OVERRIDE { |
| + return SkString("etc1bitmap_npot"); |
| + } |
| + |
| + virtual SkISize onISize() SK_OVERRIDE { |
|
robertphillips
2014/06/04 14:36:50
508 -> 124 ?
krajcevski
2014/06/04 14:52:17
Done.
|
| + return make_isize(508, 508); |
| + } |
| + |
| + virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| + |
| + SkBitmap bm; |
| + SkString filename = SkOSPath::SkPathJoin( |
| + INHERITED::gResourcePath.c_str(), "mandrill_128.pkm"); |
| + |
| + SkAutoDataUnref fileData(SkData::NewFromFileName(filename.c_str())); |
| + if (NULL == fileData) { |
| + SkDebugf("Could not open the file. Did you forget to set the resourcePath?\n"); |
| + return; |
| + } |
| + |
| + SkAutoMalloc am(fileData->size()); |
| + memcpy(am.get(), fileData->data(), fileData->size()); |
| + |
| + int width, height; |
| + if (!slice_etc1_data(am.get(), &width, &height)) { |
| + SkDebugf("ETC1 Data is poorly formatted.\n"); |
| + return; |
| + } |
| + |
| + SkASSERT(124 == width); |
| + SkASSERT(124 == height); |
| + |
| + size_t dataSz = etc1_get_encoded_data_size(width, height) + ETC_PKM_HEADER_SIZE; |
| + SkAutoDataUnref nonPOTData(SkData::NewWithCopy(am.get(), dataSz)); |
| + |
| + if (!SkInstallDiscardablePixelRef( |
| + SkDecodingImageGenerator::Create( |
| + nonPOTData, SkDecodingImageGenerator::Options()), &bm)) { |
| + SkDebugf("Could not install discardable pixel ref.\n"); |
| + return; |
| + } |
| + |
| + canvas->drawBitmap(bm, 0, 0); |
| + } |
| + |
| +private: |
| + typedef GM INHERITED; |
| +}; |
| + |
| } // namespace skiagm |
| ////////////////////////////////////////////////////////////////////////////// |
| DEF_GM( return SkNEW(skiagm::ETC1Bitmap_PKM_GM); ) |
| DEF_GM( return SkNEW(skiagm::ETC1Bitmap_KTX_GM); ) |
| +DEF_GM( return SkNEW(skiagm::ETC1Bitmap_NPOT_GM); ) |