File: icns_encoder.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (244 lines) | stat: -rw-r--r-- 8,894 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/web_applications/os_integration/mac/icns_encoder.h"

#include <algorithm>

#include "base/files/file.h"
#include "base/notreached.h"
#include "base/numerics/byte_conversions.h"
#include "base/numerics/checked_math.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/image/image.h"

namespace web_app {

namespace {

// Mapping of image size to the type identifiers used in the .icns file format
// for the png representation of the image as well as the RGB and Alpha channel
// representations.
struct IcnsBlockTypes {
  int size;
  uint32_t png_type;
  uint32_t image_type = 0;
  uint32_t mask_type = 0;
};

constexpr IcnsBlockTypes kIcnsBlockTypes[] = {
    {16, 'icp4', 'is32', 's8mk'},
    {32, 'icp5', 'il32', 'l8mk'},
    {48, 'icp6', 'ih32', 'h8mk'},
    {128, 'ic07'},
    {256, 'ic08'},
    {512, 'ic09'},
};

std::vector<uint8_t> CreateBlockHeader(uint32_t type, size_t data_length) {
  std::vector<uint8_t> result(8u);
  auto [first, second] = base::span(result).split_at<4u>();
  first.copy_from(base::U32ToBigEndian(type));
  second.copy_from(
      base::U32ToBigEndian(base::checked_cast<uint32_t>(data_length + 8u)));
  return result;
}

// Struct containing the red, green, blue and alpha channels extracted from an
// image as four separate vectors.
struct ImageBytes {
  std::vector<uint8_t> r, g, b, a;
};

// Extracts the red, green, blue and alpha channels from `bitmap` as four
// separate vectors. The red, green and blue channels will contain the
// unpremultiplied values, as that is how data is stored in an .icns file.
ImageBytes ExtractImageBytes(const SkBitmap& bitmap) {
  ImageBytes result;
  const size_t pixel_count = bitmap.height() * bitmap.width();
  result.r.reserve(pixel_count);
  result.g.reserve(pixel_count);
  result.b.reserve(pixel_count);
  result.a.reserve(pixel_count);
  for (int y = 0; y < bitmap.height(); ++y) {
    for (int x = 0; x < bitmap.width(); ++x) {
      SkColor c = bitmap.getColor(x, y);
      result.r.push_back(SkColorGetR(c));
      result.g.push_back(SkColorGetG(c));
      result.b.push_back(SkColorGetB(c));
      result.a.push_back(SkColorGetA(c));
    }
  }
  return result;
}

}  // namespace

IcnsEncoder::Block::Block(uint32_t type, std::vector<uint8_t> data)
    : type(type), data(std::move(data)) {}
IcnsEncoder::Block::~Block() = default;
IcnsEncoder::Block::Block(Block&&) = default;
IcnsEncoder::Block& IcnsEncoder::Block::operator=(Block&&) = default;

IcnsEncoder::IcnsEncoder() = default;
IcnsEncoder::~IcnsEncoder() = default;

bool IcnsEncoder::AddImage(const gfx::Image& image) {
  if (image.IsEmpty())
    return false;

  SkBitmap bitmap = image.AsBitmap();
  if (bitmap.colorType() != kN32_SkColorType ||
      bitmap.width() != bitmap.height())
    return false;

  const IcnsBlockTypes* block_types =
      std::ranges::find(kIcnsBlockTypes, bitmap.width(), &IcnsBlockTypes::size);
  if (block_types == std::end(kIcnsBlockTypes))
    return false;

  if (block_types->image_type != 0) {
    // If there is a legacy image type for this size we should use that rather
    // than the png format, as many places in Mac OS do not properly support png
    // icons for sizes that also support a legacy format.
    DCHECK(block_types->mask_type != 0);
    ImageBytes bytes = ExtractImageBytes(bitmap);
    std::vector<uint8_t> image_data;
    AppendRLEImageData(bytes.r, &image_data);
    AppendRLEImageData(bytes.g, &image_data);
    AppendRLEImageData(bytes.b, &image_data);
    AppendBlock(block_types->image_type, std::move(image_data));
    AppendBlock(block_types->mask_type, std::move(bytes.a));
  } else {
    DCHECK(block_types->png_type != 0);
    std::optional<std::vector<uint8_t>> png_data =
        gfx::PNGCodec::EncodeBGRASkBitmap(bitmap,
                                          /*discard_transparency=*/false);
    if (!png_data) {
      return false;
    }
    AppendBlock(block_types->png_type, std::move(png_data).value());
  }
  return true;
}

bool IcnsEncoder::WriteToFile(const base::FilePath& path) const {
  // Build the Table of Contents, which is simply the headers of all the blocks
  // concatenated.
  Block toc('TOC ');
  toc.data.reserve(8 * blocks_.size());
  for (const auto& block : blocks_) {
    auto header = CreateBlockHeader(block.type, block.data.size());
    toc.data.insert(toc.data.end(), header.begin(), header.end());
  }

  size_t total_data_size =
      total_block_size_ + toc.data.size() + kBlockHeaderSize;

  base::File output(path, base::File::Flags::FLAG_CREATE_ALWAYS |
                              base::File::Flags::FLAG_WRITE);
  if (!output.IsValid())
    return false;

  if (!output.WriteAtCurrentPosAndCheck(
          ::web_app::CreateBlockHeader('icns', total_data_size))) {
    return false;
  }
  if (!WriteBlockToFile(output, toc))
    return false;
  for (const auto& block : blocks_) {
    if (!WriteBlockToFile(output, block))
      return false;
  }

  return true;
}

// static
void IcnsEncoder::AppendRLEImageData(base::span<const uint8_t> data,
                                     std::vector<uint8_t>* rle_data) {
  // The packing loop is done with two pieces of state:
  //   - data: at any point in the loop this only contains the bytes that have
  //           not yet been written to the block
  //   - search_offset: this is the offset within |data| used to search for
  //                    byte runs
  //
  // The code scours through the data, looking for runs of length greater than 3
  // (since only runs of 3 or longer can be compressed). As soon as a run is
  // found, all the data up to `search_offset` is dumped as literal data,
  // `data` is updated to only point at the remaining data, then the run is
  // dumped (and `data` updated again), and then the search continues.

  size_t search_offset = 0;

  // Search for runs through the block of data, byte by byte.
  while (search_offset < data.size()) {
    uint8_t current_byte = data[search_offset];
    size_t run_length = 1;
    while (search_offset + run_length < data.size() && run_length < 130 &&
           data[search_offset + run_length] == current_byte) {
      ++run_length;
    }
    if (run_length >= 3) {
      // A long-enough run was found. First, dump all the data before the run
      // into the output block.
      while (search_offset > 0) {
        // Because uncompressed data runs max out at 128 bytes of data, cap the
        // uncompressed run at 128 bytes.
        base::span<const uint8_t> uncompressed_chunk =
            data.first(std::min<size_t>(search_offset, 128));
        // Key byte values of 0..127 mean 1..128 bytes of uncompressed data.
        uint8_t key_byte = uncompressed_chunk.size() - 1;
        rle_data->push_back(key_byte);
        rle_data->insert(rle_data->end(), uncompressed_chunk.begin(),
                         uncompressed_chunk.end());
        data = data.subspan(uncompressed_chunk.size());
        search_offset -= uncompressed_chunk.size();
      }
      // Now that the output block is caught up, put the run that was just found
      // into it. Key byte values of 128..255 mean 3..130 copies of the
      // following byte, thus the addition of 125 to the run length.
      uint8_t key_byte = run_length + 125;
      rle_data->push_back(key_byte);
      rle_data->push_back(current_byte);
      data = data.subspan(run_length);
    } else {
      // The run is too small, so keep looking.
      search_offset += run_length;
    }
  }
  // At this point, there are no more runs, so pack the rest of the data into
  // the output block.
  while (search_offset > 0) {
    // Because uncompressed data runs max out at 128 bytes of data, cap the
    // uncompressed run at 128 bytes.
    base::span<const uint8_t> uncompressed_chunk =
        data.first(std::min<size_t>(search_offset, 128));
    // Key byte values of 0..127 mean 1..128 bytes of uncompressed data.
    uint8_t key_byte = uncompressed_chunk.size() - 1;
    rle_data->push_back(key_byte);
    rle_data->insert(rle_data->end(), uncompressed_chunk.begin(),
                     uncompressed_chunk.end());
    data = data.subspan(uncompressed_chunk.size());
    search_offset -= uncompressed_chunk.size();
  }
}

void IcnsEncoder::AppendBlock(uint32_t type, std::vector<uint8_t> data) {
  total_block_size_ += data.size() + kBlockHeaderSize;
  blocks_.emplace_back(type, std::move(data));
}

// static
bool IcnsEncoder::WriteBlockToFile(base::File& file, const Block& block) {
  if (!file.WriteAtCurrentPosAndCheck(
          CreateBlockHeader(block.type, block.data.size())))
    return false;
  if (!file.WriteAtCurrentPosAndCheck(block.data))
    return false;
  return true;
}

}  // namespace web_app