~ubuntu-branches/ubuntu/wily/opencolorio/wily

« back to all changes in this revision

Viewing changes to src/apps/ocioconvert/main.cpp

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2014-06-06 09:57:28 UTC
  • mfrom: (1.2.1)
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: package-import@ubuntu.com-20140606095728-67c856ldcl1lj3go
* New upstream release
  - debian/rules: dh_auto_test overridden
  - debian/patches/: patchset updated
    - 0001-Fix_built-in_documentation_dependencies.patch refreshed
* debian/control: Uploader e-mail address updated
* debian/control: S-V 3.9.4 => 3.9.5 (no changes needed)

Show diffs side-by-side

added added

removed removed

Lines of Context:
68
68
 
69
69
bool StringToInt(int * ival, const char * str);
70
70
 
 
71
bool StringToVector(std::vector<int> * ivector, const char * str);
 
72
 
71
73
int main(int argc, const char **argv)
72
74
{
73
75
    ArgParse ap;
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;
78
82
     
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\")",
86
92
               NULL
87
93
               );
88
94
    if (ap.parse (argc, argv) < 0) {
138
144
        
139
145
        f->read_image(OIIO::TypeDesc::TypeFloat, &img[0]);
140
146
        delete f;
 
147
        
 
148
        std::vector<int> kchannels;
 
149
        //parse --ch argument
 
150
        if (keepChannels != "" && !StringToVector(&kchannels,keepChannels.c_str()))
 
151
        {
 
152
            std::cerr << "Error: --ch: '" << keepChannels << "' should be comma-seperated integers\n";
 
153
            exit(1);
 
154
        }
 
155
        
 
156
        //if kchannels not specified, then keep all channels
 
157
        if (kchannels.size() == 0)
 
158
        {
 
159
            kchannels.resize(components);
 
160
            for (int channel=0; channel < components; channel++)
 
161
            {
 
162
                kchannels[channel] = channel;
 
163
            }
 
164
        }
 
165
        
 
166
        if (croptofull)
 
167
        {
 
168
            imgwidth = spec.full_width;
 
169
            imgheight = spec.full_height;
 
170
            std::cerr << "cropping to " << imgwidth;
 
171
            std::cerr << "x" << imgheight << std::endl;
 
172
        }
 
173
        
 
174
        if (croptofull || (int)kchannels.size() < spec.nchannels)
 
175
        {
 
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++)
 
182
            {
 
183
                for (int x=0 ; x < spec.width; x++)
 
184
                {
 
185
                    for (int k=0; k < (int)kchannels.size(); k++)
 
186
                    {
 
187
                        int channel = kchannels[k];
 
188
                        int current_pixel_y = y + spec.y;
 
189
                        int current_pixel_x = x + spec.x;
 
190
                        
 
191
                        if (current_pixel_y >= 0 &&
 
192
                            current_pixel_x >= 0 &&
 
193
                            current_pixel_y < imgheight &&
 
194
                            current_pixel_x < imgwidth)
 
195
                        {
 
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;
 
203
                        }
 
204
                    }
 
205
                }
 
206
            }
 
207
            // redefine the spec so it matches the new bounding box
 
208
            spec.x = 0;
 
209
            spec.y = 0;
 
210
            spec.height = imgheight;
 
211
            spec.width = imgwidth;
 
212
            spec.nchannels = (int)(kchannels.size());
 
213
            components = (int)(kchannels.size());
 
214
            img = croppedimg;
 
215
        }
141
216
    
142
217
    }
143
218
    catch(...)
304
379
    return true;
305
380
}
306
381
 
 
382
bool StringToVector(std::vector<int> * ivector, const char * str)
 
383
{
 
384
    std::stringstream ss(str);
 
385
    int i;
 
386
    while (ss >> i)
 
387
    {
 
388
        ivector->push_back(i);
 
389
        if (ss.peek() == ',')
 
390
        {
 
391
          ss.ignore();
 
392
        }
 
393
    }
 
394
    return ivector->size() != 0;
 
395
}
 
396
 
307
397
 
308
398
 
309
399