OLD | NEW |
(Empty) | |
| 1 #include "DMImageTask.h" |
| 2 #include "DMUtil.h" |
| 3 #include "DMWriteTask.h" |
| 4 #include "SkImageDecoder.h" |
| 5 #include "SkRandom.h" |
| 6 |
| 7 #include <string.h> |
| 8 |
| 9 namespace DM { |
| 10 |
| 11 // This converts file names like "mandrill_128.r11.ktx" into |
| 12 // "mandrill-128-r11_ktx" or "mandrill-128-r11-5-subsets_ktx". |
| 13 static SkString task_name(SkString filename, int subsets) { |
| 14 const char* ext = strrchr(filename.c_str(), '.'); |
| 15 SkString name(filename.c_str(), ext - filename.c_str()); |
| 16 if (subsets > 0) { |
| 17 name.appendf("_%d_subsets", subsets); |
| 18 } |
| 19 name = FileToTaskName(name); // Promote any stray '.' in the filename to '_
'. |
| 20 name.append(ext); // Tack on the extension, including the '.'. |
| 21 return FileToTaskName(name); // Promote that last '.' to '_', other '_' to
'-'. |
| 22 } |
| 23 |
| 24 ImageTask::ImageTask(Reporter* r, TaskRunner* t, const SkData* encoded, SkString
name, int subsets) |
| 25 : CpuTask(r, t) |
| 26 , fEncoded(SkRef(encoded)) |
| 27 , fName(task_name(name, subsets)) |
| 28 , fSubsets(subsets) {} |
| 29 |
| 30 void ImageTask::draw() { |
| 31 if (fSubsets == 0) { |
| 32 // Decoding the whole image is considerably simpler than decoding subset
s! |
| 33 SkBitmap bitmap; |
| 34 if (!SkImageDecoder::DecodeMemory(fEncoded->data(), fEncoded->size(), &b
itmap)) { |
| 35 return this->fail("Can't DecodeMemory"); |
| 36 } |
| 37 this->spawnChild(SkNEW_ARGS(WriteTask, (*this, "image", bitmap))); |
| 38 return; |
| 39 } |
| 40 |
| 41 SkMemoryStream stream(fEncoded->data(), fEncoded->size()); |
| 42 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream)); |
| 43 if (!decoder) { |
| 44 return this->fail("Can't find good decoder."); |
| 45 } |
| 46 |
| 47 int w,h; |
| 48 if (!decoder->buildTileIndex(&stream, &w, &h) || w*h == 1) { |
| 49 return; // Subset decoding is not always supported. |
| 50 } |
| 51 |
| 52 SkBitmap composite; |
| 53 composite.allocN32Pixels(w,h); // We're lazy here and just always use nativ
e 8888. |
| 54 composite.eraseColor(SK_ColorTRANSPARENT); |
| 55 SkCanvas canvas(composite); |
| 56 |
| 57 SkRandom rand; |
| 58 for (int i = 0; i < fSubsets; i++) { |
| 59 SkIRect rect; |
| 60 do { |
| 61 rect.fLeft = rand.nextULessThan(w); |
| 62 rect.fTop = rand.nextULessThan(h); |
| 63 rect.fRight = rand.nextULessThan(w); |
| 64 rect.fBottom = rand.nextULessThan(h); |
| 65 rect.sort(); |
| 66 } while (rect.isEmpty()); |
| 67 |
| 68 SkBitmap subset; |
| 69 if (!decoder->decodeSubset(&subset, rect, kN32_SkColorType)) { |
| 70 return this->fail("Could not decode subset."); |
| 71 } |
| 72 canvas.drawBitmap(subset, SkIntToScalar(rect.fLeft), SkIntToScalar(rect.
fTop)); |
| 73 } |
| 74 canvas.flush(); |
| 75 this->spawnChild(SkNEW_ARGS(WriteTask, (*this, "image", composite))); |
| 76 } |
| 77 |
| 78 } // namespace DM |
OLD | NEW |