22 set_program_brief(
"change the color of transparent pixels in an image");
23 set_program_description
24 (
"This program is designed to fix the color channels of an " 25 "alpha-cutout image, making the \"color\" of the invisible part of the " 26 "image consistent with the rest of the image. It does this by " 27 "analyzing the RGB values of the image where alpha == 1, and using the " 28 "average of these values as the overall image color, which it then " 29 "applies to the image wherever alpha == 0. When the alpha value is " 30 "neither 1 nor 0, the pixel is ignored.\n\n" 32 "This process is important for applications that filter the texture " 33 "size down by averaging neighboring pixels. If the color under the " 34 "alpha == 0 pixels is very different from the color elsewhere, this " 35 "kind of filtering can have a visible effect on the image's color, even " 39 (
"alpha",
"filename", 0,
40 "Specifies a separate filename that will be used in lieu of the " 41 "alpha channel on the source image. If this file has an alpha " 42 "channel, that alpha channel is used; otherwise, the grayscale " 43 "value of the image is used.",
44 &ImageFixHiddenColor::dispatch_filename,
nullptr, &_alpha_filename);
47 (
"opaque",
"alpha", 0,
48 "Specifies the minimum alpha value (in the range of 0 to 1) for a " 49 "pixel to be considered fully opaque. The default is 1.",
50 &ImageFixHiddenColor::dispatch_double,
nullptr, &_min_opaque_alpha);
53 (
"transparent",
"alpha", 0,
54 "Specifies the maximum alpha value (in the range of 0 to 1) for a " 55 "pixel to be considered fully transparent. The default is 0.",
56 &ImageFixHiddenColor::dispatch_double,
nullptr, &_max_transparent_alpha);
58 _min_opaque_alpha = 1.0;
59 _max_transparent_alpha = 0.0;
65 void ImageFixHiddenColor::
69 if (_alpha_filename.empty()) {
71 if (!_image.has_alpha()) {
72 nout <<
"Image does not have an alpha channel.\n";
79 if (!alpha_image.
read(_alpha_filename)) {
80 nout <<
"Unable to read " << _alpha_filename <<
".\n";
89 for (yi = 0; yi < alpha_image.
get_y_size(); ++yi) {
90 for (xi = 0; xi < alpha_image.
get_x_size(); ++xi) {
97 if (alpha_image.
get_x_size() != _image.get_x_size() ||
98 alpha_image.
get_y_size() != _image.get_y_size()) {
101 alpha_image = scaled;
107 LRGBColor color(0.0, 0.0, 0.0);
109 for (yi = 0; yi < _image.get_y_size(); ++yi) {
110 for (xi = 0; xi < _image.get_x_size(); ++xi) {
111 if (alpha_image.
get_alpha(xi, yi) >= _min_opaque_alpha) {
112 color += _image.get_xel(xi, yi);
118 nout <<
"Image has no opaque pixels.\n";
121 color /= (double)count;
122 nout <<
" average color of " << count <<
" opaque pixels is " << color <<
"\n";
126 for (yi = 0; yi < _image.get_y_size(); ++yi) {
127 for (xi = 0; xi < _image.get_x_size(); ++xi) {
128 if (alpha_image.
get_alpha(xi, yi) <= _max_transparent_alpha) {
129 _image.set_xel(xi, yi, color);
135 nout <<
"Image has no transparent pixels.\n";
138 nout <<
" applied to " << count <<
" transparent pixels.\n";
144 int main(
int argc,
char *argv[]) {
The name of this class derives from the fact that we originally implemented it as a layer on top of t...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual void parse_command_line(int argc, char **argv)
Dispatches on each of the options on the command line, and passes the remaining parameters to handle_...
float get_gray(int x, int y) const
Returns the gray component color at the indicated pixel.
bool read(const Filename &filename, PNMFileType *type=nullptr, bool report_unknown_type=true)
Reads the indicated image filename.
This is the base class for a program that reads an image file, operates on it, and writes another ima...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void quick_filter_from(const PNMImage ©, int xborder=0, int yborder=0)
Resizes from the given image, with a fixed radius of 0.5.
This program repairs an image's RGB values hidden behind an A value of 0.
void add_alpha()
Adds an alpha channel to the image, if it does not already have one.
void set_alpha(int x, int y, float a)
Sets the alpha component color only at the indicated pixel.
void write_image()
Writes the generated to the user's specified output filename.
float get_alpha(int x, int y) const
Returns the alpha component color at the indicated pixel.