Image_process.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "tr_local.h"
  23. /*
  24. ================
  25. R_ResampleTexture
  26. Used to resample images in a more general than quartering fashion.
  27. This will only have filter coverage if the resampled size
  28. is greater than half the original size.
  29. If a larger shrinking is needed, use the mipmap function
  30. after resampling to the next lower power of two.
  31. ================
  32. */
  33. #define MAX_DIMENSION 4096
  34. byte *R_ResampleTexture( const byte *in, int inwidth, int inheight,
  35. int outwidth, int outheight ) {
  36. int i, j;
  37. const byte *inrow, *inrow2;
  38. unsigned int frac, fracstep;
  39. unsigned int p1[MAX_DIMENSION], p2[MAX_DIMENSION];
  40. const byte *pix1, *pix2, *pix3, *pix4;
  41. byte *out, *out_p;
  42. if ( outwidth > MAX_DIMENSION ) {
  43. outwidth = MAX_DIMENSION;
  44. }
  45. if ( outheight > MAX_DIMENSION ) {
  46. outheight = MAX_DIMENSION;
  47. }
  48. out = (byte *)R_StaticAlloc( outwidth * outheight * 4 );
  49. out_p = out;
  50. fracstep = inwidth*0x10000/outwidth;
  51. frac = fracstep>>2;
  52. for ( i=0 ; i<outwidth ; i++ ) {
  53. p1[i] = 4*(frac>>16);
  54. frac += fracstep;
  55. }
  56. frac = 3*(fracstep>>2);
  57. for ( i=0 ; i<outwidth ; i++ ) {
  58. p2[i] = 4*(frac>>16);
  59. frac += fracstep;
  60. }
  61. for (i=0 ; i<outheight ; i++, out_p += outwidth*4 ) {
  62. inrow = in + 4 * inwidth * (int)( ( i + 0.25f ) * inheight / outheight );
  63. inrow2 = in + 4 * inwidth * (int)( ( i + 0.75f ) * inheight / outheight );
  64. frac = fracstep >> 1;
  65. for (j=0 ; j<outwidth ; j++) {
  66. pix1 = inrow + p1[j];
  67. pix2 = inrow + p2[j];
  68. pix3 = inrow2 + p1[j];
  69. pix4 = inrow2 + p2[j];
  70. out_p[j*4+0] = (pix1[0] + pix2[0] + pix3[0] + pix4[0])>>2;
  71. out_p[j*4+1] = (pix1[1] + pix2[1] + pix3[1] + pix4[1])>>2;
  72. out_p[j*4+2] = (pix1[2] + pix2[2] + pix3[2] + pix4[2])>>2;
  73. out_p[j*4+3] = (pix1[3] + pix2[3] + pix3[3] + pix4[3])>>2;
  74. }
  75. }
  76. return out;
  77. }
  78. /*
  79. ================
  80. R_Dropsample
  81. Used to resample images in a more general than quartering fashion.
  82. Normal maps and such should not be bilerped.
  83. ================
  84. */
  85. byte *R_Dropsample( const byte *in, int inwidth, int inheight,
  86. int outwidth, int outheight ) {
  87. int i, j, k;
  88. const byte *inrow;
  89. const byte *pix1;
  90. byte *out, *out_p;
  91. out = (byte *)R_StaticAlloc( outwidth * outheight * 4 );
  92. out_p = out;
  93. for (i=0 ; i<outheight ; i++, out_p += outwidth*4 ) {
  94. inrow = in + 4*inwidth*(int)((i+0.25)*inheight/outheight);
  95. for (j=0 ; j<outwidth ; j++) {
  96. k = j * inwidth / outwidth;
  97. pix1 = inrow + k * 4;
  98. out_p[j*4+0] = pix1[0];
  99. out_p[j*4+1] = pix1[1];
  100. out_p[j*4+2] = pix1[2];
  101. out_p[j*4+3] = pix1[3];
  102. }
  103. }
  104. return out;
  105. }
  106. /*
  107. ===============
  108. R_SetBorderTexels
  109. ===============
  110. */
  111. void R_SetBorderTexels( byte *inBase, int width, int height, const byte border[4] ) {
  112. int i;
  113. byte *out;
  114. out = inBase;
  115. for (i=0 ; i<height ; i++, out+=width*4) {
  116. out[0] = border[0];
  117. out[1] = border[1];
  118. out[2] = border[2];
  119. out[3] = border[3];
  120. }
  121. out = inBase+(width-1)*4;
  122. for (i=0 ; i<height ; i++, out+=width*4) {
  123. out[0] = border[0];
  124. out[1] = border[1];
  125. out[2] = border[2];
  126. out[3] = border[3];
  127. }
  128. out = inBase;
  129. for (i=0 ; i<width ; i++, out+=4) {
  130. out[0] = border[0];
  131. out[1] = border[1];
  132. out[2] = border[2];
  133. out[3] = border[3];
  134. }
  135. out = inBase+width*4*(height-1);
  136. for (i=0 ; i<width ; i++, out+=4) {
  137. out[0] = border[0];
  138. out[1] = border[1];
  139. out[2] = border[2];
  140. out[3] = border[3];
  141. }
  142. }
  143. /*
  144. ===============
  145. R_SetBorderTexels3D
  146. ===============
  147. */
  148. void R_SetBorderTexels3D( byte *inBase, int width, int height, int depth, const byte border[4] ) {
  149. int i, j;
  150. byte *out;
  151. int row, plane;
  152. row = width * 4;
  153. plane = row * depth;
  154. for ( j = 1 ; j < depth - 1 ; j++ ) {
  155. out = inBase + j * plane;
  156. for (i=0 ; i<height ; i++, out+=row) {
  157. out[0] = border[0];
  158. out[1] = border[1];
  159. out[2] = border[2];
  160. out[3] = border[3];
  161. }
  162. out = inBase+(width-1)*4 + j * plane;
  163. for (i=0 ; i<height ; i++, out+=row) {
  164. out[0] = border[0];
  165. out[1] = border[1];
  166. out[2] = border[2];
  167. out[3] = border[3];
  168. }
  169. out = inBase + j * plane;
  170. for (i=0 ; i<width ; i++, out+=4) {
  171. out[0] = border[0];
  172. out[1] = border[1];
  173. out[2] = border[2];
  174. out[3] = border[3];
  175. }
  176. out = inBase+width*4*(height-1) + j * plane;
  177. for (i=0 ; i<width ; i++, out+=4) {
  178. out[0] = border[0];
  179. out[1] = border[1];
  180. out[2] = border[2];
  181. out[3] = border[3];
  182. }
  183. }
  184. out = inBase;
  185. for ( i = 0 ; i < plane ; i += 4, out += 4 ) {
  186. out[0] = border[0];
  187. out[1] = border[1];
  188. out[2] = border[2];
  189. out[3] = border[3];
  190. }
  191. out = inBase+(depth-1)*plane;
  192. for ( i = 0 ; i < plane ; i += 4, out += 4 ) {
  193. out[0] = border[0];
  194. out[1] = border[1];
  195. out[2] = border[2];
  196. out[3] = border[3];
  197. }
  198. }
  199. /*
  200. ================
  201. R_SetAlphaNormalDivergence
  202. If any of the angles inside the cone would directly reflect to the light, there will be
  203. a specular highlight. The intensity of the highlight is inversely proportional to the
  204. area of the spread.
  205. Light source area is important for the base size.
  206. area subtended in light is the divergence times the distance
  207. Shininess value is subtracted from the divergence
  208. Sets the alpha channel to the greatest divergence dot product of the surrounding texels.
  209. 1.0 = flat, 0.0 = turns a 90 degree angle
  210. Lower values give less shiny specular
  211. With mip maps, the lowest samnpled value will be retained
  212. Should we rewrite the normal as the centered average?
  213. ================
  214. */
  215. void R_SetAlphaNormalDivergence( byte *in, int width, int height ) {
  216. for ( int y = 0 ; y < height ; y++ ) {
  217. for ( int x = 0 ; x < width ; x++ ) {
  218. // the divergence is the smallest dot product of any of the eight surrounding texels
  219. byte *pic_p = in + ( y * width + x ) * 4;
  220. idVec3 center;
  221. center[0] = ( pic_p[0] - 128 ) / 127;
  222. center[1] = ( pic_p[1] - 128 ) / 127;
  223. center[2] = ( pic_p[2] - 128 ) / 127;
  224. center.Normalize();
  225. float maxDiverge = 1.0;
  226. // FIXME: this assumes wrap mode, but should handle clamp modes and border colors
  227. for ( int yy = -1 ; yy <= 1 ; yy++ ) {
  228. for ( int xx = -1 ; xx <= 1 ; xx++ ) {
  229. if ( yy == 0 && xx == 0 ) {
  230. continue;
  231. }
  232. byte *corner_p = in + ( ((y+yy)&(height-1)) * width + ((x+xx)&width-1) ) * 4;
  233. idVec3 corner;
  234. corner[0] = ( corner_p[0] - 128 ) / 127;
  235. corner[1] = ( corner_p[1] - 128 ) / 127;
  236. corner[2] = ( corner_p[2] - 128 ) / 127;
  237. corner.Normalize();
  238. float diverge = corner * center;
  239. if ( diverge < maxDiverge ) {
  240. maxDiverge = diverge;
  241. }
  242. }
  243. }
  244. // we can get a diverge < 0 in some extreme cases
  245. if ( maxDiverge < 0 ) {
  246. maxDiverge = 0;
  247. }
  248. pic_p[3] = maxDiverge * 255;
  249. }
  250. }
  251. }
  252. /*
  253. ================
  254. R_MipMapWithAlphaSpecularity
  255. Returns a new copy of the texture, quartered in size and filtered.
  256. The alpha channel is taken to be the minimum of the dots of all surrounding normals.
  257. ================
  258. */
  259. #define MIP_MIN(a,b) (a<b?a:b)
  260. byte *R_MipMapWithAlphaSpecularity( const byte *in, int width, int height ) {
  261. int i, j, c, x, y, sx, sy;
  262. const byte *in_p;
  263. byte *out, *out_p;
  264. int row;
  265. int newWidth, newHeight;
  266. float *fbuf, *fbuf_p;
  267. if ( width < 1 || height < 1 || ( width + height == 2 ) ) {
  268. common->FatalError( "R_MipMapWithAlphaMin called with size %i,%i", width, height );
  269. }
  270. // convert the incoming texture to centered floating point
  271. c = width * height;
  272. fbuf = (float *)_alloca( c * 4 * sizeof( *fbuf ) );
  273. in_p = in;
  274. fbuf_p = fbuf;
  275. for ( i = 0 ; i < c ; i++, in_p+=4, fbuf_p += 4 ) {
  276. fbuf_p[0] = ( in_p[0] / 255.0 ) * 2.0 - 1.0; // convert to a normal
  277. fbuf_p[1] = ( in_p[1] / 255.0 ) * 2.0 - 1.0;
  278. fbuf_p[2] = ( in_p[2] / 255.0 ) * 2.0 - 1.0;
  279. fbuf_p[3] = ( in_p[3] / 255.0 ); // filtered divegence / specularity
  280. }
  281. row = width * 4;
  282. newWidth = width >> 1;
  283. newHeight = height >> 1;
  284. if ( !newWidth ) {
  285. newWidth = 1;
  286. }
  287. if ( !newHeight ) {
  288. newHeight = 1;
  289. }
  290. out = (byte *)R_StaticAlloc( newWidth * newHeight * 4 );
  291. out_p = out;
  292. in_p = in;
  293. for ( i=0 ; i<newHeight ; i++ ) {
  294. for ( j=0 ; j<newWidth ; j++, out_p+=4 ) {
  295. idVec3 total;
  296. float totalSpec;
  297. total.Zero();
  298. totalSpec = 0;
  299. // find the average normal
  300. for ( x = -1 ; x <= 1 ; x++ ) {
  301. sx = ( j * 2 + x ) & (width-1);
  302. for ( y = -1 ; y <= 1 ; y++ ) {
  303. sy = ( i * 2 + y ) & (height-1);
  304. fbuf_p = fbuf + ( sy * width + sx ) * 4;
  305. total[0] += fbuf_p[0];
  306. total[1] += fbuf_p[1];
  307. total[2] += fbuf_p[2];
  308. totalSpec += fbuf_p[3];
  309. }
  310. }
  311. total.Normalize();
  312. totalSpec /= 9.0;
  313. // find the maximum divergence
  314. for ( x = -1 ; x <= 1 ; x++ ) {
  315. for ( y = -1 ; y <= 1 ; y++ ) {
  316. }
  317. }
  318. // store the average normal and divergence
  319. }
  320. }
  321. return out;
  322. }
  323. /*
  324. ================
  325. R_MipMap
  326. Returns a new copy of the texture, quartered in size and filtered.
  327. If a texture is intended to be used in GL_CLAMP or GL_CLAMP_TO_EDGE mode with
  328. a completely transparent border, we must prevent any blurring into the outer
  329. ring of texels by filling it with the border from the previous level. This
  330. will result in a slight shrinking of the texture as it mips, but better than
  331. smeared clamps...
  332. ================
  333. */
  334. byte *R_MipMap( const byte *in, int width, int height, bool preserveBorder ) {
  335. int i, j;
  336. const byte *in_p;
  337. byte *out, *out_p;
  338. int row;
  339. byte border[4];
  340. int newWidth, newHeight;
  341. if ( width < 1 || height < 1 || ( width + height == 2 ) ) {
  342. common->FatalError( "R_MipMap called with size %i,%i", width, height );
  343. }
  344. border[0] = in[0];
  345. border[1] = in[1];
  346. border[2] = in[2];
  347. border[3] = in[3];
  348. row = width * 4;
  349. newWidth = width >> 1;
  350. newHeight = height >> 1;
  351. if ( !newWidth ) {
  352. newWidth = 1;
  353. }
  354. if ( !newHeight ) {
  355. newHeight = 1;
  356. }
  357. out = (byte *)R_StaticAlloc( newWidth * newHeight * 4 );
  358. out_p = out;
  359. in_p = in;
  360. width >>= 1;
  361. height >>= 1;
  362. if ( width == 0 || height == 0 ) {
  363. width += height; // get largest
  364. if ( preserveBorder ) {
  365. for (i=0 ; i<width ; i++, out_p+=4 ) {
  366. out_p[0] = border[0];
  367. out_p[1] = border[1];
  368. out_p[2] = border[2];
  369. out_p[3] = border[3];
  370. }
  371. } else {
  372. for (i=0 ; i<width ; i++, out_p+=4, in_p+=8 ) {
  373. out_p[0] = ( in_p[0] + in_p[4] )>>1;
  374. out_p[1] = ( in_p[1] + in_p[5] )>>1;
  375. out_p[2] = ( in_p[2] + in_p[6] )>>1;
  376. out_p[3] = ( in_p[3] + in_p[7] )>>1;
  377. }
  378. }
  379. return out;
  380. }
  381. for (i=0 ; i<height ; i++, in_p+=row) {
  382. for (j=0 ; j<width ; j++, out_p+=4, in_p+=8) {
  383. out_p[0] = (in_p[0] + in_p[4] + in_p[row+0] + in_p[row+4])>>2;
  384. out_p[1] = (in_p[1] + in_p[5] + in_p[row+1] + in_p[row+5])>>2;
  385. out_p[2] = (in_p[2] + in_p[6] + in_p[row+2] + in_p[row+6])>>2;
  386. out_p[3] = (in_p[3] + in_p[7] + in_p[row+3] + in_p[row+7])>>2;
  387. }
  388. }
  389. // copy the old border texel back around if desired
  390. if ( preserveBorder ) {
  391. R_SetBorderTexels( out, width, height, border );
  392. }
  393. return out;
  394. }
  395. /*
  396. ================
  397. R_MipMap3D
  398. Returns a new copy of the texture, eigthed in size and filtered.
  399. If a texture is intended to be used in GL_CLAMP or GL_CLAMP_TO_EDGE mode with
  400. a completely transparent border, we must prevent any blurring into the outer
  401. ring of texels by filling it with the border from the previous level. This
  402. will result in a slight shrinking of the texture as it mips, but better than
  403. smeared clamps...
  404. ================
  405. */
  406. byte *R_MipMap3D( const byte *in, int width, int height, int depth, bool preserveBorder ) {
  407. int i, j, k;
  408. const byte *in_p;
  409. byte *out, *out_p;
  410. int row, plane;
  411. byte border[4];
  412. int newWidth, newHeight, newDepth;
  413. if ( depth == 1 ) {
  414. return R_MipMap( in, width, height, preserveBorder );
  415. }
  416. // assume symetric for now
  417. if ( width < 2 || height < 2 || depth < 2 ) {
  418. common->FatalError( "R_MipMap3D called with size %i,%i,%i", width, height, depth );
  419. }
  420. border[0] = in[0];
  421. border[1] = in[1];
  422. border[2] = in[2];
  423. border[3] = in[3];
  424. row = width * 4;
  425. plane = row * height;
  426. newWidth = width >> 1;
  427. newHeight = height >> 1;
  428. newDepth = depth >> 1;
  429. out = (byte *)R_StaticAlloc( newWidth * newHeight * newDepth * 4 );
  430. out_p = out;
  431. in_p = in;
  432. width >>= 1;
  433. height >>= 1;
  434. depth >>= 1;
  435. for (k=0 ; k<depth ; k++, in_p+=plane) {
  436. for (i=0 ; i<height ; i++, in_p+=row) {
  437. for (j=0 ; j<width ; j++, out_p+=4, in_p+=8) {
  438. out_p[0] = (in_p[0] + in_p[4] + in_p[row+0] + in_p[row+4] +
  439. in_p[plane+0] + in_p[plane+4] + in_p[plane+row+0] + in_p[plane+row+4]
  440. )>>3;
  441. out_p[1] = (in_p[1] + in_p[5] + in_p[row+1] + in_p[row+5] +
  442. in_p[plane+1] + in_p[plane+5] + in_p[plane+row+1] + in_p[plane+row+5]
  443. )>>3;
  444. out_p[2] = (in_p[2] + in_p[6] + in_p[row+2] + in_p[row+6] +
  445. in_p[plane+2] + in_p[plane+6] + in_p[plane+row+2] + in_p[plane+row+6]
  446. )>>3;
  447. out_p[3] = (in_p[3] + in_p[7] + in_p[row+3] + in_p[row+7] +
  448. in_p[plane+3] + in_p[plane+6] + in_p[plane+row+3] + in_p[plane+row+6]
  449. )>>3;
  450. }
  451. }
  452. }
  453. // copy the old border texel back around if desired
  454. if ( preserveBorder ) {
  455. R_SetBorderTexels3D( out, width, height, depth, border );
  456. }
  457. return out;
  458. }
  459. /*
  460. ==================
  461. R_BlendOverTexture
  462. Apply a color blend over a set of pixels
  463. ==================
  464. */
  465. void R_BlendOverTexture( byte *data, int pixelCount, const byte blend[4] ) {
  466. int i;
  467. int inverseAlpha;
  468. int premult[3];
  469. inverseAlpha = 255 - blend[3];
  470. premult[0] = blend[0] * blend[3];
  471. premult[1] = blend[1] * blend[3];
  472. premult[2] = blend[2] * blend[3];
  473. for ( i = 0 ; i < pixelCount ; i++, data+=4 ) {
  474. data[0] = ( data[0] * inverseAlpha + premult[0] ) >> 9;
  475. data[1] = ( data[1] * inverseAlpha + premult[1] ) >> 9;
  476. data[2] = ( data[2] * inverseAlpha + premult[2] ) >> 9;
  477. }
  478. }
  479. /*
  480. ==================
  481. R_HorizontalFlip
  482. Flip the image in place
  483. ==================
  484. */
  485. void R_HorizontalFlip( byte *data, int width, int height ) {
  486. int i, j;
  487. int temp;
  488. for ( i = 0 ; i < height ; i++ ) {
  489. for ( j = 0 ; j < width / 2 ; j++ ) {
  490. temp = *( (int *)data + i * width + j );
  491. *( (int *)data + i * width + j ) = *( (int *)data + i * width + width - 1 - j );
  492. *( (int *)data + i * width + width - 1 - j ) = temp;
  493. }
  494. }
  495. }
  496. void R_VerticalFlip( byte *data, int width, int height ) {
  497. int i, j;
  498. int temp;
  499. for ( i = 0 ; i < width ; i++ ) {
  500. for ( j = 0 ; j < height / 2 ; j++ ) {
  501. temp = *( (int *)data + j * width + i );
  502. *( (int *)data + j * width + i ) = *( (int *)data + ( height - 1 - j ) * width + i );
  503. *( (int *)data + ( height - 1 - j ) * width + i ) = temp;
  504. }
  505. }
  506. }
  507. void R_RotatePic( byte *data, int width ) {
  508. int i, j;
  509. int *temp;
  510. temp = (int *)R_StaticAlloc( width * width * 4 );
  511. for ( i = 0 ; i < width ; i++ ) {
  512. for ( j = 0 ; j < width ; j++ ) {
  513. *( temp + i * width + j ) = *( (int *)data + j * width + i );
  514. }
  515. }
  516. memcpy( data, temp, width * width * 4 );
  517. R_StaticFree( temp );
  518. }