SkJpegCodec.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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 "src/codec/SkJpegCodec.h"
  8. #include "include/codec/SkCodec.h"
  9. #include "include/core/SkStream.h"
  10. #include "include/core/SkTypes.h"
  11. #include "include/private/SkColorData.h"
  12. #include "include/private/SkTemplates.h"
  13. #include "include/private/SkTo.h"
  14. #include "src/codec/SkCodecPriv.h"
  15. #include "src/codec/SkJpegDecoderMgr.h"
  16. #include "src/pdf/SkJpegInfo.h"
  17. // stdio is needed for libjpeg-turbo
  18. #include <stdio.h>
  19. #include "src/codec/SkJpegUtility.h"
  20. // This warning triggers false postives way too often in here.
  21. #if defined(__GNUC__) && !defined(__clang__)
  22. #pragma GCC diagnostic ignored "-Wclobbered"
  23. #endif
  24. extern "C" {
  25. #include "jerror.h"
  26. #include "jpeglib.h"
  27. }
  28. bool SkJpegCodec::IsJpeg(const void* buffer, size_t bytesRead) {
  29. constexpr uint8_t jpegSig[] = { 0xFF, 0xD8, 0xFF };
  30. return bytesRead >= 3 && !memcmp(buffer, jpegSig, sizeof(jpegSig));
  31. }
  32. static uint32_t get_endian_int(const uint8_t* data, bool littleEndian) {
  33. if (littleEndian) {
  34. return (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | (data[0]);
  35. }
  36. return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | (data[3]);
  37. }
  38. const uint32_t kExifHeaderSize = 14;
  39. const uint32_t kExifMarker = JPEG_APP0 + 1;
  40. static bool is_orientation_marker(jpeg_marker_struct* marker, SkEncodedOrigin* orientation) {
  41. if (kExifMarker != marker->marker || marker->data_length < kExifHeaderSize) {
  42. return false;
  43. }
  44. constexpr uint8_t kExifSig[] { 'E', 'x', 'i', 'f', '\0' };
  45. if (memcmp(marker->data, kExifSig, sizeof(kExifSig))) {
  46. return false;
  47. }
  48. // Account for 'E', 'x', 'i', 'f', '\0', '<fill byte>'.
  49. constexpr size_t kOffset = 6;
  50. return is_orientation_marker(marker->data + kOffset, marker->data_length - kOffset,
  51. orientation);
  52. }
  53. bool is_orientation_marker(const uint8_t* data, size_t data_length, SkEncodedOrigin* orientation) {
  54. bool littleEndian;
  55. // We need eight bytes to read the endian marker and the offset, below.
  56. if (data_length < 8 || !is_valid_endian_marker(data, &littleEndian)) {
  57. return false;
  58. }
  59. // Get the offset from the start of the marker.
  60. // Though this only reads four bytes, use a larger int in case it overflows.
  61. uint64_t offset = get_endian_int(data + 4, littleEndian);
  62. // Require that the marker is at least large enough to contain the number of entries.
  63. if (data_length < offset + 2) {
  64. return false;
  65. }
  66. uint32_t numEntries = get_endian_short(data + offset, littleEndian);
  67. // Tag (2 bytes), Datatype (2 bytes), Number of elements (4 bytes), Data (4 bytes)
  68. const uint32_t kEntrySize = 12;
  69. const auto max = SkTo<uint32_t>((data_length - offset - 2) / kEntrySize);
  70. numEntries = SkTMin(numEntries, max);
  71. // Advance the data to the start of the entries.
  72. data += offset + 2;
  73. const uint16_t kOriginTag = 0x112;
  74. const uint16_t kOriginType = 3;
  75. for (uint32_t i = 0; i < numEntries; i++, data += kEntrySize) {
  76. uint16_t tag = get_endian_short(data, littleEndian);
  77. uint16_t type = get_endian_short(data + 2, littleEndian);
  78. uint32_t count = get_endian_int(data + 4, littleEndian);
  79. if (kOriginTag == tag && kOriginType == type && 1 == count) {
  80. uint16_t val = get_endian_short(data + 8, littleEndian);
  81. if (0 < val && val <= kLast_SkEncodedOrigin) {
  82. *orientation = (SkEncodedOrigin) val;
  83. return true;
  84. }
  85. }
  86. }
  87. return false;
  88. }
  89. static SkEncodedOrigin get_exif_orientation(jpeg_decompress_struct* dinfo) {
  90. SkEncodedOrigin orientation;
  91. for (jpeg_marker_struct* marker = dinfo->marker_list; marker; marker = marker->next) {
  92. if (is_orientation_marker(marker, &orientation)) {
  93. return orientation;
  94. }
  95. }
  96. return kDefault_SkEncodedOrigin;
  97. }
  98. static bool is_icc_marker(jpeg_marker_struct* marker) {
  99. if (kICCMarker != marker->marker || marker->data_length < kICCMarkerHeaderSize) {
  100. return false;
  101. }
  102. return !memcmp(marker->data, kICCSig, sizeof(kICCSig));
  103. }
  104. /*
  105. * ICC profiles may be stored using a sequence of multiple markers. We obtain the ICC profile
  106. * in two steps:
  107. * (1) Discover all ICC profile markers and verify that they are numbered properly.
  108. * (2) Copy the data from each marker into a contiguous ICC profile.
  109. */
  110. static std::unique_ptr<SkEncodedInfo::ICCProfile> read_color_profile(jpeg_decompress_struct* dinfo)
  111. {
  112. // Note that 256 will be enough storage space since each markerIndex is stored in 8-bits.
  113. jpeg_marker_struct* markerSequence[256];
  114. memset(markerSequence, 0, sizeof(markerSequence));
  115. uint8_t numMarkers = 0;
  116. size_t totalBytes = 0;
  117. // Discover any ICC markers and verify that they are numbered properly.
  118. for (jpeg_marker_struct* marker = dinfo->marker_list; marker; marker = marker->next) {
  119. if (is_icc_marker(marker)) {
  120. // Verify that numMarkers is valid and consistent.
  121. if (0 == numMarkers) {
  122. numMarkers = marker->data[13];
  123. if (0 == numMarkers) {
  124. SkCodecPrintf("ICC Profile Error: numMarkers must be greater than zero.\n");
  125. return nullptr;
  126. }
  127. } else if (numMarkers != marker->data[13]) {
  128. SkCodecPrintf("ICC Profile Error: numMarkers must be consistent.\n");
  129. return nullptr;
  130. }
  131. // Verify that the markerIndex is valid and unique. Note that zero is not
  132. // a valid index.
  133. uint8_t markerIndex = marker->data[12];
  134. if (markerIndex == 0 || markerIndex > numMarkers) {
  135. SkCodecPrintf("ICC Profile Error: markerIndex is invalid.\n");
  136. return nullptr;
  137. }
  138. if (markerSequence[markerIndex]) {
  139. SkCodecPrintf("ICC Profile Error: Duplicate value of markerIndex.\n");
  140. return nullptr;
  141. }
  142. markerSequence[markerIndex] = marker;
  143. SkASSERT(marker->data_length >= kICCMarkerHeaderSize);
  144. totalBytes += marker->data_length - kICCMarkerHeaderSize;
  145. }
  146. }
  147. if (0 == totalBytes) {
  148. // No non-empty ICC profile markers were found.
  149. return nullptr;
  150. }
  151. // Combine the ICC marker data into a contiguous profile.
  152. sk_sp<SkData> iccData = SkData::MakeUninitialized(totalBytes);
  153. void* dst = iccData->writable_data();
  154. for (uint32_t i = 1; i <= numMarkers; i++) {
  155. jpeg_marker_struct* marker = markerSequence[i];
  156. if (!marker) {
  157. SkCodecPrintf("ICC Profile Error: Missing marker %d of %d.\n", i, numMarkers);
  158. return nullptr;
  159. }
  160. void* src = SkTAddOffset<void>(marker->data, kICCMarkerHeaderSize);
  161. size_t bytes = marker->data_length - kICCMarkerHeaderSize;
  162. memcpy(dst, src, bytes);
  163. dst = SkTAddOffset<void>(dst, bytes);
  164. }
  165. return SkEncodedInfo::ICCProfile::Make(std::move(iccData));
  166. }
  167. SkCodec::Result SkJpegCodec::ReadHeader(SkStream* stream, SkCodec** codecOut,
  168. JpegDecoderMgr** decoderMgrOut,
  169. std::unique_ptr<SkEncodedInfo::ICCProfile> defaultColorProfile) {
  170. // Create a JpegDecoderMgr to own all of the decompress information
  171. std::unique_ptr<JpegDecoderMgr> decoderMgr(new JpegDecoderMgr(stream));
  172. // libjpeg errors will be caught and reported here
  173. skjpeg_error_mgr::AutoPushJmpBuf jmp(decoderMgr->errorMgr());
  174. if (setjmp(jmp)) {
  175. return decoderMgr->returnFailure("ReadHeader", kInvalidInput);
  176. }
  177. // Initialize the decompress info and the source manager
  178. decoderMgr->init();
  179. auto* dinfo = decoderMgr->dinfo();
  180. // Instruct jpeg library to save the markers that we care about. Since
  181. // the orientation and color profile will not change, we can skip this
  182. // step on rewinds.
  183. if (codecOut) {
  184. jpeg_save_markers(dinfo, kExifMarker, 0xFFFF);
  185. jpeg_save_markers(dinfo, kICCMarker, 0xFFFF);
  186. }
  187. // Read the jpeg header
  188. switch (jpeg_read_header(dinfo, true)) {
  189. case JPEG_HEADER_OK:
  190. break;
  191. case JPEG_SUSPENDED:
  192. return decoderMgr->returnFailure("ReadHeader", kIncompleteInput);
  193. default:
  194. return decoderMgr->returnFailure("ReadHeader", kInvalidInput);
  195. }
  196. if (codecOut) {
  197. // Get the encoded color type
  198. SkEncodedInfo::Color color;
  199. if (!decoderMgr->getEncodedColor(&color)) {
  200. return kInvalidInput;
  201. }
  202. SkEncodedOrigin orientation = get_exif_orientation(dinfo);
  203. auto profile = read_color_profile(dinfo);
  204. if (profile) {
  205. auto type = profile->profile()->data_color_space;
  206. switch (decoderMgr->dinfo()->jpeg_color_space) {
  207. case JCS_CMYK:
  208. case JCS_YCCK:
  209. if (type != skcms_Signature_CMYK) {
  210. profile = nullptr;
  211. }
  212. break;
  213. case JCS_GRAYSCALE:
  214. if (type != skcms_Signature_Gray &&
  215. type != skcms_Signature_RGB)
  216. {
  217. profile = nullptr;
  218. }
  219. break;
  220. default:
  221. if (type != skcms_Signature_RGB) {
  222. profile = nullptr;
  223. }
  224. break;
  225. }
  226. }
  227. if (!profile) {
  228. profile = std::move(defaultColorProfile);
  229. }
  230. SkEncodedInfo info = SkEncodedInfo::Make(dinfo->image_width, dinfo->image_height,
  231. color, SkEncodedInfo::kOpaque_Alpha, 8,
  232. std::move(profile));
  233. SkJpegCodec* codec = new SkJpegCodec(std::move(info), std::unique_ptr<SkStream>(stream),
  234. decoderMgr.release(), orientation);
  235. *codecOut = codec;
  236. } else {
  237. SkASSERT(nullptr != decoderMgrOut);
  238. *decoderMgrOut = decoderMgr.release();
  239. }
  240. return kSuccess;
  241. }
  242. std::unique_ptr<SkCodec> SkJpegCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
  243. Result* result) {
  244. return SkJpegCodec::MakeFromStream(std::move(stream), result, nullptr);
  245. }
  246. std::unique_ptr<SkCodec> SkJpegCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
  247. Result* result, std::unique_ptr<SkEncodedInfo::ICCProfile> defaultColorProfile) {
  248. SkCodec* codec = nullptr;
  249. *result = ReadHeader(stream.get(), &codec, nullptr, std::move(defaultColorProfile));
  250. if (kSuccess == *result) {
  251. // Codec has taken ownership of the stream, we do not need to delete it
  252. SkASSERT(codec);
  253. stream.release();
  254. return std::unique_ptr<SkCodec>(codec);
  255. }
  256. return nullptr;
  257. }
  258. SkJpegCodec::SkJpegCodec(SkEncodedInfo&& info, std::unique_ptr<SkStream> stream,
  259. JpegDecoderMgr* decoderMgr, SkEncodedOrigin origin)
  260. : INHERITED(std::move(info), skcms_PixelFormat_RGBA_8888, std::move(stream), origin)
  261. , fDecoderMgr(decoderMgr)
  262. , fReadyState(decoderMgr->dinfo()->global_state)
  263. , fSwizzleSrcRow(nullptr)
  264. , fColorXformSrcRow(nullptr)
  265. , fSwizzlerSubset(SkIRect::MakeEmpty())
  266. {}
  267. /*
  268. * Return the row bytes of a particular image type and width
  269. */
  270. static size_t get_row_bytes(const j_decompress_ptr dinfo) {
  271. const size_t colorBytes = (dinfo->out_color_space == JCS_RGB565) ? 2 :
  272. dinfo->out_color_components;
  273. return dinfo->output_width * colorBytes;
  274. }
  275. /*
  276. * Calculate output dimensions based on the provided factors.
  277. *
  278. * Not to be used on the actual jpeg_decompress_struct used for decoding, since it will
  279. * incorrectly modify num_components.
  280. */
  281. void calc_output_dimensions(jpeg_decompress_struct* dinfo, unsigned int num, unsigned int denom) {
  282. dinfo->num_components = 0;
  283. dinfo->scale_num = num;
  284. dinfo->scale_denom = denom;
  285. jpeg_calc_output_dimensions(dinfo);
  286. }
  287. /*
  288. * Return a valid set of output dimensions for this decoder, given an input scale
  289. */
  290. SkISize SkJpegCodec::onGetScaledDimensions(float desiredScale) const {
  291. // libjpeg-turbo supports scaling by 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, and 1/1, so we will
  292. // support these as well
  293. unsigned int num;
  294. unsigned int denom = 8;
  295. if (desiredScale >= 0.9375) {
  296. num = 8;
  297. } else if (desiredScale >= 0.8125) {
  298. num = 7;
  299. } else if (desiredScale >= 0.6875f) {
  300. num = 6;
  301. } else if (desiredScale >= 0.5625f) {
  302. num = 5;
  303. } else if (desiredScale >= 0.4375f) {
  304. num = 4;
  305. } else if (desiredScale >= 0.3125f) {
  306. num = 3;
  307. } else if (desiredScale >= 0.1875f) {
  308. num = 2;
  309. } else {
  310. num = 1;
  311. }
  312. // Set up a fake decompress struct in order to use libjpeg to calculate output dimensions
  313. jpeg_decompress_struct dinfo;
  314. sk_bzero(&dinfo, sizeof(dinfo));
  315. dinfo.image_width = this->dimensions().width();
  316. dinfo.image_height = this->dimensions().height();
  317. dinfo.global_state = fReadyState;
  318. calc_output_dimensions(&dinfo, num, denom);
  319. // Return the calculated output dimensions for the given scale
  320. return SkISize::Make(dinfo.output_width, dinfo.output_height);
  321. }
  322. bool SkJpegCodec::onRewind() {
  323. JpegDecoderMgr* decoderMgr = nullptr;
  324. if (kSuccess != ReadHeader(this->stream(), nullptr, &decoderMgr, nullptr)) {
  325. return fDecoderMgr->returnFalse("onRewind");
  326. }
  327. SkASSERT(nullptr != decoderMgr);
  328. fDecoderMgr.reset(decoderMgr);
  329. fSwizzler.reset(nullptr);
  330. fSwizzleSrcRow = nullptr;
  331. fColorXformSrcRow = nullptr;
  332. fStorage.reset();
  333. return true;
  334. }
  335. bool SkJpegCodec::conversionSupported(const SkImageInfo& dstInfo, bool srcIsOpaque,
  336. bool needsColorXform) {
  337. SkASSERT(srcIsOpaque);
  338. if (kUnknown_SkAlphaType == dstInfo.alphaType()) {
  339. return false;
  340. }
  341. if (kOpaque_SkAlphaType != dstInfo.alphaType()) {
  342. SkCodecPrintf("Warning: an opaque image should be decoded as opaque "
  343. "- it is being decoded as non-opaque, which will draw slower\n");
  344. }
  345. J_COLOR_SPACE encodedColorType = fDecoderMgr->dinfo()->jpeg_color_space;
  346. // Check for valid color types and set the output color space
  347. switch (dstInfo.colorType()) {
  348. case kRGBA_8888_SkColorType:
  349. fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA;
  350. break;
  351. case kBGRA_8888_SkColorType:
  352. if (needsColorXform) {
  353. // Always using RGBA as the input format for color xforms makes the
  354. // implementation a little simpler.
  355. fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA;
  356. } else {
  357. fDecoderMgr->dinfo()->out_color_space = JCS_EXT_BGRA;
  358. }
  359. break;
  360. case kRGB_565_SkColorType:
  361. if (needsColorXform) {
  362. fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA;
  363. } else {
  364. fDecoderMgr->dinfo()->dither_mode = JDITHER_NONE;
  365. fDecoderMgr->dinfo()->out_color_space = JCS_RGB565;
  366. }
  367. break;
  368. case kGray_8_SkColorType:
  369. if (JCS_GRAYSCALE != encodedColorType) {
  370. return false;
  371. }
  372. if (needsColorXform) {
  373. fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA;
  374. } else {
  375. fDecoderMgr->dinfo()->out_color_space = JCS_GRAYSCALE;
  376. }
  377. break;
  378. case kRGBA_F16_SkColorType:
  379. SkASSERT(needsColorXform);
  380. fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA;
  381. break;
  382. default:
  383. return false;
  384. }
  385. // Check if we will decode to CMYK. libjpeg-turbo does not convert CMYK to RGBA, so
  386. // we must do it ourselves.
  387. if (JCS_CMYK == encodedColorType || JCS_YCCK == encodedColorType) {
  388. fDecoderMgr->dinfo()->out_color_space = JCS_CMYK;
  389. }
  390. return true;
  391. }
  392. /*
  393. * Checks if we can natively scale to the requested dimensions and natively scales the
  394. * dimensions if possible
  395. */
  396. bool SkJpegCodec::onDimensionsSupported(const SkISize& size) {
  397. skjpeg_error_mgr::AutoPushJmpBuf jmp(fDecoderMgr->errorMgr());
  398. if (setjmp(jmp)) {
  399. return fDecoderMgr->returnFalse("onDimensionsSupported");
  400. }
  401. const unsigned int dstWidth = size.width();
  402. const unsigned int dstHeight = size.height();
  403. // Set up a fake decompress struct in order to use libjpeg to calculate output dimensions
  404. // FIXME: Why is this necessary?
  405. jpeg_decompress_struct dinfo;
  406. sk_bzero(&dinfo, sizeof(dinfo));
  407. dinfo.image_width = this->dimensions().width();
  408. dinfo.image_height = this->dimensions().height();
  409. dinfo.global_state = fReadyState;
  410. // libjpeg-turbo can scale to 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, and 1/1
  411. unsigned int num = 8;
  412. const unsigned int denom = 8;
  413. calc_output_dimensions(&dinfo, num, denom);
  414. while (dinfo.output_width != dstWidth || dinfo.output_height != dstHeight) {
  415. // Return a failure if we have tried all of the possible scales
  416. if (1 == num || dstWidth > dinfo.output_width || dstHeight > dinfo.output_height) {
  417. return false;
  418. }
  419. // Try the next scale
  420. num -= 1;
  421. calc_output_dimensions(&dinfo, num, denom);
  422. }
  423. fDecoderMgr->dinfo()->scale_num = num;
  424. fDecoderMgr->dinfo()->scale_denom = denom;
  425. return true;
  426. }
  427. int SkJpegCodec::readRows(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, int count,
  428. const Options& opts) {
  429. // Set the jump location for libjpeg-turbo errors
  430. skjpeg_error_mgr::AutoPushJmpBuf jmp(fDecoderMgr->errorMgr());
  431. if (setjmp(jmp)) {
  432. return 0;
  433. }
  434. // When fSwizzleSrcRow is non-null, it means that we need to swizzle. In this case,
  435. // we will always decode into fSwizzlerSrcRow before swizzling into the next buffer.
  436. // We can never swizzle "in place" because the swizzler may perform sampling and/or
  437. // subsetting.
  438. // When fColorXformSrcRow is non-null, it means that we need to color xform and that
  439. // we cannot color xform "in place" (many times we can, but not when the src and dst
  440. // are different sizes).
  441. // In this case, we will color xform from fColorXformSrcRow into the dst.
  442. JSAMPLE* decodeDst = (JSAMPLE*) dst;
  443. uint32_t* swizzleDst = (uint32_t*) dst;
  444. size_t decodeDstRowBytes = rowBytes;
  445. size_t swizzleDstRowBytes = rowBytes;
  446. int dstWidth = opts.fSubset ? opts.fSubset->width() : dstInfo.width();
  447. if (fSwizzleSrcRow && fColorXformSrcRow) {
  448. decodeDst = (JSAMPLE*) fSwizzleSrcRow;
  449. swizzleDst = fColorXformSrcRow;
  450. decodeDstRowBytes = 0;
  451. swizzleDstRowBytes = 0;
  452. dstWidth = fSwizzler->swizzleWidth();
  453. } else if (fColorXformSrcRow) {
  454. decodeDst = (JSAMPLE*) fColorXformSrcRow;
  455. swizzleDst = fColorXformSrcRow;
  456. decodeDstRowBytes = 0;
  457. swizzleDstRowBytes = 0;
  458. } else if (fSwizzleSrcRow) {
  459. decodeDst = (JSAMPLE*) fSwizzleSrcRow;
  460. decodeDstRowBytes = 0;
  461. dstWidth = fSwizzler->swizzleWidth();
  462. }
  463. for (int y = 0; y < count; y++) {
  464. uint32_t lines = jpeg_read_scanlines(fDecoderMgr->dinfo(), &decodeDst, 1);
  465. if (0 == lines) {
  466. return y;
  467. }
  468. if (fSwizzler) {
  469. fSwizzler->swizzle(swizzleDst, decodeDst);
  470. }
  471. if (this->colorXform()) {
  472. this->applyColorXform(dst, swizzleDst, dstWidth);
  473. dst = SkTAddOffset<void>(dst, rowBytes);
  474. }
  475. decodeDst = SkTAddOffset<JSAMPLE>(decodeDst, decodeDstRowBytes);
  476. swizzleDst = SkTAddOffset<uint32_t>(swizzleDst, swizzleDstRowBytes);
  477. }
  478. return count;
  479. }
  480. /*
  481. * This is a bit tricky. We only need the swizzler to do format conversion if the jpeg is
  482. * encoded as CMYK.
  483. * And even then we still may not need it. If the jpeg has a CMYK color profile and a color
  484. * xform, the color xform will handle the CMYK->RGB conversion.
  485. */
  486. static inline bool needs_swizzler_to_convert_from_cmyk(J_COLOR_SPACE jpegColorType,
  487. const skcms_ICCProfile* srcProfile,
  488. bool hasColorSpaceXform) {
  489. if (JCS_CMYK != jpegColorType) {
  490. return false;
  491. }
  492. bool hasCMYKColorSpace = srcProfile && srcProfile->data_color_space == skcms_Signature_CMYK;
  493. return !hasCMYKColorSpace || !hasColorSpaceXform;
  494. }
  495. /*
  496. * Performs the jpeg decode
  497. */
  498. SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo,
  499. void* dst, size_t dstRowBytes,
  500. const Options& options,
  501. int* rowsDecoded) {
  502. if (options.fSubset) {
  503. // Subsets are not supported.
  504. return kUnimplemented;
  505. }
  506. // Get a pointer to the decompress info since we will use it quite frequently
  507. jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
  508. // Set the jump location for libjpeg errors
  509. skjpeg_error_mgr::AutoPushJmpBuf jmp(fDecoderMgr->errorMgr());
  510. if (setjmp(jmp)) {
  511. return fDecoderMgr->returnFailure("setjmp", kInvalidInput);
  512. }
  513. if (!jpeg_start_decompress(dinfo)) {
  514. return fDecoderMgr->returnFailure("startDecompress", kInvalidInput);
  515. }
  516. // The recommended output buffer height should always be 1 in high quality modes.
  517. // If it's not, we want to know because it means our strategy is not optimal.
  518. SkASSERT(1 == dinfo->rec_outbuf_height);
  519. if (needs_swizzler_to_convert_from_cmyk(dinfo->out_color_space,
  520. this->getEncodedInfo().profile(), this->colorXform())) {
  521. this->initializeSwizzler(dstInfo, options, true);
  522. }
  523. this->allocateStorage(dstInfo);
  524. int rows = this->readRows(dstInfo, dst, dstRowBytes, dstInfo.height(), options);
  525. if (rows < dstInfo.height()) {
  526. *rowsDecoded = rows;
  527. return fDecoderMgr->returnFailure("Incomplete image data", kIncompleteInput);
  528. }
  529. return kSuccess;
  530. }
  531. void SkJpegCodec::allocateStorage(const SkImageInfo& dstInfo) {
  532. int dstWidth = dstInfo.width();
  533. size_t swizzleBytes = 0;
  534. if (fSwizzler) {
  535. swizzleBytes = get_row_bytes(fDecoderMgr->dinfo());
  536. dstWidth = fSwizzler->swizzleWidth();
  537. SkASSERT(!this->colorXform() || SkIsAlign4(swizzleBytes));
  538. }
  539. size_t xformBytes = 0;
  540. if (this->colorXform() && sizeof(uint32_t) != dstInfo.bytesPerPixel()) {
  541. xformBytes = dstWidth * sizeof(uint32_t);
  542. }
  543. size_t totalBytes = swizzleBytes + xformBytes;
  544. if (totalBytes > 0) {
  545. fStorage.reset(totalBytes);
  546. fSwizzleSrcRow = (swizzleBytes > 0) ? fStorage.get() : nullptr;
  547. fColorXformSrcRow = (xformBytes > 0) ?
  548. SkTAddOffset<uint32_t>(fStorage.get(), swizzleBytes) : nullptr;
  549. }
  550. }
  551. void SkJpegCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& options,
  552. bool needsCMYKToRGB) {
  553. Options swizzlerOptions = options;
  554. if (options.fSubset) {
  555. // Use fSwizzlerSubset if this is a subset decode. This is necessary in the case
  556. // where libjpeg-turbo provides a subset and then we need to subset it further.
  557. // Also, verify that fSwizzlerSubset is initialized and valid.
  558. SkASSERT(!fSwizzlerSubset.isEmpty() && fSwizzlerSubset.x() <= options.fSubset->x() &&
  559. fSwizzlerSubset.width() == options.fSubset->width());
  560. swizzlerOptions.fSubset = &fSwizzlerSubset;
  561. }
  562. SkImageInfo swizzlerDstInfo = dstInfo;
  563. if (this->colorXform()) {
  564. // The color xform will be expecting RGBA 8888 input.
  565. swizzlerDstInfo = swizzlerDstInfo.makeColorType(kRGBA_8888_SkColorType);
  566. }
  567. if (needsCMYKToRGB) {
  568. // The swizzler is used to convert to from CMYK.
  569. // The swizzler does not use the width or height on SkEncodedInfo.
  570. auto swizzlerInfo = SkEncodedInfo::Make(0, 0, SkEncodedInfo::kInvertedCMYK_Color,
  571. SkEncodedInfo::kOpaque_Alpha, 8);
  572. fSwizzler = SkSwizzler::Make(swizzlerInfo, nullptr, swizzlerDstInfo, swizzlerOptions);
  573. } else {
  574. int srcBPP = 0;
  575. switch (fDecoderMgr->dinfo()->out_color_space) {
  576. case JCS_EXT_RGBA:
  577. case JCS_EXT_BGRA:
  578. case JCS_CMYK:
  579. srcBPP = 4;
  580. break;
  581. case JCS_RGB565:
  582. srcBPP = 2;
  583. break;
  584. case JCS_GRAYSCALE:
  585. srcBPP = 1;
  586. break;
  587. default:
  588. SkASSERT(false);
  589. break;
  590. }
  591. fSwizzler = SkSwizzler::MakeSimple(srcBPP, swizzlerDstInfo, swizzlerOptions);
  592. }
  593. SkASSERT(fSwizzler);
  594. }
  595. SkSampler* SkJpegCodec::getSampler(bool createIfNecessary) {
  596. if (!createIfNecessary || fSwizzler) {
  597. SkASSERT(!fSwizzler || (fSwizzleSrcRow && fStorage.get() == fSwizzleSrcRow));
  598. return fSwizzler.get();
  599. }
  600. bool needsCMYKToRGB = needs_swizzler_to_convert_from_cmyk(
  601. fDecoderMgr->dinfo()->out_color_space, this->getEncodedInfo().profile(),
  602. this->colorXform());
  603. this->initializeSwizzler(this->dstInfo(), this->options(), needsCMYKToRGB);
  604. this->allocateStorage(this->dstInfo());
  605. return fSwizzler.get();
  606. }
  607. SkCodec::Result SkJpegCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
  608. const Options& options) {
  609. // Set the jump location for libjpeg errors
  610. skjpeg_error_mgr::AutoPushJmpBuf jmp(fDecoderMgr->errorMgr());
  611. if (setjmp(jmp)) {
  612. SkCodecPrintf("setjmp: Error from libjpeg\n");
  613. return kInvalidInput;
  614. }
  615. if (!jpeg_start_decompress(fDecoderMgr->dinfo())) {
  616. SkCodecPrintf("start decompress failed\n");
  617. return kInvalidInput;
  618. }
  619. bool needsCMYKToRGB = needs_swizzler_to_convert_from_cmyk(
  620. fDecoderMgr->dinfo()->out_color_space, this->getEncodedInfo().profile(),
  621. this->colorXform());
  622. if (options.fSubset) {
  623. uint32_t startX = options.fSubset->x();
  624. uint32_t width = options.fSubset->width();
  625. // libjpeg-turbo may need to align startX to a multiple of the IDCT
  626. // block size. If this is the case, it will decrease the value of
  627. // startX to the appropriate alignment and also increase the value
  628. // of width so that the right edge of the requested subset remains
  629. // the same.
  630. jpeg_crop_scanline(fDecoderMgr->dinfo(), &startX, &width);
  631. SkASSERT(startX <= (uint32_t) options.fSubset->x());
  632. SkASSERT(width >= (uint32_t) options.fSubset->width());
  633. SkASSERT(startX + width >= (uint32_t) options.fSubset->right());
  634. // Instruct the swizzler (if it is necessary) to further subset the
  635. // output provided by libjpeg-turbo.
  636. //
  637. // We set this here (rather than in the if statement below), so that
  638. // if (1) we don't need a swizzler for the subset, and (2) we need a
  639. // swizzler for CMYK, the swizzler will still use the proper subset
  640. // dimensions.
  641. //
  642. // Note that the swizzler will ignore the y and height parameters of
  643. // the subset. Since the scanline decoder (and the swizzler) handle
  644. // one row at a time, only the subsetting in the x-dimension matters.
  645. fSwizzlerSubset.setXYWH(options.fSubset->x() - startX, 0,
  646. options.fSubset->width(), options.fSubset->height());
  647. // We will need a swizzler if libjpeg-turbo cannot provide the exact
  648. // subset that we request.
  649. if (startX != (uint32_t) options.fSubset->x() ||
  650. width != (uint32_t) options.fSubset->width()) {
  651. this->initializeSwizzler(dstInfo, options, needsCMYKToRGB);
  652. }
  653. }
  654. // Make sure we have a swizzler if we are converting from CMYK.
  655. if (!fSwizzler && needsCMYKToRGB) {
  656. this->initializeSwizzler(dstInfo, options, true);
  657. }
  658. this->allocateStorage(dstInfo);
  659. return kSuccess;
  660. }
  661. int SkJpegCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
  662. int rows = this->readRows(this->dstInfo(), dst, dstRowBytes, count, this->options());
  663. if (rows < count) {
  664. // This allows us to skip calling jpeg_finish_decompress().
  665. fDecoderMgr->dinfo()->output_scanline = this->dstInfo().height();
  666. }
  667. return rows;
  668. }
  669. bool SkJpegCodec::onSkipScanlines(int count) {
  670. // Set the jump location for libjpeg errors
  671. skjpeg_error_mgr::AutoPushJmpBuf jmp(fDecoderMgr->errorMgr());
  672. if (setjmp(jmp)) {
  673. return fDecoderMgr->returnFalse("onSkipScanlines");
  674. }
  675. return (uint32_t) count == jpeg_skip_scanlines(fDecoderMgr->dinfo(), count);
  676. }
  677. static bool is_yuv_supported(jpeg_decompress_struct* dinfo) {
  678. // Scaling is not supported in raw data mode.
  679. SkASSERT(dinfo->scale_num == dinfo->scale_denom);
  680. // I can't imagine that this would ever change, but we do depend on it.
  681. static_assert(8 == DCTSIZE, "DCTSIZE (defined in jpeg library) should always be 8.");
  682. if (JCS_YCbCr != dinfo->jpeg_color_space) {
  683. return false;
  684. }
  685. SkASSERT(3 == dinfo->num_components);
  686. SkASSERT(dinfo->comp_info);
  687. // It is possible to perform a YUV decode for any combination of
  688. // horizontal and vertical sampling that is supported by
  689. // libjpeg/libjpeg-turbo. However, we will start by supporting only the
  690. // common cases (where U and V have samp_factors of one).
  691. //
  692. // The definition of samp_factor is kind of the opposite of what SkCodec
  693. // thinks of as a sampling factor. samp_factor is essentially a
  694. // multiplier, and the larger the samp_factor is, the more samples that
  695. // there will be. Ex:
  696. // U_plane_width = image_width * (U_h_samp_factor / max_h_samp_factor)
  697. //
  698. // Supporting cases where the samp_factors for U or V were larger than
  699. // that of Y would be an extremely difficult change, given that clients
  700. // allocate memory as if the size of the Y plane is always the size of the
  701. // image. However, this case is very, very rare.
  702. if ((1 != dinfo->comp_info[1].h_samp_factor) ||
  703. (1 != dinfo->comp_info[1].v_samp_factor) ||
  704. (1 != dinfo->comp_info[2].h_samp_factor) ||
  705. (1 != dinfo->comp_info[2].v_samp_factor))
  706. {
  707. return false;
  708. }
  709. // Support all common cases of Y samp_factors.
  710. // TODO (msarett): As mentioned above, it would be possible to support
  711. // more combinations of samp_factors. The issues are:
  712. // (1) Are there actually any images that are not covered
  713. // by these cases?
  714. // (2) How much complexity would be added to the
  715. // implementation in order to support these rare
  716. // cases?
  717. int hSampY = dinfo->comp_info[0].h_samp_factor;
  718. int vSampY = dinfo->comp_info[0].v_samp_factor;
  719. return (1 == hSampY && 1 == vSampY) ||
  720. (2 == hSampY && 1 == vSampY) ||
  721. (2 == hSampY && 2 == vSampY) ||
  722. (1 == hSampY && 2 == vSampY) ||
  723. (4 == hSampY && 1 == vSampY) ||
  724. (4 == hSampY && 2 == vSampY);
  725. }
  726. bool SkJpegCodec::onQueryYUV8(SkYUVASizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const {
  727. jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
  728. if (!is_yuv_supported(dinfo)) {
  729. return false;
  730. }
  731. jpeg_component_info * comp_info = dinfo->comp_info;
  732. for (int i = 0; i < 3; ++i) {
  733. sizeInfo->fSizes[i].set(comp_info[i].downsampled_width, comp_info[i].downsampled_height);
  734. sizeInfo->fWidthBytes[i] = comp_info[i].width_in_blocks * DCTSIZE;
  735. }
  736. // JPEG never has an alpha channel
  737. sizeInfo->fSizes[3].fHeight = sizeInfo->fSizes[3].fWidth = sizeInfo->fWidthBytes[3] = 0;
  738. sizeInfo->fOrigin = this->getOrigin();
  739. if (colorSpace) {
  740. *colorSpace = kJPEG_SkYUVColorSpace;
  741. }
  742. return true;
  743. }
  744. SkCodec::Result SkJpegCodec::onGetYUV8Planes(const SkYUVASizeInfo& sizeInfo,
  745. void* planes[SkYUVASizeInfo::kMaxCount]) {
  746. SkYUVASizeInfo defaultInfo;
  747. // This will check is_yuv_supported(), so we don't need to here.
  748. bool supportsYUV = this->onQueryYUV8(&defaultInfo, nullptr);
  749. if (!supportsYUV ||
  750. sizeInfo.fSizes[0] != defaultInfo.fSizes[0] ||
  751. sizeInfo.fSizes[1] != defaultInfo.fSizes[1] ||
  752. sizeInfo.fSizes[2] != defaultInfo.fSizes[2] ||
  753. sizeInfo.fWidthBytes[0] < defaultInfo.fWidthBytes[0] ||
  754. sizeInfo.fWidthBytes[1] < defaultInfo.fWidthBytes[1] ||
  755. sizeInfo.fWidthBytes[2] < defaultInfo.fWidthBytes[2]) {
  756. return fDecoderMgr->returnFailure("onGetYUV8Planes", kInvalidInput);
  757. }
  758. // Set the jump location for libjpeg errors
  759. skjpeg_error_mgr::AutoPushJmpBuf jmp(fDecoderMgr->errorMgr());
  760. if (setjmp(jmp)) {
  761. return fDecoderMgr->returnFailure("setjmp", kInvalidInput);
  762. }
  763. // Get a pointer to the decompress info since we will use it quite frequently
  764. jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
  765. dinfo->raw_data_out = TRUE;
  766. if (!jpeg_start_decompress(dinfo)) {
  767. return fDecoderMgr->returnFailure("startDecompress", kInvalidInput);
  768. }
  769. // A previous implementation claims that the return value of is_yuv_supported()
  770. // may change after calling jpeg_start_decompress(). It looks to me like this
  771. // was caused by a bug in the old code, but we'll be safe and check here.
  772. SkASSERT(is_yuv_supported(dinfo));
  773. // Currently, we require that the Y plane dimensions match the image dimensions
  774. // and that the U and V planes are the same dimensions.
  775. SkASSERT(sizeInfo.fSizes[1] == sizeInfo.fSizes[2]);
  776. SkASSERT((uint32_t) sizeInfo.fSizes[0].width() == dinfo->output_width &&
  777. (uint32_t) sizeInfo.fSizes[0].height() == dinfo->output_height);
  778. // Build a JSAMPIMAGE to handle output from libjpeg-turbo. A JSAMPIMAGE has
  779. // a 2-D array of pixels for each of the components (Y, U, V) in the image.
  780. // Cheat Sheet:
  781. // JSAMPIMAGE == JSAMPLEARRAY* == JSAMPROW** == JSAMPLE***
  782. JSAMPARRAY yuv[3];
  783. // Set aside enough space for pointers to rows of Y, U, and V.
  784. JSAMPROW rowptrs[2 * DCTSIZE + DCTSIZE + DCTSIZE];
  785. yuv[0] = &rowptrs[0]; // Y rows (DCTSIZE or 2 * DCTSIZE)
  786. yuv[1] = &rowptrs[2 * DCTSIZE]; // U rows (DCTSIZE)
  787. yuv[2] = &rowptrs[3 * DCTSIZE]; // V rows (DCTSIZE)
  788. // Initialize rowptrs.
  789. int numYRowsPerBlock = DCTSIZE * dinfo->comp_info[0].v_samp_factor;
  790. for (int i = 0; i < numYRowsPerBlock; i++) {
  791. rowptrs[i] = SkTAddOffset<JSAMPLE>(planes[0], i * sizeInfo.fWidthBytes[0]);
  792. }
  793. for (int i = 0; i < DCTSIZE; i++) {
  794. rowptrs[i + 2 * DCTSIZE] =
  795. SkTAddOffset<JSAMPLE>(planes[1], i * sizeInfo.fWidthBytes[1]);
  796. rowptrs[i + 3 * DCTSIZE] =
  797. SkTAddOffset<JSAMPLE>(planes[2], i * sizeInfo.fWidthBytes[2]);
  798. }
  799. // After each loop iteration, we will increment pointers to Y, U, and V.
  800. size_t blockIncrementY = numYRowsPerBlock * sizeInfo.fWidthBytes[0];
  801. size_t blockIncrementU = DCTSIZE * sizeInfo.fWidthBytes[1];
  802. size_t blockIncrementV = DCTSIZE * sizeInfo.fWidthBytes[2];
  803. uint32_t numRowsPerBlock = numYRowsPerBlock;
  804. // We intentionally round down here, as this first loop will only handle
  805. // full block rows. As a special case at the end, we will handle any
  806. // remaining rows that do not make up a full block.
  807. const int numIters = dinfo->output_height / numRowsPerBlock;
  808. for (int i = 0; i < numIters; i++) {
  809. JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock);
  810. if (linesRead < numRowsPerBlock) {
  811. // FIXME: Handle incomplete YUV decodes without signalling an error.
  812. return kInvalidInput;
  813. }
  814. // Update rowptrs.
  815. for (int i = 0; i < numYRowsPerBlock; i++) {
  816. rowptrs[i] += blockIncrementY;
  817. }
  818. for (int i = 0; i < DCTSIZE; i++) {
  819. rowptrs[i + 2 * DCTSIZE] += blockIncrementU;
  820. rowptrs[i + 3 * DCTSIZE] += blockIncrementV;
  821. }
  822. }
  823. uint32_t remainingRows = dinfo->output_height - dinfo->output_scanline;
  824. SkASSERT(remainingRows == dinfo->output_height % numRowsPerBlock);
  825. SkASSERT(dinfo->output_scanline == numIters * numRowsPerBlock);
  826. if (remainingRows > 0) {
  827. // libjpeg-turbo needs memory to be padded by the block sizes. We will fulfill
  828. // this requirement using a dummy row buffer.
  829. // FIXME: Should SkCodec have an extra memory buffer that can be shared among
  830. // all of the implementations that use temporary/garbage memory?
  831. SkAutoTMalloc<JSAMPLE> dummyRow(sizeInfo.fWidthBytes[0]);
  832. for (int i = remainingRows; i < numYRowsPerBlock; i++) {
  833. rowptrs[i] = dummyRow.get();
  834. }
  835. int remainingUVRows = dinfo->comp_info[1].downsampled_height - DCTSIZE * numIters;
  836. for (int i = remainingUVRows; i < DCTSIZE; i++) {
  837. rowptrs[i + 2 * DCTSIZE] = dummyRow.get();
  838. rowptrs[i + 3 * DCTSIZE] = dummyRow.get();
  839. }
  840. JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock);
  841. if (linesRead < remainingRows) {
  842. // FIXME: Handle incomplete YUV decodes without signalling an error.
  843. return kInvalidInput;
  844. }
  845. }
  846. return kSuccess;
  847. }
  848. // This function is declared in SkJpegInfo.h, used by SkPDF.
  849. bool SkGetJpegInfo(const void* data, size_t len,
  850. SkISize* size,
  851. SkEncodedInfo::Color* colorType,
  852. SkEncodedOrigin* orientation) {
  853. if (!SkJpegCodec::IsJpeg(data, len)) {
  854. return false;
  855. }
  856. SkMemoryStream stream(data, len);
  857. JpegDecoderMgr decoderMgr(&stream);
  858. // libjpeg errors will be caught and reported here
  859. skjpeg_error_mgr::AutoPushJmpBuf jmp(decoderMgr.errorMgr());
  860. if (setjmp(jmp)) {
  861. return false;
  862. }
  863. decoderMgr.init();
  864. jpeg_decompress_struct* dinfo = decoderMgr.dinfo();
  865. jpeg_save_markers(dinfo, kExifMarker, 0xFFFF);
  866. jpeg_save_markers(dinfo, kICCMarker, 0xFFFF);
  867. if (JPEG_HEADER_OK != jpeg_read_header(dinfo, true)) {
  868. return false;
  869. }
  870. SkEncodedInfo::Color encodedColorType;
  871. if (!decoderMgr.getEncodedColor(&encodedColorType)) {
  872. return false; // Unable to interpret the color channels as colors.
  873. }
  874. if (colorType) {
  875. *colorType = encodedColorType;
  876. }
  877. if (orientation) {
  878. *orientation = get_exif_orientation(dinfo);
  879. }
  880. if (size) {
  881. *size = {SkToS32(dinfo->image_width), SkToS32(dinfo->image_height)};
  882. }
  883. return true;
  884. }