imageInfo.cxx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file imageInfo.cxx
  10. * @author drose
  11. * @date 2003-03-13
  12. */
  13. #include "imageInfo.h"
  14. #include "pnmImageHeader.h"
  15. /**
  16. *
  17. */
  18. ImageInfo::
  19. ImageInfo() {
  20. set_program_brief("report the size of image files");
  21. set_program_description
  22. ("This program reads the headers of a series of one or more "
  23. "image files and reports the image sizes to standard output.");
  24. add_option
  25. ("2", "", 0,
  26. "Report only images that have a non-power-of-two size in either "
  27. "dimension. Images whose dimensions are both a power of two will "
  28. "not be mentioned.",
  29. &ImageInfo::dispatch_none, &_report_power_2, nullptr);
  30. }
  31. /**
  32. *
  33. */
  34. void ImageInfo::
  35. run() {
  36. Args::const_iterator ai;
  37. for (ai = _filenames.begin(); ai != _filenames.end(); ++ai) {
  38. Filename filename = (*ai);
  39. PNMImageHeader header;
  40. if (!header.read_header(filename)) {
  41. // Could not read the image header.
  42. if (filename.exists()) {
  43. nout << filename << ": could not read image.\n";
  44. } else {
  45. nout << filename << ": does not exist.\n";
  46. }
  47. } else {
  48. // Successfully read the image header.
  49. if (!_report_power_2 ||
  50. !is_power_2(header.get_x_size()) ||
  51. !is_power_2(header.get_y_size())) {
  52. nout << filename << ": " << header.get_x_size() << " x "
  53. << header.get_y_size() << " x " << header.get_num_channels()
  54. << " (maxval = " << header.get_maxval() << ")\n";
  55. }
  56. }
  57. }
  58. }
  59. /**
  60. * Does something with the additional arguments on the command line (after all
  61. * the -options have been parsed). Returns true if the arguments are good,
  62. * false otherwise.
  63. */
  64. bool ImageInfo::
  65. handle_args(ProgramBase::Args &args) {
  66. if (args.empty()) {
  67. nout << "List one or more image filenames on command line.\n";
  68. return false;
  69. }
  70. _filenames = args;
  71. return true;
  72. }
  73. /**
  74. * Returns true if the indicated value is a power of 2, false otherwise.
  75. */
  76. bool ImageInfo::
  77. is_power_2(int value) const {
  78. return (value & (value - 1)) == 0;
  79. }
  80. int main(int argc, char *argv[]) {
  81. ImageInfo prog;
  82. prog.parse_command_line(argc, argv);
  83. prog.run();
  84. return 0;
  85. }