|
| 1 | +/* |
| 2 | + * Copyright 2022 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "celebi.h" |
| 18 | + |
| 19 | +#include <cstddef> |
| 20 | +#include <cstdint> |
| 21 | +#include <cstdio> |
| 22 | +#include <cstdlib> |
| 23 | +#include <iostream> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +#include "wsmeans.h" |
| 27 | +#include "wu.h" |
| 28 | +#include "utils.h" |
| 29 | +#include "pybind11/pybind11.h" |
| 30 | +#include "pybind11/stl.h" |
| 31 | +#define STB_IMAGE_IMPLEMENTATION |
| 32 | +#include "stb_image.h" |
| 33 | + |
| 34 | +namespace python = pybind11; |
| 35 | + |
| 36 | +// std::map<Argb, uint32_t> |
| 37 | +std::map<uint32_t, uint32_t> QuantizeCelebi(const std::vector<std::vector<int>>& pixels, |
| 38 | + int max_colors) { |
| 39 | + if (max_colors > 256) { |
| 40 | + max_colors = 256; |
| 41 | + } |
| 42 | + int pixel_count = pixels.size(); |
| 43 | + |
| 44 | + std::vector<material_color_utilities::Argb> opaque_pixels; |
| 45 | + opaque_pixels.reserve(pixel_count); |
| 46 | + for (int i = 0; i < pixel_count; i++) { |
| 47 | + uint32_t pixel = (pixels[i][0] << 16) | |
| 48 | + (pixels[i][1] << 8) | |
| 49 | + (pixels[i][2]); |
| 50 | + //if (pixels[i].size() > 3 && pixels[i][3] == 255) |
| 51 | + opaque_pixels.push_back(pixel); |
| 52 | + } |
| 53 | + |
| 54 | + std::vector<material_color_utilities::Argb> wu_result = material_color_utilities::QuantizeWu( |
| 55 | + opaque_pixels, max_colors); |
| 56 | + |
| 57 | + material_color_utilities::QuantizerResult result = |
| 58 | + material_color_utilities::QuantizeWsmeans(opaque_pixels, wu_result, max_colors); |
| 59 | + |
| 60 | + return result.color_to_count; |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +std::map<uint32_t, uint32_t> ImageQuantizeCelebi(const char* image_path, const int quality, int max_colors) { |
| 65 | + int width, height, channels; |
| 66 | + std::vector<std::vector<int>> pixel_array = {}; |
| 67 | + unsigned char* pixel_result = stbi_load(image_path, &width, &height, &channels, 4); |
| 68 | + if (!pixel_result) {return QuantizeCelebi(pixel_array, max_colors);} |
| 69 | + pixel_array.reserve( (width * height) / quality ); |
| 70 | + unsigned char* pixel_position; |
| 71 | + int _quality = quality; |
| 72 | + for (int y = 0; y < height; ++y) { |
| 73 | + for (int x = 0; x < width; x = x+_quality) { |
| 74 | + pixel_position = pixel_result + (x + y * width) * 4; |
| 75 | + std::vector<int> current_color = { |
| 76 | + pixel_position[0], pixel_position[1], pixel_position[2]}; |
| 77 | + if (channels > 3) {current_color.push_back(pixel_position[3]);} |
| 78 | + pixel_array.push_back(current_color); |
| 79 | + if (_quality < quality) {_quality = quality;} |
| 80 | + } |
| 81 | + if (y % 2 == 0) { |
| 82 | + _quality = quality / 2; |
| 83 | + } else {_quality = quality;} |
| 84 | + } |
| 85 | + stbi_image_free(pixel_result); |
| 86 | + return QuantizeCelebi(pixel_array, max_colors); |
| 87 | +} |
| 88 | + |
| 89 | + |
| 90 | +PYBIND11_MODULE(celebi, m) { |
| 91 | + m.doc() = "Functions from cpp backend"; |
| 92 | + m.def("QuantizeCelebi", &QuantizeCelebi, "Get dominant colors"); |
| 93 | + m.def("ImageQuantizeCelebi", &ImageQuantizeCelebi, "Get pixel array"); |
| 94 | +} |
0 commit comments