CTiffWriter.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /***************************************************************************
  2. * Copyright ?2007 TWAIN Working Group:
  3. * Adobe Systems Incorporated, AnyDoc Software Inc., Eastman Kodak Company,
  4. * Fujitsu Computer Products of America, JFL Peripheral Solutions Inc.,
  5. * Ricoh Corporation, and Xerox Corporation.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * * Neither the name of the TWAIN Working Group nor the
  16. * names of its contributors may be used to endorse or promote products
  17. * derived from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY TWAIN Working Group ``AS IS'' AND ANY
  20. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL TWAIN Working Group BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. ***************************************************************************/
  31. /**
  32. * @file CTiffWriter.h
  33. * Write an image to disk as a tiff file.
  34. * @author TWAIN Working Group
  35. * @date October 2007
  36. */
  37. #ifndef __TIFFWRITER_H__
  38. #define __TIFFWRITER_H__
  39. #include <string>
  40. #include <fstream>
  41. using namespace std;
  42. #ifdef _MSC_VER
  43. #include <windows.h>
  44. #else
  45. typedef uint16_t WORD;
  46. typedef uint32_t DWORD;
  47. #endif // _MSC_VER
  48. #define TIFF_UNCOMPRESSED 1 /**< TIFF compression types */
  49. #define TIFF_CCITTGROUP3 3
  50. #define TIFF_CCITTGROUP4 4
  51. // TIFF types
  52. #define kTIFF_TY_BYTE 1 /**< 8-bit unsigned int */
  53. #define kTIFF_TY_ASCII 2 /**< 8-bit byte that contains a 7-bit ASCII code; last byte must be binary 0 (NULL) */
  54. #define kTIFF_TY_SHORT 3 /**< 16-bit (2-byte) unsigned int */
  55. #define kTIFF_TY_LONG 4 /**< 32-bit (4-byte) unsigned int */
  56. #define kTIFF_TY_RATIONAL 5 /**< two LONGs; the first is the numerator of a fraction; the second, the denominator */
  57. // these field types where introduced in TIFF 6.0
  58. #define kTIFF_TY_SBYTE 6 /**< 8-bit signed int */
  59. #define kTIFF_TY_UNDEFINED 7 /**< 8-bit byte that may contain anything, depending on the definition of the field */
  60. #define kTIFF_TY_SSHORT 8 /**< 16-bit (2-byte) signed int */
  61. #define kTIFF_TY_SLONG 9 /**< 32-bit (4-byte) signed int */
  62. #define kTIFF_TY_SRATIONAL 10 /**< two SLONG's; first is numerator of fraction, second is denominator */
  63. #define kTIFF_TY_FLOAT 11 /**< single precision (4-byte) IEEE format */
  64. #define kTIFF_TY_DOUBLE 12 /**< double precision (8-byte) IEEE format */
  65. // TIFF Tags
  66. #define kTIFF_TAG_IMGWIDTH 0x0100 /**< Image width, short or long */
  67. #define kTIFF_TAG_IMGLENGTH 0x0101 /**< Image length, short or long */
  68. #define kTIFF_TAG_BITSPERSAMPLE 0x0102 /**< BitsPerSample, short */
  69. #define kTIFF_TAG_COMPRESSION 0x0103 /**< Compression, short */
  70. #define kTIFF_TAG_PHOTOMETRICINT 0x0106 /**< PhotometricInterpretation, short */
  71. #define kTIFF_TAG_STRIPOFFSETS 0x0111 /**< StripOffsets, short or long */
  72. #define kTIFF_TAG_SAMPLESPERPIXEL 0x0115 /**< Samples per pixel, short */
  73. #define kTIFF_TAG_ROWSPERSTRIP 0x0116 /**< RowsPerStrip, short or long */
  74. #define kTIFF_TAG_STRIPBYTECOUNTS 0x0117 /**< StripByteCounts, short or long */
  75. #define kTIFF_TAG_XRESOLUTION 0x011A /**< X Resolution, rational */
  76. #define kTIFF_TAG_YRESOLUTION 0x011B /**< Y Resolution, rational */
  77. #define kTIFF_TAG_RESOLUTIONUNIT 0x0128 /**< Resolution unit, short */
  78. #define kTIFF_TAG_COLORMAP 0x0140 /**< ColorMap, short, RGB order, black = 0,0,0, TWAIN supports max 256 entry pallette */
  79. /**
  80. * TIFF Image File Header
  81. */
  82. struct TIFFIFH
  83. {
  84. WORD Identifier;
  85. WORD Version;
  86. DWORD IFDOffset;
  87. };
  88. /**
  89. * A TIFF Tag
  90. * If the actual value of the tag is less then a DWORD, then offset will contain
  91. * it, else offset is truly an offset to the value.
  92. */
  93. struct TIFFTag
  94. {
  95. WORD TagID;
  96. WORD DataType;
  97. DWORD DataCount;
  98. DWORD DataOffset;
  99. };
  100. /**
  101. * This is a class that will progressively write a TIFF image to a file.
  102. */
  103. class CTiffWriter
  104. {
  105. public:
  106. /**
  107. * Constructor for CTiffWriter. This is a class that will progressively
  108. * write a TIFF image to a file.
  109. * @param[in] _filename name of file to write to.
  110. * @param[in] _width image width.
  111. * @param[in] _height image height.
  112. * @param[in] _bitsPerPixel number of bits per each pixel.
  113. * @param[in] _bytesPerRow number of bytes per row of data.
  114. */
  115. CTiffWriter(const string& _filename,
  116. const long int _width,
  117. const long int _height,
  118. const int _bitsPerPixel,
  119. const unsigned long int _bytesPerRow);
  120. /**
  121. * Deconstructor for CTiffWriter.
  122. */
  123. virtual ~CTiffWriter();
  124. /**
  125. * Set the width of the image.
  126. * @param[in] _v the new image width
  127. */
  128. void setImageWidth(const long int _v);
  129. /**
  130. * Set the height of the image.
  131. * @param[in] _v the new image height
  132. */
  133. void setImageHeight(const long int _v);
  134. /**
  135. * Set the bits per sample of the image.
  136. * @param[in] _v the new bits per sample
  137. */
  138. void setBitsPerSample(const int _v);
  139. /**
  140. * Set the compression method to use.
  141. * @param[in] _v the new compression method
  142. */
  143. void setCompression(const int _v);
  144. /**
  145. * Set the Photometric Interpretation.
  146. * @param[in] _v the new Photometric Interpretation
  147. */
  148. void setPhotometricInterp(const int _v);
  149. /**
  150. * Set the number of samples per pixel of the image.
  151. * @param[in] _v the new samples per pixel
  152. */
  153. void setSamplesPerPixel(const int _v);
  154. /**
  155. * Set the x resolution of the image. Using type kTIFF_TY_RATIONAL (fraction)
  156. * @param[in] _numerator the numerator part of the fraction
  157. * @param[in] _denominator the denominator part of the fraction
  158. */
  159. void setXResolution(const int _numerator, const int _denominator);
  160. /**
  161. * Set the y resolution of the image. Using type kTIFF_TY_RATIONAL (fraction)
  162. * @param[in] _numerator the numerator part of the fraction
  163. * @param[in] _denominator the denominator part of the fraction
  164. */
  165. void setYResolution(const int _numerator, const int _denominator);
  166. /**
  167. * Set the Bytes per row of the image.
  168. * @param[in] _v the new bytes per row
  169. */
  170. void setBytesPerRow(const int _v);
  171. /**
  172. * Write the prepaired image header to the file.
  173. * @return true for succes
  174. */
  175. bool writeImageHeader();
  176. /**
  177. * Write the data for the image to the file.
  178. * @param[in] _pData pointer to the image data
  179. * @param[in] _nCount number of bytes to write
  180. * @return true for success
  181. */
  182. bool WriteTIFFData(char *_pData, DWORD _nCount);
  183. /**
  184. * Return the size of the TIFF header for the image file.
  185. * @return the size of the header
  186. */
  187. unsigned int getSizeofHeader();
  188. void GetImageHeader(stringstream &Header);
  189. protected:
  190. string m_filename; /**< Name and or path of file */
  191. int m_nOffset; /**< Current offset into file */
  192. DWORD m_xres[2]; /**< The X resolution of the image */
  193. DWORD m_yres[2]; /**< The Y resolution of the image */
  194. TIFFTag m_ImageWidth; /**< The image width in pixels */
  195. TIFFTag m_ImageLength; /**< The image height in pixels */
  196. TIFFTag m_BitsPerSample; /**< The number of Bits per sample */
  197. TIFFTag m_Compression; /**< The compression method to use */
  198. TIFFTag m_PhotometricInterp; /**< The Photometric Interpretation to use */
  199. TIFFTag m_StripOffsets; /**< The strip offset, where image data starts */
  200. TIFFTag m_SamplesPerPixel; /**< The number of channels (RGB, G, )*/
  201. TIFFTag m_RowsPerStrip; /**< The number of rows that make up each strip */
  202. TIFFTag m_StripByteCounts; /**< The size of each strip of image data */
  203. TIFFTag m_XResolution; /**< The offset to the X resolution */
  204. TIFFTag m_YResolution; /**< The offset to the Y resolution */
  205. TIFFTag m_ResolutionUnit; /**< The units of the Resolution */
  206. ofstream* m_pImageStream; /**< The output stream to write the file to */
  207. };
  208. #endif // __TIFFWRITER_H__