69
69
bool StringToInt(int * ival, const char * str);
71
bool StringToVector(std::vector<int> * ivector, const char * str);
71
73
int main(int argc, const char **argv)
75
77
std::vector<std::string> floatAttrs;
76
78
std::vector<std::string> intAttrs;
77
79
std::vector<std::string> stringAttrs;
80
std::string keepChannels;
81
bool croptofull = false;
79
83
ap.options("ocioconvert -- apply colorspace transform to an image \n\n"
80
84
"usage: ocioconvert [options] inputimage inputcolorspace outputimage outputcolorspace\n\n",
83
87
"--float-attribute %L", &floatAttrs, "name=float pair defining OIIO float attribute",
84
88
"--int-attribute %L", &intAttrs, "name=int pair defining OIIO int attribute",
85
89
"--string-attribute %L", &stringAttrs, "name=string pair defining OIIO string attribute",
90
"--croptofull", &croptofull, "name=Crop or pad to make pixel data region match the \"full\" region",
91
"--ch %s", &keepChannels, "name=Select channels (e.g., \"2,3,4\")",
88
94
if (ap.parse (argc, argv) < 0) {
139
145
f->read_image(OIIO::TypeDesc::TypeFloat, &img[0]);
148
std::vector<int> kchannels;
149
//parse --ch argument
150
if (keepChannels != "" && !StringToVector(&kchannels,keepChannels.c_str()))
152
std::cerr << "Error: --ch: '" << keepChannels << "' should be comma-seperated integers\n";
156
//if kchannels not specified, then keep all channels
157
if (kchannels.size() == 0)
159
kchannels.resize(components);
160
for (int channel=0; channel < components; channel++)
162
kchannels[channel] = channel;
168
imgwidth = spec.full_width;
169
imgheight = spec.full_height;
170
std::cerr << "cropping to " << imgwidth;
171
std::cerr << "x" << imgheight << std::endl;
174
if (croptofull || (int)kchannels.size() < spec.nchannels)
176
// crop down bounding box and ditch all but n channels
177
// img is a flattened 3 dimensional matrix heightxwidthxchannels
178
// fill croppedimg with only the needed pixels
179
std::vector<float> croppedimg;
180
croppedimg.resize(imgwidth*imgheight*kchannels.size());
181
for (int y=0 ; y < spec.height ; y++)
183
for (int x=0 ; x < spec.width; x++)
185
for (int k=0; k < (int)kchannels.size(); k++)
187
int channel = kchannels[k];
188
int current_pixel_y = y + spec.y;
189
int current_pixel_x = x + spec.x;
191
if (current_pixel_y >= 0 &&
192
current_pixel_x >= 0 &&
193
current_pixel_y < imgheight &&
194
current_pixel_x < imgwidth)
196
// get the value at the desired pixel
197
float current_pixel = img[(y*spec.width*components)
198
+ (x*components)+channel];
199
// put in croppedimg.
200
croppedimg[(current_pixel_y*imgwidth*kchannels.size())
201
+ (current_pixel_x*kchannels.size())
202
+ channel] = current_pixel;
207
// redefine the spec so it matches the new bounding box
210
spec.height = imgheight;
211
spec.width = imgwidth;
212
spec.nchannels = (int)(kchannels.size());
213
components = (int)(kchannels.size());