write.inc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**--------------------------------------------------------------------------**\
  2. =================================
  3. y_bitmap - Generate bitmaps.
  4. =================================
  5. Description:
  6. Code to generate images on the server in the bitmap format. This is by far
  7. the simplest format to write to as it is just a huge array of colours (at
  8. least 24-bit bitmaps are, and we only do them).
  9. This file is responsible for writing the generated data out to a file.
  10. Legal:
  11. Version: MPL 1.1
  12. The contents of this file are subject to the Mozilla Public License Version
  13. 1.1 (the "License"); you may not use this file except in compliance with
  14. the License. You may obtain a copy of the License at
  15. http://www.mozilla.org/MPL/
  16. Software distributed under the License is distributed on an "AS IS" basis,
  17. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  18. for the specific language governing rights and limitations under the
  19. License.
  20. The Original Code is the YSI utils include.
  21. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  22. Portions created by the Initial Developer are Copyright (C) 2011
  23. the Initial Developer. All Rights Reserved.
  24. Contributors:
  25. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  26. Thanks:
  27. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  28. ZeeX - Very productive conversations.
  29. koolk - IsPlayerinAreaEx code.
  30. TheAlpha - Danish translation.
  31. breadfish - German translation.
  32. Fireburn - Dutch translation.
  33. yom - French translation.
  34. 50p - Polish translation.
  35. Zamaroht - Spanish translation.
  36. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  37. for me to strive to better.
  38. Pixels^ - Running XScripters where the idea was born.
  39. Matite - Pestering me to release it and using it.
  40. Very special thanks to:
  41. Thiadmer - PAWN, whose limits continue to amaze me!
  42. Kye/Kalcor - SA:MP.
  43. SA:MP Team past, present and future - SA:MP.
  44. Version:
  45. 0.1
  46. Changelog:
  47. 29/03/13:
  48. First version.
  49. Functions:
  50. Stock:
  51. -
  52. Inline:
  53. -
  54. Variables:
  55. Global:
  56. -
  57. \**--------------------------------------------------------------------------**/
  58. static stock
  59. YSI_g_sFileHeader[] =
  60. {
  61. 'B', 'M', // bfType (Just "BM" for Windows BMP).
  62. 1, 1, 1, 1, // bfSize (File size in bytes).
  63. 0, 0, // bfReserved1 (Unused).
  64. 0, 0, // bfReserved2 (Unused).
  65. 2, 2, 2, 2 // bfOffBits (Offset to the start of the data).
  66. },
  67. YSI_g_sInfoHeader[] =
  68. {
  69. 40, 0, 0, 0, // biSize (This header's size).
  70. 4, 4, 4, 4, // biWidth (Image width).
  71. 5, 5, 5, 5, // biHeight (Image height).
  72. 1, 0, // biPlanes (1 "plane").
  73. 24, 0, // biBitCount (24-bit image).
  74. 0, 0, 0, 0, // biCompression (Unused).
  75. 0, 0, 0, 0, // biSizeImage (Unused).
  76. 0, 0, 0, 0, // biXPelsPerMeter (Unused).
  77. 0, 0, 0, 0, // biYPelsPerMeter (Unused).
  78. 0, 0, 0, 0, // biClrUsed (Unused).
  79. 0, 0, 0, 0 // biClrImportant (Unused).
  80. };
  81. static stock MKLE32(dest[], num, &idx = 0)
  82. {
  83. dest[idx++] = num & 0xFF;
  84. dest[idx++] = num >>> 8 & 0xFF;
  85. dest[idx++] = num >>> 16 & 0xFF;
  86. dest[idx++] = num >>> 24 & 0xFF;
  87. }
  88. static stock MK24(dest[], num, &idx = 0)
  89. {
  90. //if (num != -1) printf("colour %d", num);
  91. dest[idx++] = num >>> 8 & 0xFF;
  92. dest[idx++] = num >>> 16 & 0xFF;
  93. dest[idx++] = num >>> 24 & 0xFF;
  94. }
  95. static stock Bitmap_PadRow(dest[], &idx)
  96. {
  97. while (idx & 0x03)
  98. {
  99. dest[idx++] = 0;
  100. }
  101. }
  102. static stock Bitmap_WriteHeader(Bitmap:ctx, File:bmp)
  103. {
  104. MKLE32(YSI_g_sInfoHeader[4], Bitmap_Width(ctx));
  105. MKLE32(YSI_g_sInfoHeader[8], Bitmap_Height(ctx));
  106. // Pad to a 4 byte boundary with 3 bytes per pixel.
  107. MKLE32(YSI_g_sFileHeader[2], ceildiv(Bitmap_Width(ctx) * 3, 4) * 4 * Bitmap_Height(ctx) + sizeof (YSI_g_sFileHeader) + sizeof (YSI_g_sInfoHeader));
  108. MKLE32(YSI_g_sFileHeader[10], sizeof (YSI_g_sFileHeader) + sizeof (YSI_g_sInfoHeader));
  109. for (new j = 0; j != sizeof (YSI_g_sFileHeader); ++j)
  110. {
  111. fputchar(bmp, YSI_g_sFileHeader[j], false);
  112. }
  113. for (new j = 0; j != sizeof (YSI_g_sInfoHeader); ++j)
  114. {
  115. fputchar(bmp, YSI_g_sInfoHeader[j], false);
  116. }
  117. }
  118. static stock Bitmap_WriteBlock(File:bmp, buf[], len)
  119. {
  120. for (new i = 0; i != len; ++i)
  121. {
  122. fputchar(bmp, buf[i], false);
  123. }
  124. }
  125. static stock Bitmap_WriteBody(Bitmap:ctx, File:bmp)
  126. {
  127. // Write 4 pixels in to 3 blocks.
  128. static
  129. sWriteBlock[12];
  130. new
  131. width = Bitmap_Width(ctx),
  132. w2 = width & ~0x3,
  133. height = Bitmap_Height(ctx);
  134. //width &= 0x3;
  135. //for (new y = height; y-- > 0; )
  136. for (new y = height; y-- != 0; )
  137. {
  138. // Go through the array backwards (bottom to top).
  139. new
  140. x = 0;
  141. //printf(": %d = %d", Bitmap_IndexCtx(ctx, 10, y), Bitmap_ReadCtx(ctx, 10, y));
  142. //printf(": %d = %d", Bitmap_IndexInt(ctx, 10, y), Bitmap_ReadInt(ctx, 10, y));
  143. for ( ; x != w2; x += 4)
  144. {
  145. new
  146. i = 0;
  147. MK24(sWriteBlock, Bitmap_ReadInt(ctx, x, width, y), i);
  148. MK24(sWriteBlock, Bitmap_ReadInt(ctx, x + 1, width, y), i);
  149. MK24(sWriteBlock, Bitmap_ReadInt(ctx, x + 2, width, y), i);
  150. MK24(sWriteBlock, Bitmap_ReadInt(ctx, x + 3, width, y), i);
  151. Bitmap_WriteBlock(bmp, sWriteBlock, 12);
  152. }
  153. switch (width & 0x03)
  154. {
  155. case 1:
  156. {
  157. // Write 1, pad 1.
  158. MK24(sWriteBlock[0], Bitmap_ReadInt(ctx, x, width, y));
  159. sWriteBlock[3] = 0;
  160. Bitmap_WriteBlock(bmp, sWriteBlock, 4);
  161. }
  162. case 2:
  163. {
  164. // Write 2, pad 2.
  165. MK24(sWriteBlock[0], Bitmap_ReadInt(ctx, x, width, y));
  166. MK24(sWriteBlock[3], Bitmap_ReadInt(ctx, x + 1, width, y));
  167. sWriteBlock[6] = 0;
  168. sWriteBlock[7] = 0;
  169. Bitmap_WriteBlock(bmp, sWriteBlock, 8);
  170. }
  171. case 3:
  172. {
  173. // Write 3, pad 3.
  174. MK24(sWriteBlock[0], Bitmap_ReadInt(ctx, x, width, y));
  175. MK24(sWriteBlock[3], Bitmap_ReadInt(ctx, x + 1, width, y));
  176. MK24(sWriteBlock[6], Bitmap_ReadInt(ctx, x + 2, width, y));
  177. sWriteBlock[9] = 0;
  178. sWriteBlock[10] = 0;
  179. sWriteBlock[11] = 0;
  180. Bitmap_WriteBlock(bmp, sWriteBlock, 12);
  181. }
  182. }
  183. }
  184. }
  185. stock bool:Bitmap_Write(Bitmap:ctx, const file[])
  186. {
  187. if (fexist(file)) fremove(file);
  188. new
  189. File:bmp = fopen(file, io_write);
  190. if (!bmp) return false;
  191. Bitmap_WriteHeader(ctx, bmp);
  192. Bitmap_WriteBody(ctx, bmp);
  193. fclose(bmp);
  194. return true;
  195. }