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 "core/include/fpdfapi/fpdf_module.h"
8#include "core/include/fpdfapi/fpdf_page.h"
9#include "core/include/fpdfapi/fpdf_render.h"
10#include "core/include/fxcodec/fx_codec.h"
11#include "core/src/fpdfapi/fpdf_page/pageint.h"
12#include "core/src/fpdfapi/fpdf_render/render_int.h"
13
14CPDF_Dictionary* CPDF_Image::InitJPEG(uint8_t* pData, FX_DWORD size) {
15  int32_t width;
16  int32_t height;
17  int32_t num_comps;
18  int32_t bits;
19  FX_BOOL color_trans;
20  if (!CPDF_ModuleMgr::Get()->GetJpegModule()->LoadInfo(
21          pData, size, width, height, num_comps, bits, color_trans)) {
22    return NULL;
23  }
24  CPDF_Dictionary* pDict = new CPDF_Dictionary;
25  pDict->SetAtName("Type", "XObject");
26  pDict->SetAtName("Subtype", "Image");
27  pDict->SetAtInteger("Width", width);
28  pDict->SetAtInteger("Height", height);
29  const FX_CHAR* csname = NULL;
30  if (num_comps == 1) {
31    csname = "DeviceGray";
32  } else if (num_comps == 3) {
33    csname = "DeviceRGB";
34  } else if (num_comps == 4) {
35    csname = "DeviceCMYK";
36    CPDF_Array* pDecode = new CPDF_Array;
37    for (int n = 0; n < 4; n++) {
38      pDecode->AddInteger(1);
39      pDecode->AddInteger(0);
40    }
41    pDict->SetAt("Decode", pDecode);
42  }
43  pDict->SetAtName("ColorSpace", csname);
44  pDict->SetAtInteger("BitsPerComponent", bits);
45  pDict->SetAtName("Filter", "DCTDecode");
46  if (!color_trans) {
47    CPDF_Dictionary* pParms = new CPDF_Dictionary;
48    pDict->SetAt("DecodeParms", pParms);
49    pParms->SetAtInteger("ColorTransform", 0);
50  }
51  m_bIsMask = FALSE;
52  m_Width = width;
53  m_Height = height;
54  if (!m_pStream) {
55    m_pStream = new CPDF_Stream(NULL, 0, NULL);
56  }
57  return pDict;
58}
59void CPDF_Image::SetJpegImage(uint8_t* pData, FX_DWORD size) {
60  CPDF_Dictionary* pDict = InitJPEG(pData, size);
61  if (!pDict) {
62    return;
63  }
64  m_pStream->InitStream(pData, size, pDict);
65}
66void CPDF_Image::SetJpegImage(IFX_FileRead* pFile) {
67  FX_DWORD size = (FX_DWORD)pFile->GetSize();
68  if (!size) {
69    return;
70  }
71  FX_DWORD dwEstimateSize = size;
72  if (dwEstimateSize > 8192) {
73    dwEstimateSize = 8192;
74  }
75  uint8_t* pData = FX_Alloc(uint8_t, dwEstimateSize);
76  pFile->ReadBlock(pData, 0, dwEstimateSize);
77  CPDF_Dictionary* pDict = InitJPEG(pData, dwEstimateSize);
78  FX_Free(pData);
79  if (!pDict && size > dwEstimateSize) {
80    pData = FX_Alloc(uint8_t, size);
81    pFile->ReadBlock(pData, 0, size);
82    pDict = InitJPEG(pData, size);
83    FX_Free(pData);
84  }
85  if (!pDict) {
86    return;
87  }
88  m_pStream->InitStreamFromFile(pFile, pDict);
89}
90void _DCTEncodeBitmap(CPDF_Dictionary* pBitmapDict,
91                      const CFX_DIBitmap* pBitmap,
92                      int quality,
93                      uint8_t*& buf,
94                      FX_STRSIZE& size) {}
95void _JBIG2EncodeBitmap(CPDF_Dictionary* pBitmapDict,
96                        const CFX_DIBitmap* pBitmap,
97                        CPDF_Document* pDoc,
98                        uint8_t*& buf,
99                        FX_STRSIZE& size,
100                        FX_BOOL bLossLess) {}
101void CPDF_Image::SetImage(const CFX_DIBitmap* pBitmap,
102                          int32_t iCompress,
103                          IFX_FileWrite* pFileWrite,
104                          IFX_FileRead* pFileRead,
105                          const CFX_DIBitmap* pMask,
106                          const CPDF_ImageSetParam* pParam) {
107  int32_t BitmapWidth = pBitmap->GetWidth();
108  int32_t BitmapHeight = pBitmap->GetHeight();
109  if (BitmapWidth < 1 || BitmapHeight < 1) {
110    return;
111  }
112  uint8_t* src_buf = pBitmap->GetBuffer();
113  int32_t src_pitch = pBitmap->GetPitch();
114  int32_t bpp = pBitmap->GetBPP();
115  FX_BOOL bUseMatte =
116      pParam && pParam->pMatteColor && (pBitmap->GetFormat() == FXDIB_Argb);
117  CPDF_Dictionary* pDict = new CPDF_Dictionary;
118  pDict->SetAtName("Type", "XObject");
119  pDict->SetAtName("Subtype", "Image");
120  pDict->SetAtInteger("Width", BitmapWidth);
121  pDict->SetAtInteger("Height", BitmapHeight);
122  uint8_t* dest_buf = NULL;
123  FX_STRSIZE dest_pitch = 0, dest_size = 0, opType = -1;
124  if (bpp == 1) {
125    int32_t reset_a = 0, reset_r = 0, reset_g = 0, reset_b = 0;
126    int32_t set_a = 0, set_r = 0, set_g = 0, set_b = 0;
127    if (!pBitmap->IsAlphaMask()) {
128      ArgbDecode(pBitmap->GetPaletteArgb(0), reset_a, reset_r, reset_g,
129                 reset_b);
130      ArgbDecode(pBitmap->GetPaletteArgb(1), set_a, set_r, set_g, set_b);
131    }
132    if (set_a == 0 || reset_a == 0) {
133      pDict->SetAt("ImageMask", new CPDF_Boolean(TRUE));
134      if (reset_a == 0) {
135        CPDF_Array* pArray = new CPDF_Array;
136        pArray->AddInteger(1);
137        pArray->AddInteger(0);
138        pDict->SetAt("Decode", pArray);
139      }
140    } else {
141      CPDF_Array* pCS = new CPDF_Array;
142      pCS->AddName("Indexed");
143      pCS->AddName("DeviceRGB");
144      pCS->AddInteger(1);
145      CFX_ByteString ct;
146      FX_CHAR* pBuf = ct.GetBuffer(6);
147      pBuf[0] = (FX_CHAR)reset_r;
148      pBuf[1] = (FX_CHAR)reset_g;
149      pBuf[2] = (FX_CHAR)reset_b;
150      pBuf[3] = (FX_CHAR)set_r;
151      pBuf[4] = (FX_CHAR)set_g;
152      pBuf[5] = (FX_CHAR)set_b;
153      ct.ReleaseBuffer(6);
154      pCS->Add(new CPDF_String(ct, TRUE));
155      pDict->SetAt("ColorSpace", pCS);
156    }
157    pDict->SetAtInteger("BitsPerComponent", 1);
158    dest_pitch = (BitmapWidth + 7) / 8;
159    if ((iCompress & 0x03) == PDF_IMAGE_NO_COMPRESS) {
160      opType = 1;
161    } else {
162      opType = 0;
163    }
164  } else if (bpp == 8) {
165    int32_t iPalette = pBitmap->GetPaletteSize();
166    if (iPalette > 0) {
167      CPDF_Array* pCS = new CPDF_Array;
168      m_pDocument->AddIndirectObject(pCS);
169      pCS->AddName("Indexed");
170      pCS->AddName("DeviceRGB");
171      pCS->AddInteger(iPalette - 1);
172      uint8_t* pColorTable = FX_Alloc2D(uint8_t, iPalette, 3);
173      uint8_t* ptr = pColorTable;
174      for (int32_t i = 0; i < iPalette; i++) {
175        FX_DWORD argb = pBitmap->GetPaletteArgb(i);
176        ptr[0] = (uint8_t)(argb >> 16);
177        ptr[1] = (uint8_t)(argb >> 8);
178        ptr[2] = (uint8_t)argb;
179        ptr += 3;
180      }
181      CPDF_Stream* pCTS =
182          new CPDF_Stream(pColorTable, iPalette * 3, new CPDF_Dictionary);
183      m_pDocument->AddIndirectObject(pCTS);
184      pCS->AddReference(m_pDocument, pCTS);
185      pDict->SetAtReference("ColorSpace", m_pDocument, pCS);
186    } else {
187      pDict->SetAtName("ColorSpace", "DeviceGray");
188    }
189    pDict->SetAtInteger("BitsPerComponent", 8);
190    if ((iCompress & 0x03) == PDF_IMAGE_NO_COMPRESS) {
191      dest_pitch = BitmapWidth;
192      opType = 1;
193    } else {
194      opType = 0;
195    }
196  } else {
197    pDict->SetAtName("ColorSpace", "DeviceRGB");
198    pDict->SetAtInteger("BitsPerComponent", 8);
199    if ((iCompress & 0x03) == PDF_IMAGE_NO_COMPRESS) {
200      dest_pitch = BitmapWidth * 3;
201      opType = 2;
202    } else {
203      opType = 0;
204    }
205  }
206  const CFX_DIBitmap* pMaskBitmap = NULL;
207  FX_BOOL bDeleteMask = FALSE;
208  if (pBitmap->HasAlpha()) {
209    pMaskBitmap = pBitmap->GetAlphaMask();
210    bDeleteMask = TRUE;
211  }
212  if (!pMaskBitmap && pMask) {
213    FXDIB_Format maskFormat = pMask->GetFormat();
214    if (maskFormat == FXDIB_1bppMask || maskFormat == FXDIB_8bppMask) {
215      pMaskBitmap = pMask;
216    }
217  }
218  if (pMaskBitmap) {
219    int32_t maskWidth = pMaskBitmap->GetWidth();
220    int32_t maskHeight = pMaskBitmap->GetHeight();
221    uint8_t* mask_buf = NULL;
222    FX_STRSIZE mask_size = 0;
223    CPDF_Dictionary* pMaskDict = new CPDF_Dictionary;
224    pMaskDict->SetAtName("Type", "XObject");
225    pMaskDict->SetAtName("Subtype", "Image");
226    pMaskDict->SetAtInteger("Width", maskWidth);
227    pMaskDict->SetAtInteger("Height", maskHeight);
228    pMaskDict->SetAtName("ColorSpace", "DeviceGray");
229    pMaskDict->SetAtInteger("BitsPerComponent", 8);
230    if (pMaskBitmap->GetBPP() == 8 &&
231        (iCompress & PDF_IMAGE_MASK_LOSSY_COMPRESS) != 0) {
232      _DCTEncodeBitmap(pMaskDict, pMaskBitmap, pParam ? pParam->nQuality : 75,
233                       mask_buf, mask_size);
234    } else if (pMaskBitmap->GetFormat() == FXDIB_1bppMask) {
235      _JBIG2EncodeBitmap(pMaskDict, pMaskBitmap, m_pDocument, mask_buf,
236                         mask_size, TRUE);
237    } else {
238      mask_buf = FX_Alloc2D(uint8_t, maskHeight, maskWidth);
239      mask_size = maskHeight * maskWidth;  // Safe since checked alloc returned.
240      for (int32_t a = 0; a < maskHeight; a++) {
241        FXSYS_memcpy(mask_buf + a * maskWidth, pMaskBitmap->GetScanline(a),
242                     maskWidth);
243      }
244    }
245    pMaskDict->SetAtInteger("Length", mask_size);
246    if (bUseMatte) {
247      int a, r, g, b;
248      ArgbDecode(*(pParam->pMatteColor), a, r, g, b);
249      CPDF_Array* pMatte = new CPDF_Array;
250      pMatte->AddInteger(r);
251      pMatte->AddInteger(g);
252      pMatte->AddInteger(b);
253      pMaskDict->SetAt("Matte", pMatte);
254    }
255    CPDF_Stream* pMaskStream = new CPDF_Stream(mask_buf, mask_size, pMaskDict);
256    m_pDocument->AddIndirectObject(pMaskStream);
257    pDict->SetAtReference("SMask", m_pDocument, pMaskStream);
258    if (bDeleteMask) {
259      delete pMaskBitmap;
260    }
261  }
262  FX_BOOL bStream = pFileWrite && pFileRead;
263  if (opType == 0) {
264    if (iCompress & PDF_IMAGE_LOSSLESS_COMPRESS) {
265      if (pBitmap->GetBPP() == 1) {
266        _JBIG2EncodeBitmap(pDict, pBitmap, m_pDocument, dest_buf, dest_size,
267                           TRUE);
268      }
269    } else {
270      if (pBitmap->GetBPP() == 1) {
271        _JBIG2EncodeBitmap(pDict, pBitmap, m_pDocument, dest_buf, dest_size,
272                           FALSE);
273      } else if (pBitmap->GetBPP() >= 8 && pBitmap->GetPalette()) {
274        CFX_DIBitmap* pNewBitmap = new CFX_DIBitmap();
275        pNewBitmap->Copy(pBitmap);
276        pNewBitmap->ConvertFormat(FXDIB_Rgb);
277        SetImage(pNewBitmap, iCompress, pFileWrite, pFileRead);
278        if (pDict) {
279          pDict->Release();
280          pDict = NULL;
281        }
282        FX_Free(dest_buf);
283        dest_buf = NULL;
284        dest_size = 0;
285        delete pNewBitmap;
286        return;
287      } else {
288        if (bUseMatte) {
289          CFX_DIBitmap* pNewBitmap = new CFX_DIBitmap();
290          pNewBitmap->Create(BitmapWidth, BitmapHeight, FXDIB_Argb);
291          uint8_t* dst_buf = pNewBitmap->GetBuffer();
292          int32_t src_offset = 0;
293          for (int32_t row = 0; row < BitmapHeight; row++) {
294            src_offset = row * src_pitch;
295            for (int32_t column = 0; column < BitmapWidth; column++) {
296              FX_FLOAT alpha = src_buf[src_offset + 3] / 255.0f;
297              dst_buf[src_offset] = (uint8_t)(src_buf[src_offset] * alpha);
298              dst_buf[src_offset + 1] =
299                  (uint8_t)(src_buf[src_offset + 1] * alpha);
300              dst_buf[src_offset + 2] =
301                  (uint8_t)(src_buf[src_offset + 2] * alpha);
302              dst_buf[src_offset + 3] = (uint8_t)(src_buf[src_offset + 3]);
303              src_offset += 4;
304            }
305          }
306          _DCTEncodeBitmap(pDict, pNewBitmap, pParam ? pParam->nQuality : 75,
307                           dest_buf, dest_size);
308          delete pNewBitmap;
309        } else {
310          _DCTEncodeBitmap(pDict, pBitmap, pParam ? pParam->nQuality : 75,
311                           dest_buf, dest_size);
312        }
313      }
314    }
315    if (bStream) {
316      pFileWrite->WriteBlock(dest_buf, dest_size);
317      FX_Free(dest_buf);
318      dest_buf = NULL;
319    }
320  } else if (opType == 1) {
321    if (!bStream) {
322      dest_buf = FX_Alloc2D(uint8_t, dest_pitch, BitmapHeight);
323      dest_size =
324          dest_pitch * BitmapHeight;  // Safe since checked alloc returned.
325    }
326    uint8_t* pDest = dest_buf;
327    for (int32_t i = 0; i < BitmapHeight; i++) {
328      if (!bStream) {
329        FXSYS_memcpy(pDest, src_buf, dest_pitch);
330        pDest += dest_pitch;
331      } else {
332        pFileWrite->WriteBlock(src_buf, dest_pitch);
333      }
334      src_buf += src_pitch;
335    }
336  } else if (opType == 2) {
337    if (!bStream) {
338      dest_buf = FX_Alloc2D(uint8_t, dest_pitch, BitmapHeight);
339      dest_size =
340          dest_pitch * BitmapHeight;  // Safe since checked alloc returned.
341    } else {
342      dest_buf = FX_Alloc(uint8_t, dest_pitch);
343    }
344    uint8_t* pDest = dest_buf;
345    int32_t src_offset = 0;
346    int32_t dest_offset = 0;
347    for (int32_t row = 0; row < BitmapHeight; row++) {
348      src_offset = row * src_pitch;
349      for (int32_t column = 0; column < BitmapWidth; column++) {
350        FX_FLOAT alpha = bUseMatte ? src_buf[src_offset + 3] / 255.0f : 1;
351        pDest[dest_offset] = (uint8_t)(src_buf[src_offset + 2] * alpha);
352        pDest[dest_offset + 1] = (uint8_t)(src_buf[src_offset + 1] * alpha);
353        pDest[dest_offset + 2] = (uint8_t)(src_buf[src_offset] * alpha);
354        dest_offset += 3;
355        src_offset += bpp == 24 ? 3 : 4;
356      }
357      if (bStream) {
358        pFileWrite->WriteBlock(pDest, dest_pitch);
359        pDest = dest_buf;
360      } else {
361        pDest += dest_pitch;
362      }
363      dest_offset = 0;
364    }
365    if (bStream) {
366      FX_Free(dest_buf);
367      dest_buf = NULL;
368    }
369  }
370  if (!m_pStream) {
371    m_pStream = new CPDF_Stream(NULL, 0, NULL);
372  }
373  if (!bStream) {
374    m_pStream->InitStream(dest_buf, dest_size, pDict);
375  } else {
376    pFileWrite->Flush();
377    m_pStream->InitStreamFromFile(pFileRead, pDict);
378  }
379  m_bIsMask = pBitmap->IsAlphaMask();
380  m_Width = BitmapWidth;
381  m_Height = BitmapHeight;
382  FX_Free(dest_buf);
383}
384void CPDF_Image::ResetCache(CPDF_Page* pPage, const CFX_DIBitmap* pBitmap) {
385  pPage->GetRenderCache()->ResetBitmap(m_pStream, pBitmap);
386}
387