windowsmediaphoto.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. //*@@@+++@@@@******************************************************************
  2. //
  3. // Copyright © Microsoft Corp.
  4. // All rights reserved.
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are met:
  8. //
  9. // • Redistributions of source code must retain the above copyright notice,
  10. // this list of conditions and the following disclaimer.
  11. // • Redistributions in binary form must reproduce the above copyright notice,
  12. // this list of conditions and the following disclaimer in the documentation
  13. // and/or other materials provided with the distribution.
  14. //
  15. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  19. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. // POSSIBILITY OF SUCH DAMAGE.
  26. //
  27. //*@@@---@@@@******************************************************************
  28. #ifndef WMI_WINDOWSMEDIAPHOTO_H
  29. #define WMI_WINDOWSMEDIAPHOTO_H
  30. //================================================================
  31. #include <assert.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #if defined(__cplusplus) && !defined(EXTERN_C)
  36. #define EXTERN_C extern "C"
  37. #elif !defined(EXTERN_C)// __cplusplus
  38. #define EXTERN_C extern
  39. #endif // __cplusplus
  40. /********************************************************************************
  41. Type definitions
  42. ********************************************************************************/
  43. typedef int Bool;
  44. typedef char Char;
  45. typedef double Double;
  46. typedef int Int;
  47. typedef signed char I8;
  48. typedef short I16; // 16 bit int
  49. typedef int I32;
  50. typedef long Long;
  51. typedef unsigned char PixelC;
  52. typedef int PixelI;
  53. typedef unsigned int UInt;
  54. typedef unsigned long ULong;
  55. typedef unsigned char U8; // 8 bit uint
  56. typedef unsigned short U16;
  57. typedef unsigned int U32; // 32 bit uint
  58. typedef void Void;
  59. typedef void* CTXSTRCODEC;
  60. #define REENTRANT_MODE 1
  61. /*
  62. DESCRIPTION OF COMPILER FLAG REENTRANT_MODE:
  63. //#define REENTRANT_MODE 1
  64. This compiler flag is related to the capability of banded decode
  65. (decoding only one MB row of the source JPEG XR image at a time).
  66. With REENTRANT_MODE defined, the decoder decodes one MB row on each call to
  67. ImageStrDecDecode().
  68. The decoder acts as if it can only write to the single MBRow whose pointer was passed to it.
  69. This acts as a proof of concept that the API would work if you passed it a small buffer
  70. on each call to ImageStrDecDecode().
  71. The REENTRANT_MODE flag only works when the output image is in Orientations 0, 1
  72. (vertically flipped) or 2 (horizontally flipped).
  73. With REENTRANT_MODE defined, the function PKImageDecode_Copy_WMP()
  74. decodes only as far as the pRect parameter indicates. The width of the rectangle must be the width
  75. of the image, but on each call, this function will decode the image up to the end of the MB Row
  76. which contains the i-th pixel row, where i = pRect->Y.
  77. A target use of this version would be to have PKImageDecode_Copy_WMP() called in a loop, once for
  78. each MB row. On each call, pRect would specify a 1-MB-Row-tall rectangle that is the width of the
  79. image. The decoder state is preserved until the Decoder finishes decoding the image.
  80. If, at a certain point, a request is made for a rectangle _above_ the last row decoded, then the
  81. decoder instance is terminated and re-initiated, and decoding re-starts, going from the beginning
  82. of the image to the end of the current rectangle.
  83. ***
  84. We've chosen to uncomment-out this definition in this header file. An alternate method would be
  85. to allow the user to define this in the PREPROCESSOR DEFINITIONS section of the properties page
  86. for each of the following projects: CommonLib, DecodeLib, JXRDecApp and JXRGlueLib.
  87. */
  88. /*************************************************************************
  89. enums
  90. *************************************************************************/
  91. typedef enum {
  92. ICERR_OK = 0, ICERR_ERROR = -1
  93. } ERR_CODE;
  94. typedef enum BITDEPTH {
  95. BD_SHORT, BD_LONG,
  96. /* add new BITDEPTH here */ BD_MAX
  97. } BITDEPTH;
  98. typedef enum BITDEPTH_BITS {
  99. // regular ones
  100. BD_1, //White is foreground
  101. BD_8, BD_16, BD_16S, BD_16F, BD_32, BD_32S, BD_32F,
  102. // irregular ones
  103. BD_5, BD_10, BD_565,
  104. /* add new BITDEPTH_BITS here */ BDB_MAX,
  105. BD_1alt = 0xf, //Black is foreground
  106. } BITDEPTH_BITS;
  107. typedef enum OVERLAP {
  108. OL_NONE = 0, OL_ONE, OL_TWO,
  109. /* add new OVERLAP here */ OL_MAX
  110. } OVERLAP;
  111. typedef enum BITSTREAMFORMAT {
  112. SPATIAL = 0, // spatial order
  113. FREQUENCY, // frequency order
  114. } BITSTREAMFORMAT;
  115. typedef enum COLORFORMAT {
  116. Y_ONLY = 0,
  117. YUV_420 = 1,
  118. YUV_422 = 2,
  119. YUV_444 = 3,
  120. CMYK = 4,
  121. //CMYKDIRECT = 5,
  122. NCOMPONENT = 6,
  123. // these are external-only
  124. CF_RGB = 7,
  125. CF_RGBE = 8,
  126. /* add new COLORFORMAT here */ CFT_MAX
  127. } COLORFORMAT;
  128. // rotation and flip
  129. typedef enum ORIENTATION {
  130. // CRW: Clock Wise 90% Rotation; FlipH: Flip Horizontally; FlipV: Flip Vertically
  131. // Peform rotation FIRST!
  132. // CRW FlipH FlipV
  133. O_NONE = 0, // 0 0 0
  134. O_FLIPV, // 0 0 1
  135. O_FLIPH, // 0 1 0
  136. O_FLIPVH, // 0 1 1
  137. O_RCW, // 1 0 0
  138. O_RCW_FLIPV, // 1 0 1
  139. O_RCW_FLIPH, // 1 1 0
  140. O_RCW_FLIPVH, // 1 1 1
  141. /* add new ORIENTATION here */ O_MAX
  142. } ORIENTATION;
  143. typedef enum SUBBAND {
  144. SB_ALL = 0, // keep all subbands
  145. SB_NO_FLEXBITS, // skip flex bits
  146. SB_NO_HIGHPASS, // skip highpass
  147. SB_DC_ONLY, // skip lowpass and highpass, DC only
  148. SB_ISOLATED, // not decodable
  149. /* add new SUBBAND here */ SB_MAX
  150. } SUBBAND;
  151. enum { RAW = 0, BMP = 1, PPM = 2, TIF = 3, HDR = 4, IYUV = 5, YUV422 = 6, YUV444 = 7};
  152. typedef enum {ERROR_FAIL = -1, SUCCESS_DONE, PRE_READ_HDR, PRE_SETUP, PRE_DECODE, POST_READ_HDR } WMIDecoderStatus;
  153. #ifndef FALSE
  154. #define FALSE 0
  155. #endif // FALSE
  156. #ifndef TRUE
  157. #define TRUE 1
  158. #endif // TRUE
  159. #define MAX_CHANNELS 16
  160. #define LOG_MAX_TILES 12
  161. #define MAX_TILES (1 << LOG_MAX_TILES)
  162. //================================================================
  163. // Codec-specific constants
  164. #define MB_WIDTH_PIXEL 16
  165. #define MB_HEIGHT_PIXEL 16
  166. #define BLK_WIDTH_PIXEL 4
  167. #define BLK_HEIGHT_PIXEL 4
  168. #define MB_WIDTH_BLK 4
  169. #define MB_HEIGHT_BLK 4
  170. // The codec operates most efficiently when the framebuffers for encoder input
  171. // and decoder output are: 1) aligned on a particular boundary, and 2) the stride
  172. // is also aligned to this boundary (so that each scanline is also aligned).
  173. // This boundary is defined below.
  174. #define FRAMEBUFFER_ALIGNMENT 128
  175. //================================================================
  176. #define WMP_errSuccess 0
  177. #define WMP_errFail -1
  178. #define WMP_errNotYetImplemented -2
  179. #define WMP_errAbstractMethod -3
  180. #define WMP_errOutOfMemory -101
  181. #define WMP_errFileIO -102
  182. #define WMP_errBufferOverflow -103
  183. #define WMP_errInvalidParameter -104
  184. #define WMP_errInvalidArgument -105
  185. #define WMP_errUnsupportedFormat -106
  186. #define WMP_errIncorrectCodecVersion -107
  187. #define WMP_errIndexNotFound -108
  188. #define WMP_errOutOfSequence -109
  189. #define WMP_errNotInitialized -110
  190. #define WMP_errMustBeMultipleOf16LinesUntilLastCall -111
  191. #define WMP_errPlanarAlphaBandedEncRequiresTempFile -112
  192. #define WMP_errAlphaModeCannotBeTranscoded -113
  193. #define WMP_errIncorrectCodecSubVersion -114
  194. //================================================================
  195. typedef long ERR;
  196. #define Failed(err) ((err)<0)
  197. #define CRLF "\r\n"
  198. #define CT_ASSERT(exp, uniq) typedef char __CT_ASSERT__##uniq[(exp) ? 1 : -1] // Caller must provide a unique tag, or this fails to compile under GCC
  199. #if defined(_DEBUG) || defined(DBG)
  200. #define Report(err, szExp, szFile, nLine) \
  201. fprintf(stderr, "FAILED: %ld=%s" CRLF, (err), (szExp)); \
  202. fprintf(stderr, " %s:%ld" CRLF, (szFile), (nLine)); \
  203. #else
  204. #define Report(err, szExp, szFile, lLine) err = err
  205. #endif
  206. #define Call(exp) \
  207. if (Failed(err = (exp))) \
  208. { \
  209. Report(err, #exp, __FILE__, (long)__LINE__); \
  210. goto Cleanup; \
  211. } \
  212. else err = err
  213. #define CallIgnoreError(errTmp, exp) \
  214. if (Failed(errTmp = (exp))) \
  215. { \
  216. Report(errTmp, #exp, __FILE__, (long)__LINE__); \
  217. } \
  218. else errTmp = errTmp
  219. #define Test(exp, err) Call((exp) ? WMP_errSuccess : (err))
  220. #define FailIf(exp, err) Call((exp) ? (err) : WMP_errSuccess)
  221. //================================================================
  222. // WMPStream interface
  223. //================================================================
  224. struct WMPStream
  225. {
  226. union
  227. {
  228. struct tagFile
  229. {
  230. FILE* pFile;
  231. } file;
  232. struct tagBuf
  233. {
  234. U8* pbBuf;
  235. size_t cbBuf;
  236. size_t cbCur;
  237. size_t cbBufCount;
  238. } buf;
  239. void* pvObj;
  240. } state;
  241. Bool fMem;
  242. ERR (*Close)(struct WMPStream** pme);
  243. Bool (*EOS)(struct WMPStream* me);
  244. ERR (*Read)(struct WMPStream* me, void* pv, size_t cb);
  245. ERR (*Write)(struct WMPStream* me, const void* pv, size_t cb);
  246. //ERR (*GetLine)(struct WMPStream* me, void* pv, size_t cb);
  247. ERR (*SetPos)(struct WMPStream* me, size_t offPos);
  248. ERR (*GetPos)(struct WMPStream* me, size_t* poffPos);
  249. };
  250. EXTERN_C ERR CreateWS_File(struct WMPStream** ppWS, const char* szFilename, const char* szMode);
  251. EXTERN_C ERR CloseWS_File(struct WMPStream** ppWS);
  252. EXTERN_C ERR CreateWS_Memory(struct WMPStream** ppWS, void* pv, size_t cb);
  253. EXTERN_C ERR CloseWS_Memory(struct WMPStream** ppWS);
  254. //================================================================
  255. // Enc/Dec data structure
  256. //================================================================
  257. typedef struct tagCWMImageInfo {
  258. size_t cWidth;
  259. size_t cHeight;
  260. COLORFORMAT cfColorFormat;
  261. BITDEPTH_BITS bdBitDepth;
  262. size_t cBitsPerUnit;
  263. size_t cLeadingPadding; // number of leading padding
  264. Bool bRGB; // true: RGB; false: BGR
  265. U8 cChromaCenteringX; // Relative location of Chroma w.r.t Luma
  266. U8 cChromaCenteringY; // Relative location of Chroma w.r.t Luma
  267. // Region of interest decoding
  268. size_t cROILeftX;
  269. size_t cROIWidth;
  270. size_t cROITopY;
  271. size_t cROIHeight;
  272. // thumbnail decode
  273. Bool bSkipFlexbits;
  274. size_t cThumbnailWidth;
  275. size_t cThumbnailHeight;
  276. // image orientation
  277. ORIENTATION oOrientation;
  278. // post processing
  279. U8 cPostProcStrength; // 0(none) 1(light) 2(medium) 3(strong) 4(very strong)
  280. // user buffer is always padded to whole MB
  281. Bool fPaddedUserBuffer;
  282. } CWMImageInfo;
  283. typedef struct tagCWMIStrCodecParam {
  284. Bool bVerbose;
  285. // for macroblock quantization (DQUANT)
  286. U8 uiDefaultQPIndex;
  287. U8 uiDefaultQPIndexYLP;
  288. U8 uiDefaultQPIndexYHP;
  289. U8 uiDefaultQPIndexU;
  290. U8 uiDefaultQPIndexULP;
  291. U8 uiDefaultQPIndexUHP;
  292. U8 uiDefaultQPIndexV;
  293. U8 uiDefaultQPIndexVLP;
  294. U8 uiDefaultQPIndexVHP;
  295. U8 uiDefaultQPIndexAlpha;
  296. COLORFORMAT cfColorFormat;
  297. BITDEPTH bdBitDepth;
  298. OVERLAP olOverlap;
  299. BITSTREAMFORMAT bfBitstreamFormat;
  300. size_t cChannel; // number of color channels including alpha
  301. U8 uAlphaMode; // 0:no alpha 1: alpha only else: something + alpha
  302. SUBBAND sbSubband; // which subbands to keep
  303. U8 uiTrimFlexBits;
  304. struct WMPStream* pWStream;
  305. size_t cbStream;
  306. // tiling info
  307. U32 cNumOfSliceMinus1V; // # of vertical slices
  308. U32 uiTileX[MAX_TILES]; // width in MB of each veritical slice
  309. U32 cNumOfSliceMinus1H; // # of horizontal slices
  310. U32 uiTileY[MAX_TILES]; // height in MB of each horizontal slice
  311. //32f and 32s conversion parameters
  312. U8 nLenMantissaOrShift;
  313. I8 nExpBias;
  314. Bool bBlackWhite;
  315. Bool bUseHardTileBoundaries; //default is soft tile boundaries
  316. Bool bProgressiveMode; //default is sequential mode
  317. Bool bYUVData; //default is cfColorFormat data
  318. Bool bUnscaledArith; //force unscaled arithmetic
  319. // Perf measurement
  320. Bool fMeasurePerf;
  321. } CWMIStrCodecParam;
  322. typedef struct tagCWMImageBufferInfo {
  323. void* pv; // pointer to scanline buffer
  324. size_t cLine; // count of scanlines
  325. size_t cbStride; // count of BYTE for stride
  326. #ifdef REENTRANT_MODE
  327. unsigned int uiFirstMBRow; // Current First MB Row being decoded
  328. unsigned int uiLastMBRow; // Current Last MB Row being decoded
  329. size_t cLinesDecoded; // Number of lines decoded and returned in low-mem mode
  330. #endif // REENTRANT_MODE
  331. } CWMImageBufferInfo;
  332. /****************************************************************/
  333. /* Encode API */
  334. /****************************************************************/
  335. EXTERN_C Int ImageStrEncInit(
  336. CWMImageInfo* pII,
  337. CWMIStrCodecParam *pSCP,
  338. CTXSTRCODEC* pctxSC);
  339. EXTERN_C Int ImageStrEncEncode(
  340. CTXSTRCODEC ctxSC,
  341. const CWMImageBufferInfo* pBI);
  342. EXTERN_C Int ImageStrEncTerm(
  343. CTXSTRCODEC ctxSC);
  344. /****************************************************************/
  345. /* Decode API */
  346. /****************************************************************/
  347. struct CWMImageStrCodec;
  348. EXTERN_C Int ImageStrDecGetInfo(
  349. CWMImageInfo* pII,
  350. CWMIStrCodecParam *pSCP);
  351. EXTERN_C Int ImageStrDecInit(
  352. CWMImageInfo* pII,
  353. CWMIStrCodecParam *pSCP,
  354. CTXSTRCODEC* pctxSC);
  355. EXTERN_C Int ImageStrDecDecode(
  356. CTXSTRCODEC ctxSC,
  357. const CWMImageBufferInfo* pBI
  358. #ifdef REENTRANT_MODE
  359. , size_t *pcDecodedLines
  360. #endif
  361. );
  362. EXTERN_C Int ImageStrDecTerm(
  363. CTXSTRCODEC ctxSC);
  364. EXTERN_C Int WMPhotoValidate(
  365. CWMImageInfo * pII,
  366. CWMIStrCodecParam * pSCP);
  367. /****************************************************************/
  368. /* Transcoding API */
  369. /****************************************************************/
  370. typedef struct tagCWMTranscodingParam {
  371. size_t cLeftX;
  372. size_t cWidth;
  373. size_t cTopY;
  374. size_t cHeight; // interested region
  375. BITSTREAMFORMAT bfBitstreamFormat; // desired bitstream format
  376. // COLORFORMAT cfColorFormat; // desired color format
  377. U8 uAlphaMode; // 0:no alpha 1: alpha only else: something + alpha
  378. SUBBAND sbSubband; // which subbands to keep
  379. ORIENTATION oOrientation; // flip / right angle rotation
  380. Bool bIgnoreOverlap;
  381. } CWMTranscodingParam;
  382. EXTERN_C Int WMPhotoTranscode(
  383. struct WMPStream* pStreamDec, // input bitstrean
  384. struct WMPStream* pStreamEnc, // output bitstream
  385. CWMTranscodingParam* pParam // transcoding parameters
  386. );
  387. typedef struct tagCWMDetilingParam {
  388. size_t cWidth;
  389. size_t cHeight; // image size
  390. size_t cChannel; // # of channels
  391. OVERLAP olOverlap; // overlap
  392. BITDEPTH_BITS bdBitdepth; // bit depth
  393. // tiling info
  394. U32 cNumOfSliceMinus1V; // # of vertical slices
  395. U32 uiTileX[MAX_TILES]; // position in MB of each veritical slice
  396. U32 cNumOfSliceMinus1H; // # of horizontal slices
  397. U32 uiTileY[MAX_TILES]; // position in MB of each horizontal slice
  398. // image info
  399. void * pImage;
  400. size_t cbStride;
  401. } CWMDetilingParam;
  402. EXTERN_C Int WMPhotoDetile(
  403. CWMDetilingParam * pParam // detiling parameters
  404. );
  405. #endif // WMI_WINDOWSMEDIAPHOTO_H