| 127 | } |
| 128 | |
| 129 | static bool WriteP4(const Image& image, IWriter& writer) |
| 130 | { |
| 131 | writer.write("P4\n", 3); |
| 132 | WriteNumber(writer, image.width()); |
| 133 | writer.write(' '); |
| 134 | WriteNumber(writer, image.height()); |
| 135 | writer.write('\n'); |
| 136 | |
| 137 | const auto binarize = [&](const Color& c)->bool {return 128000 <= 299 * c.r + 587 * c.g + 114 * c.b; }; |
| 138 | |
| 139 | for (int32 y = 0; y < image.height(); ++y) |
| 140 | { |
| 141 | int32 x = 0; |
| 142 | for (; x + 7 < image.width(); x += 8) |
| 143 | { |
| 144 | const uint8 c = |
| 145 | (binarize(image[y][x]) ? 128 : 0) + |
| 146 | (binarize(image[y][x + 1]) ? 64 : 0) + |
| 147 | (binarize(image[y][x + 2]) ? 32 : 0) + |
| 148 | (binarize(image[y][x + 3]) ? 16 : 0) + |
| 149 | (binarize(image[y][x + 4]) ? 8 : 0) + |
| 150 | (binarize(image[y][x + 5]) ? 4 : 0) + |
| 151 | (binarize(image[y][x + 6]) ? 2 : 0) + |
| 152 | (binarize(image[y][x + 7]) ? 1 : 0); |
| 153 | |
| 154 | writer.write(c); |
| 155 | } |
| 156 | |
| 157 | if (x < image.width()) |
| 158 | { |
| 159 | uint8 c = 0; |
| 160 | for (uint8 i = 128; x < image.width(); ++x, i >>= 1) |
| 161 | { |
| 162 | c += binarize(image[y][x]) ? i : 0; |
| 163 | } |
| 164 | |
| 165 | writer.write(c); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | static bool WriteP5(const Image& image, IWriter& writer) |
| 173 | { |
no test coverage detected