OLD | NEW |
1 #include "DMSrcSink.h" | 1 #include "DMSrcSink.h" |
2 #include "SamplePipeControllers.h" | 2 #include "SamplePipeControllers.h" |
3 #include "SkCommonFlags.h" | 3 #include "SkCommonFlags.h" |
4 #include "SkDocument.h" | 4 #include "SkDocument.h" |
5 #include "SkMultiPictureDraw.h" | 5 #include "SkMultiPictureDraw.h" |
6 #include "SkOSFile.h" | 6 #include "SkOSFile.h" |
7 #include "SkPictureRecorder.h" | 7 #include "SkPictureRecorder.h" |
8 #include "SkRandom.h" | 8 #include "SkRandom.h" |
9 #include "SkStream.h" | 9 #include "SkStream.h" |
10 | 10 |
(...skipping 13 matching lines...) Expand all Loading... |
24 return gm->getISize(); | 24 return gm->getISize(); |
25 } | 25 } |
26 | 26 |
27 Name GMSrc::name() const { | 27 Name GMSrc::name() const { |
28 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); | 28 SkAutoTDelete<skiagm::GM> gm(fFactory(NULL)); |
29 return gm->getName(); | 29 return gm->getName(); |
30 } | 30 } |
31 | 31 |
32 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ | 32 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ |
33 | 33 |
34 ImageSrc::ImageSrc(Path path, int subsets) : fPath(path), fSubsets(subsets) {} | 34 ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {} |
35 | 35 |
36 Error ImageSrc::draw(SkCanvas* canvas) const { | 36 Error ImageSrc::draw(SkCanvas* canvas) const { |
37 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); | 37 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
38 if (!encoded) { | 38 if (!encoded) { |
39 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); | 39 return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
40 } | 40 } |
41 if (fSubsets == 0) { | 41 const SkColorType dstColorType = canvas->imageInfo().colorType(); |
| 42 if (fDivisor == 0) { |
42 // Decode the full image. | 43 // Decode the full image. |
43 SkBitmap bitmap; | 44 SkBitmap bitmap; |
44 if (!SkImageDecoder::DecodeMemory(encoded->data(), encoded->size(), &bit
map)) { | 45 if (!SkImageDecoder::DecodeMemory(encoded->data(), encoded->size(), &bit
map, |
| 46 dstColorType, SkImageDecoder::kDecodeP
ixels_Mode)) { |
45 return SkStringPrintf("Couldn't decode %s.", fPath.c_str()); | 47 return SkStringPrintf("Couldn't decode %s.", fPath.c_str()); |
46 } | 48 } |
47 encoded.reset((SkData*)NULL); // Might as well drop this when we're don
e with it. | 49 encoded.reset((SkData*)NULL); // Might as well drop this when we're don
e with it. |
48 canvas->drawBitmap(bitmap, 0,0); | 50 canvas->drawBitmap(bitmap, 0,0); |
49 return ""; | 51 return ""; |
50 } | 52 } |
51 // Decode random subsets. This is a little involved. | 53 // Decode subsets. This is a little involved. |
52 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded)); | 54 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded)); |
53 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream.get()))
; | 55 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream.get()))
; |
54 if (!decoder) { | 56 if (!decoder) { |
55 return SkStringPrintf("Can't find a good decoder for %s.", fPath.c_str()
); | 57 return SkStringPrintf("Can't find a good decoder for %s.", fPath.c_str()
); |
56 } | 58 } |
57 stream->rewind(); | 59 stream->rewind(); |
58 int w,h; | 60 int w,h; |
59 if (!decoder->buildTileIndex(stream.detach(), &w, &h) || w*h == 1) { | 61 if (!decoder->buildTileIndex(stream.detach(), &w, &h) || w*h == 1) { |
60 return ""; // Not an error. Subset decoding is not always supported. | 62 return ""; // Not an error. Subset decoding is not always supported. |
61 } | 63 } |
62 SkRandom rand; | 64 |
63 for (int i = 0; i < fSubsets; i++) { | 65 // Divide the image into subsets that cover the entire image. |
64 SkIRect rect; | 66 if (fDivisor > w || fDivisor > h) { |
65 do { | 67 return SkStringPrintf("divisor %d is too big for %s with dimensions (%d
x %d)", |
66 rect.fLeft = rand.nextULessThan(w); | 68 fDivisor, fPath.c_str(), w, h); |
67 rect.fTop = rand.nextULessThan(h); | 69 } |
68 rect.fRight = rand.nextULessThan(w); | 70 const int subsetWidth = w / fDivisor, |
69 rect.fBottom = rand.nextULessThan(h); | 71 subsetHeight = h / fDivisor; |
70 rect.sort(); | 72 for (int y = 0; y < h; y += subsetHeight) { |
71 } while (rect.isEmpty()); | 73 for (int x = 0; x < w; x += subsetWidth) { |
72 SkBitmap subset; | 74 SkBitmap subset; |
73 if (!decoder->decodeSubset(&subset, rect, kUnknown_SkColorType/*use best
fit*/)) { | 75 SkIRect rect = SkIRect::MakeXYWH(x, y, subsetWidth, subsetHeight); |
74 return SkStringPrintf("Could not decode subset %d.\n", i); | 76 if (!decoder->decodeSubset(&subset, rect, dstColorType)) { |
| 77 return SkStringPrintf("Could not decode subset (%d, %d, %d, %d).
", |
| 78 x, y, x+subsetWidth, y+subsetHeight); |
| 79 } |
| 80 canvas->drawBitmap(subset, SkIntToScalar(x), SkIntToScalar(y)); |
75 } | 81 } |
76 canvas->drawBitmap(subset, SkIntToScalar(rect.fLeft), SkIntToScalar(rect
.fTop)); | |
77 } | 82 } |
78 return ""; | 83 return ""; |
79 } | 84 } |
80 | 85 |
81 SkISize ImageSrc::size() const { | 86 SkISize ImageSrc::size() const { |
82 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); | 87 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
83 SkBitmap bitmap; | 88 SkBitmap bitmap; |
84 if (!encoded || !SkImageDecoder::DecodeMemory(encoded->data(), | 89 if (!encoded || !SkImageDecoder::DecodeMemory(encoded->data(), |
85 encoded->size(), | 90 encoded->size(), |
86 &bitmap, | 91 &bitmap, |
87 kUnknown_SkColorType, | 92 kUnknown_SkColorType, |
88 SkImageDecoder::kDecodeBounds_
Mode)) { | 93 SkImageDecoder::kDecodeBounds_
Mode)) { |
89 return SkISize::Make(0,0); | 94 return SkISize::Make(0,0); |
90 } | 95 } |
91 return bitmap.dimensions(); | 96 return bitmap.dimensions(); |
92 } | 97 } |
93 | 98 |
94 Name ImageSrc::name() const { | 99 Name ImageSrc::name() const { |
95 Name name = SkOSPath::Basename(fPath.c_str()); | 100 return SkOSPath::Basename(fPath.c_str()); |
96 if (fSubsets > 0) { | |
97 name.appendf("-%d-subsets", fSubsets); | |
98 } | |
99 return name; | |
100 } | 101 } |
101 | 102 |
102 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ | 103 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ |
103 | 104 |
104 static const SkRect kSKPViewport = {0,0, 1000,1000}; | 105 static const SkRect kSKPViewport = {0,0, 1000,1000}; |
105 | 106 |
106 SKPSrc::SKPSrc(Path path) : fPath(path) {} | 107 SKPSrc::SKPSrc(Path path) : fPath(path) {} |
107 | 108 |
108 Error SKPSrc::draw(SkCanvas* canvas) const { | 109 Error SKPSrc::draw(SkCanvas* canvas) const { |
109 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str())); | 110 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str())); |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 surfaces.unrefAll(); | 382 surfaces.unrefAll(); |
382 return ""; | 383 return ""; |
383 } | 384 } |
384 SkISize size() const SK_OVERRIDE { return fSize; } | 385 SkISize size() const SK_OVERRIDE { return fSize; } |
385 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one shou
ld be calling this. | 386 Name name() const SK_OVERRIDE { sk_throw(); return ""; } // No one shou
ld be calling this. |
386 } proxy(fW, fH, pic, src.size()); | 387 } proxy(fW, fH, pic, src.size()); |
387 return fSink->draw(proxy, bitmap, stream); | 388 return fSink->draw(proxy, bitmap, stream); |
388 } | 389 } |
389 | 390 |
390 } // namespace DM | 391 } // namespace DM |
OLD | NEW |