Panda3D
imageInfo.cxx
1 // Filename: imageInfo.cxx
2 // Created by: drose (13Mar03)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "imageInfo.h"
16 #include "pnmImageHeader.h"
17 #include "pystub.h"
18 
19 ////////////////////////////////////////////////////////////////////
20 // Function: ImageInfo::Constructor
21 // Access: Public
22 // Description:
23 ////////////////////////////////////////////////////////////////////
24 ImageInfo::
25 ImageInfo() {
26  set_program_brief("report the size of image files");
27  set_program_description
28  ("This program reads the headers of a series of one or more "
29  "image files and reports the image sizes to standard output.");
30 
31  add_option
32  ("2", "", 0,
33  "Report only images that have a non-power-of-two size in either "
34  "dimension. Images whose dimensions are both a power of two will "
35  "not be mentioned.",
36  &ImageInfo::dispatch_none, &_report_power_2, NULL);
37 }
38 
39 ////////////////////////////////////////////////////////////////////
40 // Function: ImageInfo::run
41 // Access: Public
42 // Description:
43 ////////////////////////////////////////////////////////////////////
44 void ImageInfo::
45 run() {
46  Args::const_iterator ai;
47  for (ai = _filenames.begin(); ai != _filenames.end(); ++ai) {
48  Filename filename = (*ai);
49  PNMImageHeader header;
50  if (!header.read_header(filename)) {
51  // Could not read the image header.
52  if (filename.exists()) {
53  nout << filename << ": could not read image.\n";
54  } else {
55  nout << filename << ": does not exist.\n";
56  }
57  } else {
58  // Successfully read the image header.
59  if (!_report_power_2 ||
60  !is_power_2(header.get_x_size()) ||
61  !is_power_2(header.get_y_size())) {
62  nout << filename << ": " << header.get_x_size() << " x "
63  << header.get_y_size() << " x " << header.get_num_channels()
64  << " (maxval = " << header.get_maxval() << ")\n";
65  }
66  }
67  }
68 }
69 
70 ////////////////////////////////////////////////////////////////////
71 // Function: ImageInfo::handle_args
72 // Access: Protected, Virtual
73 // Description: Does something with the additional arguments on the
74 // command line (after all the -options have been
75 // parsed). Returns true if the arguments are good,
76 // false otherwise.
77 ////////////////////////////////////////////////////////////////////
78 bool ImageInfo::
79 handle_args(ProgramBase::Args &args) {
80  if (args.empty()) {
81  nout << "List one or more image filenames on command line.\n";
82  return false;
83  }
84  _filenames = args;
85 
86  return true;
87 }
88 
89 ////////////////////////////////////////////////////////////////////
90 // Function: ImageInfo::is_power_2
91 // Access: Private
92 // Description: Returns true if the indicated value is a power of 2,
93 // false otherwise.
94 ////////////////////////////////////////////////////////////////////
95 bool ImageInfo::
96 is_power_2(int value) const {
97  return (value & (value - 1)) == 0;
98 }
99 
100 
101 int main(int argc, char *argv[]) {
102  // A call to pystub() to force libpystub.so to be linked in.
103  pystub();
104 
105  ImageInfo prog;
106  prog.parse_command_line(argc, argv);
107  prog.run();
108  return 0;
109 }
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_...
bool read_header(const Filename &filename, PNMFileType *type=NULL, bool report_unknown_type=true)
Opens up the image file and tries to read its header information to determine its size...
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.
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
xelval get_maxval() const
Returns the maximum channel value allowable for any pixel in this image; for instance, 255 for a typical 8-bit-per-channel image.
int get_num_channels() const
Returns the number of channels in the image.
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:28
bool exists() const
Returns true if the filename exists on the disk, false otherwise.
Definition: filename.cxx:1356