static int action_flip (int argc, const char *argv[]) { if (ot.postpone_callback (1, action_flip, argc, argv)) return 0; ot.read (); ImageRecRef A = ot.pop(); ot.push (new ImageRec (*A, ot.allsubimages ? -1 : 0, ot.allsubimages ? -1 : 0, true, false)); int subimages = ot.curimg->subimages(); for (int s = 0; s < subimages; ++s) { int miplevels = ot.curimg->miplevels(s); for (int m = 0; m < miplevels; ++m) { const ImageBuf &Aib ((*A)(s,m)); ImageBuf &Rib ((*ot.curimg)(s,m)); ImageBuf::ConstIterator<float> a (Aib); ImageBuf::Iterator<float> r (Rib); int nchans = Rib.nchannels(); int firstscanline = Rib.ymin(); int lastscanline = Rib.ymax(); for ( ; ! r.done(); ++r) { a.pos (r.x(), lastscanline - (r.y() - firstscanline)); for (int c = 0; c < nchans; ++c) r[c] = a[c]; } } } return 0; }
static int action_add (int argc, const char *argv[]) { if (ot.postpone_callback (2, action_add, argc, argv)) return 0; ImageRecRef B (ot.pop()); ImageRecRef A (ot.pop()); ot.read (A); ot.read (B); ot.push (new ImageRec (*A, ot.allsubimages ? -1 : 0, ot.allsubimages ? -1 : 0, true, false)); int subimages = ot.curimg->subimages(); for (int s = 0; s < subimages; ++s) { int miplevels = ot.curimg->miplevels(s); for (int m = 0; m < miplevels; ++m) { const ImageBuf &Aib ((*A)(s,m)); const ImageBuf &Bib ((*B)(s,m)); if (! same_size (Aib, Bib)) { // FIXME: some day, there should be options of combining // differing images somehow. std::cerr << "oiiotool: " << argv[0] << " could not combine images of differing sizes\n"; continue; } ImageBuf &Rib ((*ot.curimg)(s,m)); ImageBufAlgo::add (Rib, Aib, Bib); } } return 0; }
static int input_file (int argc, const char *argv[]) { for (int i = 0; i < argc; i++) { int exists = 1; if (! ot.imagecache->get_image_info (ustring(argv[0]), 0, 0, ustring("exists"), TypeDesc::TypeInt, &exists) || !exists) { std::cerr << "oiiotool ERROR: Could not open file \"" << argv[0] << "\"\n"; exit (1); } if (ot.verbose) std::cout << "Reading " << argv[0] << "\n"; ot.push (ImageRecRef (new ImageRec (argv[i], ot.imagecache))); if (ot.printinfo || ot.printstats) { OiioTool::print_info_options pio; pio.verbose = ot.verbose; pio.subimages = ot.allsubimages; pio.compute_stats = ot.printstats; pio.compute_sha1 = ot.hash; long long totalsize = 0; std::string error; OiioTool::print_info (argv[i], pio, totalsize, error); } ot.process_pending (); } return 0; }
static std::string compute_sha1 (Oiiotool &ot, ImageInput *input) { SHA1 sha; const ImageSpec &spec (input->spec()); if (spec.deep) { // Special handling of deep data DeepData dd; if (! input->read_native_deep_image (dd)) { ot.error (" SHA-1: unable to compute, could not read image\n"); return std::string(); } // Hash both the sample counts and the data block sha.append (dd.all_samples()); sha.append (dd.all_data()); } else { imagesize_t size = input->spec().image_bytes (true /*native*/); if (size >= std::numeric_limits<size_t>::max()) { ot.error (" SHA-1: unable to compute, image is too big\n"); return std::string(); } else if (size != 0) { boost::scoped_array<char> buf (new char [size]); if (! input->read_image (TypeDesc::UNKNOWN /*native*/, &buf[0])) { ot.error (" SHA-1: unable to compute, could not read image\n"); return std::string(); } sha.append (&buf[0], size); } } return sha.digest().c_str(); }
static int action_colorconvert (int argc, const char *argv[]) { ASSERT (argc == 3); if (ot.postpone_callback (1, action_colorconvert, argc, argv)) return 0; std::string fromspace = argv[1]; std::string tospace = argv[2]; ot.read (); bool need_transform = false; ImageRecRef A = ot.curimg; ot.read (A); for (int s = 0, send = A->subimages(); s < send; ++s) { for (int m = 0, mend = A->miplevels(s); m < mend; ++m) { const ImageSpec *spec = A->spec(s,m); need_transform |= spec->get_string_attribute("oiio:ColorSpace") != tospace; } } if (! need_transform) return 1; // no need to do anything ot.pop (); ot.push (new ImageRec (*A, ot.allsubimages ? -1 : 0, ot.allsubimages ? -1 : 0, true, false)); if (fromspace == "current") fromspace = A->spec(0,0)->get_string_attribute ("oiio:Colorspace", "Linear"); ColorProcessor *processor = ot.colorconfig.createColorProcessor (fromspace.c_str(), tospace.c_str()); if (! processor) return 1; for (int s = 0, send = A->subimages(); s < send; ++s) { for (int m = 0, mend = A->miplevels(s); m < mend; ++m) { ImageBufAlgo::colorconvert ((*ot.curimg)(s,m), (*A)(s,m), processor, false); ot.curimg->spec(s,m)->attribute ("oiio::Colorspace", tospace); } } ot.colorconfig.deleteColorProcessor (processor); return 1; }
static int action_select_subimage (int argc, const char *argv[]) { if (ot.postpone_callback (1, action_select_subimage, argc, argv)) return 0; ot.read (); if (ot.curimg->subimages() == 1) return 0; // --subimage on a single-image file is a no-op int subimage = std::min (atoi(argv[1]), ot.curimg->subimages()); ImageRecRef A = ot.pop(); ot.push (new ImageRec (*A, subimage)); return 0; }
static int action_pop (int argc, const char *argv[]) { ASSERT (argc == 1); ot.pop (); return 0; }
static int action_dup (int argc, const char *argv[]) { ASSERT (argc == 1); ot.push (ot.curimg); return 0; }
static int set_full_to_pixels (int argc, const char *argv[]) { if (ot.postpone_callback (1, set_full_to_pixels, argc, argv)) return 0; ot.read (); ImageRecRef A = ot.curimg; ImageSpec &spec (*A->spec(0,0)); spec.full_x = spec.x; spec.full_y = spec.y; spec.full_width = spec.width; spec.full_height = spec.height; A->metadata_modified (true); return 0; }
static int action_selectmip (int argc, const char *argv[]) { if (ot.postpone_callback (1, action_unmip, argc, argv)) return 0; ot.read (); bool mipmapped = false; for (int s = 0, send = ot.curimg->subimages(); s < send; ++s) mipmapped |= (ot.curimg->miplevels(s) > 1); if (! mipmapped) { return 0; // --selectmip on an unmipped image is a no-op } ImageRecRef newimg (new ImageRec (*ot.curimg, -1, atoi(argv[1]), true, true)); ot.curimg = newimg; return 0; }
static int action_diff (int argc, const char *argv[]) { if (ot.postpone_callback (2, action_diff, argc, argv)) return 0; int ret = do_action_diff (*ot.image_stack.back(), *ot.curimg, ot); if (ret != DiffErrOK && ret != DiffErrWarn) ot.return_value = EXIT_FAILURE; return 0; }
static int action_sub (int argc, const char *argv[]) { if (ot.postpone_callback (2, action_sub, argc, argv)) return 0; ImageRecRef B (ot.pop()); ImageRecRef A (ot.pop()); ot.read (A); ot.read (B); ot.push (new ImageRec (*A, ot.allsubimages ? -1 : 0, ot.allsubimages ? -1 : 0, true, false)); int subimages = ot.curimg->subimages(); for (int s = 0; s < subimages; ++s) { int miplevels = ot.curimg->miplevels(s); for (int m = 0; m < miplevels; ++m) { const ImageBuf &Aib ((*A)(s,m)); const ImageBuf &Bib ((*B)(s,m)); if (! same_size (Aib, Bib)) { // FIXME: some day, there should be options of combining // differing images somehow. std::cerr << "oiiotool: " << argv[0] << " could not combine images of differing sizes\n"; continue; } ImageBuf &Rib ((*ot.curimg)(s,m)); ImageBuf::ConstIterator<float> a (Aib); ImageBuf::ConstIterator<float> b (Bib); ImageBuf::Iterator<float> r (Rib); int nchans = Rib.nchannels(); for ( ; ! r.done(); ++r) { a.pos (r.x(), r.y()); b.pos (r.x(), r.y()); for (int c = 0; c < nchans; ++c) r[c] = a[c] - b[c]; } } } return 0; }
static int set_origin (int argc, const char *argv[]) { if (ot.postpone_callback (1, set_origin, argc, argv)) return 0; ot.read (); ImageRecRef A = ot.curimg; ImageSpec &spec (*A->spec(0,0)); int x = spec.x, y = spec.y; int w = spec.width, h = spec.height; adjust_geometry (w, h, x, y, argv[1]); if (spec.width != w || spec.height != h) std::cerr << argv[0] << " can't be used to change the size, only the origin\n"; if (spec.x != x || spec.y != y) { spec.x = x; spec.y = y; A->metadata_modified (true); } return 0; }
static int set_fullsize (int argc, const char *argv[]) { if (ot.postpone_callback (1, set_fullsize, argc, argv)) return 0; ot.read (); ImageRecRef A = ot.curimg; ImageSpec &spec (*A->spec(0,0)); int x = spec.full_x, y = spec.full_y; int w = spec.full_width, h = spec.full_height; adjust_geometry (w, h, x, y, argv[1]); if (spec.full_x != x || spec.full_y != y || spec.full_width != w || spec.full_height != h) { spec.full_x = x; spec.full_y = y; spec.full_width = w; spec.full_height = h; A->metadata_modified (true); } return 0; }
bool OiioTool::set_attribute (ImageRecRef img, const std::string &attribname, TypeDesc type, const std::string &value) { ot.read (img); img->metadata_modified (true); if (! value.length()) { // If the value is the empty string, clear the attribute return apply_spec_mod (*img, do_erase_attribute, attribname, ot.allsubimages); } // Does it seem to be an int, or did the caller explicitly request // that it be set as an int? char *p = NULL; int i = strtol (value.c_str(), &p, 10); while (*p && isspace(*p)) ++p; if ((! *p && type == TypeDesc::UNKNOWN) || type == TypeDesc::INT) { // int conversion succeeded and accounted for the whole string -- // so set an int attribute. return apply_spec_mod (*img, do_set_any_attribute<int>, std::pair<std::string,int>(attribname,i), ot.allsubimages); } // Does it seem to be a float, or did the caller explicitly request // that it be set as a float? p = NULL; float f = (float)strtod (value.c_str(), &p); while (*p && isspace(*p)) ++p; if ((! *p && type == TypeDesc::UNKNOWN) || type == TypeDesc::FLOAT) { // float conversion succeeded and accounted for the whole string -- // so set a float attribute. return apply_spec_mod (*img, do_set_any_attribute<float>, std::pair<std::string,float>(attribname,f), ot.allsubimages); } // Otherwise, set it as a string attribute return apply_spec_mod (*img, do_set_any_attribute<std::string>, std::pair<std::string,std::string>(attribname,value), ot.allsubimages); }
static void print_stats (Oiiotool &ot, const std::string &filename, const ImageSpec &originalspec, int subimage=0, int miplevel=0, bool indentmip=false) { const char *indent = indentmip ? " " : " "; ImageBuf input; if (! read_input (filename, input, subimage, miplevel)) { ot.error ("stats", input.geterror()); return; } PixelStats stats; if (! computePixelStats (stats, input)) { std::string err = input.geterror(); ot.error ("stats", Strutil::format ("unable to compute: %s", err.empty() ? "unspecified error" : err.c_str())); return; } // The original spec is used, otherwise the bit depth will // be reported incorrectly (as FLOAT) unsigned int maxval = (unsigned int)get_intsample_maxval (originalspec); printf ("%sStats Min: ", indent); for (unsigned int i=0; i<stats.min.size(); ++i) { print_stats_num (stats.min[i], maxval, true); printf (" "); } print_stats_footer (maxval); printf ("\n"); printf ("%sStats Max: ", indent); for (unsigned int i=0; i<stats.max.size(); ++i) { print_stats_num (stats.max[i], maxval, true); printf (" "); } print_stats_footer (maxval); printf ("\n"); printf ("%sStats Avg: ", indent); for (unsigned int i=0; i<stats.avg.size(); ++i) { print_stats_num (stats.avg[i], maxval, false); printf (" "); } print_stats_footer (maxval); printf ("\n"); printf ("%sStats StdDev: ", indent); for (unsigned int i=0; i<stats.stddev.size(); ++i) { print_stats_num (stats.stddev[i], maxval, false); printf (" "); } print_stats_footer (maxval); printf ("\n"); printf ("%sStats NanCount: ", indent); for (unsigned int i=0; i<stats.nancount.size(); ++i) { printf ("%llu ", (unsigned long long)stats.nancount[i]); } printf ("\n"); printf ("%sStats InfCount: ", indent); for (unsigned int i=0; i<stats.infcount.size(); ++i) { printf ("%llu ", (unsigned long long)stats.infcount[i]); } printf ("\n"); printf ("%sStats FiniteCount: ", indent); for (unsigned int i=0; i<stats.finitecount.size(); ++i) { printf ("%llu ", (unsigned long long)stats.finitecount[i]); } printf ("\n"); if (input.deep()) { const DeepData *dd (input.deepdata()); size_t npixels = dd->pixels(); size_t totalsamples = 0, emptypixels = 0; size_t maxsamples = 0, minsamples = std::numeric_limits<size_t>::max(); size_t maxsamples_npixels = 0; float mindepth = std::numeric_limits<float>::max(); float maxdepth = -std::numeric_limits<float>::max(); Imath::V3i maxsamples_pixel(-1,-1,-1), minsamples_pixel(-1,-1,-1); Imath::V3i mindepth_pixel(-1,-1,-1), maxdepth_pixel(-1,-1,-1); Imath::V3i nonfinite_pixel(-1,-1,-1); int nonfinite_pixel_samp(-1), nonfinite_pixel_chan(-1); size_t sampoffset = 0; int nchannels = dd->channels(); int depthchannel = -1; long long nonfinites = 0; for (int c = 0; c < nchannels; ++c) if (Strutil::iequals (originalspec.channelnames[c], "Z")) depthchannel = c; int xend = originalspec.x + originalspec.width; int yend = originalspec.y + originalspec.height; int zend = originalspec.z + originalspec.depth; size_t p = 0; std::vector<size_t> nsamples_histogram; for (int z = originalspec.z; z < zend; ++z) { for (int y = originalspec.y; y < yend; ++y) { for (int x = originalspec.x; x < xend; ++x, ++p) { size_t samples = input.deep_samples (x, y, z); totalsamples += samples; if (samples == maxsamples) ++maxsamples_npixels; if (samples > maxsamples) { maxsamples = samples; maxsamples_pixel.setValue (x, y, z); maxsamples_npixels = 1; } if (samples < minsamples) minsamples = samples; if (samples == 0) ++emptypixels; if (samples >= nsamples_histogram.size()) nsamples_histogram.resize (samples+1, 0); nsamples_histogram[samples] += 1; for (unsigned int s = 0; s < samples; ++s) { for (int c = 0; c < nchannels; ++c) { float d = input.deep_value (x, y, z, c, s); if (! isfinite(d)) { if (nonfinites++ == 0) { nonfinite_pixel.setValue (x, y, z); nonfinite_pixel_samp = s; nonfinite_pixel_chan = c; } } if (depthchannel == c) { if (d < mindepth) { mindepth = d; mindepth_pixel.setValue (x, y, z); } if (d > maxdepth) { maxdepth = d; maxdepth_pixel.setValue (x, y, z); } } } } sampoffset += samples; } } } printf ("%sMin deep samples in any pixel : %llu\n", indent, (unsigned long long)minsamples); printf ("%sMax deep samples in any pixel : %llu\n", indent, (unsigned long long)maxsamples); printf ("%s%llu pixel%s had the max of %llu samples, including (x=%d, y=%d)\n", indent, (unsigned long long)maxsamples_npixels, maxsamples_npixels > 1 ? "s" : "", (unsigned long long)maxsamples, maxsamples_pixel.x, maxsamples_pixel.y); printf ("%sAverage deep samples per pixel: %.2f\n", indent, double(totalsamples)/double(npixels)); printf ("%sTotal deep samples in all pixels: %llu\n", indent, (unsigned long long)totalsamples); printf ("%sPixels with deep samples : %llu\n", indent, (unsigned long long)(npixels-emptypixels)); printf ("%sPixels with no deep samples: %llu\n", indent, (unsigned long long)emptypixels); printf ("%sSamples/pixel histogram:\n", indent); size_t grandtotal = 0; for (size_t i = 0, e = nsamples_histogram.size(); i < e; ++i) grandtotal += nsamples_histogram[i]; size_t binstart = 0, bintotal = 0; for (size_t i = 0, e = nsamples_histogram.size(); i < e; ++i) { bintotal += nsamples_histogram[i]; if (i < 8 || i == (e-1) || OIIO::ispow2(i+1)) { // batch by powers of 2, unless it's a small number if (i == binstart) printf ("%s %3lld ", indent, (long long)i); else printf ("%s %3lld-%3lld", indent, (long long)binstart, (long long)i); printf (" : %8lld (%4.1f%%)\n", (long long)bintotal, (100.0*bintotal)/grandtotal); binstart = i+1; bintotal = 0; } } if (depthchannel >= 0) { printf ("%sMinimum depth was %g at (%d, %d)\n", indent, mindepth, mindepth_pixel.x, mindepth_pixel.y); printf ("%sMaximum depth was %g at (%d, %d)\n", indent, maxdepth, maxdepth_pixel.x, maxdepth_pixel.y); } if (nonfinites > 0) { printf ("%sNonfinite values: %lld, including (x=%d, y=%d, chan=%s, samp=%d)\n", indent, nonfinites, nonfinite_pixel.x, nonfinite_pixel.y, input.spec().channelnames[nonfinite_pixel_chan].c_str(), nonfinite_pixel_samp); } } else { std::vector<float> constantValues(input.spec().nchannels); if (isConstantColor(input, &constantValues[0])) { printf ("%sConstant: Yes\n", indent); printf ("%sConstant Color: ", indent); for (unsigned int i=0; i<constantValues.size(); ++i) { print_stats_num (constantValues[i], maxval, false); printf (" "); } print_stats_footer (maxval); printf ("\n"); } else { printf ("%sConstant: No\n", indent); } if( isMonochrome(input)) { printf ("%sMonochrome: Yes\n", indent); } else { printf ("%sMonochrome: No\n", indent); } } }
static int output_file (int argc, const char *argv[]) { ASSERT (argc == 2 && !strcmp(argv[0],"-o")); std::string filename = argv[1]; if (! ot.curimg.get()) { std::cerr << "oiiotool ERROR: -o " << filename << " did not have any current image to output.\n"; return 0; } if (ot.noclobber && Filesystem::exists(filename)) { std::cerr << "oiiotool ERROR: Output file \"" << filename << "\" already exists, not overwriting.\n"; return 0; } if (ot.verbose) std::cout << "Writing " << argv[1] << "\n"; ImageOutput *out = ImageOutput::create (filename.c_str()); if (! out) { std::cerr << "oiiotool ERROR: " << geterror() << "\n"; return 0; } bool supports_displaywindow = out->supports ("displaywindow"); ot.read (); ImageRecRef saveimg = ot.curimg; ImageRecRef ir (ot.curimg); if (! supports_displaywindow && ot.output_autocrop && (ir->spec()->x != ir->spec()->full_x || ir->spec()->y != ir->spec()->full_y || ir->spec()->width != ir->spec()->full_width || ir->spec()->height != ir->spec()->full_height)) { const char *argv[] = { "croptofull" }; int action_croptofull (int argc, const char *argv[]); // forward decl action_croptofull (1, argv); ir = ot.curimg; } ImageOutput::OpenMode mode = ImageOutput::Create; // initial open for (int s = 0, send = ir->subimages(); s < send; ++s) { for (int m = 0, mend = ir->miplevels(s); m < mend; ++m) { ImageSpec spec = *ir->spec(s,m); adjust_output_options (spec, ot); if (! out->open (filename, spec, mode)) { std::cerr << "oiiotool ERROR: " << out->geterror() << "\n"; return 0; } if (! (*ir)(s,m).write (out)) { std::cerr << "oiiotool ERROR: " << (*ir)(s,m).geterror() << "\n"; return 0; } if (mend > 1) { if (out->supports("mipmap")) { mode = ImageOutput::AppendMIPLevel; // for next level } else if (out->supports("multiimage")) { mode = ImageOutput::AppendSubimage; } else { std::cout << "oiiotool WARNING: " << out->format_name() << " does not support MIP-maps for " << filename << "\n"; break; } } } mode = ImageOutput::AppendSubimage; // for next subimage if (send > 1 && ! out->supports("multiimage")) { std::cout << "oiiotool WARNING: " << out->format_name() << " does not support multiple subimages for " << filename << "\n"; break; } } out->close (); delete out; if (ot.output_adjust_time) { std::string metadatatime = ir->spec(0,0)->get_string_attribute ("DateTime"); std::time_t in_time = ir->time(); if (! metadatatime.empty()) DateTime_to_time_t (metadatatime.c_str(), in_time); boost::filesystem::last_write_time (filename, in_time); } ot.curimg = saveimg; return 0; }