15 #include "imageFixHiddenColor.h" 16 #include "string_utils.h" 26 set_program_brief(
"change the color of transparent pixels in an image");
27 set_program_description
28 (
"This program is designed to fix the color channels of an " 29 "alpha-cutout image, making the \"color\" of the invisible part of the " 30 "image consistent with the rest of the image. It does this by " 31 "analyzing the RGB values of the image where alpha == 1, and using the " 32 "average of these values as the overall image color, which it then " 33 "applies to the image wherever alpha == 0. When the alpha value is " 34 "neither 1 nor 0, the pixel is ignored.\n\n" 36 "This process is important for applications that filter the texture " 37 "size down by averaging neighboring pixels. If the color under the " 38 "alpha == 0 pixels is very different from the color elsewhere, this " 39 "kind of filtering can have a visible effect on the image's color, even " 43 (
"alpha",
"filename", 0,
44 "Specifies a separate filename that will be used in lieu of the " 45 "alpha channel on the source image. If this file has an alpha " 46 "channel, that alpha channel is used; otherwise, the grayscale " 47 "value of the image is used.",
48 &ImageFixHiddenColor::dispatch_filename, NULL, &_alpha_filename);
51 (
"opaque",
"alpha", 0,
52 "Specifies the minimum alpha value (in the range of 0 to 1) for a " 53 "pixel to be considered fully opaque. The default is 1.",
54 &ImageFixHiddenColor::dispatch_double, NULL, &_min_opaque_alpha);
57 (
"transparent",
"alpha", 0,
58 "Specifies the maximum alpha value (in the range of 0 to 1) for a " 59 "pixel to be considered fully transparent. The default is 0.",
60 &ImageFixHiddenColor::dispatch_double, NULL, &_max_transparent_alpha);
62 _min_opaque_alpha = 1.0;
63 _max_transparent_alpha = 0.0;
71 void ImageFixHiddenColor::
75 if (_alpha_filename.empty()) {
78 if (!_image.has_alpha()) {
79 nout <<
"Image does not have an alpha channel.\n";
86 if (!alpha_image.
read(_alpha_filename)) {
87 nout <<
"Unable to read " << _alpha_filename <<
".\n";
96 for (yi = 0; yi < alpha_image.
get_y_size(); ++yi) {
97 for (xi = 0; xi < alpha_image.
get_x_size(); ++xi) {
104 if (alpha_image.
get_x_size() != _image.get_x_size() ||
105 alpha_image.
get_y_size() != _image.get_y_size()) {
108 alpha_image = scaled;
116 for (yi = 0; yi < _image.get_y_size(); ++yi) {
117 for (xi = 0; xi < _image.get_x_size(); ++xi) {
118 if (alpha_image.
get_alpha(xi, yi) >= _min_opaque_alpha) {
119 color += _image.get_xel(xi, yi);
125 nout <<
"Image has no opaque pixels.\n";
128 color /= (double)count;
129 nout <<
" average color of " << count <<
" opaque pixels is " << color <<
"\n";
133 for (yi = 0; yi < _image.get_y_size(); ++yi) {
134 for (xi = 0; xi < _image.get_x_size(); ++xi) {
135 if (alpha_image.
get_alpha(xi, yi) <= _max_transparent_alpha) {
136 _image.set_xel(xi, yi, color);
142 nout <<
"Image has no transparent pixels.\n";
145 nout <<
" applied to " << count <<
" transparent pixels.\n";
151 int main(
int argc,
char *argv[]) {
This is the base class for all three-component vectors and points.
The name of this class derives from the fact that we originally implemented it as a layer on top of t...
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=NULL, 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...
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.