Ticket #2373: GIL-read-multipage-TIFF.patch

File GIL-read-multipage-TIFF.patch, 9.2 KB (added by Robert Pollak <robert.pollak@…>, 14 years ago)

proposed patch

  • boost/gil/extension/io/tiff_io.hpp

     
    122122protected:
    123123    TIFF *_tp;
    124124public:
    125     tiff_reader(const char* filename) {
     125    tiff_reader(const char* filename, tdir_t dirnum=0) {
    126126        io_error_if((_tp=TIFFOpen(filename,"r"))==NULL,
    127127                    "tiff_reader: fail to open file");
     128        if(dirnum>0) {
     129            io_error_if(TIFFSetDirectory(_tp,dirnum)!=1,
     130                        "tiff_reader: fail to set directory");
     131        }
    128132    }
    129133    ~tiff_reader() { TIFFClose(_tp); }
    130134    template <typename View>
     
    171175private:
    172176    CC _cc;
    173177public:
    174     tiff_reader_color_convert(const char* filename) :
    175         tiff_reader(filename) {}
    176     tiff_reader_color_convert(const char* filename,CC cc_in) :
    177         tiff_reader(filename),_cc(cc_in) {}
     178    tiff_reader_color_convert(const char* filename,tdir_t dirnum=0) :
     179        tiff_reader(filename,dirnum) {}
     180    tiff_reader_color_convert(const char* filename,CC cc_in,tdir_t dirnum=0) :
     181        tiff_reader(filename,dirnum),_cc(cc_in) {}
    178182    template <typename View>
    179183    void apply(const View& view) {
    180184        point2<std::ptrdiff_t> dims=get_dimensions();
     
    336340};
    337341
    338342/// \ingroup TIFF_IO
     343/// \brief Returns the number of pages in the TIFF file
     344inline int tiff_get_count(const char* filename) {
     345    TIFF *tif;
     346    io_error_if((tif=TIFFOpen(filename,"r"))==NULL,
     347                    "tiff_get_count: fail to open file");
     348
     349    int dircount = 0;
     350    do {
     351        dircount++;
     352    } while (TIFFReadDirectory(tif));
     353
     354    TIFFClose(tif);
     355    return dircount;
     356}
     357
     358/// \ingroup TIFF_IO
    339359/// \brief Returns the width and height of the TIFF file at the specified location.
    340360/// Throws std::ios_base::failure if the location does not correspond to a valid TIFF file
    341 inline point2<std::ptrdiff_t> tiff_read_dimensions(const char* filename) {
    342     detail::tiff_reader m(filename);
     361inline point2<std::ptrdiff_t> tiff_read_dimensions(const char* filename,tdir_t dirnum=0) {
     362    detail::tiff_reader m(filename,dirnum);
    343363    return m.get_dimensions();
    344364}
    345365
    346366/// \ingroup TIFF_IO
    347367/// \brief Returns the width and height of the TIFF file at the specified location.
    348368/// Throws std::ios_base::failure if the location does not correspond to a valid TIFF file
    349 inline point2<std::ptrdiff_t> tiff_read_dimensions(const std::string& filename) {
    350     return tiff_read_dimensions(filename.c_str());
     369inline point2<std::ptrdiff_t> tiff_read_dimensions(const std::string& filename,tdir_t dirnum=0) {
     370    return tiff_read_dimensions(filename.c_str(),dirnum);
    351371}
    352372
    353373/// \ingroup TIFF_IO
     
    356376/// Throws std::ios_base::failure if the file is not a valid TIFF file, or if its color space or channel depth are not
    357377/// compatible with the ones specified by View, or if its dimensions don't match the ones of the view.
    358378template <typename View>
    359 inline void tiff_read_view(const char* filename,const View& view) {
     379inline void tiff_read_view(const char* filename,const View& view,tdir_t dirnum=0) {
    360380    BOOST_STATIC_ASSERT(tiff_read_support<View>::is_supported);
    361     detail::tiff_reader m(filename);
     381    detail::tiff_reader m(filename,dirnum);
    362382    m.apply(view);
    363383}
    364384
    365385/// \ingroup TIFF_IO
    366386/// \brief Loads the image specified by the given tiff image file name into the given view.
    367387template <typename View>
    368 inline void tiff_read_view(const std::string& filename,const View& view) {
    369     tiff_read_view(filename.c_str(),view);
     388inline void tiff_read_view(const std::string& filename,const View& view,tdir_t dirnum=0) {
     389    tiff_read_view(filename.c_str(),view,dirnum);
    370390}
    371391
    372392/// \ingroup TIFF_IO
     
    375395/// Throws std::ios_base::failure if the file is not a valid TIFF file, or if its color space or channel depth are not
    376396/// compatible with the ones specified by Image
    377397template <typename Image>
    378 void tiff_read_image(const char* filename,Image& im) {
     398void tiff_read_image(const char* filename,Image& im,tdir_t dirnum=0) {
    379399    BOOST_STATIC_ASSERT(tiff_read_support<typename Image::view_t>::is_supported);
    380     detail::tiff_reader m(filename);
     400    detail::tiff_reader m(filename,dirnum);
    381401    m.read_image(im);
    382402}
    383403
    384404/// \ingroup TIFF_IO
    385405/// \brief Allocates a new image whose dimensions are determined by the given tiff image file, and loads the pixels into it.
    386406template <typename Image>
    387 inline void tiff_read_image(const std::string& filename,Image& im) {
    388     tiff_read_image(filename.c_str(),im);
     407inline void tiff_read_image(const std::string& filename,Image& im,tdir_t dirnum=0) {
     408    tiff_read_image(filename.c_str(),im,dirnum);
    389409}
    390410
    391411/// \ingroup TIFF_IO
    392412/// \brief Loads and color-converts the image specified by the given tiff image file name into the given view.
    393413/// Throws std::ios_base::failure if the file is not a valid TIFF file, or if its dimensions don't match the ones of the view.
    394414template <typename View,typename CC>
    395 inline void tiff_read_and_convert_view(const char* filename,const View& view,CC cc) {
    396     detail::tiff_reader_color_convert<CC> m(filename,cc);
     415inline void tiff_read_and_convert_view(const char* filename,const View& view,CC cc,tdir_t dirnum=0) {
     416    detail::tiff_reader_color_convert<CC> m(filename,cc,dirnum);
    397417    m.apply(view);
    398418}
    399419
     
    401421/// \brief Loads and color-converts the image specified by the given tiff image file name into the given view.
    402422/// Throws std::ios_base::failure if the file is not a valid TIFF file, or if its dimensions don't match the ones of the view.
    403423template <typename View>
    404 inline void tiff_read_and_convert_view(const char* filename,const View& view) {
    405     detail::tiff_reader_color_convert<default_color_converter> m(filename,default_color_converter());
     424inline void tiff_read_and_convert_view(const char* filename,const View& view,tdir_t dirnum=0) {
     425    detail::tiff_reader_color_convert<default_color_converter> m(filename,default_color_converter(),dirnum);
    406426    m.apply(view);
    407427}
    408428
    409429/// \ingroup TIFF_IO
    410430/// \brief Loads and color-converts the image specified by the given tiff image file name into the given view.
    411431template <typename View,typename CC>
    412 inline void tiff_read_and_convert_view(const std::string& filename,const View& view,CC cc) {
    413     tiff_read_and_convert_view(filename.c_str(),view,cc);
     432inline void tiff_read_and_convert_view(const std::string& filename,const View& view,CC cc,tdir_t dirnum=0) {
     433    tiff_read_view(filename.c_str(),view,cc,dirnum);
    414434}
    415435
    416436/// \ingroup TIFF_IO
    417437/// \brief Loads and color-converts the image specified by the given tiff image file name into the given view.
    418438template <typename View>
    419 inline void tiff_read_and_convert_view(const std::string& filename,const View& view) {
    420     tiff_read_and_convert_view(filename.c_str(),view);
     439inline void tiff_read_and_convert_view(const std::string& filename,const View& view,tdir_t dirnum=0) {
     440    tiff_read_view(filename.c_str(),view,dirnum);
    421441}
    422442
    423443/// \ingroup TIFF_IO
    424444/// \brief Allocates a new image whose dimensions are determined by the given tiff image file, loads and color-converts the pixels into it.
    425445/// Throws std::ios_base::failure if the file is not a valid TIFF file
    426446template <typename Image,typename CC>
    427 void tiff_read_and_convert_image(const char* filename,Image& im,CC cc) {
    428     detail::tiff_reader_color_convert<CC> m(filename,cc);
     447void tiff_read_and_convert_image(const char* filename,Image& im,CC cc,tdir_t dirnum=0) {
     448    detail::tiff_reader_color_convert<CC> m(filename,cc,dirnum);
    429449    m.read_image(im);
    430450}
    431451
     
    433453/// \brief Allocates a new image whose dimensions are determined by the given tiff image file, loads and color-converts the pixels into it.
    434454/// Throws std::ios_base::failure if the file is not a valid TIFF file
    435455template <typename Image>
    436 void tiff_read_and_convert_image(const char* filename,Image& im) {
    437     detail::tiff_reader_color_convert<default_color_converter> m(filename,default_color_converter());
     456void tiff_read_and_convert_image(const char* filename,Image& im,tdir_t dirnum=0) {
     457    detail::tiff_reader_color_convert<default_color_converter> m(filename,default_color_converter(),dirnum);
    438458    m.read_image(im);
    439459}
    440460
    441461/// \ingroup TIFF_IO
    442462/// \brief Allocates a new image whose dimensions are determined by the given tiff image file, loads and color-converts the pixels into it.
    443463template <typename Image,typename CC>
    444 inline void tiff_read_and_convert_image(const std::string& filename,Image& im,CC cc) {
    445     tiff_read_and_convert_image(filename.c_str(),im,cc);
     464inline void tiff_read_and_convert_image(const std::string& filename,Image& im,CC cc,tdir_t dirnum=0) {
     465    tiff_read_and_convert_image(filename.c_str(),im,cc,dirnum);
    446466}
    447467
    448468/// \ingroup TIFF_IO
    449469/// \brief Allocates a new image whose dimensions are determined by the given tiff image file, loads and color-converts the pixels into it.
    450470template <typename Image>
    451 inline void tiff_read_and_convert_image(const std::string& filename,Image& im) {
    452     tiff_read_and_convert_image(filename.c_str(),im);
     471inline void tiff_read_and_convert_image(const std::string& filename,Image& im,tdir_t dirnum=0) {
     472    tiff_read_and_convert_image(filename.c_str(),im,dirnum);
    453473}
    454474
    455475/// \ingroup TIFF_IO