• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "core/fxge/calculate_pitch.h"
6 
7 #include "core/fxcrt/fx_safe_types.h"
8 #include "core/fxge/dib/fx_dib.h"
9 
10 namespace fxge {
11 namespace {
12 
CalculatePitch8Safely(uint32_t bpc,uint32_t components,int width)13 FX_SAFE_UINT32 CalculatePitch8Safely(uint32_t bpc,
14                                      uint32_t components,
15                                      int width) {
16   FX_SAFE_UINT32 pitch = bpc;
17   pitch *= components;
18   pitch *= width;
19   pitch += 7;
20   pitch /= 8;
21   return pitch;
22 }
23 
CalculatePitch32Safely(int bpp,int width)24 FX_SAFE_UINT32 CalculatePitch32Safely(int bpp, int width) {
25   FX_SAFE_UINT32 pitch = bpp;
26   pitch *= width;
27   pitch += 31;
28   pitch /= 32;  // quantized to number of 32-bit words.
29   pitch *= 4;   // and then back to bytes, (not just /8 in one step).
30   return pitch;
31 }
32 
33 }  // namespace
34 
CalculatePitch8OrDie(uint32_t bpc,uint32_t components,int width)35 uint32_t CalculatePitch8OrDie(uint32_t bpc, uint32_t components, int width) {
36   return CalculatePitch8Safely(bpc, components, width).ValueOrDie();
37 }
38 
CalculatePitch32OrDie(int bpp,int width)39 uint32_t CalculatePitch32OrDie(int bpp, int width) {
40   return CalculatePitch32Safely(bpp, width).ValueOrDie();
41 }
42 
CalculatePitch8(uint32_t bpc,uint32_t components,int width)43 absl::optional<uint32_t> CalculatePitch8(uint32_t bpc,
44                                          uint32_t components,
45                                          int width) {
46   FX_SAFE_UINT32 pitch = CalculatePitch8Safely(bpc, components, width);
47   if (!pitch.IsValid())
48     return absl::nullopt;
49   return pitch.ValueOrDie();
50 }
51 
CalculatePitch32(int bpp,int width)52 absl::optional<uint32_t> CalculatePitch32(int bpp, int width) {
53   FX_SAFE_UINT32 pitch = CalculatePitch32Safely(bpp, width);
54   if (!pitch.IsValid())
55     return absl::nullopt;
56   return pitch.ValueOrDie();
57 }
58 
59 }  // namespace fxge
60