Panda3D
|
This is an abstract base class that defines the interface for writing image files of various types. More...
#include "pnmWriter.h"
Public Types | |
enum | ColorType { CT_invalid = 0, CT_grayscale = 1, CT_two_channel = 2, CT_color = 3, CT_four_channel = 4 } |
typedef pmap< PixelSpec, int > | HistMap |
typedef pvector< PixelSpec > | Palette |
typedef pvector< PixelSpecCount > | PixelCount |
Public Member Functions | |
void | copy_header_from (const PNMImageHeader &header) |
Initializes all the data in the header (x_size, y_size, num_channels, etc.) to the same values indicated in the given header. | |
ColorType | get_color_type () const |
Returns the image type of the image, as an enumerated value. | |
string | get_comment () const |
Gets the user comment from the file. | |
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. | |
PNMFileType * | get_type () const |
Returns a pointer to the PNMFileType object that created this PNMWriter. | |
int | get_x_size () const |
Returns the number of pixels in the X direction. | |
int | get_y_size () const |
Returns the number of pixels in the Y direction. | |
bool | has_alpha () const |
Returns true if the image includes an alpha channel, false otherwise. | |
bool | has_type () const |
Returns true if the PNMImageHeader knows what type it is, false otherwise. | |
bool | is_grayscale () const |
Returns false if the image is a full-color image, and has red, green, and blue components; true if it is a grayscale image and has only a gray component. | |
bool | is_valid () const |
Returns true if the PNMWriter can be used to write data, false if something is wrong. | |
PNMReader * | make_reader (istream *file, bool owns_file=true, const Filename &filename=Filename(), string magic_number=string(), PNMFileType *type=NULL, bool report_unknown_type=true) const |
Returns a newly-allocated PNMReader of the suitable type for reading from the already-opened image file, or NULL if the file cannot be read for some reason. | |
PNMReader * | make_reader (const Filename &filename, PNMFileType *type=NULL, bool report_unknown_type=true) const |
Returns a newly-allocated PNMReader of the suitable type for reading from the indicated image filename, or NULL if the filename cannot be read for some reason. | |
PNMWriter * | make_writer (ostream *file, bool owns_file=true, const Filename &filename=Filename(), PNMFileType *type=NULL) const |
Returns a newly-allocated PNMWriter of the suitable type for writing to the already-opened image file, or NULL if the file cannot be written for some reason. | |
PNMWriter * | make_writer (const Filename &filename, PNMFileType *type=NULL) const |
Returns a newly-allocated PNMWriter of the suitable type for writing an image to the indicated filename, or NULL if the filename cannot be written for some reason. | |
void | output (ostream &out) const |
bool | read_header (istream &data, const string &filename=string(), PNMFileType *type=NULL, bool report_unknown_type=true) |
Reads the image header information only from the indicated stream. | |
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, number of channels, etc. | |
void | set_color_type (ColorType type) |
void | set_comment (const string &comment) |
Writes a user comment string to the image (header). | |
void | set_maxval (xelval maxval) |
void | set_num_channels (int num_channels) |
void | set_type (PNMFileType *type) |
Sets the file type of this PNMImage. | |
void | set_x_size (int x_size) |
void | set_y_size (int y_size) |
virtual bool | supports_stream_write () const |
Returns true if this particular PNMWriter can write to a general stream (including pipes, etc.), or false if the writer must occasionally fseek() on its output stream, and thus only disk streams are supported. | |
virtual bool | supports_write_row () const |
Returns true if this particular PNMWriter supports a streaming interface to writing the data: that is, it is capable of writing the image one row at a time, via repeated calls to write_row(). | |
virtual int | write_data (xel *array, xelval *alpha) |
Writes out an entire image all at once, including the header, based on the image data stored in the given _x_size * _y_size array and alpha pointers. | |
virtual bool | write_header () |
If supports_write_row(), above, returns true, this function may be called to write out the image header in preparation to writing out the image data one row at a time. | |
virtual bool | write_row (xel *array, xelval *alpha) |
If supports_write_row(), above, returns true, this function may be called repeatedly to write the image, one horizontal row at a time, beginning from the top. | |
Static Public Member Functions | |
static bool | has_alpha (ColorType color_type) |
This static variant of has_alpha() returns true if the indicated image type includes an alpha channel, false otherwise. | |
static bool | is_grayscale (ColorType color_type) |
This static variant of is_grayscale() returns true if the indicated image type represents a grayscale image, false otherwise. | |
static bool | read_magic_number (istream *file, string &magic_number, int num_bytes) |
Ensures that the first n bytes of the file are read into magic_number. | |
Protected Member Functions | |
PNMWriter (PNMFileType *type, ostream *file, bool owns_file) | |
bool | compute_histogram (HistMap &hist, xel *array, xelval *alpha, int max_colors=0) |
Computes a histogram of the colors used in the indicated rgb/grayscale array and/or alpha array. | |
bool | compute_palette (Palette &palette, xel *array, xelval *alpha, int max_colors=0) |
Returns a linear list of all of the colors in the image, similar to compute_histogram(). | |
void | record_color (HistMap &hist, const PixelSpec &color) |
Records the indicated color in the histogram. | |
Protected Attributes | |
string | _comment |
ostream * | _file |
bool | _is_valid |
xelval | _maxval |
int | _num_channels |
bool | _owns_file |
PNMFileType * | _type |
int | _x_size |
int | _y_size |
This is an abstract base class that defines the interface for writing image files of various types.
Any particular image file type that can be written must define a class that inherits from PNMWriter to write it.
Definition at line 30 of file pnmWriter.h.
bool PNMImageHeader::compute_histogram | ( | PNMImageHeader::HistMap & | hist, |
xel * | array, | ||
xelval * | alpha, | ||
int | max_colors = 0 |
||
) | [protected, inherited] |
Computes a histogram of the colors used in the indicated rgb/grayscale array and/or alpha array.
This is most likely to be useful in a PNMWriter class, but it is defined at this level in case it has general utilty for PNMImages.
Also see PNMImage::make_histogram(), which is a higher-level function.
The max_colors parameter, if greater than zero, limits the maximum number of colors we are interested in. If we encounter more than this number of colors, the function aborts before completion and returns false; otherwise, it returns true.
Definition at line 450 of file pnmImageHeader.cxx.
References PNMImageHeader::get_color_type(), and PNMImageHeader::record_color().
Referenced by PNMImageHeader::compute_palette(), and PNMImage::make_histogram().
bool PNMImageHeader::compute_palette | ( | PNMImageHeader::Palette & | palette, |
xel * | array, | ||
xelval * | alpha, | ||
int | max_colors = 0 |
||
) | [protected, inherited] |
Returns a linear list of all of the colors in the image, similar to compute_histogram().
Definition at line 506 of file pnmImageHeader.cxx.
References PNMImageHeader::compute_histogram().
void PNMWriter::copy_header_from | ( | const PNMImageHeader & | header | ) | [inline] |
Initializes all the data in the header (x_size, y_size, num_channels, etc.) to the same values indicated in the given header.
This should be done before writing anything to the file.
Definition at line 103 of file pnmWriter.I.
Referenced by PNMImage::write().
PNMImageHeader::ColorType PNMImageHeader::get_color_type | ( | ) | const [inline, inherited] |
Returns the image type of the image, as an enumerated value.
This is really just the number of channels cast to the enumerated type.
Definition at line 76 of file pnmImageHeader.I.
Referenced by PNMImageHeader::compute_histogram(), osxGraphicsPipe::create_cg_image(), PNMImageHeader::has_alpha(), PNMImageHeader::is_grayscale(), and PNMImage::set_color_type().
string PNMImageHeader::get_comment | ( | ) | const [inline, inherited] |
Gets the user comment from the file.
Definition at line 186 of file pnmImageHeader.I.
xelval PNMImageHeader::get_maxval | ( | ) | const [inline, inherited] |
Returns the maximum channel value allowable for any pixel in this image; for instance, 255 for a typical 8-bit-per-channel image.
A pixel with this value is full on.
Definition at line 152 of file pnmImageHeader.I.
Referenced by PNMImage::clamp_val(), DestTextureImage::copy(), PNMImage::copy_channel(), PNMImage::copy_sub_image(), PNMImage::darken_sub_image(), Texture::do_load_one(), Texture::do_read_one(), PNMImage::expand_border(), TexturePlacement::fill_image(), TexturePlacement::fill_swapped_image(), PNMImage::from_val(), PNMImage::lighten_sub_image(), PNMImage::operator+=(), PNMImage::read(), PNMImage::threshold(), PNMImage::to_val(), and ImageFile::write().
int PNMImageHeader::get_num_channels | ( | ) | const [inline, inherited] |
Returns the number of channels in the image.
Definition at line 87 of file pnmImageHeader.I.
Referenced by osxGraphicsPipe::create_cg_image(), Texture::do_load_one(), Texture::do_read_one(), PNMImage::expand_border(), TexturePlacement::fill_image(), TexturePlacement::fill_swapped_image(), Texture::generate_simple_ram_image(), PNMImage::get_channel_val(), ImageFile::read(), PNMImage::read(), PNMImage::remix_channels(), SourceTextureImage::set_header(), and PNMImage::threshold().
PNMFileType * PNMWriter::get_type | ( | ) | const [inline] |
Returns a pointer to the PNMFileType object that created this PNMWriter.
Reimplemented from PNMImageHeader.
Definition at line 37 of file pnmWriter.I.
int PNMImageHeader::get_x_size | ( | ) | const [inline, inherited] |
Returns the number of pixels in the X direction.
This is one more than the largest allowable X coordinate.
Definition at line 164 of file pnmImageHeader.I.
Referenced by PNMImage::alpha_fill_val(), Texture::consider_rescale(), PNMImage::copy_channel(), osxGraphicsPipe::create_cg_image(), Texture::do_load_one(), Texture::do_read_one(), PNMImage::expand_border(), PNMImage::fill_val(), HeightfieldTesselator::generate(), HeightfieldTesselator::get_elevation(), PNMImage::get_read_x_size(), PNMTextGlyph::get_right(), PNMTextGlyph::get_width(), PNMImage::make_grayscale(), PNMTextGlyph::place(), ImageTransformColors::process_image(), PNMImage::quick_filter_from(), ImageFile::read(), PNMImage::remix_channels(), PNMImage::set_color_type(), SourceTextureImage::set_header(), GeoMipTerrain::set_heightfield(), PNMImage::set_maxval(), PNMImage::threshold(), and ImageFile::write().
int PNMImageHeader::get_y_size | ( | ) | const [inline, inherited] |
Returns the number of pixels in the Y direction.
This is one more than the largest allowable Y coordinate.
Definition at line 176 of file pnmImageHeader.I.
Referenced by PNMImage::alpha_fill_val(), Texture::consider_rescale(), PNMImage::copy_channel(), osxGraphicsPipe::create_cg_image(), Texture::do_load_one(), Texture::do_read_one(), PNMImage::expand_border(), PNMImage::fill_val(), HeightfieldTesselator::generate(), PNMTextGlyph::get_bottom(), HeightfieldTesselator::get_elevation(), PNMTextGlyph::get_height(), PNMImage::get_read_y_size(), PNMImage::make_grayscale(), PNMTextGlyph::place(), ImageTransformColors::process_image(), PNMImage::quick_filter_from(), ImageFile::read(), PNMImage::remix_channels(), PNMImage::set_color_type(), SourceTextureImage::set_header(), GeoMipTerrain::set_heightfield(), PNMImage::set_maxval(), PNMImage::threshold(), and ImageFile::write().
bool PNMImageHeader::has_alpha | ( | ) | const [inline, inherited] |
Returns true if the image includes an alpha channel, false otherwise.
Unlike is_grayscale(), if this returns false it is an error to call any of the functions accessing the alpha channel.
Definition at line 139 of file pnmImageHeader.I.
References PNMImageHeader::get_color_type().
Referenced by PNMImage::alpha_fill_val(), PNMImage::blend(), PNMImage::clear(), PNMImage::copy_from(), PNMImage::copy_header_from(), PNMImage::copy_sub_image(), PNMImage::darken_sub_image(), PNMImage::expand_border(), PNMImage::get_xel_a(), PNMImage::lighten_sub_image(), PNMImage::make_grayscale(), PNMImage::make_rgb(), PNMImage::quick_filter_from(), PNMReader::read_data(), PNMImage::set_color_type(), PNMImage::set_maxval(), PNMImage::set_xel_a(), PNMImage::take_from(), and PNMImage::threshold().
bool PNMImageHeader::has_alpha | ( | PNMImageHeader::ColorType | color_type | ) | [inline, static, inherited] |
This static variant of has_alpha() returns true if the indicated image type includes an alpha channel, false otherwise.
Definition at line 126 of file pnmImageHeader.I.
Referenced by PNMImage::blend_sub_image(), PNMImage::copy_sub_image(), PNMImage::darken_sub_image(), Texture::do_read_one(), TexturePlacement::fill_image(), TexturePlacement::fill_swapped_image(), TexturePlacement::flag_error_image(), PNMImage::lighten_sub_image(), PNMTextGlyph::place(), PNMImage::threshold(), and ImageFile::write().
bool PNMImageHeader::has_type | ( | ) | const [inline, inherited] |
Returns true if the PNMImageHeader knows what type it is, false otherwise.
Definition at line 207 of file pnmImageHeader.I.
bool PNMImageHeader::is_grayscale | ( | PNMImageHeader::ColorType | color_type | ) | [inline, static, inherited] |
This static variant of is_grayscale() returns true if the indicated image type represents a grayscale image, false otherwise.
Definition at line 100 of file pnmImageHeader.I.
bool PNMImageHeader::is_grayscale | ( | ) | const [inline, inherited] |
Returns false if the image is a full-color image, and has red, green, and blue components; true if it is a grayscale image and has only a gray component.
(The gray color is actually stored in the blue channel, and the red and green channels are ignored.)
Definition at line 114 of file pnmImageHeader.I.
References PNMImageHeader::get_color_type().
Referenced by PNMImage::add_alpha(), PNMImage::make_grayscale(), PNMImage::remove_alpha(), PNMImage::set_color_type(), and PNMImage::set_maxval().
bool PNMWriter::is_valid | ( | ) | const [inline] |
Returns true if the PNMWriter can be used to write data, false if something is wrong.
Definition at line 114 of file pnmWriter.I.
Referenced by PNMImageHeader::make_writer().
PNMReader * PNMImageHeader::make_reader | ( | const Filename & | filename, |
PNMFileType * | type = NULL , |
||
bool | report_unknown_type = true |
||
) | const [inherited] |
Returns a newly-allocated PNMReader of the suitable type for reading from the indicated image filename, or NULL if the filename cannot be read for some reason.
The filename "-" always stands for standard input. If type is specified, it is a suggestion for the file type to use.
The PNMReader should be deleted when it is no longer needed.
Definition at line 89 of file pnmImageHeader.cxx.
References VirtualFileSystem::get_global_ptr(), and VirtualFileSystem::open_read_file().
Referenced by PNMImage::read(), and PNMImageHeader::read_header().
PNMReader * PNMImageHeader::make_reader | ( | istream * | file, |
bool | owns_file = true , |
||
const Filename & | filename = Filename() , |
||
string | magic_number = string() , |
||
PNMFileType * | type = NULL , |
||
bool | report_unknown_type = true |
||
) | const [inherited] |
Returns a newly-allocated PNMReader of the suitable type for reading from the already-opened image file, or NULL if the file cannot be read for some reason.
owns_file should be set true if the PNMReader is to be considered the owner of the stream pointer (in which case the stream will be deleted on completion, whether successful or not), or false if it should not delete it.
The filename parameter is optional here, since the file has already been opened; it is only used to examine the extension and attempt to guess the file type.
If magic_number is nonempty, it is assumed to represent the first few bytes that have already been read from the file. Some file types may have difficulty if this is more than two bytes.
If type is non-NULL, it is a suggestion for the file type to use.
The PNMReader should be deleted when it is no longer needed.
Definition at line 155 of file pnmImageHeader.cxx.
References VirtualFileSystem::close_read_file(), PNMFileTypeRegistry::get_global_ptr(), VirtualFileSystem::get_global_ptr(), PNMFileTypeRegistry::get_type_from_extension(), PNMReader::is_valid(), PNMFileType::make_reader(), and PNMImageHeader::read_magic_number().
PNMWriter * PNMImageHeader::make_writer | ( | ostream * | file, |
bool | owns_file = true , |
||
const Filename & | filename = Filename() , |
||
PNMFileType * | type = NULL |
||
) | const [inherited] |
Returns a newly-allocated PNMWriter of the suitable type for writing to the already-opened image file, or NULL if the file cannot be written for some reason.
owns_file should be set true if the PNMWriter is to be considered the owner of the stream pointer (in which case the stream will be deleted on completion, whether successful or not), or false if it should not delete it.
The filename parameter is optional here, since the file has already been opened; it is only used to examine the extension and attempt to guess the intended file type.
If type is non-NULL, it is a suggestion for the file type to use.
The PNMWriter should be deleted when it is no longer needed.
Definition at line 343 of file pnmImageHeader.cxx.
References PNMFileTypeRegistry::get_global_ptr(), PNMFileTypeRegistry::get_type_from_extension(), is_valid(), and PNMFileType::make_writer().
PNMWriter * PNMImageHeader::make_writer | ( | const Filename & | filename, |
PNMFileType * | type = NULL |
||
) | const [inherited] |
Returns a newly-allocated PNMWriter of the suitable type for writing an image to the indicated filename, or NULL if the filename cannot be written for some reason.
The filename "-" always stands for standard output. If type is specified, it is a suggestion for the file type to use.
The PNMWriter should be deleted when it is no longer needed.
Definition at line 270 of file pnmImageHeader.cxx.
References Filename::get_extension(), and Filename::open_write().
Referenced by PNMImage::write().
bool PNMImageHeader::read_header | ( | const Filename & | filename, |
PNMFileType * | type = NULL , |
||
bool | report_unknown_type = true |
||
) | [inherited] |
Opens up the image file and tries to read its header information to determine its size, number of channels, etc.
If successful, updates the header information and returns true; otherwise, returns false.
Definition at line 34 of file pnmImageHeader.cxx.
References PNMImageHeader::make_reader().
Referenced by Texture::do_read_one(), SourceTextureImage::read_header(), and GeoMipTerrain::set_heightfield().
bool PNMImageHeader::read_header | ( | istream & | data, |
const string & | filename = string() , |
||
PNMFileType * | type = NULL , |
||
bool | report_unknown_type = true |
||
) | [inherited] |
Reads the image header information only from the indicated stream.
The filename is advisory only, and may be used to suggest a type if it has a known extension.
If type is non-NULL, it is a suggestion for the type of file it is (and a non-NULL type will override any magic number test or filename extension lookup).
Returns true if successful, false on error.
Definition at line 62 of file pnmImageHeader.cxx.
References PNMImageHeader::make_reader().
bool PNMImageHeader::read_magic_number | ( | istream * | file, |
string & | magic_number, | ||
int | num_bytes | ||
) | [static, inherited] |
Ensures that the first n bytes of the file are read into magic_number.
If magic_number is initially nonempty, assumes these represent the first few bytes already extracted. Returns true if successful, false if an end of file or error occurred before num_bytes could be read.
Definition at line 408 of file pnmImageHeader.cxx.
Referenced by PNMImageHeader::make_reader().
void PNMImageHeader::record_color | ( | PNMImageHeader::HistMap & | hist, |
const PixelSpec & | color | ||
) | [inline, protected, inherited] |
Records the indicated color in the histogram.
Definition at line 244 of file pnmImageHeader.I.
Referenced by PNMImageHeader::compute_histogram().
void PNMImageHeader::set_comment | ( | const string & | comment | ) | [inline, inherited] |
Writes a user comment string to the image (header).
Definition at line 196 of file pnmImageHeader.I.
Referenced by DisplayRegion::save_screenshot().
void PNMImageHeader::set_type | ( | PNMFileType * | type | ) | [inline, inherited] |
Sets the file type of this PNMImage.
This will be the default type used when an image is read, if the type cannot be determined by magic number or inferred by extension, or the type used when the image is written, if the type cannot be inferred from the filename extension.
Definition at line 234 of file pnmImageHeader.I.
Referenced by ImageFile::read().
bool PNMWriter::supports_stream_write | ( | ) | const [virtual] |
Returns true if this particular PNMWriter can write to a general stream (including pipes, etc.), or false if the writer must occasionally fseek() on its output stream, and thus only disk streams are supported.
Definition at line 138 of file pnmWriter.cxx.
bool PNMWriter::supports_write_row | ( | ) | const [virtual] |
Returns true if this particular PNMWriter supports a streaming interface to writing the data: that is, it is capable of writing the image one row at a time, via repeated calls to write_row().
Returns false if the only way to write from this file is all at once, via write_data().
Definition at line 85 of file pnmWriter.cxx.
int PNMWriter::write_data | ( | xel * | array, |
xelval * | alpha | ||
) | [virtual] |
Writes out an entire image all at once, including the header, based on the image data stored in the given _x_size * _y_size array and alpha pointers.
(If the image type has no alpha channel, alpha is ignored.) Returns the number of rows correctly written.
It is the user's responsibility to fill in the header data via calls to set_x_size(), set_num_channels(), etc., or copy_header_from(), before calling write_data().
It is important to delete the PNMWriter class after successfully writing the data. Failing to do this may result in some data not getting flushed!
Derived classes need not override this if they instead provide supports_streaming() and write_row(), below.
Definition at line 54 of file pnmWriter.cxx.
References Thread::consider_yield(), write_header(), and write_row().
Referenced by PNMImage::write().
bool PNMWriter::write_header | ( | ) | [virtual] |
If supports_write_row(), above, returns true, this function may be called to write out the image header in preparation to writing out the image data one row at a time.
Returns true if the header is successfully written, false if there is an error.
It is the user's responsibility to fill in the header data via calls to set_x_size(), set_num_channels(), etc., or copy_header_from(), before calling write_header().
Definition at line 104 of file pnmWriter.cxx.
Referenced by write_data().
bool PNMWriter::write_row | ( | xel * | array, |
xelval * | alpha | ||
) | [virtual] |
If supports_write_row(), above, returns true, this function may be called repeatedly to write the image, one horizontal row at a time, beginning from the top.
Returns true if the row is successfully written, false if there is an error.
You must first call write_header() before writing the individual rows. It is also important to delete the PNMWriter class after successfully writing the last row. Failing to do this may result in some data not getting flushed!
Definition at line 124 of file pnmWriter.cxx.
Referenced by write_data().