Home | History | Annotate | Download | only in codec
      1 // Copyright 2014 PDFium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
      6 
      7 #include <algorithm>
      8 #include <limits>
      9 #include <memory>
     10 #include <utility>
     11 #include <vector>
     12 
     13 #include "core/fpdfapi/page/cpdf_colorspace.h"
     14 #include "core/fxcodec/codec/ccodec_jpxmodule.h"
     15 #include "core/fxcodec/codec/cjpx_decoder.h"
     16 #include "core/fxcrt/fx_memory.h"
     17 #include "core/fxcrt/fx_safe_types.h"
     18 #include "third_party/base/ptr_util.h"
     19 #include "third_party/libopenjpeg20/openjpeg.h"
     20 #include "third_party/libopenjpeg20/opj_malloc.h"
     21 
     22 namespace {
     23 
     24 void fx_ignore_callback(const char* msg, void* client_data) {}
     25 
     26 opj_stream_t* fx_opj_stream_create_memory_stream(DecodeData* data,
     27                                                  OPJ_SIZE_T p_size,
     28                                                  OPJ_BOOL p_is_read_stream) {
     29   if (!data || !data->src_data || data->src_size <= 0)
     30     return nullptr;
     31 
     32   opj_stream_t* stream = opj_stream_create(p_size, p_is_read_stream);
     33   if (!stream)
     34     return nullptr;
     35 
     36   opj_stream_set_user_data(stream, data, nullptr);
     37   opj_stream_set_user_data_length(stream, data->src_size);
     38   opj_stream_set_read_function(stream, opj_read_from_memory);
     39   opj_stream_set_skip_function(stream, opj_skip_from_memory);
     40   opj_stream_set_seek_function(stream, opj_seek_from_memory);
     41   return stream;
     42 }
     43 
     44 void sycc_to_rgb(int offset,
     45                  int upb,
     46                  int y,
     47                  int cb,
     48                  int cr,
     49                  int* out_r,
     50                  int* out_g,
     51                  int* out_b) {
     52   cb -= offset;
     53   cr -= offset;
     54   *out_r = pdfium::clamp(y + static_cast<int>(1.402 * cr), 0, upb);
     55   *out_g = pdfium::clamp(y - static_cast<int>(0.344 * cb + 0.714 * cr), 0, upb);
     56   *out_b = pdfium::clamp(y + static_cast<int>(1.772 * cb), 0, upb);
     57 }
     58 
     59 void sycc444_to_rgb(opj_image_t* img) {
     60   int prec = img->comps[0].prec;
     61   // If we shift 31 we're going to go negative, then things go bad.
     62   if (prec > 30)
     63     return;
     64   int offset = 1 << (prec - 1);
     65   int upb = (1 << prec) - 1;
     66   OPJ_UINT32 maxw =
     67       std::min({img->comps[0].w, img->comps[1].w, img->comps[2].w});
     68   OPJ_UINT32 maxh =
     69       std::min({img->comps[0].h, img->comps[1].h, img->comps[2].h});
     70   FX_SAFE_SIZE_T max_size = maxw;
     71   max_size *= maxh;
     72   max_size *= sizeof(int);
     73   if (!max_size.IsValid())
     74     return;
     75 
     76   const int* y = img->comps[0].data;
     77   const int* cb = img->comps[1].data;
     78   const int* cr = img->comps[2].data;
     79   if (!y || !cb || !cr)
     80     return;
     81 
     82   int* r = static_cast<int*>(opj_image_data_alloc(max_size.ValueOrDie()));
     83   int* g = static_cast<int*>(opj_image_data_alloc(max_size.ValueOrDie()));
     84   int* b = static_cast<int*>(opj_image_data_alloc(max_size.ValueOrDie()));
     85   int* d0 = r;
     86   int* d1 = g;
     87   int* d2 = b;
     88   max_size /= sizeof(int);
     89   for (size_t i = 0; i < max_size.ValueOrDie(); ++i) {
     90     sycc_to_rgb(offset, upb, *y++, *cb++, *cr++, r++, g++, b++);
     91   }
     92   opj_image_data_free(img->comps[0].data);
     93   opj_image_data_free(img->comps[1].data);
     94   opj_image_data_free(img->comps[2].data);
     95   img->comps[0].data = d0;
     96   img->comps[1].data = d1;
     97   img->comps[2].data = d2;
     98 }
     99 
    100 bool sycc420_422_size_is_valid(opj_image_t* img) {
    101   return img && img->comps[0].w != std::numeric_limits<OPJ_UINT32>::max() &&
    102          (img->comps[0].w + 1) / 2 == img->comps[1].w &&
    103          img->comps[1].w == img->comps[2].w &&
    104          img->comps[1].h == img->comps[2].h;
    105 }
    106 
    107 bool sycc420_size_is_valid(opj_image_t* img) {
    108   return sycc420_422_size_is_valid(img) &&
    109          img->comps[0].h != std::numeric_limits<OPJ_UINT32>::max() &&
    110          (img->comps[0].h + 1) / 2 == img->comps[1].h;
    111 }
    112 
    113 bool sycc422_size_is_valid(opj_image_t* img) {
    114   return sycc420_422_size_is_valid(img) && img->comps[0].h == img->comps[1].h;
    115 }
    116 
    117 void sycc422_to_rgb(opj_image_t* img) {
    118   if (!sycc422_size_is_valid(img))
    119     return;
    120 
    121   int prec = img->comps[0].prec;
    122   if (prec <= 0 || prec >= 32)
    123     return;
    124 
    125   int offset = 1 << (prec - 1);
    126   int upb = (1 << prec) - 1;
    127   OPJ_UINT32 maxw = img->comps[0].w;
    128   OPJ_UINT32 maxh = img->comps[0].h;
    129   FX_SAFE_SIZE_T max_size = maxw;
    130   max_size *= maxh;
    131   max_size *= sizeof(int);
    132   if (!max_size.IsValid())
    133     return;
    134 
    135   const int* y = img->comps[0].data;
    136   const int* cb = img->comps[1].data;
    137   const int* cr = img->comps[2].data;
    138   if (!y || !cb || !cr)
    139     return;
    140 
    141   int* r = static_cast<int*>(opj_image_data_alloc(max_size.ValueOrDie()));
    142   int* g = static_cast<int*>(opj_image_data_alloc(max_size.ValueOrDie()));
    143   int* b = static_cast<int*>(opj_image_data_alloc(max_size.ValueOrDie()));
    144   int* d0 = r;
    145   int* d1 = g;
    146   int* d2 = b;
    147   for (uint32_t i = 0; i < maxh; ++i) {
    148     OPJ_UINT32 j;
    149     for (j = 0; j < (maxw & ~static_cast<OPJ_UINT32>(1)); j += 2) {
    150       sycc_to_rgb(offset, upb, *y++, *cb, *cr, r++, g++, b++);
    151       sycc_to_rgb(offset, upb, *y++, *cb++, *cr++, r++, g++, b++);
    152     }
    153     if (j < maxw) {
    154       sycc_to_rgb(offset, upb, *y++, *cb++, *cr++, r++, g++, b++);
    155     }
    156   }
    157   opj_image_data_free(img->comps[0].data);
    158   opj_image_data_free(img->comps[1].data);
    159   opj_image_data_free(img->comps[2].data);
    160   img->comps[0].data = d0;
    161   img->comps[1].data = d1;
    162   img->comps[2].data = d2;
    163   img->comps[1].w = maxw;
    164   img->comps[1].h = maxh;
    165   img->comps[2].w = maxw;
    166   img->comps[2].h = maxh;
    167   img->comps[1].dx = img->comps[0].dx;
    168   img->comps[2].dx = img->comps[0].dx;
    169   img->comps[1].dy = img->comps[0].dy;
    170   img->comps[2].dy = img->comps[0].dy;
    171 }
    172 
    173 bool sycc420_must_extend_cbcr(OPJ_UINT32 y, OPJ_UINT32 cbcr) {
    174   return (y & 1) && (cbcr == y / 2);
    175 }
    176 
    177 bool is_sycc420(const opj_image_t* img) {
    178   return img->comps[0].dx == 1 && img->comps[0].dy == 1 &&
    179          img->comps[1].dx == 2 && img->comps[1].dy == 2 &&
    180          img->comps[2].dx == 2 && img->comps[2].dy == 2;
    181 }
    182 
    183 bool is_sycc422(const opj_image_t* img) {
    184   return img->comps[0].dx == 1 && img->comps[0].dy == 1 &&
    185          img->comps[1].dx == 2 && img->comps[1].dy == 1 &&
    186          img->comps[2].dx == 2 && img->comps[2].dy == 1;
    187 }
    188 
    189 bool is_sycc444(const opj_image_t* img) {
    190   return img->comps[0].dx == 1 && img->comps[0].dy == 1 &&
    191          img->comps[1].dx == 1 && img->comps[1].dy == 1 &&
    192          img->comps[2].dx == 1 && img->comps[2].dy == 1;
    193 }
    194 
    195 void color_sycc_to_rgb(opj_image_t* img) {
    196   if (img->numcomps < 3) {
    197     img->color_space = OPJ_CLRSPC_GRAY;
    198     return;
    199   }
    200   if (is_sycc420(img))
    201     sycc420_to_rgb(img);
    202   else if (is_sycc422(img))
    203     sycc422_to_rgb(img);
    204   else if (is_sycc444(img))
    205     sycc444_to_rgb(img);
    206   else
    207     return;
    208 
    209   img->color_space = OPJ_CLRSPC_SRGB;
    210 }
    211 
    212 }  // namespace
    213 
    214 OPJ_SIZE_T opj_read_from_memory(void* p_buffer,
    215                                 OPJ_SIZE_T nb_bytes,
    216                                 void* p_user_data) {
    217   DecodeData* srcData = static_cast<DecodeData*>(p_user_data);
    218   if (!srcData || !srcData->src_data || srcData->src_size == 0)
    219     return static_cast<OPJ_SIZE_T>(-1);
    220 
    221   // Reads at EOF return an error code.
    222   if (srcData->offset >= srcData->src_size)
    223     return static_cast<OPJ_SIZE_T>(-1);
    224 
    225   OPJ_SIZE_T bufferLength = srcData->src_size - srcData->offset;
    226   OPJ_SIZE_T readlength = nb_bytes < bufferLength ? nb_bytes : bufferLength;
    227   memcpy(p_buffer, &srcData->src_data[srcData->offset], readlength);
    228   srcData->offset += readlength;
    229   return readlength;
    230 }
    231 
    232 OPJ_OFF_T opj_skip_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data) {
    233   DecodeData* srcData = static_cast<DecodeData*>(p_user_data);
    234   if (!srcData || !srcData->src_data || srcData->src_size == 0)
    235     return static_cast<OPJ_OFF_T>(-1);
    236 
    237   // Offsets are signed and may indicate a negative skip. Do not support this
    238   // because of the strange return convention where either bytes skipped or
    239   // -1 is returned. Following that convention, a successful relative seek of
    240   // -1 bytes would be required to to give the same result as the error case.
    241   if (nb_bytes < 0)
    242     return static_cast<OPJ_OFF_T>(-1);
    243 
    244   // FIXME: use std::make_unsigned<OPJ_OFF_T>::type once c++11 lib is OK'd.
    245   uint64_t unsignedNbBytes = static_cast<uint64_t>(nb_bytes);
    246   // Additionally, the offset may take us beyond the range of a size_t (e.g.
    247   // 32-bit platforms). If so, just clamp at EOF.
    248   if (unsignedNbBytes >
    249       std::numeric_limits<OPJ_SIZE_T>::max() - srcData->offset) {
    250     srcData->offset = srcData->src_size;
    251   } else {
    252     OPJ_SIZE_T checkedNbBytes = static_cast<OPJ_SIZE_T>(unsignedNbBytes);
    253     // Otherwise, mimic fseek() semantics to always succeed, even past EOF,
    254     // clamping at EOF.  We can get away with this since we don't actually
    255     // provide negative relative skips from beyond EOF back to inside the
    256     // data, which would be the only reason to need to know exactly how far
    257     // beyond EOF we are.
    258     srcData->offset =
    259         std::min(srcData->offset + checkedNbBytes, srcData->src_size);
    260   }
    261   return nb_bytes;
    262 }
    263 
    264 OPJ_BOOL opj_seek_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data) {
    265   DecodeData* srcData = static_cast<DecodeData*>(p_user_data);
    266   if (!srcData || !srcData->src_data || srcData->src_size == 0)
    267     return OPJ_FALSE;
    268 
    269   // Offsets are signed and may indicate a negative position, which would
    270   // be before the start of the file. Do not support this.
    271   if (nb_bytes < 0)
    272     return OPJ_FALSE;
    273 
    274   // FIXME: use std::make_unsigned<OPJ_OFF_T>::type once c++11 lib is OK'd.
    275   uint64_t unsignedNbBytes = static_cast<uint64_t>(nb_bytes);
    276   // Additionally, the offset may take us beyond the range of a size_t (e.g.
    277   // 32-bit platforms). If so, just clamp at EOF.
    278   if (unsignedNbBytes > std::numeric_limits<OPJ_SIZE_T>::max()) {
    279     srcData->offset = srcData->src_size;
    280   } else {
    281     OPJ_SIZE_T checkedNbBytes = static_cast<OPJ_SIZE_T>(nb_bytes);
    282     // Otherwise, mimic fseek() semantics to always succeed, even past EOF,
    283     // again clamping at EOF.
    284     srcData->offset = std::min(checkedNbBytes, srcData->src_size);
    285   }
    286   return OPJ_TRUE;
    287 }
    288 
    289 void sycc420_to_rgb(opj_image_t* img) {
    290   if (!sycc420_size_is_valid(img))
    291     return;
    292 
    293   OPJ_UINT32 prec = img->comps[0].prec;
    294   if (!prec)
    295     return;
    296 
    297   OPJ_UINT32 offset = 1 << (prec - 1);
    298   OPJ_UINT32 upb = (1 << prec) - 1;
    299   OPJ_UINT32 yw = img->comps[0].w;
    300   OPJ_UINT32 yh = img->comps[0].h;
    301   OPJ_UINT32 cbw = img->comps[1].w;
    302   OPJ_UINT32 cbh = img->comps[1].h;
    303   OPJ_UINT32 crw = img->comps[2].w;
    304   bool extw = sycc420_must_extend_cbcr(yw, cbw);
    305   bool exth = sycc420_must_extend_cbcr(yh, cbh);
    306   FX_SAFE_UINT32 safeSize = yw;
    307   safeSize *= yh;
    308   safeSize *= sizeof(int);
    309   if (!safeSize.IsValid())
    310     return;
    311 
    312   int* r = static_cast<int*>(opj_image_data_alloc(safeSize.ValueOrDie()));
    313   int* g = static_cast<int*>(opj_image_data_alloc(safeSize.ValueOrDie()));
    314   int* b = static_cast<int*>(opj_image_data_alloc(safeSize.ValueOrDie()));
    315   int* d0 = r;
    316   int* d1 = g;
    317   int* d2 = b;
    318   const int* y = img->comps[0].data;
    319   const int* cb = img->comps[1].data;
    320   const int* cr = img->comps[2].data;
    321   if (!y || !cb || !cr)
    322     return;
    323 
    324   const int* ny = nullptr;
    325   int* nr = nullptr;
    326   int* ng = nullptr;
    327   int* nb = nullptr;
    328   OPJ_UINT32 i = 0;
    329   OPJ_UINT32 j = 0;
    330   for (i = 0; i < (yh & ~(OPJ_UINT32)1); i += 2) {
    331     ny = y + yw;
    332     nr = r + yw;
    333     ng = g + yw;
    334     nb = b + yw;
    335     for (j = 0; j < (yw & ~(OPJ_UINT32)1); j += 2) {
    336       sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
    337       ++y;
    338       ++r;
    339       ++g;
    340       ++b;
    341       sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
    342       ++y;
    343       ++r;
    344       ++g;
    345       ++b;
    346       sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
    347       ++ny;
    348       ++nr;
    349       ++ng;
    350       ++nb;
    351       sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
    352       ++ny;
    353       ++nr;
    354       ++ng;
    355       ++nb;
    356       ++cb;
    357       ++cr;
    358     }
    359     if (j < yw) {
    360       if (extw) {
    361         --cb;
    362         --cr;
    363       }
    364       sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
    365       ++y;
    366       ++r;
    367       ++g;
    368       ++b;
    369       sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
    370       ++ny;
    371       ++nr;
    372       ++ng;
    373       ++nb;
    374       ++cb;
    375       ++cr;
    376     }
    377     y += yw;
    378     r += yw;
    379     g += yw;
    380     b += yw;
    381   }
    382   if (i < yh) {
    383     if (exth) {
    384       cb -= cbw;
    385       cr -= crw;
    386     }
    387     for (j = 0; j < (yw & ~(OPJ_UINT32)1); j += 2) {
    388       sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
    389       ++y;
    390       ++r;
    391       ++g;
    392       ++b;
    393       sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
    394       ++y;
    395       ++r;
    396       ++g;
    397       ++b;
    398       ++cb;
    399       ++cr;
    400     }
    401     if (j < yw) {
    402       if (extw) {
    403         --cb;
    404         --cr;
    405       }
    406       sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
    407     }
    408   }
    409 
    410   opj_image_data_free(img->comps[0].data);
    411   opj_image_data_free(img->comps[1].data);
    412   opj_image_data_free(img->comps[2].data);
    413   img->comps[0].data = d0;
    414   img->comps[1].data = d1;
    415   img->comps[2].data = d2;
    416   img->comps[1].w = yw;
    417   img->comps[1].h = yh;
    418   img->comps[2].w = yw;
    419   img->comps[2].h = yh;
    420   img->comps[1].w = yw;
    421   img->comps[1].h = yh;
    422   img->comps[2].w = yw;
    423   img->comps[2].h = yh;
    424   img->comps[1].dx = img->comps[0].dx;
    425   img->comps[2].dx = img->comps[0].dx;
    426   img->comps[1].dy = img->comps[0].dy;
    427   img->comps[2].dy = img->comps[0].dy;
    428 }
    429 
    430 CJPX_Decoder::CJPX_Decoder(CPDF_ColorSpace* cs)
    431     : m_Image(nullptr),
    432       m_Codec(nullptr),
    433       m_DecodeData(nullptr),
    434       m_Stream(nullptr),
    435       m_ColorSpace(cs) {}
    436 
    437 CJPX_Decoder::~CJPX_Decoder() {
    438   if (m_Codec)
    439     opj_destroy_codec(m_Codec);
    440   if (m_Stream)
    441     opj_stream_destroy(m_Stream);
    442   if (m_Image)
    443     opj_image_destroy(m_Image);
    444 }
    445 
    446 bool CJPX_Decoder::Init(const unsigned char* src_data, uint32_t src_size) {
    447   static const unsigned char szJP2Header[] = {
    448       0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50, 0x20, 0x20, 0x0d, 0x0a, 0x87, 0x0a};
    449   if (!src_data || src_size < sizeof(szJP2Header))
    450     return false;
    451 
    452   m_Image = nullptr;
    453   m_SrcData = src_data;
    454   m_SrcSize = src_size;
    455   m_DecodeData = pdfium::MakeUnique<DecodeData>(
    456       const_cast<unsigned char*>(src_data), src_size);
    457   m_Stream = fx_opj_stream_create_memory_stream(
    458       m_DecodeData.get(), static_cast<unsigned int>(OPJ_J2K_STREAM_CHUNK_SIZE),
    459       1);
    460   if (!m_Stream)
    461     return false;
    462 
    463   opj_set_default_decoder_parameters(&m_Parameters);
    464   m_Parameters.decod_format = 0;
    465   m_Parameters.cod_format = 3;
    466   if (memcmp(m_SrcData, szJP2Header, sizeof(szJP2Header)) == 0) {
    467     m_Codec = opj_create_decompress(OPJ_CODEC_JP2);
    468     m_Parameters.decod_format = 1;
    469   } else {
    470     m_Codec = opj_create_decompress(OPJ_CODEC_J2K);
    471   }
    472   if (!m_Codec)
    473     return false;
    474 
    475   if (m_ColorSpace && m_ColorSpace->GetFamily() == PDFCS_INDEXED)
    476     m_Parameters.flags |= OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
    477   opj_set_info_handler(m_Codec, fx_ignore_callback, nullptr);
    478   opj_set_warning_handler(m_Codec, fx_ignore_callback, nullptr);
    479   opj_set_error_handler(m_Codec, fx_ignore_callback, nullptr);
    480   if (!opj_setup_decoder(m_Codec, &m_Parameters))
    481     return false;
    482 
    483   if (!opj_read_header(m_Stream, m_Codec, &m_Image)) {
    484     m_Image = nullptr;
    485     return false;
    486   }
    487   m_Image->pdfium_use_colorspace = !!m_ColorSpace;
    488 
    489   if (!m_Parameters.nb_tile_to_decode) {
    490     if (!opj_set_decode_area(m_Codec, m_Image, m_Parameters.DA_x0,
    491                              m_Parameters.DA_y0, m_Parameters.DA_x1,
    492                              m_Parameters.DA_y1)) {
    493       opj_image_destroy(m_Image);
    494       m_Image = nullptr;
    495       return false;
    496     }
    497     if (!(opj_decode(m_Codec, m_Stream, m_Image) &&
    498           opj_end_decompress(m_Codec, m_Stream))) {
    499       opj_image_destroy(m_Image);
    500       m_Image = nullptr;
    501       return false;
    502     }
    503   } else if (!opj_get_decoded_tile(m_Codec, m_Stream, m_Image,
    504                                    m_Parameters.tile_index)) {
    505     return false;
    506   }
    507 
    508   opj_stream_destroy(m_Stream);
    509   m_Stream = nullptr;
    510   if (m_Image->color_space != OPJ_CLRSPC_SYCC && m_Image->numcomps == 3 &&
    511       m_Image->comps[0].dx == m_Image->comps[0].dy &&
    512       m_Image->comps[1].dx != 1) {
    513     m_Image->color_space = OPJ_CLRSPC_SYCC;
    514   } else if (m_Image->numcomps <= 2) {
    515     m_Image->color_space = OPJ_CLRSPC_GRAY;
    516   }
    517   if (m_Image->color_space == OPJ_CLRSPC_SYCC)
    518     color_sycc_to_rgb(m_Image);
    519 
    520   if (m_Image->icc_profile_buf) {
    521     // TODO(palmer): Using |opj_free| here resolves the crash described in
    522     // https://crbug.com/737033, but ultimately we need to harmonize the
    523     // memory allocation strategy across OpenJPEG and its PDFium callers.
    524     opj_free(m_Image->icc_profile_buf);
    525     m_Image->icc_profile_buf = nullptr;
    526     m_Image->icc_profile_len = 0;
    527   }
    528 
    529   return true;
    530 }
    531 
    532 void CJPX_Decoder::GetInfo(uint32_t* width,
    533                            uint32_t* height,
    534                            uint32_t* components) {
    535   *width = m_Image->x1;
    536   *height = m_Image->y1;
    537   *components = m_Image->numcomps;
    538 }
    539 
    540 bool CJPX_Decoder::Decode(uint8_t* dest_buf,
    541                           int pitch,
    542                           const std::vector<uint8_t>& offsets) {
    543   if (m_Image->comps[0].w != m_Image->x1 || m_Image->comps[0].h != m_Image->y1)
    544     return false;
    545 
    546   if (pitch<static_cast<int>(m_Image->comps[0].w * 8 * m_Image->numcomps + 31)>>
    547       5 << 2) {
    548     return false;
    549   }
    550 
    551   memset(dest_buf, 0xff, m_Image->y1 * pitch);
    552   std::vector<uint8_t*> channel_bufs(m_Image->numcomps);
    553   std::vector<int> adjust_comps(m_Image->numcomps);
    554   for (uint32_t i = 0; i < m_Image->numcomps; i++) {
    555     channel_bufs[i] = dest_buf + offsets[i];
    556     adjust_comps[i] = m_Image->comps[i].prec - 8;
    557     if (i > 0) {
    558       if (m_Image->comps[i].dx != m_Image->comps[i - 1].dx ||
    559           m_Image->comps[i].dy != m_Image->comps[i - 1].dy ||
    560           m_Image->comps[i].prec != m_Image->comps[i - 1].prec) {
    561         return false;
    562       }
    563     }
    564   }
    565   int width = m_Image->comps[0].w;
    566   int height = m_Image->comps[0].h;
    567   for (uint32_t channel = 0; channel < m_Image->numcomps; ++channel) {
    568     uint8_t* pChannel = channel_bufs[channel];
    569     if (adjust_comps[channel] < 0) {
    570       for (int row = 0; row < height; ++row) {
    571         uint8_t* pScanline = pChannel + row * pitch;
    572         for (int col = 0; col < width; ++col) {
    573           uint8_t* pPixel = pScanline + col * m_Image->numcomps;
    574           if (!m_Image->comps[channel].data)
    575             continue;
    576 
    577           int src = m_Image->comps[channel].data[row * width + col];
    578           src += m_Image->comps[channel].sgnd
    579                      ? 1 << (m_Image->comps[channel].prec - 1)
    580                      : 0;
    581           if (adjust_comps[channel] > 0) {
    582             *pPixel = 0;
    583           } else {
    584             *pPixel = static_cast<uint8_t>(src << -adjust_comps[channel]);
    585           }
    586         }
    587       }
    588     } else {
    589       for (int row = 0; row < height; ++row) {
    590         uint8_t* pScanline = pChannel + row * pitch;
    591         for (int col = 0; col < width; ++col) {
    592           uint8_t* pPixel = pScanline + col * m_Image->numcomps;
    593           if (!m_Image->comps[channel].data)
    594             continue;
    595 
    596           int src = m_Image->comps[channel].data[row * width + col];
    597           src += m_Image->comps[channel].sgnd
    598                      ? 1 << (m_Image->comps[channel].prec - 1)
    599                      : 0;
    600           if (adjust_comps[channel] - 1 < 0) {
    601             *pPixel = static_cast<uint8_t>((src >> adjust_comps[channel]));
    602           } else {
    603             int tmpPixel = (src >> adjust_comps[channel]) +
    604                            ((src >> (adjust_comps[channel] - 1)) % 2);
    605             tmpPixel = pdfium::clamp(tmpPixel, 0, 255);
    606             *pPixel = static_cast<uint8_t>(tmpPixel);
    607           }
    608         }
    609       }
    610     }
    611   }
    612   return true;
    613 }
    614 
    615 CCodec_JpxModule::CCodec_JpxModule() {}
    616 
    617 CCodec_JpxModule::~CCodec_JpxModule() {}
    618 
    619 std::unique_ptr<CJPX_Decoder> CCodec_JpxModule::CreateDecoder(
    620     const uint8_t* src_buf,
    621     uint32_t src_size,
    622     CPDF_ColorSpace* cs) {
    623   auto decoder = pdfium::MakeUnique<CJPX_Decoder>(cs);
    624   return decoder->Init(src_buf, src_size) ? std::move(decoder) : nullptr;
    625 }
    626 
    627 void CCodec_JpxModule::GetImageInfo(CJPX_Decoder* pDecoder,
    628                                     uint32_t* width,
    629                                     uint32_t* height,
    630                                     uint32_t* components) {
    631   pDecoder->GetInfo(width, height, components);
    632 }
    633 
    634 bool CCodec_JpxModule::Decode(CJPX_Decoder* pDecoder,
    635                               uint8_t* dest_data,
    636                               int pitch,
    637                               const std::vector<uint8_t>& offsets) {
    638   return pDecoder->Decode(dest_data, pitch, offsets);
    639 }
    640