1 /* 2 * Copyright 2010 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #include "SkImageInfo.h" 9 #include "SkReadBuffer.h" 10 #include "SkWriteBuffer.h" 11 12 static bool profile_type_is_valid(SkColorProfileType profileType) { 13 return (profileType >= 0) && (profileType <= kLastEnum_SkColorProfileType); 14 } 15 16 static bool alpha_type_is_valid(SkAlphaType alphaType) { 17 return (alphaType >= 0) && (alphaType <= kLastEnum_SkAlphaType); 18 } 19 20 static bool color_type_is_valid(SkColorType colorType) { 21 return (colorType >= 0) && (colorType <= kLastEnum_SkColorType); 22 } 23 24 void SkImageInfo::unflatten(SkReadBuffer& buffer) { 25 fWidth = buffer.read32(); 26 fHeight = buffer.read32(); 27 28 uint32_t packed = buffer.read32(); 29 SkASSERT(0 == (packed >> 24)); 30 fProfileType = (SkColorProfileType)((packed >> 16) & 0xFF); 31 fAlphaType = (SkAlphaType)((packed >> 8) & 0xFF); 32 fColorType = (SkColorType)((packed >> 0) & 0xFF); 33 buffer.validate(profile_type_is_valid(fProfileType) && 34 alpha_type_is_valid(fAlphaType) && 35 color_type_is_valid(fColorType)); 36 } 37 38 void SkImageInfo::flatten(SkWriteBuffer& buffer) const { 39 buffer.write32(fWidth); 40 buffer.write32(fHeight); 41 42 SkASSERT(0 == (fProfileType & ~0xFF)); 43 SkASSERT(0 == (fAlphaType & ~0xFF)); 44 SkASSERT(0 == (fColorType & ~0xFF)); 45 uint32_t packed = (fProfileType << 16) | (fAlphaType << 8) | fColorType; 46 buffer.write32(packed); 47 } 48 49 bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType, 50 SkAlphaType* canonical) { 51 switch (colorType) { 52 case kUnknown_SkColorType: 53 alphaType = kUnknown_SkAlphaType; 54 break; 55 case kAlpha_8_SkColorType: 56 if (kUnpremul_SkAlphaType == alphaType) { 57 alphaType = kPremul_SkAlphaType; 58 } 59 // fall-through 60 case kIndex_8_SkColorType: 61 case kARGB_4444_SkColorType: 62 case kRGBA_8888_SkColorType: 63 case kBGRA_8888_SkColorType: 64 if (kUnknown_SkAlphaType == alphaType) { 65 return false; 66 } 67 break; 68 case kRGB_565_SkColorType: 69 case kGray_8_SkColorType: 70 alphaType = kOpaque_SkAlphaType; 71 break; 72 default: 73 return false; 74 } 75 if (canonical) { 76 *canonical = alphaType; 77 } 78 return true; 79 } 80 81 /////////////////////////////////////////////////////////////////////////////////////////////////// 82 83 #include "SkReadPixelsRec.h" 84 85 bool SkReadPixelsRec::trim(int srcWidth, int srcHeight) { 86 switch (fInfo.colorType()) { 87 case kUnknown_SkColorType: 88 case kIndex_8_SkColorType: 89 return false; 90 default: 91 break; 92 } 93 if (NULL == fPixels || fRowBytes < fInfo.minRowBytes()) { 94 return false; 95 } 96 if (0 == fInfo.width() || 0 == fInfo.height()) { 97 return false; 98 } 99 100 int x = fX; 101 int y = fY; 102 SkIRect srcR = SkIRect::MakeXYWH(x, y, fInfo.width(), fInfo.height()); 103 if (!srcR.intersect(0, 0, srcWidth, srcHeight)) { 104 return false; 105 } 106 107 // if x or y are negative, then we have to adjust pixels 108 if (x > 0) { 109 x = 0; 110 } 111 if (y > 0) { 112 y = 0; 113 } 114 // here x,y are either 0 or negative 115 fPixels = ((char*)fPixels - y * fRowBytes - x * fInfo.bytesPerPixel()); 116 // the intersect may have shrunk info's logical size 117 fInfo = fInfo.makeWH(srcR.width(), srcR.height()); 118 fX = srcR.x(); 119 fY = srcR.y(); 120 121 return true; 122 } 123 124