Interface_ImageHandle.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. FILE * pf = fopen (szFileName, "rb") ;
  37. if (!pf)
  38. return false ;
  39. // get file length
  40. fseek (pf, 0, SEEK_END) ;
  41. const int nFileSize = (int)ftell(pf) ;
  42. if (nFileSize <= 0)
  43. {
  44. fclose (pf) ;
  45. return false ;
  46. }
  47. // read file into memory
  48. PCL_array<BYTE> pStart (nFileSize) ;
  49. fseek (pf, 0, SEEK_SET) ;
  50. fread (pStart.get(), 1, nFileSize, pf) ;
  51. fclose (pf) ;
  52. return LoadImageMemory (pStart.get(), nFileSize, rImageList, rImageProp) ;
  53. }
  54. /**
  55. * Load image from memory.
  56. * This function default return false, derived class should implement it.
  57. * @param rImageList : You must <B>new</B> FCObjImage and add them into rImageList, after add you can't delete them later.
  58. * @param rImageProp : optional, you can <B>new</B> and add property.
  59. */
  60. virtual bool LoadImageMemory (const BYTE* pStart, int nMemSize,
  61. PCL_Interface_Composite<FCObjImage>& rImageList,
  62. std::auto_ptr<FCImageProperty>& rImageProp)
  63. {
  64. return false ;
  65. }
  66. //@}
  67. /**
  68. * @name Save image.
  69. */
  70. //@{
  71. /**
  72. * Save image to file.
  73. * This function default return false, derived class should implement it.
  74. */
  75. virtual bool SaveImageFile (const char* szFileName,
  76. const std::deque<const FCObjImage*>& rImageList,
  77. const FCImageProperty& rImageProp)
  78. {
  79. return false ;
  80. }
  81. //@}
  82. virtual ~FCImageHandleBase() {}
  83. protected:
  84. /// Is use RLE arithmetic to compress image on saving tga (default not use).
  85. static bool TgaSaveUseRLE (const FCImageProperty& rImageProp)
  86. {
  87. std::string s = rImageProp.QueryPropertyValue(PROPERTY_TAG_SAVE_FLAG) ;
  88. if (s.empty())
  89. s = rImageProp.QueryPropertyValue(PROPERTY_TAG_TGA_USERLE) ;
  90. int nFlag = -1 ;
  91. if (!s.empty())
  92. FCOXOHelper::A2X (s, nFlag) ;
  93. return (nFlag != -1) ;
  94. }
  95. /// Jpeg's quality on saving jpeg (default is 82).
  96. static int JpegSaveQuality (const FCImageProperty& rImageProp)
  97. {
  98. std::string s = rImageProp.QueryPropertyValue(PROPERTY_TAG_SAVE_FLAG) ;
  99. if (s.empty())
  100. s = rImageProp.QueryPropertyValue(PROPERTY_TAG_JPEG_QUALITY) ;
  101. int nFlag = 82 ;
  102. if (!s.empty() && (s != "-1"))
  103. FCOXOHelper::A2X (s, nFlag) ;
  104. return FClamp(nFlag, 1, 100) ;
  105. }
  106. /// Gif's transparent color index in palette.
  107. static int GifSaveTransparentIndex (const FCImageProperty& rImageProp)
  108. {
  109. std::string s = rImageProp.QueryPropertyValue(PROPERTY_TAG_SAVE_FLAG) ;
  110. if (s.empty())
  111. s = rImageProp.QueryPropertyValue(PROPERTY_TAG_GIF_TRANSPARENT_INDEX) ;
  112. int nFlag = -1 ;
  113. if (!s.empty() && (s != "-1"))
  114. FCOXOHelper::A2X (s, nFlag) ;
  115. return nFlag ;
  116. }
  117. };
  118. #endif