/lib/cximage-6.0/CxImage/tif_xfile.cpp

https://github.com/energy6/xbmc · C++ · 224 lines · 174 code · 30 blank · 20 comment · 20 complexity · b2a0b65fe7d56a320465d79e1724a0bf MD5 · raw file

  1. /*
  2. * TIFF file IO, using CxFile.
  3. */
  4. #ifdef WIN32
  5. #include <windows.h>
  6. #endif
  7. #include <stdio.h>
  8. #include "ximage.h"
  9. #if CXIMAGE_SUPPORT_TIF
  10. #ifdef _LINUX
  11. #include <tiffio.h>
  12. #else
  13. #include "../tiff/tiffiop.h"
  14. #endif
  15. #include "xfile.h"
  16. static tsize_t
  17. _tiffReadProcEx(thandle_t fd, tdata_t buf, tsize_t size)
  18. {
  19. return (tsize_t)((CxFile*)fd)->Read(buf, 1, size);
  20. }
  21. static tsize_t
  22. _tiffWriteProcEx(thandle_t fd, tdata_t buf, tsize_t size)
  23. {
  24. return (tsize_t)((CxFile*)fd)->Write(buf, 1, size);
  25. }
  26. static toff_t
  27. _tiffSeekProcEx(thandle_t fd, toff_t off, int whence)
  28. {
  29. if ( off == 0xFFFFFFFF )
  30. return 0xFFFFFFFF;
  31. if (!((CxFile*)fd)->Seek(off, whence))
  32. return 0xFFFFFFFF;
  33. if (whence == SEEK_SET)
  34. return off;
  35. return (toff_t)((CxFile*)fd)->Tell();
  36. }
  37. // Return nonzero if error
  38. static int
  39. _tiffCloseProcEx(thandle_t /*fd*/)
  40. {
  41. // return !((CxFile*)fd)->Close(); // "//" needed for memory files <DP>
  42. return 0;
  43. }
  44. #include <sys/stat.h>
  45. static toff_t
  46. _tiffSizeProcEx(thandle_t fd)
  47. {
  48. return ((CxFile*)fd)->Size();
  49. }
  50. static int
  51. _tiffMapProcEx(thandle_t /*fd*/, tdata_t* /*pbase*/, toff_t* /*psize*/)
  52. {
  53. return (0);
  54. }
  55. static void
  56. _tiffUnmapProcEx(thandle_t /*fd*/, tdata_t /*base*/, toff_t /*size*/)
  57. {
  58. }
  59. // Open a TIFF file descriptor for read/writing.
  60. /*
  61. TIFF*
  62. TIFFOpen(const char* name, const char* mode)
  63. {
  64. static const char module[] = "TIFFOpen";
  65. FILE* stream = fopen(name, mode);
  66. if (stream == NULL)
  67. {
  68. TIFFError(module, "%s: Cannot open", name);
  69. return NULL;
  70. }
  71. return (TIFFFdOpen((int)stream, name, mode));
  72. }
  73. */
  74. TIFF*
  75. _TIFFFdOpen(void* fd, const char* name, const char* mode)
  76. {
  77. TIFF* tif;
  78. tif = TIFFClientOpen(name, mode,
  79. (thandle_t) fd,
  80. _tiffReadProcEx, _tiffWriteProcEx, _tiffSeekProcEx, _tiffCloseProcEx,
  81. _tiffSizeProcEx, _tiffMapProcEx, _tiffUnmapProcEx);
  82. #ifndef _LINUX
  83. if (tif)
  84. tif->tif_fd = fd;
  85. #endif
  86. return (tif);
  87. }
  88. extern "C" TIFF* _TIFFOpenEx(CxFile* stream, const char* mode)
  89. {
  90. return (_TIFFFdOpen(stream, "TIFF IMAGE", mode));
  91. }
  92. #ifdef __GNUC__
  93. extern char* malloc();
  94. extern char* realloc();
  95. #else
  96. #include <malloc.h>
  97. #endif
  98. tdata_t
  99. _TIFFmalloc(tsize_t s)
  100. {
  101. return (malloc((size_t) s));
  102. }
  103. void
  104. _TIFFfree(tdata_t p)
  105. {
  106. free(p);
  107. }
  108. tdata_t
  109. _TIFFrealloc(tdata_t p, tsize_t s)
  110. {
  111. return (realloc(p, (size_t) s));
  112. }
  113. void
  114. _TIFFmemset(tdata_t p, int v, tsize_t c)
  115. {
  116. memset(p, v, (size_t) c);
  117. }
  118. void
  119. _TIFFmemcpy(tdata_t d, const tdata_t s, tsize_t c)
  120. {
  121. memcpy(d, s, (size_t) c);
  122. }
  123. int
  124. _TIFFmemcmp(const tdata_t p1, const tdata_t p2, tsize_t c)
  125. {
  126. return (memcmp(p1, p2, (size_t) c));
  127. }
  128. #ifndef UNICODE
  129. #define DbgPrint wvsprintf
  130. #define DbgPrint2 wsprintf
  131. #define DbgMsgBox MessageBox
  132. #else
  133. #define DbgPrint wvsprintfA
  134. #define DbgPrint2 wsprintfA
  135. #define DbgMsgBox MessageBoxA
  136. #endif
  137. static void
  138. Win32WarningHandler(const char* module, const char* fmt, va_list ap)
  139. {
  140. #ifdef _DEBUG
  141. #if (!defined(_CONSOLE) && !defined(_WIN32_WCE) && defined(WIN32))
  142. LPSTR szTitle;
  143. LPSTR szTmp;
  144. LPCSTR szTitleText = "%s Warning";
  145. LPCSTR szDefaultModule = "TIFFLIB";
  146. szTmp = (module == NULL) ? (LPSTR)szDefaultModule : (LPSTR)module;
  147. if ((szTitle = (LPSTR)LocalAlloc(LMEM_FIXED, (strlen(szTmp) +
  148. strlen(szTitleText) + strlen(fmt) + 128))) == NULL)
  149. return;
  150. DbgPrint2(szTitle, szTitleText, szTmp);
  151. szTmp = szTitle + (strlen(szTitle)+2);
  152. DbgPrint(szTmp, fmt, ap);
  153. DbgMsgBox(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONINFORMATION);
  154. LocalFree(szTitle);
  155. return;
  156. #else
  157. if (module != NULL)
  158. fprintf(stderr, "%s: ", module);
  159. fprintf(stderr, "Warning, ");
  160. vfprintf(stderr, fmt, ap);
  161. fprintf(stderr, ".\n");
  162. #endif
  163. #endif
  164. }
  165. TIFFErrorHandler _TIFFwarningHandler = Win32WarningHandler;
  166. static void
  167. Win32ErrorHandler(const char* module, const char* fmt, va_list ap)
  168. {
  169. #ifdef _DEBUG
  170. #if (!defined(_CONSOLE) && !defined(_WIN32_WCE) && defined(WIN32))
  171. LPSTR szTitle;
  172. LPSTR szTmp;
  173. LPCSTR szTitleText = "%s Error";
  174. LPCSTR szDefaultModule = "TIFFLIB";
  175. szTmp = (module == NULL) ? (LPSTR)szDefaultModule : (LPSTR)module;
  176. if ((szTitle = (LPSTR)LocalAlloc(LMEM_FIXED, (strlen(szTmp) +
  177. strlen(szTitleText) + strlen(fmt) + 128))) == NULL)
  178. return;
  179. DbgPrint2(szTitle, szTitleText, szTmp);
  180. szTmp = szTitle + (strlen(szTitle)+2);
  181. DbgPrint(szTmp, fmt, ap);
  182. DbgMsgBox(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONEXCLAMATION);
  183. LocalFree(szTitle);
  184. return;
  185. #else
  186. if (module != NULL)
  187. fprintf(stderr, "%s: ", module);
  188. vfprintf(stderr, fmt, ap);
  189. fprintf(stderr, ".\n");
  190. #endif
  191. #endif
  192. }
  193. TIFFErrorHandler _TIFFerrorHandler = Win32ErrorHandler;
  194. #endif