Panda3D
imageInfo.cxx
Go to the documentation of this file.
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 
14 #include "imageInfo.h"
15 #include "pnmImageHeader.h"
16 
17 /**
18  *
19  */
20 ImageInfo::
21 ImageInfo() {
22  set_program_brief("report the size of image files");
23  set_program_description
24  ("This program reads the headers of a series of one or more "
25  "image files and reports the image sizes to standard output.");
26 
27  add_option
28  ("2", "", 0,
29  "Report only images that have a non-power-of-two size in either "
30  "dimension. Images whose dimensions are both a power of two will "
31  "not be mentioned.",
32  &ImageInfo::dispatch_none, &_report_power_2, nullptr);
33 }
34 
35 /**
36  *
37  */
38 void ImageInfo::
39 run() {
40  Args::const_iterator ai;
41  for (ai = _filenames.begin(); ai != _filenames.end(); ++ai) {
42  Filename filename = (*ai);
43  PNMImageHeader header;
44  if (!header.read_header(filename)) {
45  // Could not read the image header.
46  if (filename.exists()) {
47  nout << filename << ": could not read image.\n";
48  } else {
49  nout << filename << ": does not exist.\n";
50  }
51  } else {
52  // Successfully read the image header.
53  if (!_report_power_2 ||
54  !is_power_2(header.get_x_size()) ||
55  !is_power_2(header.get_y_size())) {
56  nout << filename << ": " << header.get_x_size() << " x "
57  << header.get_y_size() << " x " << header.get_num_channels()
58  << " (maxval = " << header.get_maxval() << ")\n";
59  }
60  }
61  }
62 }
63 
64 /**
65  * Does something with the additional arguments on the command line (after all
66  * the -options have been parsed). Returns true if the arguments are good,
67  * false otherwise.
68  */
69 bool ImageInfo::
70 handle_args(ProgramBase::Args &args) {
71  if (args.empty()) {
72  nout << "List one or more image filenames on command line.\n";
73  return false;
74  }
75  _filenames = args;
76 
77  return true;
78 }
79 
80 /**
81  * Returns true if the indicated value is a power of 2, false otherwise.
82  */
83 bool ImageInfo::
84 is_power_2(int value) const {
85  return (value & (value - 1)) == 0;
86 }
87 
88 
89 int main(int argc, char *argv[]) {
90  ImageInfo prog;
91  prog.parse_command_line(argc, argv);
92  prog.run();
93  return 0;
94 }
bool read_header(const Filename &filename, PNMFileType *type=nullptr, bool report_unknown_type=true)
Opens up the image file and tries to read its header information to determine its size,...
get_num_channels
Returns the number of channels in the image.
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_...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int get_y_size() const
Returns the number of pixels in the Y direction.
int get_x_size() const
Returns the number of pixels in the X direction.
get_maxval
Returns the maximum channel value allowable for any pixel in this image; for instance,...
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the base class of PNMImage, PNMReader, and PNMWriter.
This program reads the headers of a series of one or more images and reports their sizes to standard ...
Definition: imageInfo.h:25
bool exists() const
Returns true if the filename exists on the disk, false otherwise.
Definition: filename.cxx:1267