MimFile.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 + 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 + 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 + x * 4 + ret->width * 4 * y, &color, sizeof( ClutColor ) );
  139. }
  140. }
  141. }
  142. return ret;
  143. }
  144. void
  145. MimFile::InnerGetImage()
  146. {
  147. // get number of clut
  148. MimHeader* mim_header = ( MimHeader* )m_Buffer;
  149. mClutVramPositionX = mim_header->x;
  150. mClutVramPositionY = mim_header->y;
  151. mClutWidth = mim_header->width;
  152. mClutHeight = mim_header->height;
  153. for( int y = 0; y < mClutHeight; ++y )
  154. {
  155. for( int x = 0; x < mClutWidth; ++x )
  156. {
  157. u16 color = GetU16LE( sizeof( MimHeader ) + y * mClutWidth * 2 + x * 2 );
  158. m_Vram->PutU16(x * 2 + mClutVramPositionX, y + mClutVramPositionY, color);
  159. }
  160. }
  161. // set image buffer
  162. MimHeader2* mim_header2 = ( MimHeader2* )( ( u8* )m_Buffer + mim_header->header2_offset );
  163. mImageVramPositionX = mim_header2->x * 2;
  164. mImageVramPositionY = mim_header2->y;
  165. mImageWidth = mim_header2->width * 2;
  166. mImageHeight = mim_header2->height;
  167. for( int y = 0; y < mImageHeight; ++y )
  168. {
  169. for( int x = 0; x < mImageWidth; ++x )
  170. {
  171. u8 data = GetU8( mim_header->header2_offset + sizeof( MimHeader2 ) + y * mImageWidth + x );
  172. m_Vram->PutU8( x + mImageVramPositionX, y + mImageVramPositionY, data );
  173. }
  174. }
  175. // add to image buffer
  176. u32 data_3_offset = mim_header->header2_offset + mim_header2->data_size;
  177. if( m_BufferSize > data_3_offset )
  178. {
  179. MimHeader2* mim_header3 = ( MimHeader2* )( ( u8* )m_Buffer + data_3_offset );
  180. if (mim_header3->data_size > 0)
  181. {
  182. u16 image_vram_position_x = mim_header3->x * 2;
  183. u16 image_vram_position_y = mim_header3->y;
  184. u16 image_width = mim_header3->width * 2;
  185. u16 image_height = mim_header3->height;
  186. for( int y = 0; y < image_height; ++y )
  187. {
  188. for( int x = 0; x < image_width; ++x )
  189. {
  190. u8 data = GetU8( data_3_offset + sizeof( MimHeader2 ) + y * image_width + x );
  191. m_Vram->PutU8(x + image_vram_position_x, y + image_vram_position_y, data);
  192. }
  193. }
  194. // update image size
  195. u16 first_end_x = mImageVramPositionX + mImageWidth;
  196. u16 second_end_x = image_vram_position_x + image_width;
  197. u16 min_start_x = ( mImageVramPositionX < image_vram_position_x ) ? mImageVramPositionX : image_vram_position_x;
  198. u16 max_end_x = ( first_end_x > second_end_x ) ? first_end_x : second_end_x;
  199. mImageVramPositionX = min_start_x;
  200. mImageWidth = max_end_x - min_start_x;
  201. u16 first_end_y = mImageVramPositionY + mImageHeight;
  202. u16 second_end_y = image_vram_position_y + image_height;
  203. u16 min_start_y = (mImageVramPositionY < image_vram_position_y) ? mImageVramPositionY : image_vram_position_y;
  204. u16 max_end_y = (first_end_y > second_end_y) ? first_end_y : second_end_y;
  205. mImageVramPositionY = min_start_y;
  206. mImageHeight = max_end_y - min_start_y;
  207. }
  208. }
  209. mImageVramPositionX = 0;
  210. mImageVramPositionY = 0;
  211. mImageWidth = 2048;
  212. mImageHeight = 512;
  213. //m_Vram.Save( "export" );
  214. }
  215. void
  216. MimFile::GetModifiedClut( MimFile::ClutColor& clut, const Ogre::String& mod_type, const float mod_r, const float mod_g, const float mod_b )
  217. {
  218. if( mod_type == "mult" )
  219. {
  220. if( clut.r * mod_r >= 256 )
  221. {
  222. clut.r = 255;
  223. }
  224. else if( clut.r * mod_r < 0 )
  225. {
  226. clut.r = 0;
  227. }
  228. else
  229. {
  230. clut.r *= u8(mod_r);
  231. }
  232. if( clut.g * mod_g >= 256 )
  233. {
  234. clut.g = 255;
  235. }
  236. else if(clut.g * mod_g < 0 )
  237. {
  238. clut.g = 0;
  239. }
  240. else
  241. {
  242. clut.g *= u8(mod_g);
  243. }
  244. if( clut.b * mod_b >= 256 )
  245. {
  246. clut.b = 255;
  247. }
  248. else if( clut.b * mod_b < 0 )
  249. {
  250. clut.b = 0;
  251. }
  252. else
  253. {
  254. clut.b *= u8(mod_b);
  255. }
  256. }
  257. else if( mod_type == "add" )
  258. {
  259. if( clut.r + mod_r * 8 >= 256 )
  260. {
  261. clut.r = 255;
  262. }
  263. else if( clut.r + mod_r * 8 < 0 )
  264. {
  265. clut.r = 0;
  266. }
  267. else
  268. {
  269. clut.r += u8(mod_r * 8);
  270. }
  271. if( clut.g + mod_g * 8 >= 256 )
  272. {
  273. clut.g = 255;
  274. }
  275. else if( clut.g + mod_g * 8 < 0 )
  276. {
  277. clut.g = 0;
  278. }
  279. else
  280. {
  281. clut.g += u8(mod_g * 8);
  282. }
  283. if( clut.b + mod_b * 8 >= 256 )
  284. {
  285. clut.b = 255;
  286. }
  287. else if( clut.b + mod_b * 8 < 0 )
  288. {
  289. clut.b = 0;
  290. }
  291. else
  292. {
  293. clut.b += u8(mod_b * 8);
  294. }
  295. }
  296. }
  297. /*
  298. bool
  299. 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 )
  300. {
  301. if( bpp == 0 )
  302. {
  303. for( int y = src_y; y < src_y + height; ++y )
  304. {
  305. for( int x = src_x / 2; x < ( src_x + width ) / 2; ++x )
  306. {
  307. u16 real_x = mImageVramPositionX + page_x * 128 + x;
  308. u16 real_y = mImageVramPositionY + page_y * 256 + y;
  309. u8 data = ( m_Vram.GetU8( real_x, real_y ) & 0x0f ) * 2 + clut_x * 3;
  310. if( data > check_clut_x + check_clut_width || data < check_clut_x )
  311. {
  312. return false;
  313. }
  314. data = ( ( m_Vram.GetU8( real_x, real_y ) & 0xf0 ) >> 4 ) * 2 + clut_x * 2;
  315. if( data > check_clut_x + check_clut_width || data < check_clut_x )
  316. {
  317. return false;
  318. }
  319. }
  320. }
  321. }
  322. else if( bpp == 1 )
  323. {
  324. LOGGER->Log( "TileCompare: src_x=" + IntToString( src_x ) + " width=" + IntToString( width ) + "\n" );
  325. LOGGER->Log( "TileCompare: src_y=" + IntToString( src_y ) + " height=" + IntToString( height ) + "\n" );
  326. LOGGER->Log( "TileCompare: clut_x=" + IntToString( src_x ) + " check_clut_width=" + IntToString( check_clut_width ) + "\n" );
  327. for( int y = src_y; y < src_y + height; ++y )
  328. {
  329. for( int x = src_x; x < src_x + width; ++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 );
  334. u16 col = m_Vram.GetU16( data * 2 + clut_x * 2, 493 );
  335. LOGGER->Log( "TileCompare: x=" + IntToString( x ) + " y=" + IntToString( y ) + "\n" );
  336. LOGGER->Log( "TileCompare: data=0x" + HexToString( data, 2, '0' ) + " col=0x" + HexToString( col, 4, '0' ) + "\n" );
  337. if( data >= check_clut_x + check_clut_width || data < check_clut_x )
  338. {
  339. //return false;
  340. }
  341. }
  342. }
  343. }
  344. return true;
  345. }
  346. */