Advertisement
Guest User

Untitled

a guest
Mar 18th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Nestopia - NES/Famicom emulator written in C++
  4. //
  5. // Copyright (C) 2003-2008 Martin Freij
  6. //
  7. // This file is part of Nestopia.
  8. //
  9. // Nestopia is free software; you can redistribute it and/or modify
  10. // it under the terms of the GNU General Public License as published by
  11. // the Free Software Foundation; either version 2 of the License, or
  12. // (at your option) any later version.
  13. //
  14. // Nestopia is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. // GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with Nestopia; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. //
  23. ////////////////////////////////////////////////////////////////////////////////////////
  24.  
  25. #include "NstAssert.hpp"
  26. #include "NstVideoRenderer.hpp"
  27. #include "NstVideoFilterNtsc.hpp"
  28. #include "NstFpuPrecision.hpp"
  29.  
  30. namespace Nes
  31. {
  32. namespace Core
  33. {
  34. namespace Video
  35. {
  36. void Renderer::FilterNtsc::Blit(const Input& input,const Output& output,uint phase)
  37. {
  38. (*this.*path)( input, output, phase );
  39. }
  40.  
  41. template<typename Pixel,uint BITS>
  42. void Renderer::FilterNtsc::BlitType(const Input& input,const Output& output,uint phase) const
  43. {
  44. NST_ASSERT( phase < 3 );
  45.  
  46. const uint bgcolor = this->bgColor;
  47. const Input::Pixel* NST_RESTRICT src = input.pixels;
  48. Pixel* NST_RESTRICT dst = static_cast<Pixel*>(output.pixels);
  49. const long pitch = output.pitch;
  50. const long pad = 2*output.pitch - (NTSC_WIDTH-7) * sizeof(Pixel);
  51.  
  52. phase &= lut.noFieldMerging;
  53.  
  54. for (uint y=HEIGHT; y; --y)
  55.  
  56. NES_NTSC_BEGIN_ROW( &lut, phase, bgcolor, bgcolor, *src++ );
  57.  
  58. Pixel temp;
  59. // 50%
  60. #define PIXEL_OUT( i ) \
  61. NES_NTSC_RGB_OUT( i, temp, BITS );\
  62. dst[i] = temp;\
  63. temp >>= 1;\
  64. temp &= 0x7BEF;\
  65. reinterpret_cast<Pixel*>(reinterpret_cast<char*>(dst)+pitch)[i] = temp;
  66.  
  67. for (const Input::Pixel* const end=src+(NTSC_WIDTH/7*3-3); src != end; src += 3, dst +=7)
  68. {
  69. NES_NTSC_COLOR_IN( 0, src[0] );
  70. NES_NTSC_RGB_OUT( 0, dst[0], BITS );
  71. NES_NTSC_RGB_OUT( 1, dst[1], BITS );
  72. NES_NTSC_COLOR_IN( 2, src[2] );
  73. NES_NTSC_RGB_OUT( 4, dst[4], BITS );
  74. NES_NTSC_RGB_OUT( 5, dst[5], BITS );
  75. NES_NTSC_RGB_OUT( 6, dst[6], BITS );
  76. }
  77.  
  78. NES_NTSC_COLOR_IN( 0, lut.black );
  79. NES_NTSC_RGB_OUT( 0, dst[0], BITS );
  80. NES_NTSC_RGB_OUT( 1, dst[1], BITS );
  81.  
  82. NES_NTSC_COLOR_IN( 1, lut.black );
  83. NES_NTSC_RGB_OUT( 2, dst[2], BITS );
  84. NES_NTSC_RGB_OUT( 3, dst[3], BITS );
  85.  
  86. NES_NTSC_COLOR_IN( 2, lut.black );
  87. NES_NTSC_RGB_OUT( 4, dst[4], BITS );
  88. NES_NTSC_RGB_OUT( 5, dst[5], BITS );
  89. NES_NTSC_RGB_OUT( 6, dst[6], BITS );
  90.  
  91. dst = reinterpret_cast<Pixel*>(reinterpret_cast<byte*>(dst) + pad);
  92.  
  93. phase = (phase + 1) % 3;
  94. }
  95. }
  96.  
  97. #ifdef NST_MSVC_OPTIMIZE
  98. #pragma optimize("s", on)
  99. #endif
  100.  
  101. bool Renderer::FilterNtsc::Check(const RenderState& state)
  102. {
  103. return (state.width == NTSC_WIDTH && state.height == HEIGHT*2) &&
  104. (
  105. (state.bits.count == 16 && state.bits.mask.b == 0x001F && ((state.bits.mask.g == 0x07E0 && state.bits.mask.r == 0xF800) || (state.bits.mask.g == 0x03E0 && state.bits.mask.r == 0x7C00))) ||
  106. (state.bits.count == 32 && state.bits.mask.r == 0xFF0000 && state.bits.mask.g == 0x00FF00 && state.bits.mask.b == 0x0000FF)
  107. );
  108. }
  109.  
  110. Renderer::FilterNtsc::Path Renderer::FilterNtsc::GetPath(const RenderState& state,const Lut& lut)
  111. {
  112. if (state.bits.count == 32)
  113. {
  114. return &FilterNtsc::BlitType<dword,32>;
  115. }
  116. else if (state.bits.mask.g == 0x07E0)
  117. {
  118. return &FilterNtsc::BlitType<word,16>;
  119. }
  120. else
  121. {
  122. return &FilterNtsc::BlitType<word,15>;
  123. }
  124. }
  125.  
  126. inline uint Renderer::FilterNtsc::Lut::GetBlack(const byte (&p)[PALETTE][3])
  127. {
  128. uint index = DEF_BLACK;
  129.  
  130. for (uint i=0, intensity = 0xFF * 100; i < 64; ++i)
  131. {
  132. const uint v = p[i][0] * 30 + p[i][1] * 59 + p[i][2] * 11;
  133.  
  134. if (intensity > v)
  135. {
  136. intensity = v;
  137. index = i;
  138. }
  139. }
  140.  
  141. return index;
  142. }
  143.  
  144. Renderer::FilterNtsc::Lut::Lut
  145. (
  146. const byte (&palette)[PALETTE][3],
  147. const schar sharpness,
  148. const schar resolution,
  149. const schar bleed,
  150. const schar artifacts,
  151. const schar fringing,
  152. const bool fieldMerging
  153. )
  154. :
  155. noFieldMerging (fieldMerging ? 0U : ~0U),
  156. black (GetBlack(palette))
  157. {
  158. FpuPrecision precision;
  159.  
  160. nes_ntsc_setup_t setup;
  161.  
  162. setup.hue = 0;
  163. setup.saturation = 0;
  164. setup.contrast = 0;
  165. setup.brightness = 0;
  166. setup.sharpness = sharpness / 100.0;
  167. setup.gamma = 0;
  168. setup.resolution = resolution / 100.0;
  169. setup.artifacts = artifacts / 100.0;
  170. setup.fringing = fringing / 100.0;
  171. setup.bleed = bleed / 100.0;
  172. setup.merge_fields = fieldMerging;
  173. setup.decoder_matrix = NULL;
  174. setup.palette_out = NULL;
  175. setup.palette = *palette;
  176. setup.base_palette = NULL;
  177.  
  178. ::nes_ntsc_init( this, &setup );
  179. }
  180.  
  181. Renderer::FilterNtsc::FilterNtsc
  182. (
  183. const RenderState& state,
  184. const byte (&palette)[PALETTE][3],
  185. schar sharpness,
  186. schar resolution,
  187. schar bleed,
  188. schar artifacts,
  189. schar fringing,
  190. bool fieldMerging
  191. )
  192. :
  193. Filter (state),
  194. path (GetPath(state,lut)),
  195. lut (palette,sharpness,resolution,bleed,artifacts,fringing,fieldMerging)
  196. {
  197. }
  198.  
  199. #ifdef NST_MSVC_OPTIMIZE
  200. #pragma optimize("", on)
  201. #endif
  202. }
  203. }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement