Ramflx.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. #ifndef RAMFLX_H
  19. #define RAMFLX_H
  20. #include "image/image.h"
  21. #include "file/file.h"
  22. // Define the magic numbers for FLC and FLI files.
  23. #define FLX_MAGIC_FLI 0xAF11
  24. #define FLX_MAGIC_FLC 0xAF12
  25. // Define operation modes
  26. #define FLX_RETURN_DELTAS 0
  27. #define FLX_RETURN_FULL 1
  28. // Define data chunk types
  29. #define FLX_DATA_COLOR256 4
  30. #define FLX_DATA_SS2 7
  31. #define FLX_DATA_COLOR 11
  32. #define FLX_DATA_LC 12
  33. #define FLX_DATA_BLACK 13
  34. #define FLX_DATA_BRUN 15
  35. #define FLX_DATA_COPY 16
  36. #define FLX_DATA_PSTAMP 18
  37. // This is the value in the reserveA field of the header
  38. // when a flic with no delta compression is read.
  39. #define FLX_RSP_NODELTA 1
  40. #ifndef FLX_H // Already defined if FLX.H was included.
  41. // Define struct that describes everything in a FLC/FLI header. The header is
  42. // 128 bytes for both FLC and FLI files, but the usage is somewhat different.
  43. typedef struct tag_FLX_FILE_HDR
  44. {
  45. long lEntireFileSize; // Size of entire file, including header
  46. USHORT wMagic; // Magic number: FLC = $af12, FLI = $af11
  47. short sNumFrames; // Number of frames, not including ring. Max 4000.
  48. short sWidth; // Width in pixels (always 320 in FLI)
  49. short sHeight; // Height in pixels (always 200 in FLI)
  50. short sDepth; // Bits per pixel (always 8)
  51. USHORT sFlags; // FLC: set to 3 if properly written, FLI: always 0
  52. long lMilliPerFrame; // FLC: milliseconds between frames (4 bytes)
  53. // FLI: jiffies (1/70th) between frames (2 bytes)
  54. // The rest is for FLC files only -- for FLI files, it's all reserved.
  55. USHORT sReserveA; // Reserved -- set to zero
  56. ULONG dCreatedTime; // MS-DOS-formatted date and time of file's creation
  57. ULONG dCreator; // Serial number of Animator Pro program used to
  58. // create file -- $464c4942 is a good one ("FLIB")
  59. ULONG dUpdatedTime; // MS-DOS-formatted date and time of file's update
  60. ULONG dUpdater; // Serial number of Animator Pro program used to
  61. // update file -- $464c4942 is a good one ("FLIB")
  62. short sAspectX; // X-axis aspect ratio at which file was created
  63. short sAspectY; // Y-axis aspect ratio at which file was created
  64. UCHAR bReservedB[38]; // Reserved -- set to zeroes
  65. long lOffsetFrame1; // Offset from beginning of file to first frame chunk
  66. long lOffsetFrame2; // Offset from beginning of file to second frame chunk,
  67. // used when looping from ring back to second frame
  68. UCHAR bReservedC[40]; // Reserved -- set to zeroes
  69. } FLX_FILE_HDR;
  70. // Define struct that describes frame chunk header.
  71. typedef struct tag_FLX_FRAME_HDR
  72. {
  73. long lChunkSize; // Size of entire frame chunk, including header
  74. // and all subordinate chunks
  75. USHORT wType; // Frame header chunk id: always 0xF1FA
  76. short sNumSubChunks; // Number of subordinate chunks. 0 indicates that
  77. // this frame is identical to previous frame.
  78. UCHAR bReserved[8]; // Reserved
  79. } FLX_FRAME_HDR;
  80. // Define struct that describes data chunk header.
  81. typedef struct tag_FLX_DATA_HDR
  82. {
  83. long lChunkSize; // Size of frame data chunk, including header
  84. USHORT wType; // Type of frame data chunk
  85. // NOTE: The actual data follows these two items, but is not
  86. // included in this struct because it has a variable size!
  87. } FLX_DATA_HDR;
  88. // Define struct that describes RGB color data as used by a FLC/FLI
  89. typedef struct tag_FLX_RGB
  90. {
  91. UCHAR bR;
  92. UCHAR bG;
  93. UCHAR bB;
  94. } FLX_RGB;
  95. #endif // FLX_H
  96. // Define class
  97. class CRamFlx
  98. {
  99. public:
  100. // Default constructor. If this is used, then Setup() must be called before
  101. // any other function can be called.
  102. CRamFlx(void);
  103. // Destructor.
  104. ~CRamFlx();
  105. // Open an existing FLC/FLI file for reading. You can optionally get a copy of
  106. // the file header and can optionally have your buf memory allocated for you.
  107. // Returns 0 if successfull, non-zero otherwise.
  108. short Open(
  109. char* pszFileName, // Full path and filename of flic file
  110. FLX_FILE_HDR* pfilehdr, // Copy of header returned here if not NULL
  111. CImage* pbuf); // Memory allocated within struct if not NULL
  112. // Close the currently open file (if any).
  113. // Returns 0 if successfull, non-zero otherwise.
  114. // Modified 10/20/94 to accommodate buffer pointer
  115. short Close(CImage* pbuf = NULL);
  116. // Get copy of flic file header (file must have been opened or created). When
  117. // creating a new file, certain fields are not valid until the file is closed.
  118. // Returns 0 if successfull, non-zero otherwise.
  119. short GetHeader(FLX_FILE_HDR* pHeader);
  120. // Get the current frame number. When reading, this is the frame that was
  121. // last read. When writing, this is the frame that was last written. In both
  122. // cases, a value of 0 indicates that no frames have been read or written.
  123. // Otherwise, the number will be from 1 to n.
  124. short GetFrameNum(void);
  125. // Read the specified flic frame (1 to n, anything else is an error). The
  126. // time it takes to get the frame is proportional to the number of frames
  127. // between it and the last frame that was read. In simple mode, if the same
  128. // frame is requested more than once in a row, that frame is simply returned
  129. // each time. In non-simple mode, requesting the same frame again requires
  130. // us to restart the animation and work our way up to that frame again.
  131. // Returns 0 if successfull, non-zero otherwise.
  132. short ReadFrame(
  133. short sFrameNum, // Frame number to be read
  134. CImage* pbufRead); // Buffer for frame being read
  135. // Read the next flic frame (if flic was just opened, this will read frame 1).
  136. // Returns 0 if successfull, non-zero otherwise.
  137. short ReadNextFrame(
  138. CImage* pbufRead); // Buffer for frame being read
  139. // Return the current buffer state
  140. CImage* GetBuffer(void)
  141. { return &m_imagePrev; };
  142. // Create a CImage based on the specified width, height, and number of colors.
  143. // Returns 0 if successfull, non-zero otherwise.
  144. short CreateBuf(CImage* pimage, long lWidth, long lHeight, short sColors);
  145. // Destroy a CImage that was previously created using CreateBuf().
  146. // The CImage must not be used after this call!
  147. void DestroyBuf(CImage* pimage);
  148. // The m_bPixelsModified is set when a frame is read into the buffer
  149. // and pixels in this frame have been modified from the previous screen
  150. short IsPixelsModified(void)
  151. { return m_sPixelsModified; };
  152. // The m_bColorsModified is set when a frame is read into the buffer
  153. // and palette in this frame is different from the previous screen
  154. short IsColorsModified(void)
  155. { return m_sColorsModified; };
  156. // You can provide your own file (currently pointing to the beginning of
  157. // a FLX frame) to allow this module to decompress your data. Oh happy,
  158. // happy day.
  159. // Returns 0 on success.
  160. static short DoReadFrame(CImage* pbufRead, CNFile* pfile,
  161. short* psPixelsModified, short* psColorsModified);
  162. private:
  163. void Restart(void);
  164. short DoReadFrame(CImage* pbufRead);
  165. // Work horse functions.
  166. static short ReadDataColor(CImage* pbufRead, short sDataType, CNFile* pfile,
  167. short* psColorsModified);
  168. static short ReadDataBlack(CImage* pbufRead, short* psPixelsModified);
  169. static short ReadDataCopy(CImage* pbufRead, CNFile* pfile, short* psPixelsModified);
  170. static short ReadDataBRun(CImage* pbufRead, CNFile* pfile, short* psPixelsModified);
  171. static short ReadDataLC(CImage* pbufRead, CNFile* pfile, short* psPixelsModified);
  172. static short ReadDataSS2(CImage* pbufRead, CNFile* pfile, short* psPixelsModified);
  173. short ReadHeader(CNFile* pfile);
  174. void ClearHeader(void);
  175. void InitBuf(CImage* pbuf);
  176. short AllocBuf(CImage* pbuf, long lWidth, long lHeight, short sColors);
  177. void FreeBuf(CImage* pbuf);
  178. void CopyBuf(CImage* pbufDst, CImage* pbufSrc);
  179. short CreateFramePointers(void);
  180. private:
  181. short m_sOpenForRead; // TRUE if file is open for read
  182. FLX_FILE_HDR m_filehdr; // File header
  183. CImage m_imagePrev; // Buffer for previous frame
  184. short m_sNoDelta; // Flag to signal no delta compression
  185. UCHAR* m_pucFlxBuf; // RAM buffer for flic frames from .FLC file
  186. long* m_plFrames; // Indexices of frames in flics
  187. // with no delta compression
  188. short m_sReadColors; // Whether or not to read colors from flic
  189. short m_sReadPixels; // Whether or not to read pixels from flic
  190. short m_sPixelsModified;// Whether the pixels were modified
  191. short m_sColorsModified;// Whether the colors were modified
  192. short m_sFrameNum; // Current frame number, 1 to n, 0 means none
  193. CNFile m_file; // File stream (for buffered file I/O)
  194. };
  195. #endif // RAMFLX_H