palcommon.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 3; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2009, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // All rights reserved.
  5. //
  6. // Based on PAL MapEditor by Baldur.
  7. //
  8. // This file is part of SDLPAL.
  9. //
  10. // SDLPAL is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #include "palcommon.h"
  24. #include "global.h"
  25. #include "palcfg.h"
  26. INT
  27. PAL_RLEBlitToSurface(
  28. LPCBITMAPRLE lpBitmapRLE,
  29. SDL_Surface *lpDstSurface,
  30. PAL_POS pos
  31. )
  32. /*++
  33. Purpose:
  34. Blit an RLE-compressed bitmap to an SDL surface.
  35. NOTE: Assume the surface is already locked, and the surface is a 8-bit one.
  36. Parameters:
  37. [IN] lpBitmapRLE - pointer to the RLE-compressed bitmap to be decoded.
  38. [OUT] lpDstSurface - pointer to the destination SDL surface.
  39. [IN] pos - position of the destination area.
  40. Return value:
  41. 0 = success, -1 = error.
  42. --*/
  43. {
  44. UINT i, j;
  45. INT x, y;
  46. UINT uiLen = 0;
  47. UINT uiWidth = 0;
  48. UINT uiHeight = 0;
  49. BYTE T;
  50. INT dx = PAL_X(pos);
  51. INT dy = PAL_Y(pos);
  52. //
  53. // Check for NULL pointer.
  54. //
  55. if (lpBitmapRLE == NULL || lpDstSurface == NULL)
  56. {
  57. return -1;
  58. }
  59. //
  60. // Skip the 0x00000002 in the file header.
  61. //
  62. if (lpBitmapRLE[0] == 0x02 && lpBitmapRLE[1] == 0x00 &&
  63. lpBitmapRLE[2] == 0x00 && lpBitmapRLE[3] == 0x00)
  64. {
  65. lpBitmapRLE += 4;
  66. }
  67. //
  68. // Get the width and height of the bitmap.
  69. //
  70. uiWidth = lpBitmapRLE[0] | (lpBitmapRLE[1] << 8);
  71. uiHeight = lpBitmapRLE[2] | (lpBitmapRLE[3] << 8);
  72. //
  73. // Calculate the total length of the bitmap.
  74. // The bitmap is 8-bpp, each pixel will use 1 byte.
  75. //
  76. uiLen = uiWidth * uiHeight;
  77. //
  78. // Start decoding and blitting the bitmap.
  79. //
  80. lpBitmapRLE += 4;
  81. for (i = 0; i < uiLen;)
  82. {
  83. T = *lpBitmapRLE++;
  84. if ((T & 0x80) && T <= 0x80 + uiWidth)
  85. {
  86. i += T - 0x80;
  87. }
  88. else
  89. {
  90. for (j = 0; j < T; j++)
  91. {
  92. //
  93. // Calculate the destination coordination.
  94. // FIXME: This could be optimized
  95. //
  96. y = (i + j) / uiWidth + dy;
  97. x = (i + j) % uiWidth + dx;
  98. //
  99. // Skip the points which are out of the surface.
  100. //
  101. if (x < 0)
  102. {
  103. j += -x - 1;
  104. continue;
  105. }
  106. else if (x >= lpDstSurface->w)
  107. {
  108. j += x - lpDstSurface->w;
  109. continue;
  110. }
  111. if (y < 0)
  112. {
  113. j += -y * uiWidth - 1;
  114. continue;
  115. }
  116. else if (y >= lpDstSurface->h)
  117. {
  118. goto end; // No more pixels needed, break out
  119. }
  120. //
  121. // Put the pixel onto the surface (FIXME: inefficient).
  122. //
  123. ((LPBYTE)lpDstSurface->pixels)[y * lpDstSurface->pitch + x] = lpBitmapRLE[j];
  124. }
  125. lpBitmapRLE += T;
  126. i += T;
  127. }
  128. }
  129. end:
  130. //
  131. // Success
  132. //
  133. return 0;
  134. }
  135. INT
  136. PAL_RLEBlitWithColorShift(
  137. LPCBITMAPRLE lpBitmapRLE,
  138. SDL_Surface *lpDstSurface,
  139. PAL_POS pos,
  140. INT iColorShift
  141. )
  142. /*++
  143. Purpose:
  144. Blit an RLE-compressed bitmap to an SDL surface.
  145. NOTE: Assume the surface is already locked, and the surface is a 8-bit one.
  146. Parameters:
  147. [IN] lpBitmapRLE - pointer to the RLE-compressed bitmap to be decoded.
  148. [OUT] lpDstSurface - pointer to the destination SDL surface.
  149. [IN] pos - position of the destination area.
  150. [IN] iColorShift - shift the color by this value.
  151. Return value:
  152. 0 = success, -1 = error.
  153. --*/
  154. {
  155. UINT i, j;
  156. INT x, y;
  157. UINT uiLen = 0;
  158. UINT uiWidth = 0;
  159. UINT uiHeight = 0;
  160. BYTE T, b;
  161. INT dx = PAL_X(pos);
  162. INT dy = PAL_Y(pos);
  163. //
  164. // Check for NULL pointer.
  165. //
  166. if (lpBitmapRLE == NULL || lpDstSurface == NULL)
  167. {
  168. return -1;
  169. }
  170. //
  171. // Skip the 0x00000002 in the file header.
  172. //
  173. if (lpBitmapRLE[0] == 0x02 && lpBitmapRLE[1] == 0x00 &&
  174. lpBitmapRLE[2] == 0x00 && lpBitmapRLE[3] == 0x00)
  175. {
  176. lpBitmapRLE += 4;
  177. }
  178. //
  179. // Get the width and height of the bitmap.
  180. //
  181. uiWidth = lpBitmapRLE[0] | (lpBitmapRLE[1] << 8);
  182. uiHeight = lpBitmapRLE[2] | (lpBitmapRLE[3] << 8);
  183. //
  184. // Calculate the total length of the bitmap.
  185. // The bitmap is 8-bpp, each pixel will use 1 byte.
  186. //
  187. uiLen = uiWidth * uiHeight;
  188. //
  189. // Start decoding and blitting the bitmap.
  190. //
  191. lpBitmapRLE += 4;
  192. for (i = 0; i < uiLen;)
  193. {
  194. T = *lpBitmapRLE++;
  195. if ((T & 0x80) && T <= 0x80 + uiWidth)
  196. {
  197. i += T - 0x80;
  198. }
  199. else
  200. {
  201. for (j = 0; j < T; j++)
  202. {
  203. //
  204. // Calculate the destination coordination.
  205. // FIXME: This could be optimized
  206. //
  207. y = (i + j) / uiWidth + dy;
  208. x = (i + j) % uiWidth + dx;
  209. //
  210. // Skip the points which are out of the surface.
  211. //
  212. if (x < 0)
  213. {
  214. j += -x - 1;
  215. continue;
  216. }
  217. else if (x >= lpDstSurface->w)
  218. {
  219. j += x - lpDstSurface->w;
  220. continue;
  221. }
  222. if (y < 0)
  223. {
  224. j += -y * uiWidth - 1;
  225. continue;
  226. }
  227. else if (y >= lpDstSurface->h)
  228. {
  229. goto end; // No more pixels needed, break out
  230. }
  231. //
  232. // Put the pixel onto the surface (FIXME: inefficient).
  233. //
  234. b = (lpBitmapRLE[j] & 0x0F);
  235. if ((INT)b + iColorShift > 0x0F)
  236. {
  237. b = 0x0F;
  238. }
  239. else if ((INT)b + iColorShift < 0)
  240. {
  241. b = 0;
  242. }
  243. else
  244. {
  245. b += iColorShift;
  246. }
  247. ((LPBYTE)lpDstSurface->pixels)[y * lpDstSurface->pitch + x] =
  248. (b | (lpBitmapRLE[j] & 0xF0));
  249. }
  250. lpBitmapRLE += T;
  251. i += T;
  252. }
  253. }
  254. end:
  255. //
  256. // Success
  257. //
  258. return 0;
  259. }
  260. INT
  261. PAL_RLEBlitMonoColor(
  262. LPCBITMAPRLE lpBitmapRLE,
  263. SDL_Surface *lpDstSurface,
  264. PAL_POS pos,
  265. BYTE bColor,
  266. INT iColorShift
  267. )
  268. /*++
  269. Purpose:
  270. Blit an RLE-compressed bitmap to an SDL surface in mono-color form.
  271. NOTE: Assume the surface is already locked, and the surface is a 8-bit one.
  272. Parameters:
  273. [IN] lpBitmapRLE - pointer to the RLE-compressed bitmap to be decoded.
  274. [OUT] lpDstSurface - pointer to the destination SDL surface.
  275. [IN] pos - position of the destination area.
  276. [IN] bColor - the color to be used while drawing.
  277. [IN] iColorShift - shift the color by this value.
  278. Return value:
  279. 0 = success, -1 = error.
  280. --*/
  281. {
  282. UINT i, j;
  283. INT x, y;
  284. UINT uiLen = 0;
  285. UINT uiWidth = 0;
  286. UINT uiHeight = 0;
  287. BYTE T, b;
  288. INT dx = PAL_X(pos);
  289. INT dy = PAL_Y(pos);
  290. //
  291. // Check for NULL pointer.
  292. //
  293. if (lpBitmapRLE == NULL || lpDstSurface == NULL)
  294. {
  295. return -1;
  296. }
  297. //
  298. // Skip the 0x00000002 in the file header.
  299. //
  300. if (lpBitmapRLE[0] == 0x02 && lpBitmapRLE[1] == 0x00 &&
  301. lpBitmapRLE[2] == 0x00 && lpBitmapRLE[3] == 0x00)
  302. {
  303. lpBitmapRLE += 4;
  304. }
  305. //
  306. // Get the width and height of the bitmap.
  307. //
  308. uiWidth = lpBitmapRLE[0] | (lpBitmapRLE[1] << 8);
  309. uiHeight = lpBitmapRLE[2] | (lpBitmapRLE[3] << 8);
  310. //
  311. // Calculate the total length of the bitmap.
  312. // The bitmap is 8-bpp, each pixel will use 1 byte.
  313. //
  314. uiLen = uiWidth * uiHeight;
  315. //
  316. // Start decoding and blitting the bitmap.
  317. //
  318. lpBitmapRLE += 4;
  319. bColor &= 0xF0;
  320. for (i = 0; i < uiLen;)
  321. {
  322. T = *lpBitmapRLE++;
  323. if ((T & 0x80) && T <= 0x80 + uiWidth)
  324. {
  325. i += T - 0x80;
  326. }
  327. else
  328. {
  329. for (j = 0; j < T; j++)
  330. {
  331. //
  332. // Calculate the destination coordination.
  333. // FIXME: This could be optimized
  334. //
  335. y = (i + j) / uiWidth + dy;
  336. x = (i + j) % uiWidth + dx;
  337. //
  338. // Skip the points which are out of the surface.
  339. //
  340. if (x < 0)
  341. {
  342. j += -x - 1;
  343. continue;
  344. }
  345. else if (x >= lpDstSurface->w)
  346. {
  347. j += x - lpDstSurface->w;
  348. continue;
  349. }
  350. if (y < 0)
  351. {
  352. j += -y * uiWidth - 1;
  353. continue;
  354. }
  355. else if (y >= lpDstSurface->h)
  356. {
  357. goto end; // No more pixels needed, break out
  358. }
  359. //
  360. // Put the pixel onto the surface (FIXME: inefficient).
  361. //
  362. b = lpBitmapRLE[j] & 0x0F;
  363. if ((INT)b + iColorShift > 0x0F)
  364. {
  365. b = 0x0F;
  366. }
  367. else if ((INT)b + iColorShift < 0)
  368. {
  369. b = 0;
  370. }
  371. else
  372. {
  373. b += iColorShift;
  374. }
  375. ((LPBYTE)lpDstSurface->pixels)[y * lpDstSurface->pitch + x] = (b | bColor);
  376. }
  377. lpBitmapRLE += T;
  378. i += T;
  379. }
  380. }
  381. end:
  382. //
  383. // Success
  384. //
  385. return 0;
  386. }
  387. INT
  388. PAL_FBPBlitToSurface(
  389. LPBYTE lpBitmapFBP,
  390. SDL_Surface *lpDstSurface
  391. )
  392. /*++
  393. Purpose:
  394. Blit an uncompressed bitmap in FBP.MKF to an SDL surface.
  395. NOTE: Assume the surface is already locked, and the surface is a 8-bit 320x200 one.
  396. Parameters:
  397. [IN] lpBitmapFBP - pointer to the RLE-compressed bitmap to be decoded.
  398. [OUT] lpDstSurface - pointer to the destination SDL surface.
  399. Return value:
  400. 0 = success, -1 = error.
  401. --*/
  402. {
  403. int x, y;
  404. LPBYTE p;
  405. if (lpBitmapFBP == NULL || lpDstSurface == NULL ||
  406. lpDstSurface->w != 320 || lpDstSurface->h != 200)
  407. {
  408. return -1;
  409. }
  410. //
  411. // simply copy everything to the surface
  412. //
  413. for (y = 0; y < 200; y++)
  414. {
  415. p = (LPBYTE)(lpDstSurface->pixels) + y * lpDstSurface->pitch;
  416. for (x = 0; x < 320; x++)
  417. {
  418. *(p++) = *(lpBitmapFBP++);
  419. }
  420. }
  421. return 0;
  422. }
  423. INT
  424. PAL_RLEGetWidth(
  425. LPCBITMAPRLE lpBitmapRLE
  426. )
  427. /*++
  428. Purpose:
  429. Get the width of an RLE-compressed bitmap.
  430. Parameters:
  431. [IN] lpBitmapRLE - pointer to an RLE-compressed bitmap.
  432. Return value:
  433. Integer value which indicates the height of the bitmap.
  434. --*/
  435. {
  436. if (lpBitmapRLE == NULL)
  437. {
  438. return 0;
  439. }
  440. //
  441. // Skip the 0x00000002 in the file header.
  442. //
  443. if (lpBitmapRLE[0] == 0x02 && lpBitmapRLE[1] == 0x00 &&
  444. lpBitmapRLE[2] == 0x00 && lpBitmapRLE[3] == 0x00)
  445. {
  446. lpBitmapRLE += 4;
  447. }
  448. //
  449. // Return the width of the bitmap.
  450. //
  451. return lpBitmapRLE[0] | (lpBitmapRLE[1] << 8);
  452. }
  453. INT
  454. PAL_RLEGetHeight(
  455. LPCBITMAPRLE lpBitmapRLE
  456. )
  457. /*++
  458. Purpose:
  459. Get the height of an RLE-compressed bitmap.
  460. Parameters:
  461. [IN] lpBitmapRLE - pointer of an RLE-compressed bitmap.
  462. Return value:
  463. Integer value which indicates the height of the bitmap.
  464. --*/
  465. {
  466. if (lpBitmapRLE == NULL)
  467. {
  468. return 0;
  469. }
  470. //
  471. // Skip the 0x00000002 in the file header.
  472. //
  473. if (lpBitmapRLE[0] == 0x02 && lpBitmapRLE[1] == 0x00 &&
  474. lpBitmapRLE[2] == 0x00 && lpBitmapRLE[3] == 0x00)
  475. {
  476. lpBitmapRLE += 4;
  477. }
  478. //
  479. // Return the height of the bitmap.
  480. //
  481. return lpBitmapRLE[2] | (lpBitmapRLE[3] << 8);
  482. }
  483. WORD
  484. PAL_SpriteGetNumFrames(
  485. LPCSPRITE lpSprite
  486. )
  487. /*++
  488. Purpose:
  489. Get the total number of frames of a sprite.
  490. Parameters:
  491. [IN] lpSprite - pointer to the sprite.
  492. Return value:
  493. Number of frames of the sprite.
  494. --*/
  495. {
  496. if (lpSprite == NULL)
  497. {
  498. return 0;
  499. }
  500. return (lpSprite[0] | (lpSprite[1] << 8)) - 1;
  501. }
  502. LPCBITMAPRLE
  503. PAL_SpriteGetFrame(
  504. LPCSPRITE lpSprite,
  505. INT iFrameNum
  506. )
  507. /*++
  508. Purpose:
  509. Get the pointer to the specified frame from a sprite.
  510. Parameters:
  511. [IN] lpSprite - pointer to the sprite.
  512. [IN] iFrameNum - number of the frame.
  513. Return value:
  514. Pointer to the specified frame. NULL if the frame does not exist.
  515. --*/
  516. {
  517. int imagecount, offset;
  518. if (lpSprite == NULL)
  519. {
  520. return NULL;
  521. }
  522. //
  523. // Hack for broken sprites like the Bloody-Mouth Bug
  524. //
  525. // imagecount = (lpSprite[0] | (lpSprite[1] << 8)) - 1;
  526. imagecount = (lpSprite[0] | (lpSprite[1] << 8));
  527. if (iFrameNum < 0 || iFrameNum >= imagecount)
  528. {
  529. //
  530. // The frame does not exist
  531. //
  532. return NULL;
  533. }
  534. //
  535. // Get the offset of the frame
  536. //
  537. iFrameNum <<= 1;
  538. offset = ((lpSprite[iFrameNum] | (lpSprite[iFrameNum + 1] << 8)) << 1);
  539. if (!gConfig.fIsWIN95) offset = (WORD)offset;
  540. return &lpSprite[offset];
  541. }
  542. INT
  543. PAL_MKFGetChunkCount(
  544. FILE *fp
  545. )
  546. /*++
  547. Purpose:
  548. Get the number of chunks in an MKF archive.
  549. Parameters:
  550. [IN] fp - pointer to an fopen'ed MKF file.
  551. Return value:
  552. Integer value which indicates the number of chunks in the specified MKF file.
  553. --*/
  554. {
  555. INT iNumChunk;
  556. if (fp == NULL)
  557. {
  558. return 0;
  559. }
  560. fseek(fp, 0, SEEK_SET);
  561. fread(&iNumChunk, sizeof(INT), 1, fp);
  562. iNumChunk = (SDL_SwapLE32(iNumChunk) - 4) / 4;
  563. return iNumChunk;
  564. }
  565. INT
  566. PAL_MKFGetChunkSize(
  567. UINT uiChunkNum,
  568. FILE *fp
  569. )
  570. /*++
  571. Purpose:
  572. Get the size of a chunk in an MKF archive.
  573. Parameters:
  574. [IN] uiChunkNum - the number of the chunk in the MKF archive.
  575. [IN] fp - pointer to the fopen'ed MKF file.
  576. Return value:
  577. Integer value which indicates the size of the chunk.
  578. -1 if the chunk does not exist.
  579. --*/
  580. {
  581. UINT uiOffset = 0;
  582. UINT uiNextOffset = 0;
  583. UINT uiChunkCount = 0;
  584. //
  585. // Get the total number of chunks.
  586. //
  587. uiChunkCount = PAL_MKFGetChunkCount(fp);
  588. if (uiChunkNum >= uiChunkCount)
  589. {
  590. return -1;
  591. }
  592. //
  593. // Get the offset of the specified chunk and the next chunk.
  594. //
  595. fseek(fp, 4 * uiChunkNum, SEEK_SET);
  596. fread(&uiOffset, sizeof(UINT), 1, fp);
  597. fread(&uiNextOffset, sizeof(UINT), 1, fp);
  598. uiOffset = SDL_SwapLE32(uiOffset);
  599. uiNextOffset = SDL_SwapLE32(uiNextOffset);
  600. //
  601. // Return the length of the chunk.
  602. //
  603. return uiNextOffset - uiOffset;
  604. }
  605. INT
  606. PAL_MKFReadChunk(
  607. LPBYTE lpBuffer,
  608. UINT uiBufferSize,
  609. UINT uiChunkNum,
  610. FILE *fp
  611. )
  612. /*++
  613. Purpose:
  614. Read a chunk from an MKF archive into lpBuffer.
  615. Parameters:
  616. [OUT] lpBuffer - pointer to the destination buffer.
  617. [IN] uiBufferSize - size of the destination buffer.
  618. [IN] uiChunkNum - the number of the chunk in the MKF archive to read.
  619. [IN] fp - pointer to the fopen'ed MKF file.
  620. Return value:
  621. Integer value which indicates the size of the chunk.
  622. -1 if there are error in parameters.
  623. -2 if buffer size is not enough.
  624. --*/
  625. {
  626. UINT uiOffset = 0;
  627. UINT uiNextOffset = 0;
  628. UINT uiChunkCount;
  629. UINT uiChunkLen;
  630. if (lpBuffer == NULL || fp == NULL || uiBufferSize == 0)
  631. {
  632. return -1;
  633. }
  634. //
  635. // Get the total number of chunks.
  636. //
  637. uiChunkCount = PAL_MKFGetChunkCount(fp);
  638. if (uiChunkNum >= uiChunkCount)
  639. {
  640. return -1;
  641. }
  642. //
  643. // Get the offset of the chunk.
  644. //
  645. fseek(fp, 4 * uiChunkNum, SEEK_SET);
  646. fread(&uiOffset, 4, 1, fp);
  647. fread(&uiNextOffset, 4, 1, fp);
  648. uiOffset = SDL_SwapLE32(uiOffset);
  649. uiNextOffset = SDL_SwapLE32(uiNextOffset);
  650. //
  651. // Get the length of the chunk.
  652. //
  653. uiChunkLen = uiNextOffset - uiOffset;
  654. if (uiChunkLen > uiBufferSize)
  655. {
  656. return -2;
  657. }
  658. if (uiChunkLen != 0)
  659. {
  660. fseek(fp, uiOffset, SEEK_SET);
  661. fread(lpBuffer, uiChunkLen, 1, fp);
  662. }
  663. else
  664. {
  665. return -1;
  666. }
  667. return (INT)uiChunkLen;
  668. }
  669. INT
  670. PAL_MKFGetDecompressedSize(
  671. UINT uiChunkNum,
  672. FILE *fp
  673. )
  674. /*++
  675. Purpose:
  676. Get the decompressed size of a compressed chunk in an MKF archive.
  677. Parameters:
  678. [IN] uiChunkNum - the number of the chunk in the MKF archive.
  679. [IN] fp - pointer to the fopen'ed MKF file.
  680. Return value:
  681. Integer value which indicates the size of the chunk.
  682. -1 if the chunk does not exist.
  683. --*/
  684. {
  685. DWORD buf[2];
  686. UINT uiOffset;
  687. UINT uiChunkCount;
  688. if (fp == NULL)
  689. {
  690. return -1;
  691. }
  692. //
  693. // Get the total number of chunks.
  694. //
  695. uiChunkCount = PAL_MKFGetChunkCount(fp);
  696. if (uiChunkNum >= uiChunkCount)
  697. {
  698. return -1;
  699. }
  700. //
  701. // Get the offset of the chunk.
  702. //
  703. fseek(fp, 4 * uiChunkNum, SEEK_SET);
  704. fread(&uiOffset, 4, 1, fp);
  705. uiOffset = SDL_SwapLE32(uiOffset);
  706. //
  707. // Read the header.
  708. //
  709. fseek(fp, uiOffset, SEEK_SET);
  710. if (gConfig.fIsWIN95)
  711. {
  712. fread(buf, sizeof(DWORD), 1, fp);
  713. buf[0] = SDL_SwapLE32(buf[0]);
  714. return (INT)buf[0];
  715. }
  716. else
  717. {
  718. fread(buf, sizeof(DWORD), 2, fp);
  719. buf[0] = SDL_SwapLE32(buf[0]);
  720. buf[1] = SDL_SwapLE32(buf[1]);
  721. return (buf[0] != 0x315f4a59) ? -1 : (INT)buf[1];
  722. }
  723. }
  724. INT
  725. PAL_MKFDecompressChunk(
  726. LPBYTE lpBuffer,
  727. UINT uiBufferSize,
  728. UINT uiChunkNum,
  729. FILE *fp
  730. )
  731. /*++
  732. Purpose:
  733. Decompress a compressed chunk from an MKF archive into lpBuffer.
  734. Parameters:
  735. [OUT] lpBuffer - pointer to the destination buffer.
  736. [IN] uiBufferSize - size of the destination buffer.
  737. [IN] uiChunkNum - the number of the chunk in the MKF archive to read.
  738. [IN] fp - pointer to the fopen'ed MKF file.
  739. Return value:
  740. Integer value which indicates the size of the chunk.
  741. -1 if there are error in parameters, or buffer size is not enough.
  742. -3 if cannot allocate memory for decompression.
  743. --*/
  744. {
  745. LPBYTE buf;
  746. int len;
  747. len = PAL_MKFGetChunkSize(uiChunkNum, fp);
  748. if (len <= 0)
  749. {
  750. return len;
  751. }
  752. buf = (LPBYTE)malloc(len);
  753. if (buf == NULL)
  754. {
  755. return -3;
  756. }
  757. PAL_MKFReadChunk(buf, len, uiChunkNum, fp);
  758. len = Decompress(buf, lpBuffer, uiBufferSize);
  759. free(buf);
  760. return len;
  761. }