Interface_ImageHandle.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (C) =USTC= Fu Li
  3. *
  4. * Author : Fu Li
  5. * Create : 2004-4-2
  6. * Home : http://www.crazy-bit.com/
  7. * Mail : crazybit@263.net
  8. * History :
  9. */
  10. #ifndef __FOO_INTERFACE_IMAGEHANDLE__2005_04_02__H__
  11. #define __FOO_INTERFACE_IMAGEHANDLE__2005_04_02__H__
  12. #include "../ImageProperty.h"
  13. class FCObjImage ; // external class
  14. //=============================================================================
  15. /**
  16. * Interface of image handle.
  17. * Used by FCObjImage/FCObjMultiFrame object to implement Load/Save image.
  18. */
  19. class FCImageHandleBase
  20. {
  21. public:
  22. /**
  23. * @name Load image.
  24. */
  25. //@{
  26. /**
  27. * Load image from file.
  28. * This function default load file into memory and then call FCImageHandleBase::LoadImageMemory, you can override it to load directly.
  29. * @param rImageList : You must <B>new</B> FCObjImage and add them into rImageList, after add you can't delete them later.
  30. * @param rImageProp : optional, you can <B>new</B> and add property.
  31. */
  32. virtual bool LoadImageFile (const char* szFileName,
  33. PCL_Interface_Composite<FCObjImage>& rImageList,
  34. std::auto_ptr<FCImageProperty>& rImageProp)
  35. {
  36. #ifdef VC60
  37. FILE * pf = fopen (szFileName, "rb") ;
  38. if (!pf)
  39. return false ;
  40. #else
  41. FILE *pf = NULL;
  42. _tfopen_s(&pf, szFileName, _T("rb"));
  43. if ( !pf )
  44. {
  45. return false;
  46. }
  47. #endif
  48. // get file length
  49. fseek (pf, 0, SEEK_END) ;
  50. const int nFileSize = (int)ftell(pf) ;
  51. if (nFileSize <= 0)
  52. {
  53. fclose (pf) ;
  54. return false ;
  55. }
  56. // read file into memory
  57. PCL_array<BYTE> pStart (nFileSize) ;
  58. fseek (pf, 0, SEEK_SET) ;
  59. fread (pStart.get(), 1, nFileSize, pf) ;
  60. fclose (pf) ;
  61. return LoadImageMemory (pStart.get(), nFileSize, rImageList, rImageProp) ;
  62. }
  63. /**
  64. * Load image from memory.
  65. * This function default return false, derived class should implement it.
  66. * @param rImageList : You must <B>new</B> FCObjImage and add them into rImageList, after add you can't delete them later.
  67. * @param rImageProp : optional, you can <B>new</B> and add property.
  68. */
  69. virtual bool LoadImageMemory (const BYTE* pStart, int nMemSize,
  70. PCL_Interface_Composite<FCObjImage>& rImageList,
  71. std::auto_ptr<FCImageProperty>& rImageProp)
  72. {
  73. return false ;
  74. }
  75. //@}
  76. /**
  77. * @name Save image.
  78. */
  79. //@{
  80. /**
  81. * Save image to file.
  82. * This function default return false, derived class should implement it.
  83. */
  84. virtual bool SaveImageFile (const char* szFileName,
  85. const std::deque<const FCObjImage*>& rImageList,
  86. const FCImageProperty& rImageProp)
  87. {
  88. return false ;
  89. }
  90. //@}
  91. virtual ~FCImageHandleBase() {}
  92. protected:
  93. /// Is use RLE arithmetic to compress image on saving tga (default not use).
  94. static bool TgaSaveUseRLE (const FCImageProperty& rImageProp)
  95. {
  96. std::string s = rImageProp.QueryPropertyValue(PROPERTY_TAG_SAVE_FLAG) ;
  97. if (s.empty())
  98. s = rImageProp.QueryPropertyValue(PROPERTY_TAG_TGA_USERLE) ;
  99. int nFlag = -1 ;
  100. if (!s.empty())
  101. FCOXOHelper::A2X (s, nFlag) ;
  102. return (nFlag != -1) ;
  103. }
  104. /// Jpeg's quality on saving jpeg (default is 82).
  105. static int JpegSaveQuality (const FCImageProperty& rImageProp)
  106. {
  107. std::string s = rImageProp.QueryPropertyValue(PROPERTY_TAG_SAVE_FLAG) ;
  108. if (s.empty())
  109. s = rImageProp.QueryPropertyValue(PROPERTY_TAG_JPEG_QUALITY) ;
  110. int nFlag = 82 ;
  111. if (!s.empty() && (s != "-1"))
  112. FCOXOHelper::A2X (s, nFlag) ;
  113. return FClamp(nFlag, 1, 100) ;
  114. }
  115. /// Gif's transparent color index in palette.
  116. static int GifSaveTransparentIndex (const FCImageProperty& rImageProp)
  117. {
  118. std::string s = rImageProp.QueryPropertyValue(PROPERTY_TAG_SAVE_FLAG) ;
  119. if (s.empty())
  120. s = rImageProp.QueryPropertyValue(PROPERTY_TAG_GIF_TRANSPARENT_INDEX) ;
  121. int nFlag = -1 ;
  122. if (!s.empty() && (s != "-1"))
  123. FCOXOHelper::A2X (s, nFlag) ;
  124. return nFlag ;
  125. }
  126. };
  127. #endif