nsJPEGEncoder.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. * This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsJPEGEncoder.h"
  6. #include "prprf.h"
  7. #include "nsString.h"
  8. #include "nsStreamUtils.h"
  9. #include "gfxColor.h"
  10. #include "mozilla/CheckedInt.h"
  11. #include <setjmp.h>
  12. #include "jerror.h"
  13. using namespace mozilla;
  14. NS_IMPL_ISUPPORTS(nsJPEGEncoder, imgIEncoder, nsIInputStream,
  15. nsIAsyncInputStream)
  16. // used to pass error info through the JPEG library
  17. struct encoder_error_mgr {
  18. jpeg_error_mgr pub;
  19. jmp_buf setjmp_buffer;
  20. };
  21. nsJPEGEncoder::nsJPEGEncoder()
  22. : mFinished(false),
  23. mImageBuffer(nullptr),
  24. mImageBufferSize(0),
  25. mImageBufferUsed(0),
  26. mImageBufferReadPoint(0),
  27. mCallback(nullptr),
  28. mCallbackTarget(nullptr),
  29. mNotifyThreshold(0),
  30. mReentrantMonitor("nsJPEGEncoder.mReentrantMonitor")
  31. {
  32. }
  33. nsJPEGEncoder::~nsJPEGEncoder()
  34. {
  35. if (mImageBuffer) {
  36. free(mImageBuffer);
  37. mImageBuffer = nullptr;
  38. }
  39. }
  40. // nsJPEGEncoder::InitFromData
  41. //
  42. // One output option is supported: "quality=X" where X is an integer in the
  43. // range 0-100. Higher values for X give better quality.
  44. //
  45. // Transparency is always discarded.
  46. NS_IMETHODIMP
  47. nsJPEGEncoder::InitFromData(const uint8_t* aData,
  48. uint32_t aLength, // (unused, req'd by JS)
  49. uint32_t aWidth,
  50. uint32_t aHeight,
  51. uint32_t aStride,
  52. uint32_t aInputFormat,
  53. const nsAString& aOutputOptions)
  54. {
  55. NS_ENSURE_ARG(aData);
  56. // validate input format
  57. if (aInputFormat != INPUT_FORMAT_RGB &&
  58. aInputFormat != INPUT_FORMAT_RGBA &&
  59. aInputFormat != INPUT_FORMAT_HOSTARGB)
  60. return NS_ERROR_INVALID_ARG;
  61. // Stride is the padded width of each row, so it better be longer (I'm afraid
  62. // people will not understand what stride means, so check it well)
  63. if ((aInputFormat == INPUT_FORMAT_RGB &&
  64. aStride < aWidth * 3) ||
  65. ((aInputFormat == INPUT_FORMAT_RGBA ||
  66. aInputFormat == INPUT_FORMAT_HOSTARGB) &&
  67. aStride < aWidth * 4)) {
  68. NS_WARNING("Invalid stride for InitFromData");
  69. return NS_ERROR_INVALID_ARG;
  70. }
  71. // can't initialize more than once
  72. if (mImageBuffer != nullptr) {
  73. return NS_ERROR_ALREADY_INITIALIZED;
  74. }
  75. // options: we only have one option so this is easy
  76. int quality = 92;
  77. if (aOutputOptions.Length() > 0) {
  78. // have options string
  79. const nsString qualityPrefix(NS_LITERAL_STRING("quality="));
  80. if (aOutputOptions.Length() > qualityPrefix.Length() &&
  81. StringBeginsWith(aOutputOptions, qualityPrefix)) {
  82. // have quality string
  83. nsCString value =
  84. NS_ConvertUTF16toUTF8(Substring(aOutputOptions,
  85. qualityPrefix.Length()));
  86. int newquality = -1;
  87. if (PR_sscanf(value.get(), "%d", &newquality) == 1) {
  88. if (newquality >= 0 && newquality <= 100) {
  89. quality = newquality;
  90. } else {
  91. NS_WARNING("Quality value out of range, should be 0-100,"
  92. " using default");
  93. }
  94. } else {
  95. NS_WARNING("Quality value invalid, should be integer 0-100,"
  96. " using default");
  97. }
  98. }
  99. else {
  100. return NS_ERROR_INVALID_ARG;
  101. }
  102. }
  103. jpeg_compress_struct cinfo;
  104. // We set up the normal JPEG error routines, then override error_exit.
  105. // This must be done before the call to create_compress
  106. encoder_error_mgr errmgr;
  107. cinfo.err = jpeg_std_error(&errmgr.pub);
  108. errmgr.pub.error_exit = errorExit;
  109. // Establish the setjmp return context for my_error_exit to use.
  110. if (setjmp(errmgr.setjmp_buffer)) {
  111. // If we get here, the JPEG code has signaled an error.
  112. // We need to clean up the JPEG object, close the input file, and return.
  113. return NS_ERROR_FAILURE;
  114. }
  115. jpeg_create_compress(&cinfo);
  116. cinfo.image_width = aWidth;
  117. cinfo.image_height = aHeight;
  118. cinfo.input_components = 3;
  119. cinfo.in_color_space = JCS_RGB;
  120. cinfo.data_precision = 8;
  121. jpeg_set_defaults(&cinfo);
  122. jpeg_set_quality(&cinfo, quality, 1); // quality here is 0-100
  123. if (quality >= 90) {
  124. int i;
  125. for (i=0; i < MAX_COMPONENTS; i++) {
  126. cinfo.comp_info[i].h_samp_factor=1;
  127. cinfo.comp_info[i].v_samp_factor=1;
  128. }
  129. }
  130. // set up the destination manager
  131. jpeg_destination_mgr destmgr;
  132. destmgr.init_destination = initDestination;
  133. destmgr.empty_output_buffer = emptyOutputBuffer;
  134. destmgr.term_destination = termDestination;
  135. cinfo.dest = &destmgr;
  136. cinfo.client_data = this;
  137. jpeg_start_compress(&cinfo, 1);
  138. // feed it the rows
  139. if (aInputFormat == INPUT_FORMAT_RGB) {
  140. while (cinfo.next_scanline < cinfo.image_height) {
  141. const uint8_t* row = &aData[cinfo.next_scanline * aStride];
  142. jpeg_write_scanlines(&cinfo, const_cast<uint8_t**>(&row), 1);
  143. }
  144. } else if (aInputFormat == INPUT_FORMAT_RGBA) {
  145. UniquePtr<uint8_t[]> rowptr = MakeUnique<uint8_t[]>(aWidth * 3);
  146. uint8_t* row = rowptr.get();
  147. while (cinfo.next_scanline < cinfo.image_height) {
  148. ConvertRGBARow(&aData[cinfo.next_scanline * aStride], row, aWidth);
  149. jpeg_write_scanlines(&cinfo, &row, 1);
  150. }
  151. } else if (aInputFormat == INPUT_FORMAT_HOSTARGB) {
  152. UniquePtr<uint8_t[]> rowptr = MakeUnique<uint8_t[]>(aWidth * 3);
  153. uint8_t* row = rowptr.get();
  154. while (cinfo.next_scanline < cinfo.image_height) {
  155. ConvertHostARGBRow(&aData[cinfo.next_scanline * aStride], row, aWidth);
  156. jpeg_write_scanlines(&cinfo, &row, 1);
  157. }
  158. }
  159. jpeg_finish_compress(&cinfo);
  160. jpeg_destroy_compress(&cinfo);
  161. mFinished = true;
  162. NotifyListener();
  163. // if output callback can't get enough memory, it will free our buffer
  164. if (!mImageBuffer) {
  165. return NS_ERROR_OUT_OF_MEMORY;
  166. }
  167. return NS_OK;
  168. }
  169. NS_IMETHODIMP
  170. nsJPEGEncoder::StartImageEncode(uint32_t aWidth,
  171. uint32_t aHeight,
  172. uint32_t aInputFormat,
  173. const nsAString& aOutputOptions)
  174. {
  175. return NS_ERROR_NOT_IMPLEMENTED;
  176. }
  177. // Returns the number of bytes in the image buffer used.
  178. NS_IMETHODIMP
  179. nsJPEGEncoder::GetImageBufferUsed(uint32_t* aOutputSize)
  180. {
  181. NS_ENSURE_ARG_POINTER(aOutputSize);
  182. *aOutputSize = mImageBufferUsed;
  183. return NS_OK;
  184. }
  185. // Returns a pointer to the start of the image buffer
  186. NS_IMETHODIMP
  187. nsJPEGEncoder::GetImageBuffer(char** aOutputBuffer)
  188. {
  189. NS_ENSURE_ARG_POINTER(aOutputBuffer);
  190. *aOutputBuffer = reinterpret_cast<char*>(mImageBuffer);
  191. return NS_OK;
  192. }
  193. NS_IMETHODIMP
  194. nsJPEGEncoder::AddImageFrame(const uint8_t* aData,
  195. uint32_t aLength,
  196. uint32_t aWidth,
  197. uint32_t aHeight,
  198. uint32_t aStride,
  199. uint32_t aFrameFormat,
  200. const nsAString& aFrameOptions)
  201. {
  202. return NS_ERROR_NOT_IMPLEMENTED;
  203. }
  204. NS_IMETHODIMP
  205. nsJPEGEncoder::EndImageEncode()
  206. {
  207. return NS_ERROR_NOT_IMPLEMENTED;
  208. }
  209. NS_IMETHODIMP
  210. nsJPEGEncoder::Close()
  211. {
  212. if (mImageBuffer != nullptr) {
  213. free(mImageBuffer);
  214. mImageBuffer = nullptr;
  215. mImageBufferSize = 0;
  216. mImageBufferUsed = 0;
  217. mImageBufferReadPoint = 0;
  218. }
  219. return NS_OK;
  220. }
  221. NS_IMETHODIMP
  222. nsJPEGEncoder::Available(uint64_t* _retval)
  223. {
  224. if (!mImageBuffer) {
  225. return NS_BASE_STREAM_CLOSED;
  226. }
  227. *_retval = mImageBufferUsed - mImageBufferReadPoint;
  228. return NS_OK;
  229. }
  230. NS_IMETHODIMP
  231. nsJPEGEncoder::Read(char* aBuf, uint32_t aCount, uint32_t* _retval)
  232. {
  233. return ReadSegments(NS_CopySegmentToBuffer, aBuf, aCount, _retval);
  234. }
  235. NS_IMETHODIMP
  236. nsJPEGEncoder::ReadSegments(nsWriteSegmentFun aWriter,
  237. void* aClosure, uint32_t aCount, uint32_t* _retval)
  238. {
  239. // Avoid another thread reallocing the buffer underneath us
  240. ReentrantMonitorAutoEnter autoEnter(mReentrantMonitor);
  241. uint32_t maxCount = mImageBufferUsed - mImageBufferReadPoint;
  242. if (maxCount == 0) {
  243. *_retval = 0;
  244. return mFinished ? NS_OK : NS_BASE_STREAM_WOULD_BLOCK;
  245. }
  246. if (aCount > maxCount) {
  247. aCount = maxCount;
  248. }
  249. nsresult rv = aWriter(this, aClosure,
  250. reinterpret_cast<const char*>
  251. (mImageBuffer+mImageBufferReadPoint),
  252. 0, aCount, _retval);
  253. if (NS_SUCCEEDED(rv)) {
  254. NS_ASSERTION(*_retval <= aCount, "bad write count");
  255. mImageBufferReadPoint += *_retval;
  256. }
  257. // errors returned from the writer end here!
  258. return NS_OK;
  259. }
  260. NS_IMETHODIMP
  261. nsJPEGEncoder::IsNonBlocking(bool* _retval)
  262. {
  263. *_retval = true;
  264. return NS_OK;
  265. }
  266. NS_IMETHODIMP
  267. nsJPEGEncoder::AsyncWait(nsIInputStreamCallback* aCallback,
  268. uint32_t aFlags, uint32_t aRequestedCount,
  269. nsIEventTarget* aTarget)
  270. {
  271. if (aFlags != 0) {
  272. return NS_ERROR_NOT_IMPLEMENTED;
  273. }
  274. if (mCallback || mCallbackTarget) {
  275. return NS_ERROR_UNEXPECTED;
  276. }
  277. mCallbackTarget = aTarget;
  278. // 0 means "any number of bytes except 0"
  279. mNotifyThreshold = aRequestedCount;
  280. if (!aRequestedCount) {
  281. mNotifyThreshold = 1024; // 1 KB seems good. We don't want to
  282. // notify incessantly
  283. }
  284. // We set the callback absolutely last, because NotifyListener uses it to
  285. // determine if someone needs to be notified. If we don't set it last,
  286. // NotifyListener might try to fire off a notification to a null target
  287. // which will generally cause non-threadsafe objects to be used off the
  288. // main thread
  289. mCallback = aCallback;
  290. // What we are being asked for may be present already
  291. NotifyListener();
  292. return NS_OK;
  293. }
  294. NS_IMETHODIMP
  295. nsJPEGEncoder::CloseWithStatus(nsresult aStatus)
  296. {
  297. return Close();
  298. }
  299. // nsJPEGEncoder::ConvertHostARGBRow
  300. //
  301. // Our colors are stored with premultiplied alphas, but we need
  302. // an output with no alpha in machine-independent byte order.
  303. //
  304. // See gfx/cairo/cairo/src/cairo-png.c
  305. void
  306. nsJPEGEncoder::ConvertHostARGBRow(const uint8_t* aSrc, uint8_t* aDest,
  307. uint32_t aPixelWidth)
  308. {
  309. for (uint32_t x = 0; x < aPixelWidth; x++) {
  310. const uint32_t& pixelIn = ((const uint32_t*)(aSrc))[x];
  311. uint8_t* pixelOut = &aDest[x * 3];
  312. pixelOut[0] = (pixelIn & 0xff0000) >> 16;
  313. pixelOut[1] = (pixelIn & 0x00ff00) >> 8;
  314. pixelOut[2] = (pixelIn & 0x0000ff) >> 0;
  315. }
  316. }
  317. /**
  318. * nsJPEGEncoder::ConvertRGBARow
  319. *
  320. * Input is RGBA, output is RGB, so we should alpha-premultiply.
  321. */
  322. void
  323. nsJPEGEncoder::ConvertRGBARow(const uint8_t* aSrc, uint8_t* aDest,
  324. uint32_t aPixelWidth)
  325. {
  326. for (uint32_t x = 0; x < aPixelWidth; x++) {
  327. const uint8_t* pixelIn = &aSrc[x * 4];
  328. uint8_t* pixelOut = &aDest[x * 3];
  329. uint8_t alpha = pixelIn[3];
  330. pixelOut[0] = gfxPreMultiply(pixelIn[0], alpha);
  331. pixelOut[1] = gfxPreMultiply(pixelIn[1], alpha);
  332. pixelOut[2] = gfxPreMultiply(pixelIn[2], alpha);
  333. }
  334. }
  335. // nsJPEGEncoder::initDestination
  336. //
  337. // Initialize destination. This is called by jpeg_start_compress() before
  338. // any data is actually written. It must initialize next_output_byte and
  339. // free_in_buffer. free_in_buffer must be initialized to a positive value.
  340. void // static
  341. nsJPEGEncoder::initDestination(jpeg_compress_struct* cinfo)
  342. {
  343. nsJPEGEncoder* that = static_cast<nsJPEGEncoder*>(cinfo->client_data);
  344. NS_ASSERTION(!that->mImageBuffer, "Image buffer already initialized");
  345. that->mImageBufferSize = 8192;
  346. that->mImageBuffer = (uint8_t*)malloc(that->mImageBufferSize);
  347. that->mImageBufferUsed = 0;
  348. cinfo->dest->next_output_byte = that->mImageBuffer;
  349. cinfo->dest->free_in_buffer = that->mImageBufferSize;
  350. }
  351. // nsJPEGEncoder::emptyOutputBuffer
  352. //
  353. // This is called whenever the buffer has filled (free_in_buffer reaches
  354. // zero). In typical applications, it should write out the *entire* buffer
  355. // (use the saved start address and buffer length; ignore the current state
  356. // of next_output_byte and free_in_buffer). Then reset the pointer & count
  357. // to the start of the buffer, and return TRUE indicating that the buffer
  358. // has been dumped. free_in_buffer must be set to a positive value when
  359. // TRUE is returned. A FALSE return should only be used when I/O suspension
  360. // is desired (this operating mode is discussed in the next section).
  361. boolean // static
  362. nsJPEGEncoder::emptyOutputBuffer(jpeg_compress_struct* cinfo)
  363. {
  364. nsJPEGEncoder* that = static_cast<nsJPEGEncoder*>(cinfo->client_data);
  365. NS_ASSERTION(that->mImageBuffer, "No buffer to empty!");
  366. // When we're reallocing the buffer we need to take the lock to ensure
  367. // that nobody is trying to read from the buffer we are destroying
  368. ReentrantMonitorAutoEnter autoEnter(that->mReentrantMonitor);
  369. that->mImageBufferUsed = that->mImageBufferSize;
  370. // expand buffer, just double size each time
  371. uint8_t* newBuf = nullptr;
  372. CheckedInt<uint32_t> bufSize =
  373. CheckedInt<uint32_t>(that->mImageBufferSize) * 2;
  374. if (bufSize.isValid()) {
  375. that->mImageBufferSize = bufSize.value();
  376. newBuf = (uint8_t*)realloc(that->mImageBuffer, that->mImageBufferSize);
  377. }
  378. if (!newBuf) {
  379. // can't resize, just zero (this will keep us from writing more)
  380. free(that->mImageBuffer);
  381. that->mImageBuffer = nullptr;
  382. that->mImageBufferSize = 0;
  383. that->mImageBufferUsed = 0;
  384. // This seems to be the only way to do errors through the JPEG library. We
  385. // pass an nsresult masquerading as an int, which works because the
  386. // setjmp() caller casts it back.
  387. longjmp(((encoder_error_mgr*)(cinfo->err))->setjmp_buffer,
  388. static_cast<int>(NS_ERROR_OUT_OF_MEMORY));
  389. }
  390. that->mImageBuffer = newBuf;
  391. cinfo->dest->next_output_byte = &that->mImageBuffer[that->mImageBufferUsed];
  392. cinfo->dest->free_in_buffer = that->mImageBufferSize - that->mImageBufferUsed;
  393. return 1;
  394. }
  395. // nsJPEGEncoder::termDestination
  396. //
  397. // Terminate destination --- called by jpeg_finish_compress() after all data
  398. // has been written. In most applications, this must flush any data
  399. // remaining in the buffer. Use either next_output_byte or free_in_buffer
  400. // to determine how much data is in the buffer.
  401. void // static
  402. nsJPEGEncoder::termDestination(jpeg_compress_struct* cinfo)
  403. {
  404. nsJPEGEncoder* that = static_cast<nsJPEGEncoder*>(cinfo->client_data);
  405. if (!that->mImageBuffer) {
  406. return;
  407. }
  408. that->mImageBufferUsed = cinfo->dest->next_output_byte - that->mImageBuffer;
  409. NS_ASSERTION(that->mImageBufferUsed < that->mImageBufferSize,
  410. "JPEG library busted, got a bad image buffer size");
  411. that->NotifyListener();
  412. }
  413. // nsJPEGEncoder::errorExit
  414. //
  415. // Override the standard error method in the IJG JPEG decoder code. This
  416. // was mostly copied from nsJPEGDecoder.cpp
  417. void // static
  418. nsJPEGEncoder::errorExit(jpeg_common_struct* cinfo)
  419. {
  420. nsresult error_code;
  421. encoder_error_mgr* err = (encoder_error_mgr*) cinfo->err;
  422. // Convert error to a browser error code
  423. switch (cinfo->err->msg_code) {
  424. case JERR_OUT_OF_MEMORY:
  425. error_code = NS_ERROR_OUT_OF_MEMORY;
  426. break;
  427. default:
  428. error_code = NS_ERROR_FAILURE;
  429. }
  430. // Return control to the setjmp point. We pass an nsresult masquerading as
  431. // an int, which works because the setjmp() caller casts it back.
  432. longjmp(err->setjmp_buffer, static_cast<int>(error_code));
  433. }
  434. void
  435. nsJPEGEncoder::NotifyListener()
  436. {
  437. // We might call this function on multiple threads (any threads that call
  438. // AsyncWait and any that do encoding) so we lock to avoid notifying the
  439. // listener twice about the same data (which generally leads to a truncated
  440. // image).
  441. ReentrantMonitorAutoEnter autoEnter(mReentrantMonitor);
  442. if (mCallback &&
  443. (mImageBufferUsed - mImageBufferReadPoint >= mNotifyThreshold ||
  444. mFinished)) {
  445. nsCOMPtr<nsIInputStreamCallback> callback;
  446. if (mCallbackTarget) {
  447. callback = NS_NewInputStreamReadyEvent(mCallback, mCallbackTarget);
  448. } else {
  449. callback = mCallback;
  450. }
  451. NS_ASSERTION(callback, "Shouldn't fail to make the callback");
  452. // Null the callback first because OnInputStreamReady could reenter
  453. // AsyncWait
  454. mCallback = nullptr;
  455. mCallbackTarget = nullptr;
  456. mNotifyThreshold = 0;
  457. callback->OnInputStreamReady(this);
  458. }
  459. }