TriggerRgn.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. // TriggerRgn.H
  19. // Project: Nostril (aka Postal)
  20. //
  21. // History:
  22. // 05/01/97 JMI Started.
  23. //
  24. // 05/01/97 JMI Added Create(), Destroy(), SetMode(), and Macros and
  25. // Mode enum.
  26. //
  27. // 05/02/97 JMI SetMode() was using the return type from RImage::Convert
  28. // wrong.
  29. // Also, added use of SetConvertFromFSPR1() which allows
  30. // us to control what colors the FSPR1 takes on when
  31. // converted to another format.
  32. //
  33. // 05/09/97 JMI Added u16InstanceId so the multigrid for pylon triggers
  34. // can contain a mapping from pylon ID to instance ID.
  35. // Load() and Save() read and write this value.
  36. //
  37. //////////////////////////////////////////////////////////////////////////////
  38. //
  39. // This structure defines a trigger region which consists, basically, of an
  40. // image, the 'set' pixels of which represent the region, and a location for
  41. // the image in the greater universe (a realm in this case).
  42. //
  43. //////////////////////////////////////////////////////////////////////////////
  44. #ifndef TRIGGERRGN_H
  45. #define TRIGGERRGN_H
  46. //////////////////////////////////////////////////////////////////////////////
  47. // C Headers -- Must be included before RSPiX.h b/c RSPiX utilizes SHMalloc.
  48. //////////////////////////////////////////////////////////////////////////////
  49. ///////////////////////////////////////////////////////////////////////////////
  50. // RSPiX Headers.
  51. ///////////////////////////////////////////////////////////////////////////////
  52. #include "RSPiX.h"
  53. //////////////////////////////////////////////////////////////////////////////
  54. // Postal headers.
  55. //////////////////////////////////////////////////////////////////////////////
  56. //////////////////////////////////////////////////////////////////////////////
  57. // Macros.
  58. //////////////////////////////////////////////////////////////////////////////
  59. //////////////////////////////////////////////////////////////////////////////
  60. // Protos.
  61. //////////////////////////////////////////////////////////////////////////////
  62. //////////////////////////////////////////////////////////////////////////////
  63. // Typedefs.
  64. //////////////////////////////////////////////////////////////////////////////
  65. struct TriggerRgn
  66. {
  67. ///////////////////////////////////////////////////////////////////////////
  68. // Typedefs/enums.
  69. ///////////////////////////////////////////////////////////////////////////
  70. enum // Macros.
  71. {
  72. MaxRgnWidth = 300,
  73. MaxRgnHeight = 300
  74. };
  75. typedef enum
  76. {
  77. Edit, // Indicates edittable mode for modifying region.
  78. Storage // Indicates storage mode for memory conservation.
  79. } Mode;
  80. ///////////////////////////////////////////////////////////////////////////
  81. // Instantiable data.
  82. ///////////////////////////////////////////////////////////////////////////
  83. short sX;
  84. short sY;
  85. RImage* pimRgn;
  86. U16 u16InstanceId;
  87. ///////////////////////////////////////////////////////////////////////////
  88. // Constructor.
  89. ///////////////////////////////////////////////////////////////////////////
  90. TriggerRgn()
  91. {
  92. pimRgn = NULL;
  93. }
  94. ///////////////////////////////////////////////////////////////////////////
  95. // Destructor.
  96. ///////////////////////////////////////////////////////////////////////////
  97. ~TriggerRgn()
  98. {
  99. Destroy();
  100. }
  101. ///////////////////////////////////////////////////////////////////////////
  102. // Destroy.
  103. ///////////////////////////////////////////////////////////////////////////
  104. void Destroy(void)
  105. {
  106. delete pimRgn;
  107. pimRgn = NULL;
  108. }
  109. ///////////////////////////////////////////////////////////////////////////
  110. // Create.
  111. ///////////////////////////////////////////////////////////////////////////
  112. short Create( // Returns 0 on success.
  113. short sWidth, // In: Max width of region (width of image).
  114. short sHeight) // In: Max height of region (height of image).
  115. {
  116. short sRes = 0; // Assume success.
  117. Destroy();
  118. pimRgn = new RImage;
  119. if (pimRgn != NULL)
  120. {
  121. sRes = pimRgn->CreateImage( // Returns 0 if successful.
  122. sWidth, // Width of new buffer.
  123. sHeight, // Height of new buffer.
  124. RImage::BMP8); // Type of new buffer.
  125. // If any errors occurred after allocation . . .
  126. if (sRes != 0)
  127. {
  128. Destroy();
  129. }
  130. }
  131. else
  132. {
  133. TRACE("Create(): Failed to allocate new RImage.\n");
  134. sRes = -1;
  135. }
  136. return sRes;
  137. }
  138. ///////////////////////////////////////////////////////////////////////////
  139. // Set current mode { Edit or Storage }.
  140. // Edit mode is the 8 bit mode used when editing the region.
  141. // Storage is whatever compressed mode is used for the sake of saving mem.
  142. ///////////////////////////////////////////////////////////////////////////
  143. short SetMode(
  144. Mode mode) // In: New mode { Edit, Storage }.
  145. {
  146. short sRes = 0; // Assume success.
  147. // If we have no image . . .
  148. if (pimRgn == NULL)
  149. {
  150. sRes = Create(MaxRgnWidth, MaxRgnHeight);
  151. }
  152. // If successful so far . . .
  153. if (sRes == 0)
  154. {
  155. switch (mode)
  156. {
  157. case Edit:
  158. // This allows us to choose the colors used when coming out
  159. // of the FSPR1 format.
  160. SetConvertFromFSPR1
  161. (
  162. 250, // u32ForeColor, // Make it this color
  163. TRUE, // sTransparent = TRUE, // 1 or 2 color?
  164. 0 // u32BackColor = (U32)0 // matters only if sTransparent = FALSE
  165. );
  166. if (pimRgn->Convert(RImage::BMP8) != RImage::BMP8)
  167. {
  168. sRes = -1;
  169. }
  170. break;
  171. case Storage:
  172. if (pimRgn->Convert(RImage::FSPR1) != RImage::FSPR1)
  173. {
  174. sRes = -1;
  175. }
  176. break;
  177. }
  178. }
  179. return sRes;
  180. }
  181. ///////////////////////////////////////////////////////////////////////////
  182. // Load.
  183. ///////////////////////////////////////////////////////////////////////////
  184. short Load( // Returns 0 on success.
  185. RFile* pfile) // In: File to load from.
  186. {
  187. short sRes = 0; // Assume success.
  188. Destroy();
  189. // Always a boolean indicating whether we exist . . .
  190. short sExist = FALSE; // Safety.
  191. if (pfile->Read(&sExist) == 1)
  192. {
  193. if (sExist != FALSE)
  194. {
  195. pimRgn = new RImage;
  196. if (pimRgn != NULL)
  197. {
  198. // Read position.
  199. pfile->Read(&sX);
  200. pfile->Read(&sY);
  201. // Read instance ID.
  202. pfile->Read(&u16InstanceId);
  203. // Load image.
  204. sRes = pimRgn->Load(pfile);
  205. if (sRes == 0)
  206. {
  207. // Success.
  208. }
  209. else
  210. {
  211. TRACE("Load(): RImage::Load() failed.\n");
  212. delete pimRgn;
  213. pimRgn = NULL;
  214. }
  215. }
  216. else
  217. {
  218. TRACE("Load(): Failed to allocate new RImage.\n");
  219. sRes = -2;
  220. }
  221. }
  222. }
  223. else
  224. {
  225. TRACE("Load(): Failed to read existence flag.\n");
  226. sRes = -1;
  227. }
  228. return sRes;
  229. }
  230. ///////////////////////////////////////////////////////////////////////////
  231. // Save.
  232. ///////////////////////////////////////////////////////////////////////////
  233. short Save( // Returns 0 on success.
  234. RFile* pfile) // In: File to save to.
  235. {
  236. short sRes = 0; // Assume success.
  237. // Always a boolean indicating whether we exist . . .
  238. short sExist = (pimRgn != NULL) ? TRUE : FALSE;
  239. if (pfile->Write(sExist) == 1)
  240. {
  241. if (pimRgn != NULL)
  242. {
  243. // Write position.
  244. pfile->Write(sX);
  245. pfile->Write(sY);
  246. // Write instance ID.
  247. pfile->Write(&u16InstanceId);
  248. // Save image.
  249. sRes = pimRgn->Save(pfile);
  250. if (sRes == 0)
  251. {
  252. // Success.
  253. }
  254. else
  255. {
  256. TRACE("Save(): RImage::Save() failed.\n");
  257. }
  258. }
  259. }
  260. else
  261. {
  262. TRACE("Save(): Failed to write existence flag.\n");
  263. sRes = -1;
  264. }
  265. return sRes;
  266. }
  267. };
  268. #endif // TRIGGERRGN_H
  269. //////////////////////////////////////////////////////////////////////////////
  270. // EOF
  271. //////////////////////////////////////////////////////////////////////////////