1 2 /* 3 * Copyright 2006 The Android Open Source Project 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10 #include "SkDrawBitmap.h" 11 #include "SkAnimateMaker.h" 12 #include "SkCanvas.h" 13 #include "SkImageDecoder.h" 14 #include "SkPaint.h" 15 #include "SkStream.h" 16 17 #if SK_USE_CONDENSED_INFO == 0 18 19 const SkMemberInfo SkBaseBitmap::fInfo[] = { 20 SK_MEMBER(x, Float), 21 SK_MEMBER(y, Float) 22 }; 23 24 #endif 25 26 DEFINE_GET_MEMBER(SkBaseBitmap); 27 28 SkBaseBitmap::SkBaseBitmap() : x(0), y(0) { 29 } 30 31 SkBaseBitmap::~SkBaseBitmap() { 32 } 33 34 bool SkBaseBitmap::draw(SkAnimateMaker& maker) { 35 SkBoundableAuto boundable(this, maker); 36 maker.fCanvas->drawBitmap(fBitmap, x, y, maker.fPaint); 37 return false; 38 } 39 40 enum SkDrawBitmap_Properties { 41 SK_PROPERTY(erase) 42 }; 43 44 #if SK_USE_CONDENSED_INFO == 0 45 46 const SkMemberInfo SkDrawBitmap::fInfo[] = { 47 SK_MEMBER_INHERITED, 48 SK_MEMBER_PROPERTY(erase, ARGB), 49 SK_MEMBER(format, BitmapFormat), 50 SK_MEMBER(height, Int), 51 SK_MEMBER(rowBytes, Int), 52 SK_MEMBER(width, Int), 53 }; 54 55 #endif 56 57 DEFINE_GET_MEMBER(SkDrawBitmap); 58 59 SkDrawBitmap::SkDrawBitmap() : format((SkBitmap::Config) -1), height(-1), 60 rowBytes(0), width(-1), fColor(0), fColorSet(false) { 61 } 62 63 SkDrawBitmap::~SkDrawBitmap() { 64 } 65 66 #ifdef SK_DUMP_ENABLED 67 void SkDrawBitmap::dump(SkAnimateMaker* maker) { 68 dumpBase(maker); 69 dumpAttrs(maker); 70 if (fColorSet) 71 SkDebugf("erase=\"argb(%d,%d,%d,%d)\" ", SkColorGetA(fColor)/255, SkColorGetR(fColor), 72 SkColorGetG(fColor), SkColorGetB(fColor)); 73 if (rowBytes > 0) 74 SkDebugf("rowBytes=\"%d\" ", rowBytes); 75 const char* formatName; 76 switch (format) { 77 case 0: formatName = "none"; break; 78 case 1: formatName = "A1"; break; 79 case 2: formatName = "A8"; break; 80 case 3: formatName = "Index8"; break; 81 case 4: formatName = "RGB16"; break; 82 case 5: formatName = "RGB32"; break; 83 } 84 SkDebugf("format=\"%s\" />\n", formatName); 85 } 86 #endif 87 88 void SkDrawBitmap::onEndElement(SkAnimateMaker& maker) { 89 SkASSERT(format != (SkBitmap::Config) -1); 90 SkASSERT(width != -1); 91 SkASSERT(height != -1); 92 SkASSERT(rowBytes >= 0); 93 fBitmap.setConfig((SkBitmap::Config) format, width, height, rowBytes); 94 fBitmap.allocPixels(); 95 if (fColorSet) 96 fBitmap.eraseColor(fColor); 97 } 98 99 bool SkDrawBitmap::setProperty(int index, SkScriptValue& value) 100 { 101 switch (index) { 102 case SK_PROPERTY(erase): 103 SkASSERT(value.fType == SkType_ARGB); 104 fColor = value.fOperand.fS32; 105 fColorSet = true; 106 break; 107 default: 108 SkASSERT(0); 109 return false; 110 } 111 return true; 112 } 113 114 115 enum SkImage_Properties { 116 SK_PROPERTY(height), 117 SK_PROPERTY(width) 118 }; 119 120 #if SK_USE_CONDENSED_INFO == 0 121 122 const SkMemberInfo SkImage::fInfo[] = { 123 SK_MEMBER_INHERITED, 124 SK_MEMBER(base64, Base64), 125 SK_MEMBER_PROPERTY(height, Int), 126 SK_MEMBER(src, String), 127 SK_MEMBER_PROPERTY(width, Int) 128 }; 129 130 #endif 131 132 DEFINE_GET_MEMBER(SkImage); 133 134 SkImage::SkImage() : fDirty(true), fUriBase(NULL) { 135 base64.fData = NULL; 136 base64.fLength = 0; 137 } 138 139 SkImage::~SkImage() { 140 delete[] base64.fData; 141 } 142 143 SkDisplayable* SkImage::deepCopy(SkAnimateMaker* maker) { 144 SkDisplayable* copy = INHERITED::deepCopy(maker); 145 ((SkImage*) copy)->fUriBase = ((SkImage*) this)->fUriBase; 146 return copy; 147 } 148 149 void SkImage::dirty() { 150 fDirty = true; 151 } 152 153 bool SkImage::draw(SkAnimateMaker& maker) { 154 if (fDirty) 155 resolve(); 156 return INHERITED::draw(maker); 157 } 158 159 bool SkImage::getProperty(int index, SkScriptValue* value) const { 160 if (fDirty) 161 resolve(); 162 switch (index) { 163 case SK_PROPERTY(height): 164 value->fOperand.fS32 = fBitmap.height(); 165 break; 166 case SK_PROPERTY(width): 167 value->fOperand.fS32 = fBitmap.width(); 168 break; 169 default: 170 SkASSERT(0); 171 return false; 172 } 173 value->fType = SkType_Int; 174 return true; 175 } 176 177 void SkImage::onEndElement(SkAnimateMaker& maker) { 178 fUriBase = maker.fPrefix.c_str(); 179 } 180 181 void SkImage::resolve() { 182 fDirty = false; 183 if (base64.fData) { 184 fBitmap.reset(); 185 SkImageDecoder::DecodeMemory(base64.fData, base64.fLength, &fBitmap); 186 } else if (src.size()) { 187 if (fLast.equals(src)) 188 return; 189 fLast.set(src); 190 fBitmap.reset(); 191 192 //SkStream* stream = SkStream::GetURIStream(fUriBase, src.c_str()); 193 SkStream* stream = new SkFILEStream(src.c_str()); 194 195 SkAutoTDelete<SkStream> autoDel(stream); 196 SkImageDecoder::DecodeStream(stream, &fBitmap); 197 } 198 } 199