/usr/include/vigra/impex.hxx is in libvigraimpex-dev 1.7.1+dfsg1-2ubuntu4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 | /************************************************************************/
/* */
/* Copyright 2001-2002 by Gunnar Kedenburg */
/* */
/* This file is part of the VIGRA computer vision library. */
/* The VIGRA Website is */
/* http://hci.iwr.uni-heidelberg.de/vigra/ */
/* Please direct questions, bug reports, and contributions to */
/* ullrich.koethe@iwr.uni-heidelberg.de or */
/* vigra@informatik.uni-hamburg.de */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files (the "Software"), to deal in the Software without */
/* restriction, including without limitation the rights to use, */
/* copy, modify, merge, publish, distribute, sublicense, and/or */
/* sell copies of the Software, and to permit persons to whom the */
/* Software is furnished to do so, subject to the following */
/* conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the */
/* Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
/* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
/* OTHER DEALINGS IN THE SOFTWARE. */
/* */
/************************************************************************/
/* Modifications by Pablo d'Angelo
* updated to vigra 1.4 by Douglas Wilkins
* as of 18 Febuary 2006:
* - Added import/export of UINT16 and UINT32 image types.
* Modifications by Andrew Mihal
* updated to vigra 1.4 by Douglas Wilkins
* as of 18 Febuary 2006:
* - Moved some RowIterator declarations around to avoid using default ctors
* (cachedfileimages do not have default ctors for row iterators).
* - Added some case-specific optimizations
*/
/*!
\file impex.hxx
\brief image import and export functions
this file provides the declarations and implementations of importImage()
and exportImage(). the matching implementation for the given datatype is
selected by template metacode.
*/
#ifndef VIGRA_IMPEX_HXX
#define VIGRA_IMPEX_HXX
#include "sized_int.hxx"
#include "stdimage.hxx"
#include "tinyvector.hxx"
#include "imageinfo.hxx"
#include "numerictraits.hxx"
#include "codec.hxx"
#include "accessor.hxx"
#include "inspectimage.hxx"
#include "transformimage.hxx"
#include "copyimage.hxx"
#include "multi_array.hxx"
// TODO
// next refactoring: pluggable conversion algorithms
namespace vigra
{
/** \addtogroup VigraImpex
**/
//@{
/*!
\brief used for reading bands after the source data type has been figured out.
<b>\#include</b> \<<a href="impex_8hxx-source.html">vigra/impex.hxx</a>\><br>
Namespace: vigra
<b> Declaration:</b>
\code
namespace vigra {
template< class ImageIterator, class Accessor, class SrcValueType >
void read_bands( Decoder * dec, ImageIterator ys, Accessor a, SrcValueType )
}
\endcode
\param dec decoder object through which the source data will be accessed
\param ys image iterator referencing the upper left pixel of the destination image
\param a image accessor for the destination image
*/
template< class ImageIterator, class Accessor, class SrcValueType >
void read_bands( Decoder * dec, ImageIterator ys, Accessor a, SrcValueType )
{
typedef unsigned int size_type;
typedef typename ImageIterator::row_iterator DstRowIterator;
typedef typename Accessor::value_type AccessorValueType;
typedef typename AccessorValueType::value_type DstValueType;
const size_type width = dec->getWidth();
const size_type height = dec->getHeight();
const size_type num_bands = dec->getNumBands();
vigra_precondition(num_bands == (size_type)a.size(ys),
"importImage(): number of bands (color channels) in file and destination image differ.");
SrcValueType const * scanline;
// MIHAL no default constructor available for cachedfileimages.
DstRowIterator xs = ys.rowIterator();
// iterate
if (num_bands == 4) {
// Speedup for this particular case
unsigned int offset = dec->getOffset();
SrcValueType const * scanline0;
SrcValueType const * scanline1;
SrcValueType const * scanline2;
SrcValueType const * scanline3;
for( size_type y = 0; y < height; ++y, ++ys.y ) {
dec->nextScanline();
xs = ys.rowIterator();
scanline0 = static_cast< SrcValueType const * >
(dec->currentScanlineOfBand(0));
scanline1 = static_cast< SrcValueType const * >
(dec->currentScanlineOfBand(1));
scanline2 = static_cast< SrcValueType const * >
(dec->currentScanlineOfBand(2));
scanline3 = static_cast< SrcValueType const * >
(dec->currentScanlineOfBand(3));
for( size_type x = 0; x < width; ++x, ++xs ) {
/*
a.template setComponent<SrcValueType, DstRowIterator, 0>( *scanline0, xs );
a.template setComponent<SrcValueType, DstRowIterator, 1>( *scanline1, xs );
a.template setComponent<SrcValueType, DstRowIterator, 2>( *scanline2, xs );
a.template setComponent<SrcValueType, DstRowIterator, 3>( *scanline3, xs );
*/
a.setComponent( *scanline0, xs, 0);
a.setComponent( *scanline1, xs, 1);
a.setComponent( *scanline2, xs, 2);
a.setComponent( *scanline3, xs, 3);
scanline0 += offset;
scanline1 += offset;
scanline2 += offset;
scanline3 += offset;
}
}
}
else {
// General case
for( size_type y = 0; y < height; ++y, ++ys.y ) {
dec->nextScanline();
for( size_type b = 0; b < num_bands; ++b ) {
xs = ys.rowIterator();
scanline = static_cast< SrcValueType const * >
(dec->currentScanlineOfBand(b));
for( size_type x = 0; x < width; ++x, ++xs ) {
a.setComponent( *scanline, xs, b );
scanline += dec->getOffset();
}
}
}
}
} // read_bands()
/*!
\brief used for reading bands after the source data type has been figured out.
<b>\#include</b> \<<a href="impex_8hxx-source.html">vigra/impex.hxx</a>\><br>
Namespace: vigra
<b> Declaration:</b>
\code
namespace vigra {
template< class ImageIterator, class Accessor, class SrcValueType >
void read_band( Decoder * dec, ImageIterator ys, Accessor a, SrcValueType )
}
\endcode
\param dec decoder object through which the source data will be accessed
\param ys image iterator referencing the upper left pixel of the destination image
\param a image accessor for the destination image
*/
template< class ImageIterator, class Accessor, class SrcValueType >
void read_band( Decoder * dec, ImageIterator ys, Accessor a, SrcValueType )
{
typedef unsigned int size_type;
typedef typename ImageIterator::row_iterator DstRowIterator;
typedef typename Accessor::value_type DstValueType;
const size_type width = dec->getWidth();
const size_type height = dec->getHeight();
SrcValueType const * scanline;
// MIHAL no default constructor available for cachedfileimages.
DstRowIterator xs = ys.rowIterator();
for( size_type y = 0; y < height; ++y, ++ys.y ) {
dec->nextScanline();
xs = ys.rowIterator();
scanline = static_cast< SrcValueType const * >(dec->currentScanlineOfBand(0));
for( size_type x = 0; x < width; ++x, ++xs )
a.set( scanline[x], xs );
}
} // read_band()
/*!
\brief used for reading images of vector type, such as integer of float rgb.
<b>\#include</b> \<<a href="impex_8hxx-source.html">vigra/impex.hxx</a>\><br>
Namespace: vigra
<b> Declaration:</b>
\code
namespace vigra {
template< class ImageIterator, class Accessor >
void importVectorImage( const ImageImportInfo & info, ImageIterator iter, Accessor a )
}
\endcode
<b> Paramters:</b>
<DL>
<DT>ImageIterator<DD> the image iterator type for the destination image
<DT>Accessor<DD> the image accessor type for the destination image
<DT>info<DD> user supplied image import information
<DT>iter<DD> image iterator referencing the upper left pixel of the destination image
<DT>a<DD> image accessor for the destination image
</DL>
*/
doxygen_overloaded_function(template <...> void importVectorImage)
template< class ImageIterator, class Accessor >
void importVectorImage( const ImageImportInfo & info, ImageIterator iter, Accessor a )
{
std::auto_ptr<Decoder> dec = decoder(info);
std::string pixeltype = dec->getPixelType();
if ( pixeltype == "UINT8" )
read_bands( dec.get(), iter, a, (UInt8)0 );
else if ( pixeltype == "INT16" )
read_bands( dec.get(), iter, a, Int16() );
else if ( pixeltype == "UINT16" )
read_bands( dec.get(), iter, a, (UInt16)0 );
else if ( pixeltype == "INT32" )
read_bands( dec.get(), iter, a, Int32() );
else if ( pixeltype == "UINT32" )
read_bands( dec.get(), iter, a, (UInt32)0 );
else if ( pixeltype == "FLOAT" )
read_bands( dec.get(), iter, a, float() );
else if ( pixeltype == "DOUBLE" )
read_bands( dec.get(), iter, a, double() );
else
vigra_precondition( false, "invalid pixeltype" );
// close the decoder
dec->close();
}
/*!
\brief used for reading images of scalar type, such as integer and float grayscale.
<b>\#include</b> \<<a href="impex_8hxx-source.html">vigra/impex.hxx</a>\><br>
Namespace: vigra
<b> Declaration:</b>
\code
namespace vigra {
template < class ImageIterator, class Accessor >
void importScalarImage( const ImageImportInfo & info, ImageIterator iter, Accessor a )
}
\endcode
<b> Paramters:</b>
<DL>
<DT>ImageIterator<DD> the image iterator type for the destination image
<DT>Accessor<DD> the image accessor type for the destination image
<DT>info<DD> user supplied image import information
<DT>iter<DD> image iterator referencing the upper left pixel of the destination image
<DT>a<DD> image accessor for the destination image
</DL>
*/
doxygen_overloaded_function(template <...> void importScalarImage)
template < class ImageIterator, class Accessor >
void importScalarImage( const ImageImportInfo & info, ImageIterator iter, Accessor a )
{
std::auto_ptr<Decoder> dec = decoder(info);
std::string pixeltype = dec->getPixelType();
if ( pixeltype == "UINT8" )
read_band( dec.get(), iter, a, (UInt8)0 );
else if ( pixeltype == "INT16" )
read_band( dec.get(), iter, a, Int16() );
else if ( pixeltype == "UINT16" )
read_band( dec.get(), iter, a, (UInt16)0 );
else if ( pixeltype == "INT32" )
read_band( dec.get(), iter, a, Int32() );
else if ( pixeltype == "UINT32" )
read_band( dec.get(), iter, a, (UInt32)0 );
else if ( pixeltype == "FLOAT" )
read_band( dec.get(), iter, a, float() );
else if ( pixeltype == "DOUBLE" )
read_band( dec.get(), iter, a, double() );
else
vigra_precondition( false, "invalid pixeltype" );
// close the decoder
dec->close();
}
/********************************************************/
/* */
/* importImage */
/* */
/********************************************************/
/** \brief Read the image specified by the given \ref vigra::ImageImportInfo object.
<b> Declarations:</b>
pass arguments explicitly:
\code
namespace vigra {
template <class ImageIterator, class Accessor>
void
importImage(ImageImportInfo const & image, ImageIterator iter, Accessor a)
}
\endcode
use argument objects in conjunction with \ref ArgumentObjectFactories :
\code
namespace vigra {
template <class ImageIterator, class Accessor>
inline void
importImage(ImageImportInfo const & image, pair<ImageIterator, Accessor> dest)
}
\endcode
<b> Usage:</b>
<b>\#include</b> \<<a href="impex_8hxx-source.html">vigra/impex.hxx</a>\><br>
Namespace: vigra
\code
vigra::ImageImportInfo info("myimage.gif");
if(info.isGrayscale())
{
// create byte image of appropriate size
vigra::BImage in(info.width(), info.height());
vigra::importImage(info, destImage(in)); // read the image
...
}
else
{
// create byte RGB image of appropriate size
vigra::BRGBImage in(info.width(), info.height());
vigra::importImage(info, destImage(in)); // read the image
...
}
\endcode
<b> Preconditions:</b>
<UL>
<LI> the image file must be readable
<LI> the file type must be one of
<DL>
<DT>"BMP"<DD> Microsoft Windows bitmap image file.
<DT>"GIF"<DD> CompuServe graphics interchange format; 8-bit color.
<DT>"JPEG"<DD> Joint Photographic Experts Group JFIF format; compressed 24-bit color. (only available if libjpeg is installed)
<DT>"PNG"<DD> Portable Network Graphic. (only available if libpng is installed)
<DT>"PBM"<DD> Portable bitmap format (black and white).
<DT>"PGM"<DD> Portable graymap format (gray scale).
<DT>"PNM"<DD> Portable anymap.
<DT>"PPM"<DD> Portable pixmap format (color).
<DT>"SUN"<DD> SUN Rasterfile.
<DT>"TIFF"<DD> Tagged Image File Format. (only available if libtiff is installed.)
<DT>"VIFF"<DD> Khoros Visualization image file.
</DL>
</UL>
**/
doxygen_overloaded_function(template <...> void importImage)
template < class ImageIterator, class Accessor >
void importImage( const ImageImportInfo & info, ImageIterator iter, Accessor a )
{
typedef typename NumericTraits<typename Accessor::value_type>::isScalar is_scalar;
importImage( info, iter, a, is_scalar() );
}
template < class ImageIterator, class Accessor >
void importImage( const ImageImportInfo & info, pair< ImageIterator, Accessor > dest )
{
importImage( info, dest.first, dest.second );
}
template < class ImageIterator, class Accessor >
void importImage( const ImageImportInfo & info, ImageIterator iter, Accessor a, VigraFalseType )
{
importVectorImage( info, iter, a );
}
template < class ImageIterator, class Accessor >
void importImage( const ImageImportInfo & info, ImageIterator iter, Accessor a, VigraTrueType )
{
importScalarImage( info, iter, a );
}
/*!
\brief used for writing bands after the source data type has been figured out.
<b>\#include</b> \<<a href="impex_8hxx-source.html">vigra/impex.hxx</a>\><br>
Namespace: vigra
<b> Declaration:</b>
\code
namespace vigra {
template< class ImageIterator, class Accessor, class DstValueType >
void write_bands( Encoder * enc, ImageIterator ul, ImageIterator lr, Accessor a, DstValueType )
}
\endcode
\param enc encoder object through which the destination data will be accessed
\param ul image iterator referencing the upper left pixel of the source image
\param lr image iterator referencing the lower right pixel of the source image
\param a image accessor for the source image
*/
template< class ImageIterator, class Accessor, class DstValueType >
void write_bands( Encoder * enc, ImageIterator ul, ImageIterator lr, Accessor a, DstValueType)
{
typedef unsigned int size_type;
typedef typename ImageIterator::row_iterator SrcRowIterator;
typedef typename Accessor::value_type AccessorValueType;
typedef typename AccessorValueType::value_type SrcValueType;
// complete decoder settings
const size_type width = lr.x - ul.x;
const size_type height = lr.y - ul.y;
enc->setWidth(width);
enc->setHeight(height);
const size_type num_bands = a.size(ul);
enc->setNumBands(num_bands);
enc->finalizeSettings();
DstValueType * scanline;
// iterate
ImageIterator ys(ul);
// MIHAL no default constructor available for cachedfileimages
SrcRowIterator xs = ys.rowIterator();
// Speedup for the common cases
switch (num_bands)
{
case 2:
{
unsigned int offset = enc->getOffset();
DstValueType * scanline0;
DstValueType * scanline1;
for( size_type y = 0; y < height; ++y, ++ys.y ) {
xs = ys.rowIterator();
scanline0 = static_cast< DstValueType * >
(enc->currentScanlineOfBand(0));
scanline1 = static_cast< DstValueType * >
(enc->currentScanlineOfBand(1));
for( size_type x = 0; x < width; ++x, ++xs) {
*scanline0 = detail::RequiresExplicitCast<DstValueType>::cast(a.getComponent( xs, 0));
*scanline1 = detail::RequiresExplicitCast<DstValueType>::cast(a.getComponent( xs, 1));
scanline0 += offset;
scanline1 += offset;
}
enc->nextScanline();
}
break;
}
case 3:
{
unsigned int offset = enc->getOffset();
DstValueType * scanline0;
DstValueType * scanline1;
DstValueType * scanline2;
for( size_type y = 0; y < height; ++y, ++ys.y ) {
xs = ys.rowIterator();
scanline0 = static_cast< DstValueType * >
(enc->currentScanlineOfBand(0));
scanline1 = static_cast< DstValueType * >
(enc->currentScanlineOfBand(1));
scanline2 = static_cast< DstValueType * >
(enc->currentScanlineOfBand(2));
for( size_type x = 0; x < width; ++x, ++xs) {
*scanline0 = detail::RequiresExplicitCast<DstValueType>::cast(a.getComponent( xs, 0));
*scanline1 = detail::RequiresExplicitCast<DstValueType>::cast(a.getComponent( xs, 1));
*scanline2 = detail::RequiresExplicitCast<DstValueType>::cast(a.getComponent( xs, 2));
scanline0 += offset;
scanline1 += offset;
scanline2 += offset;
}
enc->nextScanline();
}
break;
}
case 4:
{
unsigned int offset = enc->getOffset();
DstValueType * scanline0;
DstValueType * scanline1;
DstValueType * scanline2;
DstValueType * scanline3;
for( size_type y = 0; y < height; ++y, ++ys.y ) {
xs = ys.rowIterator();
scanline0 = static_cast< DstValueType * >
(enc->currentScanlineOfBand(0));
scanline1 = static_cast< DstValueType * >
(enc->currentScanlineOfBand(1));
scanline2 = static_cast< DstValueType * >
(enc->currentScanlineOfBand(2));
scanline3 = static_cast< DstValueType * >
(enc->currentScanlineOfBand(3));
for( size_type x = 0; x < width; ++x, ++xs) {
*scanline0 = detail::RequiresExplicitCast<DstValueType>::cast(a.getComponent( xs, 0));
*scanline1 = detail::RequiresExplicitCast<DstValueType>::cast(a.getComponent( xs, 1));
*scanline2 = detail::RequiresExplicitCast<DstValueType>::cast(a.getComponent( xs, 2));
*scanline3 = detail::RequiresExplicitCast<DstValueType>::cast(a.getComponent( xs, 3));
scanline0 += offset;
scanline1 += offset;
scanline2 += offset;
scanline3 += offset;
}
enc->nextScanline();
}
break;
}
default:
{
// General case
for( size_type y = 0; y < height; ++y, ++ys.y ) {
for( size_type b = 0; b < num_bands; ++b ) {
xs = ys.rowIterator();
scanline = static_cast< DstValueType * >
(enc->currentScanlineOfBand(b));
for( size_type x = 0; x < width; ++x, ++xs ) {
*scanline = detail::RequiresExplicitCast<DstValueType>::cast(a.getComponent( xs, b ));
scanline += enc->getOffset();
}
}
enc->nextScanline();
}
}
}
} // write_bands()
template< class MArray, class DstValueType >
void write_bands( Encoder * enc, MArray const & array, DstValueType)
{
typedef unsigned int size_type;
// complete decoder settings
const size_type width = array.shape(0);
const size_type height = array.shape(1);
enc->setWidth(width);
enc->setHeight(height);
const size_type num_bands = array.shape(2);
enc->setNumBands(num_bands);
enc->finalizeSettings();
DstValueType * scanline;
// iterate
for( size_type y = 0; y < height; ++y ) {
for( size_type b = 0; b < num_bands; ++b ) {
scanline = static_cast< DstValueType * >
(enc->currentScanlineOfBand(b));
for( size_type x = 0; x < width; ++x) {
*scanline = array(x, y, b);
scanline += enc->getOffset();
}
}
enc->nextScanline();
}
} // write_bands()
/*!
\brief used for writing bands after the source data type has been figured out.
<b>\#include</b> \<<a href="impex_8hxx-source.html">vigra/impex.hxx</a>\><br>
Namespace: vigra
<b> Declaration:</b>
\code
namespace vigra {
template< class ImageIterator, class Accessor, class DstValueType >
void write_band( Encoder * enc, ImageIterator ul, ImageIterator lr, Accessor a, DstValueType )
}
\endcode
\param enc encoder object through which the destination data will be accessed
\param ul image iterator referencing the upper left pixel of the source image
\param lr image iterator referencing the lower right pixel of the source image
\param a image accessor for the source image
*/
template< class ImageIterator, class Accessor, class DstValueType >
void write_band( Encoder * enc, ImageIterator ul, ImageIterator lr, Accessor a, DstValueType)
{
typedef unsigned int size_type;
typedef typename ImageIterator::row_iterator SrcRowIterator;
typedef typename Accessor::value_type SrcValueType;
// complete decoder settings
const size_type width = size_type(lr.x - ul.x);
const size_type height = size_type(lr.y - ul.y);
enc->setWidth(width);
enc->setHeight(height);
enc->setNumBands(1);
enc->finalizeSettings();
DstValueType * scanline;
// iterate
ImageIterator ys(ul);
// MIHAL no default constructor available for cachedfileimages.
SrcRowIterator xs = ys.rowIterator();
size_type y;
for( y = 0; y < height; ++y, ++ys.y ) {
xs = ys.rowIterator();
scanline = static_cast< DstValueType * >(enc->currentScanlineOfBand(0));
for( size_type x = 0; x < width; ++x, ++xs, ++scanline )
*scanline = detail::RequiresExplicitCast<DstValueType>::cast(a(xs));
enc->nextScanline();
}
} // write_band()
namespace detail {
// export scalar images without conversion
template < class SrcIterator, class SrcAccessor, class T >
void exportScalarImage(SrcIterator sul, SrcIterator slr, SrcAccessor sget,
Encoder * enc, T zero)
{
write_band( enc, sul, slr, sget, zero );
}
// export scalar images with conversion
template < class SrcIterator, class SrcAccessor, class T >
void exportScalarImage(SrcIterator sul, SrcIterator slr, SrcAccessor sget,
Encoder * enc,
const ImageExportInfo & info,
T zero)
{
double fromMin, fromMax, toMin, toMax;
if(info.getFromMin() < info.getFromMax())
{
fromMin = info.getFromMin();
fromMax = info.getFromMax();
}
else
{
typedef typename SrcAccessor::value_type SrcValue;
FindMinMax<SrcValue> minmax;
inspectImage( sul, slr, sget, minmax );
fromMin = (double)minmax.min;
fromMax = (double)minmax.max;
if(fromMax <= fromMin)
fromMax = fromMin + 1.0;
}
if(info.getToMin() < info.getToMax())
{
toMin = info.getToMin();
toMax = info.getToMax();
}
else
{
toMin = (double)NumericTraits<T>::min();
toMax = (double)NumericTraits<T>::max();
}
double scale = (toMax - toMin) / (fromMax - fromMin);
double offset = (toMin / scale) - fromMin;
BasicImage<T> image(slr-sul);
transformImage( sul, slr, sget, image.upperLeft(), image.accessor(),
linearIntensityTransform(scale, offset));
write_band( enc, image.upperLeft(),
image.lowerRight(), image.accessor(), zero );
}
// export vector images without conversion
template < class SrcIterator, class SrcAccessor, class T >
void exportVectorImage(SrcIterator sul, SrcIterator slr, SrcAccessor sget,
Encoder * enc, T zero)
{
int bands = sget.size(sul);
vigra_precondition(isBandNumberSupported(enc->getFileType(), bands),
"exportImage(): file format does not support requested number of bands (color channels)");
write_bands( enc, sul, slr, sget, zero );
}
// export vector images with conversion
template < class SrcIterator, class SrcAccessor, class T >
void exportVectorImage(SrcIterator sul, SrcIterator slr, SrcAccessor sget,
Encoder * enc,
const ImageExportInfo & info,
T zero)
{
unsigned int bands = sget.size(sul);
vigra_precondition(isBandNumberSupported(enc->getFileType(), bands),
"exportImage(): file format does not support requested number of bands (color channels)");
typedef typename SrcAccessor::ElementAccessor SrcElementAccessor;
typedef typename SrcElementAccessor::value_type SrcComponent;
double fromMin, fromMax, toMin, toMax;
if(info.getFromMin() < info.getFromMax())
{
fromMin = info.getFromMin();
fromMax = info.getFromMax();
}
else
{
FindMinMax<SrcComponent> minmax;
for(unsigned int i=0; i<bands; ++i)
{
SrcElementAccessor band(i, sget);
inspectImage( sul, slr, band, minmax );
}
fromMin = (double)minmax.min;
fromMax = (double)minmax.max;
if(fromMax <= fromMin)
fromMax = fromMin + 1.0;
}
if(info.getToMin() < info.getToMax())
{
toMin = info.getToMin();
toMax = info.getToMax();
}
else
{
toMin = (double)NumericTraits<T>::min();
toMax = (double)NumericTraits<T>::max();
}
double scale = (toMax - toMin) / (fromMax - fromMin);
double offset = (toMin / scale) - fromMin;
int w = slr.x - sul.x;
int h = slr.y - sul.y;
typedef vigra::MultiArray<3, T> MArray;
MArray array(typename MArray::difference_type(w, h, bands));
for(unsigned int i=0; i<bands; ++i)
{
BasicImageView<T> subImage = makeBasicImageView(array.bindOuter(i));
SrcElementAccessor band(i, sget);
transformImage( sul, slr, band, subImage.upperLeft(), subImage.accessor(),
linearIntensityTransform( scale, offset ) );
}
write_bands( enc, array, zero );
}
} // namespace detail
/*!
\brief Deprecated.
Use \ref exportImage() instead.
<b> Declaration:</b>
\code
namespace vigra {
template < class SrcIterator, class SrcAccessor >
void exportFloatingVectorImage( SrcIterator sul, SrcIterator slr, SrcAccessor sget,
const ImageExportInfo & info )
}
\endcode
*/
doxygen_overloaded_function(template <...> void exportFloatingVectorImage)
template < class SrcIterator, class SrcAccessor >
void exportFloatingVectorImage( SrcIterator sul, SrcIterator slr, SrcAccessor sget,
const ImageExportInfo & info )
{
exportImage(sul, slr, sget, info);
}
/*!
\brief Deprecated.
Use \ref exportImage() instead.
<b> Declaration:</b>
\code
namespace vigra {
template < class SrcIterator, class SrcAccessor >
void exportIntegralVectorImage( SrcIterator sul, SrcIterator slr, SrcAccessor sget,
const ImageExportInfo & info )
}
\endcode
*/
doxygen_overloaded_function(template <...> void exportIntegralVectorImage)
template < class SrcIterator, class SrcAccessor >
void exportIntegralVectorImage( SrcIterator sul, SrcIterator slr, SrcAccessor sget,
const ImageExportInfo & info )
{
exportImage(sul, slr, sget, info);
}
/*!
\brief Deprecated.
Use \ref exportImage() instead.
<b> Declaration:</b>
\code
namespace vigra {
template < class SrcIterator, class SrcAccessor >
void exportFloatingScalarImage( SrcIterator sul, SrcIterator slr, SrcAccessor sget,
const ImageExportInfo & info )
}
\endcode
*/
doxygen_overloaded_function(template <...> void exportFloatingScalarImage)
template < class SrcIterator, class SrcAccessor >
void exportFloatingScalarImage( SrcIterator sul, SrcIterator slr, SrcAccessor sget,
const ImageExportInfo & info )
{
exportImage(sul, slr, sget, info);
}
/*!
\brief Deprecated.
Use \ref exportImage() instead.
<b> Declaration:</b>
\code
namespace vigra {
template < class SrcIterator, class SrcAccessor >
void exportIntegralScalarImage( SrcIterator sul, SrcIterator slr, SrcAccessor sget,
const ImageExportInfo & info )
}
\endcode
*/
doxygen_overloaded_function(template <...> void exportIntegralScalarImage)
template < class SrcIterator, class SrcAccessor >
void exportIntegralScalarImage( SrcIterator sul, SrcIterator slr, SrcAccessor sget,
const ImageExportInfo & info )
{
exportImage(sul, slr, sget, info);
}
/********************************************************/
/* */
/* exportImage */
/* */
/********************************************************/
/** \brief Write an image, given an \ref vigra::ImageExportInfo object.
If the file format to be exported to supports the pixel type of the
source image, the pixel type will be kept (e.g. <tt>float</tt>
can be stored as TIFF without conversion, in contrast to most other
image export toolkits). Otherwise, the pixel values are transformed
to the range 0.255 and converted to <tt>unsigned char</tt>. Currently,
the following file formats are supported. The pixel types given in
brackets are those that are written without conversion:
<DL>
<DT>"BMP"<DD> Microsoft Windows bitmap image file (pixel types: UINT8 as gray and RGB).
<DT>"GIF"<DD> CompuServe graphics interchange format; 8-bit color (pixel types: UINT8 as gray and RGB).
<DT>"JPEG"<DD> Joint Photographic Experts Group JFIF format; compressed 24-bit color
(pixel types: UINT8 as gray and RGB). (only available if libjpeg is installed)
<DT>"PNG"<DD> Portable Network Graphic (pixel types: UINT8 and UINT16 with up to 4 channels).
(only available if libpng is installed)
<DT>"PBM"<DD> Portable bitmap format (black and white).
<DT>"PGM"<DD> Portable graymap format (pixel types: UINT8, INT16, INT32 as gray scale)).
<DT>"PNM"<DD> Portable anymap (pixel types: UINT8, INT16, INT32 as gray and RGB).
<DT>"PPM"<DD> Portable pixmap format (pixel types: UINT8, INT16, INT32 as RGB).
<DT>"SUN"<DD> SUN Rasterfile (pixel types: UINT8 as gray and RGB).
<DT>"TIFF"<DD> Tagged Image File Format
(pixel types: UINT8, INT16, INT32, FLOAT, DOUBLE with up to 4 channels).
(only available if libtiff is installed.)
<DT>"VIFF"<DD> Khoros Visualization image file
(pixel types: UINT8, INT16, INT32, FLOAT, DOUBLE with arbitrary many channels).
</DL>
<b> Declarations:</b>
pass arguments explicitly:
\code
namespace vigra {
template <class SrcIterator, class SrcAccessor>
void exportImage(SrcIterator sul, SrcIterator slr, SrcAccessor sget,
ImageExportInfo const & info)
}
\endcode
use argument objects in conjunction with \ref ArgumentObjectFactories :
\code
namespace vigra {
template <class SrcIterator, class SrcAccessor>
void exportImage(SrcIterator sul, SrcIterator slr, SrcAccessor sget,
ImageExportInfo const & info)
}
\endcode
<b> Usage:</b>
<b>\#include</b> \<<a href="impex_8hxx-source.html">vigra/impex.hxx</a>\><br>
Namespace: vigra
\code
vigra::BRGBImage out(w, h);
...
// write as JPEG image, using compression quality 80
vigra::exportImage(srcImageRange(out),
vigra::ImageExportInfo("myimage.jpg").setCompression("80"));
// force it to a particular pixel type (the pixel type must be supported by the
// desired image file format, otherwise an \ref vigra::PreconditionViolation exception will be thrown)
vigra::exportImage(srcImageRange(out),
vigra::ImageExportInfo("myINT16image.tif").setPixelType("INT16"));
\endcode
<b> Preconditions:</b>
<UL>
<LI> the image file must be writable.
<LI> the file type must be one of the supported file types.
</UL>
**/
doxygen_overloaded_function(template <...> void exportImage)
template < class SrcIterator, class SrcAccessor >
void exportImage( SrcIterator sul, SrcIterator slr, SrcAccessor sget,
const ImageExportInfo & info )
{
typedef typename NumericTraits<typename SrcAccessor::value_type>::isScalar is_scalar;
try
{
exportImage( sul, slr, sget, info, is_scalar() );
}
catch(Encoder::TIFFCompressionException &)
{
const_cast<ImageExportInfo &>(info).setCompression("");
exportImage( sul, slr, sget, info, is_scalar() );
}
}
template < class SrcIterator, class SrcAccessor >
inline
void exportImage( triple<SrcIterator, SrcIterator, SrcAccessor> src,
const ImageExportInfo & info )
{
exportImage( src.first, src.second, src.third, info );
}
template < class SrcIterator, class SrcAccessor >
void exportImage( SrcIterator sul, SrcIterator slr, SrcAccessor sget,
const ImageExportInfo & info, VigraFalseType /*not scalar */)
{
typedef typename SrcAccessor::value_type AccessorValueType;
typedef typename AccessorValueType::value_type SrcValueType;
std::string pixeltype = info.getPixelType();
std::auto_ptr<Encoder> enc = encoder(info);
bool downcast = negotiatePixelType(enc->getFileType(),
TypeAsString<SrcValueType>::result(), pixeltype);
enc->setPixelType(pixeltype);
if(downcast || info.hasForcedRangeMapping())
{
if(pixeltype == "UINT8")
detail::exportVectorImage( sul, slr, sget, enc.get(), info, (UInt8)0);
else if(pixeltype == "INT16")
detail::exportVectorImage( sul, slr, sget, enc.get(), info, Int16());
else if(pixeltype == "UINT16")
detail::exportVectorImage( sul, slr, sget, enc.get(), info, (UInt16)0);
else if(pixeltype == "INT32")
detail::exportVectorImage( sul, slr, sget, enc.get(), info, Int32());
else if(pixeltype == "UINT32")
detail::exportVectorImage( sul, slr, sget, enc.get(), info, (UInt32)0);
else if(pixeltype == "FLOAT")
detail::exportVectorImage( sul, slr, sget, enc.get(), info, float());
else if(pixeltype == "DOUBLE")
detail::exportVectorImage( sul, slr, sget, enc.get(), info, double());
}
else
{
if(pixeltype == "UINT8")
detail::exportVectorImage( sul, slr, sget, enc.get(), (UInt8)0);
else if(pixeltype == "INT16")
detail::exportVectorImage( sul, slr, sget, enc.get(), Int16());
else if(pixeltype == "UINT16")
detail::exportVectorImage( sul, slr, sget, enc.get(), (UInt16)0);
else if(pixeltype == "INT32")
detail::exportVectorImage( sul, slr, sget, enc.get(), Int32());
else if(pixeltype == "UINT32")
detail::exportVectorImage( sul, slr, sget, enc.get(), (UInt32)0);
else if(pixeltype == "FLOAT")
detail::exportVectorImage( sul, slr, sget, enc.get(), float());
else if(pixeltype == "DOUBLE")
detail::exportVectorImage( sul, slr, sget, enc.get(), double());
}
enc->close();
}
template < class SrcIterator, class SrcAccessor >
void exportImage( SrcIterator sul, SrcIterator slr, SrcAccessor sget,
const ImageExportInfo & info, VigraTrueType /*scalar*/ )
{
typedef typename SrcAccessor::value_type SrcValueType;
std::string pixeltype = info.getPixelType();
std::auto_ptr<Encoder> enc = encoder(info);
bool downcast = negotiatePixelType(enc->getFileType(),
TypeAsString<SrcValueType>::result(), pixeltype);
enc->setPixelType(pixeltype);
if(downcast || info.hasForcedRangeMapping())
{
if(pixeltype == "UINT8")
detail::exportScalarImage( sul, slr, sget, enc.get(), info, (UInt8)0);
else if(pixeltype == "INT16")
detail::exportScalarImage( sul, slr, sget, enc.get(), info, Int16());
else if(pixeltype == "UINT16")
detail::exportScalarImage( sul, slr, sget, enc.get(), info, (UInt16)0);
else if(pixeltype == "INT32")
detail::exportScalarImage( sul, slr, sget, enc.get(), info, Int32());
else if(pixeltype == "UINT32")
detail::exportScalarImage( sul, slr, sget, enc.get(), info, (UInt32)0);
else if(pixeltype == "FLOAT")
detail::exportScalarImage( sul, slr, sget, enc.get(), info, float());
else if(pixeltype == "DOUBLE")
detail::exportScalarImage( sul, slr, sget, enc.get(), info, double());
}
else
{
if(pixeltype == "UINT8")
detail::exportScalarImage( sul, slr, sget, enc.get(), (UInt8)0);
else if(pixeltype == "INT16")
detail::exportScalarImage( sul, slr, sget, enc.get(), Int16());
else if(pixeltype == "UINT16")
detail::exportScalarImage( sul, slr, sget, enc.get(), (UInt16)0);
else if(pixeltype == "INT32")
detail::exportScalarImage( sul, slr, sget, enc.get(), Int32());
else if(pixeltype == "UINT32")
detail::exportScalarImage( sul, slr, sget, enc.get(), (UInt32)0);
else if(pixeltype == "FLOAT")
detail::exportScalarImage( sul, slr, sget, enc.get(), float());
else if(pixeltype == "DOUBLE")
detail::exportScalarImage( sul, slr, sget, enc.get(), double());
}
enc->close();
}
//@}
} // namespace vigra
#endif /* VIGRA_IMPEX_HXX */
|