/cximage/tiff/tif_xfile.cpp

https://github.com/tokyovigilante/xbmc-sources-fork · C++ · 225 lines · 187 code · 27 blank · 11 comment · 17 complexity · cdadcbb199d934a4a8e7689cb82e56dc MD5 · raw file

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