SkBmpCodec.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. /*
  2. * Copyright 2015 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. #include "include/core/SkStream.h"
  8. #include "include/private/SkColorData.h"
  9. #include "src/codec/SkBmpCodec.h"
  10. #include "src/codec/SkBmpMaskCodec.h"
  11. #include "src/codec/SkBmpRLECodec.h"
  12. #include "src/codec/SkBmpStandardCodec.h"
  13. #include "src/codec/SkCodecPriv.h"
  14. /*
  15. * Defines the version and type of the second bitmap header
  16. */
  17. enum BmpHeaderType {
  18. kInfoV1_BmpHeaderType,
  19. kInfoV2_BmpHeaderType,
  20. kInfoV3_BmpHeaderType,
  21. kInfoV4_BmpHeaderType,
  22. kInfoV5_BmpHeaderType,
  23. kOS2V1_BmpHeaderType,
  24. kOS2VX_BmpHeaderType,
  25. kUnknown_BmpHeaderType
  26. };
  27. /*
  28. * Possible bitmap compression types
  29. */
  30. enum BmpCompressionMethod {
  31. kNone_BmpCompressionMethod = 0,
  32. k8BitRLE_BmpCompressionMethod = 1,
  33. k4BitRLE_BmpCompressionMethod = 2,
  34. kBitMasks_BmpCompressionMethod = 3,
  35. kJpeg_BmpCompressionMethod = 4,
  36. kPng_BmpCompressionMethod = 5,
  37. kAlphaBitMasks_BmpCompressionMethod = 6,
  38. kCMYK_BmpCompressionMethod = 11,
  39. kCMYK8BitRLE_BmpCompressionMethod = 12,
  40. kCMYK4BitRLE_BmpCompressionMethod = 13
  41. };
  42. /*
  43. * Used to define the input format of the bmp
  44. */
  45. enum BmpInputFormat {
  46. kStandard_BmpInputFormat,
  47. kRLE_BmpInputFormat,
  48. kBitMask_BmpInputFormat,
  49. kUnknown_BmpInputFormat
  50. };
  51. /*
  52. * Checks the start of the stream to see if the image is a bitmap
  53. */
  54. bool SkBmpCodec::IsBmp(const void* buffer, size_t bytesRead) {
  55. // TODO: Support "IC", "PT", "CI", "CP", "BA"
  56. const char bmpSig[] = { 'B', 'M' };
  57. return bytesRead >= sizeof(bmpSig) && !memcmp(buffer, bmpSig, sizeof(bmpSig));
  58. }
  59. /*
  60. * Assumes IsBmp was called and returned true
  61. * Creates a bmp decoder
  62. * Reads enough of the stream to determine the image format
  63. */
  64. std::unique_ptr<SkCodec> SkBmpCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
  65. Result* result) {
  66. return SkBmpCodec::MakeFromStream(std::move(stream), result, false);
  67. }
  68. /*
  69. * Creates a bmp decoder for a bmp embedded in ico
  70. * Reads enough of the stream to determine the image format
  71. */
  72. std::unique_ptr<SkCodec> SkBmpCodec::MakeFromIco(std::unique_ptr<SkStream> stream, Result* result) {
  73. return SkBmpCodec::MakeFromStream(std::move(stream), result, true);
  74. }
  75. // Header size constants
  76. static constexpr uint32_t kBmpHeaderBytes = 14;
  77. static constexpr uint32_t kBmpHeaderBytesPlusFour = kBmpHeaderBytes + 4;
  78. static constexpr uint32_t kBmpOS2V1Bytes = 12;
  79. static constexpr uint32_t kBmpOS2V2Bytes = 64;
  80. static constexpr uint32_t kBmpInfoBaseBytes = 16;
  81. static constexpr uint32_t kBmpInfoV1Bytes = 40;
  82. static constexpr uint32_t kBmpInfoV2Bytes = 52;
  83. static constexpr uint32_t kBmpInfoV3Bytes = 56;
  84. static constexpr uint32_t kBmpInfoV4Bytes = 108;
  85. static constexpr uint32_t kBmpInfoV5Bytes = 124;
  86. static constexpr uint32_t kBmpMaskBytes = 12;
  87. static BmpHeaderType get_header_type(size_t infoBytes) {
  88. if (infoBytes >= kBmpInfoBaseBytes) {
  89. // Check the version of the header
  90. switch (infoBytes) {
  91. case kBmpInfoV1Bytes:
  92. return kInfoV1_BmpHeaderType;
  93. case kBmpInfoV2Bytes:
  94. return kInfoV2_BmpHeaderType;
  95. case kBmpInfoV3Bytes:
  96. return kInfoV3_BmpHeaderType;
  97. case kBmpInfoV4Bytes:
  98. return kInfoV4_BmpHeaderType;
  99. case kBmpInfoV5Bytes:
  100. return kInfoV5_BmpHeaderType;
  101. case 16:
  102. case 20:
  103. case 24:
  104. case 28:
  105. case 32:
  106. case 36:
  107. case 42:
  108. case 46:
  109. case 48:
  110. case 60:
  111. case kBmpOS2V2Bytes:
  112. return kOS2VX_BmpHeaderType;
  113. default:
  114. SkCodecPrintf("Error: unknown bmp header format.\n");
  115. return kUnknown_BmpHeaderType;
  116. }
  117. } if (infoBytes >= kBmpOS2V1Bytes) {
  118. // The OS2V1 is treated separately because it has a unique format
  119. return kOS2V1_BmpHeaderType;
  120. } else {
  121. // There are no valid bmp headers
  122. SkCodecPrintf("Error: second bitmap header size is invalid.\n");
  123. return kUnknown_BmpHeaderType;
  124. }
  125. }
  126. SkCodec::Result SkBmpCodec::ReadHeader(SkStream* stream, bool inIco,
  127. std::unique_ptr<SkCodec>* codecOut) {
  128. // The total bytes in the bmp file
  129. // We only need to use this value for RLE decoding, so we will only
  130. // check that it is valid in the RLE case.
  131. uint32_t totalBytes;
  132. // The offset from the start of the file where the pixel data begins
  133. uint32_t offset;
  134. // The size of the second (info) header in bytes
  135. uint32_t infoBytes;
  136. // Bmps embedded in Icos skip the first Bmp header
  137. if (!inIco) {
  138. // Read the first header and the size of the second header
  139. uint8_t hBuffer[kBmpHeaderBytesPlusFour];
  140. if (stream->read(hBuffer, kBmpHeaderBytesPlusFour) !=
  141. kBmpHeaderBytesPlusFour) {
  142. SkCodecPrintf("Error: unable to read first bitmap header.\n");
  143. return kIncompleteInput;
  144. }
  145. totalBytes = get_int(hBuffer, 2);
  146. offset = get_int(hBuffer, 10);
  147. if (offset < kBmpHeaderBytes + kBmpOS2V1Bytes) {
  148. SkCodecPrintf("Error: invalid starting location for pixel data\n");
  149. return kInvalidInput;
  150. }
  151. // The size of the second (info) header in bytes
  152. // The size is the first field of the second header, so we have already
  153. // read the first four infoBytes.
  154. infoBytes = get_int(hBuffer, 14);
  155. if (infoBytes < kBmpOS2V1Bytes) {
  156. SkCodecPrintf("Error: invalid second header size.\n");
  157. return kInvalidInput;
  158. }
  159. } else {
  160. // This value is only used by RLE compression. Bmp in Ico files do not
  161. // use RLE. If the compression field is incorrectly signaled as RLE,
  162. // we will catch this and signal an error below.
  163. totalBytes = 0;
  164. // Bmps in Ico cannot specify an offset. We will always assume that
  165. // pixel data begins immediately after the color table. This value
  166. // will be corrected below.
  167. offset = 0;
  168. // Read the size of the second header
  169. uint8_t hBuffer[4];
  170. if (stream->read(hBuffer, 4) != 4) {
  171. SkCodecPrintf("Error: unable to read size of second bitmap header.\n");
  172. return kIncompleteInput;
  173. }
  174. infoBytes = get_int(hBuffer, 0);
  175. if (infoBytes < kBmpOS2V1Bytes) {
  176. SkCodecPrintf("Error: invalid second header size.\n");
  177. return kInvalidInput;
  178. }
  179. }
  180. // Determine image information depending on second header format
  181. const BmpHeaderType headerType = get_header_type(infoBytes);
  182. if (kUnknown_BmpHeaderType == headerType) {
  183. return kInvalidInput;
  184. }
  185. // We already read the first four bytes of the info header to get the size
  186. const uint32_t infoBytesRemaining = infoBytes - 4;
  187. // Read the second header
  188. std::unique_ptr<uint8_t[]> iBuffer(new uint8_t[infoBytesRemaining]);
  189. if (stream->read(iBuffer.get(), infoBytesRemaining) != infoBytesRemaining) {
  190. SkCodecPrintf("Error: unable to read second bitmap header.\n");
  191. return kIncompleteInput;
  192. }
  193. // The number of bits used per pixel in the pixel data
  194. uint16_t bitsPerPixel;
  195. // The compression method for the pixel data
  196. uint32_t compression = kNone_BmpCompressionMethod;
  197. // Number of colors in the color table, defaults to 0 or max (see below)
  198. uint32_t numColors = 0;
  199. // Bytes per color in the color table, early versions use 3, most use 4
  200. uint32_t bytesPerColor;
  201. // The image width and height
  202. int width, height;
  203. switch (headerType) {
  204. case kInfoV1_BmpHeaderType:
  205. case kInfoV2_BmpHeaderType:
  206. case kInfoV3_BmpHeaderType:
  207. case kInfoV4_BmpHeaderType:
  208. case kInfoV5_BmpHeaderType:
  209. case kOS2VX_BmpHeaderType:
  210. // We check the size of the header before entering the if statement.
  211. // We should not reach this point unless the size is large enough for
  212. // these required fields.
  213. SkASSERT(infoBytesRemaining >= 12);
  214. width = get_int(iBuffer.get(), 0);
  215. height = get_int(iBuffer.get(), 4);
  216. bitsPerPixel = get_short(iBuffer.get(), 10);
  217. // Some versions do not have these fields, so we check before
  218. // overwriting the default value.
  219. if (infoBytesRemaining >= 16) {
  220. compression = get_int(iBuffer.get(), 12);
  221. if (infoBytesRemaining >= 32) {
  222. numColors = get_int(iBuffer.get(), 28);
  223. }
  224. }
  225. // All of the headers that reach this point, store color table entries
  226. // using 4 bytes per pixel.
  227. bytesPerColor = 4;
  228. break;
  229. case kOS2V1_BmpHeaderType:
  230. // The OS2V1 is treated separately because it has a unique format
  231. width = (int) get_short(iBuffer.get(), 0);
  232. height = (int) get_short(iBuffer.get(), 2);
  233. bitsPerPixel = get_short(iBuffer.get(), 6);
  234. bytesPerColor = 3;
  235. break;
  236. case kUnknown_BmpHeaderType:
  237. // We'll exit above in this case.
  238. SkASSERT(false);
  239. return kInvalidInput;
  240. }
  241. // Check for valid dimensions from header
  242. SkCodec::SkScanlineOrder rowOrder = SkCodec::kBottomUp_SkScanlineOrder;
  243. if (height < 0) {
  244. // We can't negate INT32_MIN.
  245. if (height == INT32_MIN) {
  246. return kInvalidInput;
  247. }
  248. height = -height;
  249. rowOrder = SkCodec::kTopDown_SkScanlineOrder;
  250. }
  251. // The height field for bmp in ico is double the actual height because they
  252. // contain an XOR mask followed by an AND mask
  253. if (inIco) {
  254. height /= 2;
  255. }
  256. // Arbitrary maximum. Matches Chromium.
  257. constexpr int kMaxDim = 1 << 16;
  258. if (width <= 0 || height <= 0 || width >= kMaxDim || height >= kMaxDim) {
  259. SkCodecPrintf("Error: invalid bitmap dimensions.\n");
  260. return kInvalidInput;
  261. }
  262. // Create mask struct
  263. SkMasks::InputMasks inputMasks;
  264. memset(&inputMasks, 0, sizeof(SkMasks::InputMasks));
  265. // Determine the input compression format and set bit masks if necessary
  266. uint32_t maskBytes = 0;
  267. BmpInputFormat inputFormat = kUnknown_BmpInputFormat;
  268. switch (compression) {
  269. case kNone_BmpCompressionMethod:
  270. inputFormat = kStandard_BmpInputFormat;
  271. // In addition to more standard pixel compression formats, bmp supports
  272. // the use of bit masks to determine pixel components. The standard
  273. // format for representing 16-bit colors is 555 (XRRRRRGGGGGBBBBB),
  274. // which does not map well to any Skia color formats. For this reason,
  275. // we will always enable mask mode with 16 bits per pixel.
  276. if (16 == bitsPerPixel) {
  277. inputMasks.red = 0x7C00;
  278. inputMasks.green = 0x03E0;
  279. inputMasks.blue = 0x001F;
  280. inputFormat = kBitMask_BmpInputFormat;
  281. }
  282. break;
  283. case k8BitRLE_BmpCompressionMethod:
  284. if (bitsPerPixel != 8) {
  285. SkCodecPrintf("Warning: correcting invalid bitmap format.\n");
  286. bitsPerPixel = 8;
  287. }
  288. inputFormat = kRLE_BmpInputFormat;
  289. break;
  290. case k4BitRLE_BmpCompressionMethod:
  291. if (bitsPerPixel != 4) {
  292. SkCodecPrintf("Warning: correcting invalid bitmap format.\n");
  293. bitsPerPixel = 4;
  294. }
  295. inputFormat = kRLE_BmpInputFormat;
  296. break;
  297. case kAlphaBitMasks_BmpCompressionMethod:
  298. case kBitMasks_BmpCompressionMethod:
  299. // Load the masks
  300. inputFormat = kBitMask_BmpInputFormat;
  301. switch (headerType) {
  302. case kInfoV1_BmpHeaderType: {
  303. // The V1 header stores the bit masks after the header
  304. uint8_t buffer[kBmpMaskBytes];
  305. if (stream->read(buffer, kBmpMaskBytes) != kBmpMaskBytes) {
  306. SkCodecPrintf("Error: unable to read bit inputMasks.\n");
  307. return kIncompleteInput;
  308. }
  309. maskBytes = kBmpMaskBytes;
  310. inputMasks.red = get_int(buffer, 0);
  311. inputMasks.green = get_int(buffer, 4);
  312. inputMasks.blue = get_int(buffer, 8);
  313. break;
  314. }
  315. case kInfoV2_BmpHeaderType:
  316. case kInfoV3_BmpHeaderType:
  317. case kInfoV4_BmpHeaderType:
  318. case kInfoV5_BmpHeaderType:
  319. // Header types are matched based on size. If the header
  320. // is V2+, we are guaranteed to be able to read at least
  321. // this size.
  322. SkASSERT(infoBytesRemaining >= 48);
  323. inputMasks.red = get_int(iBuffer.get(), 36);
  324. inputMasks.green = get_int(iBuffer.get(), 40);
  325. inputMasks.blue = get_int(iBuffer.get(), 44);
  326. if (kInfoV2_BmpHeaderType == headerType ||
  327. (kInfoV3_BmpHeaderType == headerType && !inIco)) {
  328. break;
  329. }
  330. // V3+ bmp files introduce an alpha mask and allow the creator of the image
  331. // to use the alpha channels. However, many of these images leave the
  332. // alpha channel blank and expect to be rendered as opaque. This is the
  333. // case for almost all V3 images, so we ignore the alpha mask. For V4+
  334. // images in kMask mode, we will use the alpha mask. Additionally, V3
  335. // bmp-in-ico expect us to use the alpha mask.
  336. //
  337. // skbug.com/4116: We should perhaps also apply the alpha mask in kStandard
  338. // mode. We just haven't seen any images that expect this
  339. // behavior.
  340. //
  341. // Header types are matched based on size. If the header is
  342. // V3+, we are guaranteed to be able to read at least this size.
  343. SkASSERT(infoBytesRemaining >= 52);
  344. inputMasks.alpha = get_int(iBuffer.get(), 48);
  345. break;
  346. case kOS2VX_BmpHeaderType:
  347. // TODO: Decide if we intend to support this.
  348. // It is unsupported in the previous version and
  349. // in chromium. I have not come across a test case
  350. // that uses this format.
  351. SkCodecPrintf("Error: huffman format unsupported.\n");
  352. return kUnimplemented;
  353. default:
  354. SkCodecPrintf("Error: invalid bmp bit masks header.\n");
  355. return kInvalidInput;
  356. }
  357. break;
  358. case kJpeg_BmpCompressionMethod:
  359. if (24 == bitsPerPixel) {
  360. inputFormat = kRLE_BmpInputFormat;
  361. break;
  362. }
  363. // Fall through
  364. case kPng_BmpCompressionMethod:
  365. // TODO: Decide if we intend to support this.
  366. // It is unsupported in the previous version and
  367. // in chromium. I think it is used mostly for printers.
  368. SkCodecPrintf("Error: compression format not supported.\n");
  369. return kUnimplemented;
  370. case kCMYK_BmpCompressionMethod:
  371. case kCMYK8BitRLE_BmpCompressionMethod:
  372. case kCMYK4BitRLE_BmpCompressionMethod:
  373. // TODO: Same as above.
  374. SkCodecPrintf("Error: CMYK not supported for bitmap decoding.\n");
  375. return kUnimplemented;
  376. default:
  377. SkCodecPrintf("Error: invalid format for bitmap decoding.\n");
  378. return kInvalidInput;
  379. }
  380. iBuffer.reset();
  381. // Calculate the number of bytes read so far
  382. const uint32_t bytesRead = kBmpHeaderBytes + infoBytes + maskBytes;
  383. if (!inIco && offset < bytesRead) {
  384. // TODO (msarett): Do we really want to fail if the offset in the header is invalid?
  385. // Seems like we can just assume that the offset is zero and try to decode?
  386. // Maybe we don't want to try to decode corrupt images?
  387. SkCodecPrintf("Error: pixel data offset less than header size.\n");
  388. return kInvalidInput;
  389. }
  390. switch (inputFormat) {
  391. case kStandard_BmpInputFormat: {
  392. // BMPs are generally opaque, however BMPs-in-ICOs may contain
  393. // a transparency mask after the image. Therefore, we mark the
  394. // alpha as kBinary if the BMP is contained in an ICO.
  395. // We use |isOpaque| to indicate if the BMP itself is opaque.
  396. SkEncodedInfo::Alpha alpha = inIco ? SkEncodedInfo::kBinary_Alpha :
  397. SkEncodedInfo::kOpaque_Alpha;
  398. bool isOpaque = true;
  399. SkEncodedInfo::Color color;
  400. uint8_t bitsPerComponent;
  401. switch (bitsPerPixel) {
  402. // Palette formats
  403. case 1:
  404. case 2:
  405. case 4:
  406. case 8:
  407. // In the case of ICO, kBGRA is actually the closest match,
  408. // since we will need to apply a transparency mask.
  409. if (inIco) {
  410. color = SkEncodedInfo::kBGRA_Color;
  411. bitsPerComponent = 8;
  412. } else {
  413. color = SkEncodedInfo::kPalette_Color;
  414. bitsPerComponent = (uint8_t) bitsPerPixel;
  415. }
  416. break;
  417. case 24:
  418. // In the case of ICO, kBGRA is actually the closest match,
  419. // since we will need to apply a transparency mask.
  420. color = inIco ? SkEncodedInfo::kBGRA_Color : SkEncodedInfo::kBGR_Color;
  421. bitsPerComponent = 8;
  422. break;
  423. case 32:
  424. // 32-bit BMP-in-ICOs actually use the alpha channel in place of a
  425. // transparency mask.
  426. if (inIco) {
  427. isOpaque = false;
  428. alpha = SkEncodedInfo::kUnpremul_Alpha;
  429. color = SkEncodedInfo::kBGRA_Color;
  430. } else {
  431. color = SkEncodedInfo::kBGRX_Color;
  432. }
  433. bitsPerComponent = 8;
  434. break;
  435. default:
  436. SkCodecPrintf("Error: invalid input value for bits per pixel.\n");
  437. return kInvalidInput;
  438. }
  439. if (codecOut) {
  440. // We require streams to have a memory base for Bmp-in-Ico decodes.
  441. SkASSERT(!inIco || nullptr != stream->getMemoryBase());
  442. // Set the image info and create a codec.
  443. auto info = SkEncodedInfo::Make(width, height, color, alpha, bitsPerComponent);
  444. codecOut->reset(new SkBmpStandardCodec(std::move(info),
  445. std::unique_ptr<SkStream>(stream),
  446. bitsPerPixel, numColors, bytesPerColor,
  447. offset - bytesRead, rowOrder, isOpaque,
  448. inIco));
  449. return static_cast<SkBmpStandardCodec*>(codecOut->get())->didCreateSrcBuffer()
  450. ? kSuccess : kInvalidInput;
  451. }
  452. return kSuccess;
  453. }
  454. case kBitMask_BmpInputFormat: {
  455. // Bmp-in-Ico must be standard mode
  456. if (inIco) {
  457. SkCodecPrintf("Error: Icos may not use bit mask format.\n");
  458. return kInvalidInput;
  459. }
  460. switch (bitsPerPixel) {
  461. case 16:
  462. case 24:
  463. case 32:
  464. break;
  465. default:
  466. SkCodecPrintf("Error: invalid input value for bits per pixel.\n");
  467. return kInvalidInput;
  468. }
  469. // Skip to the start of the pixel array.
  470. // We can do this here because there is no color table to read
  471. // in bit mask mode.
  472. if (stream->skip(offset - bytesRead) != offset - bytesRead) {
  473. SkCodecPrintf("Error: unable to skip to image data.\n");
  474. return kIncompleteInput;
  475. }
  476. if (codecOut) {
  477. // Check that input bit masks are valid and create the masks object
  478. SkASSERT(bitsPerPixel % 8 == 0);
  479. std::unique_ptr<SkMasks> masks(SkMasks::CreateMasks(inputMasks, bitsPerPixel/8));
  480. if (nullptr == masks) {
  481. SkCodecPrintf("Error: invalid input masks.\n");
  482. return kInvalidInput;
  483. }
  484. // Masked bmps are not a great fit for SkEncodedInfo, since they have
  485. // arbitrary component orderings and bits per component. Here we choose
  486. // somewhat reasonable values - it's ok that we don't match exactly
  487. // because SkBmpMaskCodec has its own mask swizzler anyway.
  488. SkEncodedInfo::Color color;
  489. SkEncodedInfo::Alpha alpha;
  490. if (masks->getAlphaMask()) {
  491. color = SkEncodedInfo::kBGRA_Color;
  492. alpha = SkEncodedInfo::kUnpremul_Alpha;
  493. } else {
  494. color = SkEncodedInfo::kBGR_Color;
  495. alpha = SkEncodedInfo::kOpaque_Alpha;
  496. }
  497. auto info = SkEncodedInfo::Make(width, height, color, alpha, 8);
  498. codecOut->reset(new SkBmpMaskCodec(std::move(info),
  499. std::unique_ptr<SkStream>(stream), bitsPerPixel,
  500. masks.release(), rowOrder));
  501. return static_cast<SkBmpMaskCodec*>(codecOut->get())->didCreateSrcBuffer()
  502. ? kSuccess : kInvalidInput;
  503. }
  504. return kSuccess;
  505. }
  506. case kRLE_BmpInputFormat: {
  507. // We should not reach this point without a valid value of bitsPerPixel.
  508. SkASSERT(4 == bitsPerPixel || 8 == bitsPerPixel || 24 == bitsPerPixel);
  509. // Check for a valid number of total bytes when in RLE mode
  510. if (totalBytes <= offset) {
  511. SkCodecPrintf("Error: RLE requires valid input size.\n");
  512. return kInvalidInput;
  513. }
  514. // Bmp-in-Ico must be standard mode
  515. // When inIco is true, this line cannot be reached, since we
  516. // require that RLE Bmps have a valid number of totalBytes, and
  517. // Icos skip the header that contains totalBytes.
  518. SkASSERT(!inIco);
  519. if (codecOut) {
  520. // RLE inputs may skip pixels, leaving them as transparent. This
  521. // is uncommon, but we cannot be certain that an RLE bmp will be
  522. // opaque or that we will be able to represent it with a palette.
  523. // For that reason, we always indicate that we are kBGRA.
  524. auto info = SkEncodedInfo::Make(width, height, SkEncodedInfo::kBGRA_Color,
  525. SkEncodedInfo::kBinary_Alpha, 8);
  526. codecOut->reset(new SkBmpRLECodec(std::move(info),
  527. std::unique_ptr<SkStream>(stream), bitsPerPixel,
  528. numColors, bytesPerColor, offset - bytesRead,
  529. rowOrder));
  530. }
  531. return kSuccess;
  532. }
  533. default:
  534. SkASSERT(false);
  535. return kInvalidInput;
  536. }
  537. }
  538. /*
  539. * Creates a bmp decoder
  540. * Reads enough of the stream to determine the image format
  541. */
  542. std::unique_ptr<SkCodec> SkBmpCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
  543. Result* result, bool inIco) {
  544. std::unique_ptr<SkCodec> codec;
  545. *result = ReadHeader(stream.get(), inIco, &codec);
  546. if (codec) {
  547. // codec has taken ownership of stream, so we do not need to delete it.
  548. stream.release();
  549. }
  550. return kSuccess == *result ? std::move(codec) : nullptr;
  551. }
  552. SkBmpCodec::SkBmpCodec(SkEncodedInfo&& info, std::unique_ptr<SkStream> stream,
  553. uint16_t bitsPerPixel, SkCodec::SkScanlineOrder rowOrder)
  554. : INHERITED(std::move(info), kXformSrcColorFormat, std::move(stream))
  555. , fBitsPerPixel(bitsPerPixel)
  556. , fRowOrder(rowOrder)
  557. , fSrcRowBytes(SkAlign4(compute_row_bytes(this->dimensions().width(), fBitsPerPixel)))
  558. , fXformBuffer(nullptr)
  559. {}
  560. bool SkBmpCodec::onRewind() {
  561. return SkBmpCodec::ReadHeader(this->stream(), this->inIco(), nullptr) == kSuccess;
  562. }
  563. int32_t SkBmpCodec::getDstRow(int32_t y, int32_t height) const {
  564. if (SkCodec::kTopDown_SkScanlineOrder == fRowOrder) {
  565. return y;
  566. }
  567. SkASSERT(SkCodec::kBottomUp_SkScanlineOrder == fRowOrder);
  568. return height - y - 1;
  569. }
  570. SkCodec::Result SkBmpCodec::prepareToDecode(const SkImageInfo& dstInfo,
  571. const SkCodec::Options& options) {
  572. return this->onPrepareToDecode(dstInfo, options);
  573. }
  574. SkCodec::Result SkBmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
  575. const SkCodec::Options& options) {
  576. return prepareToDecode(dstInfo, options);
  577. }
  578. int SkBmpCodec::onGetScanlines(void* dst, int count, size_t rowBytes) {
  579. // Create a new image info representing the portion of the image to decode
  580. SkImageInfo rowInfo = this->dstInfo().makeWH(this->dstInfo().width(), count);
  581. // Decode the requested rows
  582. return this->decodeRows(rowInfo, dst, rowBytes, this->options());
  583. }
  584. bool SkBmpCodec::skipRows(int count) {
  585. const size_t bytesToSkip = count * fSrcRowBytes;
  586. return this->stream()->skip(bytesToSkip) == bytesToSkip;
  587. }
  588. bool SkBmpCodec::onSkipScanlines(int count) {
  589. return this->skipRows(count);
  590. }