MimFile.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. #include "MimFile.h"
  2. #include "../../common/Logger.h"
  3. MimFile::MimFile( const Ogre::String& file )
  4. : LzsFile(file), m_Vram(Vram::MakeInstance())
  5. {
  6. InnerGetImage();
  7. }
  8. MimFile::MimFile( File* pFile )
  9. : LzsFile(pFile), m_Vram(Vram::MakeInstance())
  10. {
  11. InnerGetImage();
  12. }
  13. MimFile::MimFile( File* pFile, const u32 offset, const u32 length )
  14. : LzsFile(pFile, offset, length), m_Vram(Vram::MakeInstance())
  15. {
  16. InnerGetImage();
  17. }
  18. MimFile::MimFile( u8* pBuffer, const u32 offset, const u32 length )
  19. : LzsFile(pBuffer, offset, length), m_Vram(Vram::MakeInstance())
  20. {
  21. InnerGetImage();
  22. }
  23. MimFile::~MimFile()
  24. {
  25. }
  26. Surface*
  27. MimFile::GetSurface( const u16 page_x, const u16 page_y, const u16 clut_x, const u16 clut_y, const u8 bpp, const int clut_start, const int clut_width, const Ogre::String& mod_type, const float mod_r, const float mod_g, const float mod_b )
  28. {
  29. // lastest texture page x are on the border with vram so we need get smaller tex
  30. u16 size_x = ( page_x >= 15 && bpp == 1 ) ? 128 : 256;
  31. Surface* ret = CreateSurface( size_x, 256 );
  32. if( clut_y > mClutHeight + mClutVramPositionY )
  33. {
  34. LOGGER->Log( "MimFile::GetSurface: Warning: \"clut_number\" is greater than number of clut in file." );
  35. return ret;
  36. }
  37. // temp clut color
  38. ClutColor color;
  39. if( bpp == 0 )
  40. {
  41. for( int y = 0; y < 256; ++y )
  42. {
  43. for( int x = 0; x < 128; ++x )
  44. {
  45. u16 real_x = mImageVramPositionX + page_x * 128 + x;
  46. u16 real_y = mImageVramPositionY + page_y * 256 + y;
  47. int data = (m_Vram->GetU8(real_x, real_y) & 0x0f) + clut_x;
  48. u16 col = m_Vram->GetU16(data * 2, clut_y);
  49. color.r = ( ( col ) & 31 ) * 255 / 31;
  50. color.g = ( ( col >> 5 ) & 31 ) * 255 / 31;
  51. color.b = ( ( col >> 10 ) & 31 ) * 255 / 31;
  52. if( data >= clut_start && data < clut_start + clut_width )
  53. {
  54. GetModifiedClut( color, mod_type, mod_r, mod_g, mod_b );
  55. }
  56. u8 stp = (col & 0x80) >> 15;
  57. if( col == 0x0000 )
  58. {
  59. color.a = 0;
  60. }
  61. else if( stp == 1 && color.r == 0 && color.g == 0 && color.b == 0 )
  62. {
  63. color.a = 255;
  64. }
  65. else if( stp == 1 && ( color.r != 0 || color.g != 0 || color.b != 0 ) )
  66. {
  67. color.a = 127;
  68. }
  69. else if( stp == 0 && ( color.r != 0 || color.g != 0 || color.b != 0 ) )
  70. {
  71. color.a = 255;
  72. }
  73. memcpy( ret->pixels.data() + x * 8 + ret->width * 4 * y + 0x00, &color, sizeof( ClutColor ) );
  74. data = ((m_Vram->GetU8(real_x, real_y) & 0xf0) >> 4) + clut_x;
  75. col = m_Vram->GetU16(data * 2, clut_y);
  76. color.r = ( ( col ) & 31 ) * 255 / 31;
  77. color.g = ( ( col >> 5 ) & 31 ) * 255 / 31;
  78. color.b = ( ( col >> 10 ) & 31 ) * 255 / 31;
  79. if( data >= clut_start && data < clut_start + clut_width )
  80. {
  81. GetModifiedClut( color, mod_type, mod_r, mod_g, mod_b );
  82. }
  83. stp = ( col & 0x80 ) >> 15;
  84. if( col == 0x0000 )
  85. {
  86. color.a = 0;
  87. }
  88. else if( stp == 1 && color.r == 0 && color.g == 0 && color.b == 0 )
  89. {
  90. color.a = 255;
  91. }
  92. else if( stp == 1 && ( color.r != 0 || color.g != 0 || color.b != 0 ) )
  93. {
  94. color.a = 127;
  95. }
  96. else if( stp == 0 && ( color.r != 0 || color.g != 0 || color.b != 0 ) )
  97. {
  98. color.a = 255;
  99. }
  100. memcpy( ret->pixels.data() + x * 8 + ret->width * 4 * y + 0x04, &color, sizeof( ClutColor ) );
  101. }
  102. }
  103. }
  104. else if( bpp == 1 )
  105. {
  106. for( int y = 0; y < 256; ++y )
  107. {
  108. for( int x = 0; x < size_x; ++x )
  109. {
  110. u16 real_x = mImageVramPositionX + page_x * 128 + x;
  111. u16 real_y = mImageVramPositionY + page_y * 256 + y;
  112. u8 data = m_Vram->GetU8(real_x, real_y);
  113. u16 col = m_Vram->GetU16(data * 2 + clut_x * 2, clut_y);
  114. color.r = ( ( col ) & 31 ) * 255 / 31;
  115. color.g = ( ( col >> 5 ) & 31 ) * 255 / 31;
  116. color.b = ( ( col >> 10 ) & 31 ) * 255 / 31;
  117. if( data >= clut_start && data < clut_start + clut_width )
  118. {
  119. GetModifiedClut( color, mod_type, mod_r, mod_g, mod_b );
  120. }
  121. u8 stp = ( col & 0x8000 ) >> 15;
  122. if( col == 0x0000 )
  123. {
  124. color.a = 0;
  125. }
  126. else if( stp == 1 && color.r == 0 && color.g == 0 && color.b == 0 )
  127. {
  128. color.a = 255;
  129. }
  130. else if( stp == 1 && ( color.r != 0 || color.g != 0 || color.b != 0 ) )
  131. {
  132. color.a = 127;
  133. }
  134. else if( stp == 0 && ( color.r != 0 || color.g != 0 || color.b != 0 ) )
  135. {
  136. color.a = 255;
  137. }
  138. memcpy( ret->pixels.data() + x * 4 + ret->width * 4 * y, &color, sizeof( ClutColor ) );
  139. }
  140. }
  141. }
  142. else if ( bpp == 2 )
  143. {
  144. for( int y = 0; y < 256; ++y )
  145. {
  146. for( int x = 0; x < 256; ++x )
  147. {
  148. u16 real_x = mImageVramPositionX + page_x * 128 + x * 2;
  149. u16 real_y = mImageVramPositionY + page_y * 256 + y;
  150. u16 col = m_Vram->GetU16(real_x, real_y);
  151. color.r = ( ( col ) & 31 ) * 255 / 31;
  152. color.g = ( ( col >> 5 ) & 31 ) * 255 / 31;
  153. color.b = ( ( col >> 10 ) & 31 ) * 255 / 31;
  154. if( col == 0x0000 )
  155. {
  156. color.a = 0;
  157. }
  158. else
  159. {
  160. color.a = 255;
  161. }
  162. memcpy( ret->pixels.data() + x * 4 + ret->width * 4 * y, &color, sizeof( ClutColor ) );
  163. }
  164. }
  165. }
  166. return ret;
  167. }
  168. void
  169. MimFile::InnerGetImage()
  170. {
  171. // get number of clut
  172. MimHeader* mim_header = ( MimHeader* )m_Buffer;
  173. mClutVramPositionX = mim_header->x;
  174. mClutVramPositionY = mim_header->y;
  175. mClutWidth = mim_header->width;
  176. mClutHeight = mim_header->height;
  177. for( int y = 0; y < mClutHeight; ++y )
  178. {
  179. for( int x = 0; x < mClutWidth; ++x )
  180. {
  181. u16 color = GetU16LE( sizeof( MimHeader ) + y * mClutWidth * 2 + x * 2 );
  182. m_Vram->PutU16(x * 2 + mClutVramPositionX, y + mClutVramPositionY, color);
  183. }
  184. }
  185. // set image buffer
  186. MimHeader2* mim_header2 = ( MimHeader2* )( ( u8* )m_Buffer + mim_header->header2_offset );
  187. mImageVramPositionX = mim_header2->x * 2;
  188. mImageVramPositionY = mim_header2->y;
  189. mImageWidth = mim_header2->width * 2;
  190. mImageHeight = mim_header2->height;
  191. for( int y = 0; y < mImageHeight; ++y )
  192. {
  193. for( int x = 0; x < mImageWidth; ++x )
  194. {
  195. u8 data = GetU8( mim_header->header2_offset + sizeof( MimHeader2 ) + y * mImageWidth + x );
  196. m_Vram->PutU8( x + mImageVramPositionX, y + mImageVramPositionY, data );
  197. }
  198. }
  199. // add to image buffer
  200. u32 data_3_offset = mim_header->header2_offset + mim_header2->data_size;
  201. if( m_BufferSize > data_3_offset )
  202. {
  203. MimHeader2* mim_header3 = ( MimHeader2* )( ( u8* )m_Buffer + data_3_offset );
  204. if (mim_header3->data_size > 0)
  205. {
  206. u16 image_vram_position_x = mim_header3->x * 2;
  207. u16 image_vram_position_y = mim_header3->y;
  208. u16 image_width = mim_header3->width * 2;
  209. u16 image_height = mim_header3->height;
  210. for( int y = 0; y < image_height; ++y )
  211. {
  212. for( int x = 0; x < image_width; ++x )
  213. {
  214. u8 data = GetU8( data_3_offset + sizeof( MimHeader2 ) + y * image_width + x );
  215. m_Vram->PutU8(x + image_vram_position_x, y + image_vram_position_y, data);
  216. }
  217. }
  218. // update image size
  219. u16 first_end_x = mImageVramPositionX + mImageWidth;
  220. u16 second_end_x = image_vram_position_x + image_width;
  221. u16 min_start_x = ( mImageVramPositionX < image_vram_position_x ) ? mImageVramPositionX : image_vram_position_x;
  222. u16 max_end_x = ( first_end_x > second_end_x ) ? first_end_x : second_end_x;
  223. mImageVramPositionX = min_start_x;
  224. mImageWidth = max_end_x - min_start_x;
  225. u16 first_end_y = mImageVramPositionY + mImageHeight;
  226. u16 second_end_y = image_vram_position_y + image_height;
  227. u16 min_start_y = (mImageVramPositionY < image_vram_position_y) ? mImageVramPositionY : image_vram_position_y;
  228. u16 max_end_y = (first_end_y > second_end_y) ? first_end_y : second_end_y;
  229. mImageVramPositionY = min_start_y;
  230. mImageHeight = max_end_y - min_start_y;
  231. }
  232. }
  233. mImageVramPositionX = 0;
  234. mImageVramPositionY = 0;
  235. mImageWidth = 2048;
  236. mImageHeight = 512;
  237. //m_Vram.Save( "export" );
  238. }
  239. void
  240. MimFile::GetModifiedClut( MimFile::ClutColor& clut, const Ogre::String& mod_type, const float mod_r, const float mod_g, const float mod_b )
  241. {
  242. if( mod_type == "mult" )
  243. {
  244. if( clut.r * mod_r >= 256 )
  245. {
  246. clut.r = 255;
  247. }
  248. else if( clut.r * mod_r < 0 )
  249. {
  250. clut.r = 0;
  251. }
  252. else
  253. {
  254. clut.r *= u8(mod_r);
  255. }
  256. if( clut.g * mod_g >= 256 )
  257. {
  258. clut.g = 255;
  259. }
  260. else if(clut.g * mod_g < 0 )
  261. {
  262. clut.g = 0;
  263. }
  264. else
  265. {
  266. clut.g *= u8(mod_g);
  267. }
  268. if( clut.b * mod_b >= 256 )
  269. {
  270. clut.b = 255;
  271. }
  272. else if( clut.b * mod_b < 0 )
  273. {
  274. clut.b = 0;
  275. }
  276. else
  277. {
  278. clut.b *= u8(mod_b);
  279. }
  280. }
  281. else if( mod_type == "add" )
  282. {
  283. if( clut.r + mod_r * 8 >= 256 )
  284. {
  285. clut.r = 255;
  286. }
  287. else if( clut.r + mod_r * 8 < 0 )
  288. {
  289. clut.r = 0;
  290. }
  291. else
  292. {
  293. clut.r += u8(mod_r * 8);
  294. }
  295. if( clut.g + mod_g * 8 >= 256 )
  296. {
  297. clut.g = 255;
  298. }
  299. else if( clut.g + mod_g * 8 < 0 )
  300. {
  301. clut.g = 0;
  302. }
  303. else
  304. {
  305. clut.g += u8(mod_g * 8);
  306. }
  307. if( clut.b + mod_b * 8 >= 256 )
  308. {
  309. clut.b = 255;
  310. }
  311. else if( clut.b + mod_b * 8 < 0 )
  312. {
  313. clut.b = 0;
  314. }
  315. else
  316. {
  317. clut.b += u8(mod_b * 8);
  318. }
  319. }
  320. }
  321. /*
  322. bool
  323. MimFile::ClutCheck(const u16 page_x, const u16 page_y, const u16 clut_x, const u8 bpp, const u8 src_x, const u8 src_y, const u8 width, const u8 height, const u16 check_clut_x, const u16 check_clut_width )
  324. {
  325. if( bpp == 0 )
  326. {
  327. for( int y = src_y; y < src_y + height; ++y )
  328. {
  329. for( int x = src_x / 2; x < ( src_x + width ) / 2; ++x )
  330. {
  331. u16 real_x = mImageVramPositionX + page_x * 128 + x;
  332. u16 real_y = mImageVramPositionY + page_y * 256 + y;
  333. u8 data = ( m_Vram.GetU8( real_x, real_y ) & 0x0f ) * 2 + clut_x * 3;
  334. if( data > check_clut_x + check_clut_width || data < check_clut_x )
  335. {
  336. return false;
  337. }
  338. data = ( ( m_Vram.GetU8( real_x, real_y ) & 0xf0 ) >> 4 ) * 2 + clut_x * 2;
  339. if( data > check_clut_x + check_clut_width || data < check_clut_x )
  340. {
  341. return false;
  342. }
  343. }
  344. }
  345. }
  346. else if( bpp == 1 )
  347. {
  348. LOGGER->Log( "TileCompare: src_x=" + IntToString( src_x ) + " width=" + IntToString( width ) + "\n" );
  349. LOGGER->Log( "TileCompare: src_y=" + IntToString( src_y ) + " height=" + IntToString( height ) + "\n" );
  350. LOGGER->Log( "TileCompare: clut_x=" + IntToString( src_x ) + " check_clut_width=" + IntToString( check_clut_width ) + "\n" );
  351. for( int y = src_y; y < src_y + height; ++y )
  352. {
  353. for( int x = src_x; x < src_x + width; ++x )
  354. {
  355. u16 real_x = mImageVramPositionX + page_x * 128 + x;
  356. u16 real_y = mImageVramPositionY + page_y * 256 + y;
  357. u8 data = m_Vram.GetU8( real_x, real_y );
  358. u16 col = m_Vram.GetU16( data * 2 + clut_x * 2, 493 );
  359. LOGGER->Log( "TileCompare: x=" + IntToString( x ) + " y=" + IntToString( y ) + "\n" );
  360. LOGGER->Log( "TileCompare: data=0x" + HexToString( data, 2, '0' ) + " col=0x" + HexToString( col, 4, '0' ) + "\n" );
  361. if( data >= check_clut_x + check_clut_width || data < check_clut_x )
  362. {
  363. //return false;
  364. }
  365. }
  366. }
  367. }
  368. return true;
  369. }
  370. */