Path: blob/master/libmupen64plus/mupen64plus-video-glide64/src/TexModCI.h
2 views
/*1* Glide64 - Glide video plugin for Nintendo 64 emulators.2* Copyright (c) 2002 Dave20013*4* This program is free software; you can redistribute it and/or modify5* it under the terms of the GNU General Public License as published by6* the Free Software Foundation; either version 2 of the License, or7* any later version.8*9* This program is distributed in the hope that it will be useful,10* but WITHOUT ANY WARRANTY; without even the implied warranty of11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12* GNU General Public License for more details.13*14* You should have received a copy of the GNU General Public15* License along with this program; if not, write to the Free16* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,17* Boston, MA 02110-1301, USA18*/1920//****************************************************************21//22// Glide64 - Glide Plugin for Nintendo 64 emulators (tested mostly with Project64)23// Project started on December 29th, 200124//25// To modify Glide64:26// * Write your name and (optional)email, commented by your work, so I know who did it, and so that you can find which parts you modified when it comes time to send it to me.27// * Do NOT send me the whole project or file that you modified. Take out your modified code sections, and tell me where to put them. If people sent the whole thing, I would have many different versions, but no idea how to combine them all.28//29// Official Glide64 development channel: #Glide64 on EFnet30//31// Original author: Dave2001 ([email protected])32// Other authors: Gonetz, Gugaman33//34//****************************************************************3536#ifndef _WIN3237#define min(a,b) ((a) < (b) ? (a) : (b))38#define max(a,b) ((a) > (b) ? (a) : (b))39#endif // _WIN324041static void mod_tex_inter_color_using_factor_CI (DWORD color, DWORD factor)42{43float percent = factor / 255.0f;44float percent_i = 1 - percent;45BYTE cr, cg, cb;46WORD col;47BYTE a, r, g, b;4849cr = (BYTE)((color >> 24) & 0xFF);50cg = (BYTE)((color >> 16) & 0xFF);51cb = (BYTE)((color >> 8) & 0xFF);5253for (int i=0; i<256; i++)54{55col = rdp.pal_8[i];56a = (BYTE)(col&0x0001);;57r = (BYTE)((float)((col&0xF800) >> 11) / 31.0f * 255.0f);58g = (BYTE)((float)((col&0x07C0) >> 6) / 31.0f * 255.0f);59b = (BYTE)((float)((col&0x003E) >> 1) / 31.0f * 255.0f);60r = (BYTE)(min(255, percent_i * r + percent * cr));61g = (BYTE)(min(255, percent_i * g + percent * cg));62b = (BYTE)(min(255, percent_i * b + percent * cb));63rdp.pal_8[i] = (WORD)(((WORD)(r >> 3) << 11) |64((WORD)(g >> 3) << 6) |65((WORD)(b >> 3) << 1) |66((WORD)(a ) << 0));67}68}6970static void mod_tex_inter_col_using_col1_CI (DWORD color0, DWORD color1)71{72BYTE cr, cg, cb;73WORD col;74BYTE a, r, g, b;7576float percent_r = ((color1 >> 24) & 0xFF) / 255.0f;77float percent_g = ((color1 >> 16) & 0xFF) / 255.0f;78float percent_b = ((color1 >> 8) & 0xFF) / 255.0f;79float percent_r_i = 1.0f - percent_r;80float percent_g_i = 1.0f - percent_g;81float percent_b_i = 1.0f - percent_b;8283cr = (BYTE)((color0 >> 24) & 0xFF);84cg = (BYTE)((color0 >> 16) & 0xFF);85cb = (BYTE)((color0 >> 8) & 0xFF);8687for (int i=0; i<256; i++)88{89col = rdp.pal_8[i];90a = (BYTE)(col&0x0001);;91r = (BYTE)((float)((col&0xF800) >> 11) / 31.0f * 255.0f);92g = (BYTE)((float)((col&0x07C0) >> 6) / 31.0f * 255.0f);93b = (BYTE)((float)((col&0x003E) >> 1) / 31.0f * 255.0f);94r = (BYTE)(min(255, percent_r_i * r + percent_r * cr));95g = (BYTE)(min(255, percent_g_i * g + percent_g * cg));96b = (BYTE)(min(255, percent_b_i * b + percent_b * cb));97rdp.pal_8[i] = (WORD)(((WORD)(r >> 3) << 11) |98((WORD)(g >> 3) << 6) |99((WORD)(b >> 3) << 1) |100((WORD)(a ) << 0));101}102}103104static void mod_full_color_sub_tex_CI (DWORD color)105{106BYTE cr, cg, cb, ca;107WORD col;108BYTE a, r, g, b;109110cr = (BYTE)((color >> 24) & 0xFF);111cg = (BYTE)((color >> 16) & 0xFF);112cb = (BYTE)((color >> 8) & 0xFF);113ca = (BYTE)(color & 0xFF);114115for (int i=0; i<256; i++)116{117col = rdp.pal_8[i];118a = (BYTE)(col&0x0001);;119r = (BYTE)((float)((col&0xF800) >> 11) / 31.0f * 255.0f);120g = (BYTE)((float)((col&0x07C0) >> 6) / 31.0f * 255.0f);121b = (BYTE)((float)((col&0x003E) >> 1) / 31.0f * 255.0f);122a = max(0, ca - a);123r = max(0, cr - r);124g = max(0, cg - g);125b = max(0, cb - b);126rdp.pal_8[i] = (WORD)(((WORD)(r >> 3) << 11) |127((WORD)(g >> 3) << 6) |128((WORD)(b >> 3) << 1) |129((WORD)(a ) << 0));130}131}132133static void mod_col_inter_col1_using_tex_CI (DWORD color0, DWORD color1)134{135DWORD cr0, cg0, cb0, cr1, cg1, cb1;136WORD col;137BYTE a, r, g, b;138float percent_r, percent_g, percent_b;139140cr0 = (BYTE)((color0 >> 24) & 0xFF);141cg0 = (BYTE)((color0 >> 16) & 0xFF);142cb0 = (BYTE)((color0 >> 8) & 0xFF);143cr1 = (BYTE)((color1 >> 24) & 0xFF);144cg1 = (BYTE)((color1 >> 16) & 0xFF);145cb1 = (BYTE)((color1 >> 8) & 0xFF);146147for (int i=0; i<256; i++)148{149col = rdp.pal_8[i];150a = (BYTE)(col&0x0001);;151percent_r = ((col&0xF800) >> 11) / 31.0f;152percent_g = ((col&0x07C0) >> 6) / 31.0f;153percent_b = ((col&0x003E) >> 1) / 31.0f;154r = (BYTE)(min((1.0f-percent_r) * cr0 + percent_r * cr1, 255));155g = (BYTE)(min((1.0f-percent_g) * cg0 + percent_g * cg1, 255));156b = (BYTE)(min((1.0f-percent_b) * cb0 + percent_b * cb1, 255));157rdp.pal_8[i] = (WORD)(((WORD)(r >> 3) << 11) |158((WORD)(g >> 3) << 6) |159((WORD)(b >> 3) << 1) |160((WORD)(a ) << 0));161}162}163164165166static void mod_tex_sub_col_mul_fac_add_tex_CI (DWORD color, DWORD factor)167{168float percent = factor / 255.0f;169BYTE cr, cg, cb, a;170WORD col;171float r, g, b;172173cr = (BYTE)((color >> 24) & 0xFF);174cg = (BYTE)((color >> 16) & 0xFF);175cb = (BYTE)((color >> 8) & 0xFF);176177for (int i=0; i<256; i++)178{179col = rdp.pal_8[i];180a = (BYTE)(col&0x0001);;181r = (BYTE)((float)((col&0xF800) >> 11) / 31.0f * 255.0f);182g = (BYTE)((float)((col&0x07C0) >> 6) / 31.0f * 255.0f);183b = (BYTE)((float)((col&0x003E) >> 1) / 31.0f * 255.0f);184r = (r - cr) * percent + r;185if (r > 255.0f) r = 255.0f;186if (r < 0.0f) r = 0.0f;187g = (g - cg) * percent + g;188if (g > 255.0f) g = 255.0f;189if (g < 0.0f) g = 0.0f;190b = (b - cb) * percent + b;191if (b > 255.0f) g = 255.0f;192if (b < 0.0f) b = 0.0f;193rdp.pal_8[i] = (WORD)(((WORD)((BYTE)(r) >> 3) << 11) |194((WORD)((BYTE)(g) >> 3) << 6) |195((WORD)((BYTE)(b) >> 3) << 1) |196(WORD)(a) );197}198}199200static void mod_tex_scale_col_add_col_CI (DWORD color, DWORD factor)201{202float percent = factor / 255.0f;203float percent_r = (1.0f - ((color >> 24) & 0xFF) / 255.0f) * percent;204float percent_g = (1.0f - ((color >> 16) & 0xFF) / 255.0f) * percent;205float percent_b = (1.0f - ((color >> 8) & 0xFF) / 255.0f) * percent;206WORD col;207float base = (1.0f - percent) * 255.0f;208BYTE a, r, g, b;209210for (int i=0; i<256; i++)211{212col = rdp.pal_8[i];213a = (BYTE)(col&0x0001);;214r = (BYTE)((float)((col&0xF800) >> 11) / 31.0f * 255.0f);215g = (BYTE)((float)((col&0x07C0) >> 6) / 31.0f * 255.0f);216b = (BYTE)((float)((col&0x003E) >> 1) / 31.0f * 255.0f);217r = (BYTE)(min(base + percent_r * r, 255));218g = (BYTE)(min(base + percent_g * g, 255));219b = (BYTE)(min(base + percent_b * b, 255));220rdp.pal_8[i] = (WORD)(((WORD)(r >> 3) << 11) |221((WORD)(g >> 3) << 6) |222((WORD)(b >> 3) << 1) |223(WORD)(a) );224}225}226227228static void mod_tex_add_col_CI (DWORD color)229{230BYTE cr, cg, cb;231WORD col;232BYTE a, r, g, b;233234cr = (BYTE)((color >> 24) & 0xFF);235cg = (BYTE)((color >> 16) & 0xFF);236cb = (BYTE)((color >> 8) & 0xFF);237238for (int i=0; i<256; i++)239{240col = rdp.pal_8[i];241a = (BYTE)(col&0x0001);;242r = (BYTE)((float)((col&0xF800) >> 11) / 31.0f * 255.0f);243g = (BYTE)((float)((col&0x07C0) >> 6) / 31.0f * 255.0f);244b = (BYTE)((float)((col&0x003E) >> 1) / 31.0f * 255.0f);245r = min(cr + r, 255);246g = min(cg + g, 255);247b = min(cb + b, 255);248rdp.pal_8[i] = (WORD)(((WORD)(r >> 3) << 11) |249((WORD)(g >> 3) << 6) |250((WORD)(b >> 3) << 1) |251((WORD)(a ) << 0));252}253}254255static void mod_tex_sub_col_CI (DWORD color)256{257BYTE cr, cg, cb;258WORD col;259BYTE a, r, g, b;260261cr = (BYTE)((color >> 24) & 0xFF);262cg = (BYTE)((color >> 16) & 0xFF);263cb = (BYTE)((color >> 8) & 0xFF);264265for (int i=0; i<256; i++)266{267col = rdp.pal_8[i];268a = (BYTE)(col&0x0001);;269r = (BYTE)((float)((col&0xF800) >> 11) / 31.0f * 255.0f);270g = (BYTE)((float)((col&0x07C0) >> 6) / 31.0f * 255.0f);271b = (BYTE)((float)((col&0x003E) >> 1) / 31.0f * 255.0f);272r = max(r - cr, 0);273g = max(g - cg, 0);274b = max(b - cb, 0);275rdp.pal_8[i] = (WORD)(((WORD)(r >> 3) << 11) |276((WORD)(g >> 3) << 6) |277((WORD)(b >> 3) << 1) |278((WORD)(a ) << 0));279}280}281282static void mod_tex_sub_col_mul_fac_CI (DWORD color, DWORD factor)283{284float percent = factor / 255.0f;285BYTE cr, cg, cb;286WORD col;287BYTE a;288float r, g, b;289290cr = (BYTE)((color >> 24) & 0xFF);291cg = (BYTE)((color >> 16) & 0xFF);292cb = (BYTE)((color >> 8) & 0xFF);293294for (int i=0; i<256; i++)295{296col = rdp.pal_8[i];297a = (BYTE)(col&0x0001);298r = (float)((col&0xF800) >> 11) / 31.0f * 255.0f;299g = (float)((col&0x07C0) >> 6) / 31.0f * 255.0f;300b = (float)((col&0x003E) >> 1) / 31.0f * 255.0f;301r = (r - cr) * percent;302if (r > 255.0f) r = 255.0f;303if (r < 0.0f) r = 0.0f;304g = (g - cg) * percent;305if (g > 255.0f) g = 255.0f;306if (g < 0.0f) g = 0.0f;307b = (b - cb) * percent;308if (b > 255.0f) g = 255.0f;309if (b < 0.0f) b = 0.0f;310311rdp.pal_8[i] = (WORD)(((WORD)((BYTE)(r) >> 3) << 11) |312((WORD)((BYTE)(g) >> 3) << 6) |313((WORD)((BYTE)(b) >> 3) << 1) |314(WORD)(a) );315}316}317318static void mod_col_inter_tex_using_col1_CI (DWORD color0, DWORD color1)319{320BYTE cr, cg, cb;321WORD col;322BYTE a, r, g, b;323324float percent_r = ((color1 >> 24) & 0xFF) / 255.0f;325float percent_g = ((color1 >> 16) & 0xFF) / 255.0f;326float percent_b = ((color1 >> 8) & 0xFF) / 255.0f;327float percent_r_i = 1.0f - percent_r;328float percent_g_i = 1.0f - percent_g;329float percent_b_i = 1.0f - percent_b;330331cr = (BYTE)((color0 >> 24) & 0xFF);332cg = (BYTE)((color0 >> 16) & 0xFF);333cb = (BYTE)((color0 >> 8) & 0xFF);334335for (int i=0; i<256; i++)336{337col = rdp.pal_8[i];338a = (BYTE)(col&0x0001);;339r = (BYTE)((float)((col&0xF800) >> 11) / 31.0f * 255.0f);340g = (BYTE)((float)((col&0x07C0) >> 6) / 31.0f * 255.0f);341b = (BYTE)((float)((col&0x003E) >> 1) / 31.0f * 255.0f);342r = (BYTE)(min(255, percent_r * r + percent_r_i * cr));343g = (BYTE)(min(255, percent_g * g + percent_g_i * cg));344b = (BYTE)(min(255, percent_b * b + percent_b_i * cb));345rdp.pal_8[i] = (WORD)(((WORD)(r >> 3) << 11) |346((WORD)(g >> 3) << 6) |347((WORD)(b >> 3) << 1) |348((WORD)(a ) << 0));349}350}351352static void mod_tex_inter_col_using_texa_CI (DWORD color)353{354BYTE a, r, g, b;355356r = (BYTE)((float)((color >> 24) & 0xFF) / 255.0f * 31.0f);357g = (BYTE)((float)((color >> 16) & 0xFF) / 255.0f * 31.0f);358b = (BYTE)((float)((color >> 8) & 0xFF) / 255.0f * 31.0f);359a = (color&0xFF) ? 1 : 0;360WORD col16 = (WORD)((r<<11)|(g<<6)|(b<<1)|a);361362for (int i=0; i<256; i++)363{364if (rdp.pal_8[i]&1)365rdp.pal_8[i] = col16;366}367}368369static void mod_tex_mul_col_CI (DWORD color)370{371BYTE a, r, g, b;372WORD col;373float cr, cg, cb;374375cr = (float)((color >> 24) & 0xFF) / 255.0f;376cg = (float)((color >> 16) & 0xFF) / 255.0f;377cb = (float)((color >> 8) & 0xFF) / 255.0f;378379for (int i=0; i<256; i++)380{381col = rdp.pal_8[i];382a = (BYTE)(col&0x0001);;383r = (BYTE)((float)((col&0xF800) >> 11) * cr);384g = (BYTE)((float)((col&0x07C0) >> 6) * cg);385b = (BYTE)((float)((col&0x003E) >> 1) * cb);386rdp.pal_8[i] = (WORD)(((WORD)(r >> 3) << 11) |387((WORD)(g >> 3) << 6) |388((WORD)(b >> 3) << 1) |389((WORD)(a ) << 0));390}391}392393static void ModifyPalette(DWORD mod, DWORD modcolor, DWORD modcolor1, DWORD modfactor)394{395switch (mod)396{397case TMOD_TEX_INTER_COLOR_USING_FACTOR:398mod_tex_inter_color_using_factor_CI (modcolor, modfactor);399break;400case TMOD_TEX_INTER_COL_USING_COL1:401mod_tex_inter_col_using_col1_CI (modcolor, modcolor1);402break;403case TMOD_FULL_COLOR_SUB_TEX:404mod_full_color_sub_tex_CI (modcolor);405break;406case TMOD_COL_INTER_COL1_USING_TEX:407mod_col_inter_col1_using_tex_CI (modcolor, modcolor1);408break;409case TMOD_TEX_SUB_COL_MUL_FAC_ADD_TEX:410mod_tex_sub_col_mul_fac_add_tex_CI (modcolor, modfactor);411break;412case TMOD_TEX_SCALE_COL_ADD_COL:413mod_tex_scale_col_add_col_CI (modcolor, modfactor);414break;415case TMOD_TEX_ADD_COL:416mod_tex_add_col_CI (modcolor);417break;418case TMOD_TEX_SUB_COL:419mod_tex_sub_col_CI (modcolor);420break;421case TMOD_TEX_SUB_COL_MUL_FAC:422mod_tex_sub_col_mul_fac_CI (modcolor, modfactor);423break;424case TMOD_COL_INTER_TEX_USING_COL1:425mod_col_inter_tex_using_col1_CI (modcolor, modcolor1);426break;427case TMOD_TEX_INTER_COL_USING_TEXA:428mod_tex_inter_col_using_texa_CI (modcolor);429break;430case TMOD_TEX_MUL_COL:431mod_tex_mul_col_CI (modcolor);432break;433default:434;435}436}437438439440