|
14 | 14 | // limitations under the License. |
15 | 15 |
|
16 | 16 | #include "paligemma/image.h" |
17 | | -#include "compression/io.h" |
18 | 17 |
|
| 18 | +#include <stddef.h> |
19 | 19 | #include <stdint.h> |
20 | 20 |
|
21 | 21 | #include <algorithm> |
|
28 | 28 | #include <utility> |
29 | 29 | #include <vector> |
30 | 30 |
|
| 31 | +#include "compression/io.h" |
| 32 | +#include "hwy/aligned_allocator.h" // hwy::Span |
31 | 33 | #include "hwy/base.h" |
| 34 | +#include "hwy/profiler.h" |
32 | 35 |
|
33 | 36 | namespace gcpp { |
34 | 37 | namespace { |
@@ -95,12 +98,12 @@ bool Image::ReadPPM(const std::string& filename) { |
95 | 98 | std::cerr << filename << " does not exist\n"; |
96 | 99 | return false; |
97 | 100 | } |
98 | | - auto content = ReadFileToString(path); |
| 101 | + const std::string content = ReadFileToString(path); |
99 | 102 | return ReadPPM(hwy::Span<const char>(content.data(), content.size())); |
100 | 103 | } |
101 | 104 |
|
102 | 105 | bool Image::ReadPPM(const hwy::Span<const char>& buf) { |
103 | | - auto pos = CheckP6Format(buf.cbegin(), buf.cend()); |
| 106 | + const char* pos = CheckP6Format(buf.cbegin(), buf.cend()); |
104 | 107 | if (!pos) { |
105 | 108 | std::cerr << "We only support binary PPM (P6)\n"; |
106 | 109 | return false; |
@@ -134,8 +137,8 @@ bool Image::ReadPPM(const hwy::Span<const char>& buf) { |
134 | 137 | return false; |
135 | 138 | } |
136 | 139 | ++pos; |
137 | | - auto data_size = width * height * 3; |
138 | | - if (buf.cend() - pos < data_size) { |
| 140 | + const size_t data_size = width * height * 3; |
| 141 | + if (buf.cend() - pos < static_cast<ptrdiff_t>(data_size)) { |
139 | 142 | std::cerr << "Insufficient data remaining\n"; |
140 | 143 | return false; |
141 | 144 | } |
@@ -190,28 +193,30 @@ bool Image::WriteBinary(const std::string& filename) const { |
190 | 193 | // We want the N-th patch (of 256) of size kPatchSize x kPatchSize x 3. |
191 | 194 | // Patches are numbered in usual "pixel-order". |
192 | 195 | void Image::GetPatch(size_t patch_num, float* patch) const { |
| 196 | + PROFILER_FUNC; |
193 | 197 | constexpr size_t kDataSize = kImageSize * kImageSize * 3; |
194 | 198 | HWY_ASSERT(size() == kDataSize); |
195 | 199 | constexpr size_t kPatchDataSize = kPatchSize * kPatchSize * 3; |
196 | | - int i_offs = patch_num / kNumPatches; |
197 | | - int j_offs = patch_num % kNumPatches; |
| 200 | + size_t i_offs = patch_num / kNumPatches; |
| 201 | + size_t j_offs = patch_num % kNumPatches; |
198 | 202 | HWY_ASSERT(0 <= i_offs && i_offs < kNumPatches); |
199 | 203 | HWY_ASSERT(0 <= j_offs && j_offs < kNumPatches); |
200 | 204 | i_offs *= kPatchSize; |
201 | 205 | j_offs *= kPatchSize; |
202 | 206 | // This can be made faster, but let's first see whether it matters. |
203 | 207 | const float* image_data = data(); |
204 | | - for (int i = 0; i < kPatchSize; ++i) { |
205 | | - for (int j = 0; j < kPatchSize; ++j) { |
206 | | - for (int k = 0; k < 3; ++k) { |
207 | | - const int patch_index = (i * kPatchSize + j) * 3 + k; |
| 208 | + for (size_t i = 0; i < kPatchSize; ++i) { |
| 209 | + for (size_t j = 0; j < kPatchSize; ++j) { |
| 210 | + for (size_t k = 0; k < 3; ++k) { |
| 211 | + const size_t patch_index = (i * kPatchSize + j) * 3 + k; |
208 | 212 | HWY_ASSERT(patch_index < kPatchDataSize); |
209 | | - const int image_index = |
| 213 | + const size_t image_index = |
210 | 214 | ((i + i_offs) * kImageSize + (j + j_offs)) * 3 + k; |
211 | 215 | HWY_ASSERT(image_index < kDataSize); |
212 | 216 | patch[patch_index] = image_data[image_index]; |
213 | 217 | } |
214 | 218 | } |
215 | 219 | } |
216 | 220 | } |
| 221 | + |
217 | 222 | } // namespace gcpp |
0 commit comments