OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/utility/cloud_print/pwg_encoder.h" | 5 #include "chrome/utility/cloud_print/pwg_encoder.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/big_endian.h" | 9 #include "base/big_endian.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 const uint32 kHeaderCupsBytesPerLine = 392; | 33 const uint32 kHeaderCupsBytesPerLine = 392; |
34 const uint32 kHeaderCupsColorOrder = 396; | 34 const uint32 kHeaderCupsColorOrder = 396; |
35 const uint32 kHeaderCupsColorSpace = 400; | 35 const uint32 kHeaderCupsColorSpace = 400; |
36 const uint32 kHeaderCupsNumColors = 420; | 36 const uint32 kHeaderCupsNumColors = 420; |
37 const uint32 kHeaderPwgTotalPageCount = 452; | 37 const uint32 kHeaderPwgTotalPageCount = 452; |
38 | 38 |
39 const int kPwgMaxPackedRows = 256; | 39 const int kPwgMaxPackedRows = 256; |
40 | 40 |
41 const int kPwgMaxPackedPixels = 128; | 41 const int kPwgMaxPackedPixels = 128; |
42 | 42 |
| 43 inline int FlipIfNeeded(bool flip, int current, int total) { |
| 44 return flip ? total - current : current; |
| 45 } |
| 46 |
43 } // namespace | 47 } // namespace |
44 | 48 |
45 PwgEncoder::PwgEncoder() {} | 49 PwgEncoder::PwgEncoder() {} |
46 | 50 |
47 inline void encodePixelFromRGBA(const uint8* pixel, std::string* output) { | 51 inline void encodePixelFromRGBA(const uint8* pixel, std::string* output) { |
48 output->push_back(static_cast<char>(pixel[0])); | 52 output->push_back(static_cast<char>(pixel[0])); |
49 output->push_back(static_cast<char>(pixel[1])); | 53 output->push_back(static_cast<char>(pixel[1])); |
50 output->push_back(static_cast<char>(pixel[2])); | 54 output->push_back(static_cast<char>(pixel[2])); |
51 } | 55 } |
52 | 56 |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 int row) const { | 155 int row) const { |
152 return image.pixel_data() + row * image.size().width() * image.channels(); | 156 return image.pixel_data() + row * image.size().width() * image.channels(); |
153 } | 157 } |
154 | 158 |
155 // Given a pointer to a struct Image, create a PWG of the image and | 159 // Given a pointer to a struct Image, create a PWG of the image and |
156 // put the compressed image data in the std::string. Returns true on success. | 160 // put the compressed image data in the std::string. Returns true on success. |
157 // The content of the std::string is undefined on failure. | 161 // The content of the std::string is undefined on failure. |
158 bool PwgEncoder::EncodePage(const BitmapImage& image, | 162 bool PwgEncoder::EncodePage(const BitmapImage& image, |
159 const uint32 dpi, | 163 const uint32 dpi, |
160 const uint32 total_pages, | 164 const uint32 total_pages, |
161 std::string* output) const { | 165 std::string* output, |
| 166 bool rotate) const { |
162 // For now only some 4-channel colorspaces are supported. | 167 // For now only some 4-channel colorspaces are supported. |
163 if (image.channels() != 4) { | 168 if (image.channels() != 4) { |
164 LOG(ERROR) << "Unsupported colorspace."; | 169 LOG(ERROR) << "Unsupported colorspace."; |
165 return false; | 170 return false; |
166 } | 171 } |
167 | 172 |
168 EncodePageHeader(image, dpi, total_pages, output); | 173 EncodePageHeader(image, dpi, total_pages, output); |
169 | 174 |
170 int row_size = image.size().width() * image.channels(); | 175 int row_size = image.size().width() * image.channels(); |
| 176 scoped_ptr<uint8[]> current_row_cpy(new uint8[row_size]); |
171 | 177 |
172 int row_number = 0; | 178 int row_number = 0; |
173 while (row_number < image.size().height()) { | 179 int total_rows = image.size().height(); |
174 const uint8* current_row = GetRow(image, row_number++); | 180 while (row_number < total_rows) { |
| 181 const uint8* current_row = |
| 182 GetRow(image, FlipIfNeeded(rotate, row_number++, total_rows)); |
175 int num_identical_rows = 1; | 183 int num_identical_rows = 1; |
176 // We count how many times the current row is repeated. | 184 // We count how many times the current row is repeated. |
177 while (num_identical_rows < kPwgMaxPackedRows | 185 while (num_identical_rows < kPwgMaxPackedRows && |
178 && row_number < image.size().height() | 186 row_number < image.size().height() && |
179 && !memcmp(current_row, GetRow(image, row_number), row_size)) { | 187 !memcmp(current_row, |
| 188 GetRow(image, FlipIfNeeded(rotate, row_number, total_rows)), |
| 189 row_size)) { |
180 num_identical_rows++; | 190 num_identical_rows++; |
181 row_number++; | 191 row_number++; |
182 } | 192 } |
183 output->push_back(static_cast<char>(num_identical_rows - 1)); | 193 output->push_back(static_cast<char>(num_identical_rows - 1)); |
184 | 194 |
| 195 if (rotate) { |
| 196 memcpy(current_row_cpy.get(), current_row, row_size); |
| 197 std::reverse(reinterpret_cast<uint32*>(current_row_cpy.get()), |
| 198 reinterpret_cast<uint32*>(current_row_cpy.get() + row_size)); |
| 199 current_row = current_row_cpy.get(); |
| 200 } |
| 201 |
185 // Both supported colorspaces have a 32-bit pixels information. | 202 // Both supported colorspaces have a 32-bit pixels information. |
186 if (!EncodeRowFrom32Bit( | 203 if (!EncodeRowFrom32Bit( |
187 current_row, image.size().width(), image.colorspace(), output)) { | 204 current_row, image.size().width(), image.colorspace(), output)) { |
188 return false; | 205 return false; |
189 } | 206 } |
190 } | 207 } |
191 return true; | 208 return true; |
192 } | 209 } |
193 | 210 |
194 } // namespace cloud_print | 211 } // namespace cloud_print |
OLD | NEW |