OCOLTBL.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /*
  2. * Seven Kingdoms: Ancient Adversaries
  3. *
  4. * Copyright 1997,1998 Enlight Software Ltd.
  5. *
  6. * This program 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 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. // Filename : OCOLTBL.CPP
  21. // Description : generated color remap table
  22. #include <OCOLTBL.h>
  23. #include <ALL.h>
  24. #include <math.h>
  25. // ---------- define const -----------//
  26. // value of full intensity, 255 for 24-bit color, 64 for 18-bit color
  27. #define MAX_COLOUR 255
  28. #define M_PI 3.14159265359L
  29. #define NEAREST_COLOR 8
  30. BYTE ColorTable::identity_table[MAX_COLOUR_TABLE_SIZE] =
  31. {
  32. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  33. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  34. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  35. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  36. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  37. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  38. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  39. 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
  40. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  41. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
  42. 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
  43. 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  44. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
  45. 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
  46. 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  47. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
  48. };
  49. // --------- define inline function ---------//
  50. inline int sq(int a)
  51. {
  52. return a*a;
  53. }
  54. // ---------- begin of function ColorTable::ColorTable ----------//
  55. ColorTable::ColorTable()
  56. {
  57. remap_table = NULL;
  58. remap_table_array = NULL;
  59. }
  60. ColorTable::ColorTable(int absScale, int tableSize, BYTE *customTable)
  61. {
  62. remap_table = NULL;
  63. remap_table_array = NULL;
  64. init(absScale, tableSize, customTable);
  65. }
  66. // ---------- end of function ColorTable::ColorTable ----------//
  67. // ---------- begin of function ColorTable::ColorTable ----------//
  68. ColorTable::ColorTable(const ColorTable& ct) : abs_scale(ct.abs_scale),
  69. table_size(ct.table_size)
  70. {
  71. if( ct.remap_table )
  72. {
  73. remap_table = (BYTE *)mem_add(table_size * (2*abs_scale+1) );
  74. memcpy(remap_table, ct.remap_table, table_size * (2*abs_scale+1) );
  75. remap_table_array = (BYTE **)mem_add(sizeof(BYTE *) * (2*abs_scale+1) );
  76. create_table_array();
  77. }
  78. else
  79. {
  80. remap_table = NULL;
  81. remap_table_array = NULL;
  82. }
  83. }
  84. // ---------- end of function ColorTable::ColorTable ----------//
  85. // ---------- begin of function ColorTable::~ColorTable ----------//
  86. ColorTable::~ColorTable()
  87. {
  88. deinit();
  89. }
  90. // ---------- end of function ColorTable::~ColorTable ----------//
  91. // ---------- begin of function ColorTable::init ----------//
  92. void ColorTable::init()
  93. {
  94. deinit();
  95. abs_scale = 0;
  96. }
  97. // initialize a custom table, given the no. of absolute scale and table size
  98. // the customTable array is (2*absScale+1) groups
  99. // and each group has (tableSize) bytes of remapping entries
  100. void ColorTable::init(int absScale, int tableSize, BYTE *customTable)
  101. {
  102. deinit();
  103. abs_scale = absScale;
  104. table_size = table_size;
  105. remap_table = (BYTE *)mem_add(table_size * (2*absScale+1) );
  106. memcpy(remap_table, customTable, tableSize * (2*absScale+1) );
  107. remap_table_array = (BYTE **)mem_add(sizeof(BYTE *) * (2*absScale+1) );
  108. create_table_array();
  109. }
  110. // ---------- end of function ColorTable::init ----------//
  111. // ---------- begin of function ColorTable::deinit ----------//
  112. void ColorTable::deinit()
  113. {
  114. if( remap_table )
  115. {
  116. mem_del( remap_table );
  117. remap_table = NULL;
  118. }
  119. if( remap_table_array)
  120. {
  121. mem_del( remap_table_array);
  122. remap_table_array = NULL;
  123. }
  124. }
  125. // ---------- end of function ColorTable::deinit ----------//
  126. // ---------- begin of function ColorTable::operator= ----------//
  127. ColorTable& ColorTable::operator=(const ColorTable& ct)
  128. {
  129. deinit();
  130. abs_scale = ct.abs_scale;
  131. table_size= ct.table_size;
  132. if( ct.remap_table )
  133. {
  134. remap_table = (BYTE *)mem_add(table_size * (2*abs_scale+1) );
  135. memcpy(remap_table, ct.remap_table, table_size * (2*abs_scale+1) );
  136. remap_table_array = (BYTE **)mem_add(sizeof(BYTE *) * (2*abs_scale+1) );
  137. create_table_array();
  138. }
  139. else
  140. {
  141. remap_table = NULL;
  142. remap_table_array = NULL;
  143. }
  144. return *this;
  145. }
  146. // ---------- begin of function ColorTable::operator= ----------//
  147. // ---------- begin of function ColorTable::generate_table ----------//
  148. //
  149. // generate +absScale to -absScale (total 2*absScale +1 remap table )
  150. // from palette pal (768 byte)
  151. // any color in the reservedColor is unmodified
  152. // note reservedColor array must be in accending order
  153. //
  154. // <int> absScale number of scale to full white/full black
  155. // <BYTE *>pal input palette, size must be 3*table_size
  156. // <int>palSize size of palette entry
  157. // <BYTE *>reservedColor array of reserved color,
  158. // <int> reservedCount size of reservedColor
  159. // reserved color will map to itself and will not be mapped except by itself
  160. //
  161. void ColorTable::generate_table(int absScale, PalDesc & palD, RGBColor (*fp)(RGBColor, int, int))
  162. {
  163. int palSize = palD.pal_size;
  164. err_when(absScale < 0 || palD.reserved_color < 0 || palD.pal == NULL);
  165. err_when(palSize > MAX_COLOUR_TABLE_SIZE);
  166. deinit();
  167. abs_scale = absScale;
  168. table_size = palSize;
  169. BYTE *remapEntry = remap_table = (BYTE *)mem_add(table_size * (2*absScale+1) );
  170. remap_table_array = (BYTE **)mem_add(sizeof(BYTE *) * (2*absScale+1) );
  171. int scale;
  172. // ------- generate negative scale ----------//
  173. for( scale = -absScale; scale < 0; ++scale)
  174. {
  175. int reservedIndex = 0;
  176. for( int c=0; c < palSize; ++c, ++remapEntry)
  177. {
  178. *remapEntry = c;
  179. // ------ see if it is a reserved color --------//
  180. if( palD.is_reserved(c, reservedIndex) )
  181. continue;
  182. RGBColor rgb = (*fp)(palD.get_rgb(c), scale, absScale);
  183. // ------- scan the closet color, except the reserved color
  184. int cc, dist[NEAREST_COLOR], thisDiff;
  185. BYTE closeColor[NEAREST_COLOR]; // [0] is the closest
  186. for( cc = 0; cc < NEAREST_COLOR; ++cc )
  187. {
  188. closeColor[cc] = c;
  189. dist[cc] = 3*0xff*0xff+1;
  190. }
  191. int dReservedIndex = 0;
  192. int d;
  193. for( d=0; d < palSize; ++d)
  194. {
  195. // ------- skip scanning reserved color ------//
  196. if( palD.is_reserved(d, dReservedIndex) )
  197. continue;
  198. // ------- compare the sqaure distance ----------//
  199. thisDiff = color_dist(rgb, palD.get_rgb(d));
  200. if( thisDiff < dist[NEAREST_COLOR-1])
  201. {
  202. BYTE d1 = (BYTE) d;
  203. for( cc = 0; cc < NEAREST_COLOR; ++cc )
  204. {
  205. if( thisDiff < dist[cc] )
  206. {
  207. // swap thisDiff and dist[cc]
  208. // so that the replaced result will be shifted to next
  209. int tempd;
  210. BYTE tempc;
  211. tempd = dist[cc];
  212. dist[cc] = thisDiff;
  213. thisDiff = tempd;
  214. tempc = closeColor[cc];
  215. closeColor[cc] = d1;
  216. d1 = tempc;
  217. }
  218. }
  219. }
  220. }
  221. // closeColor[] are the closest 8 colours, use hsv comparison to find the nearest
  222. d = closeColor[0];
  223. *remapEntry = d;
  224. int minDiff = color_dist_hsv(rgb, palD.get_rgb(d));
  225. for( cc = 1; cc < NEAREST_COLOR; ++cc)
  226. {
  227. d = closeColor[cc];
  228. thisDiff = color_dist_hsv(rgb, palD.get_rgb(d));
  229. if( thisDiff < minDiff )
  230. {
  231. minDiff = thisDiff;
  232. *remapEntry = d;
  233. }
  234. }
  235. }
  236. }
  237. err_when( remapEntry - remap_table != table_size * abs_scale);
  238. // scale == 0
  239. memcpy( remapEntry, identity_table, palSize);
  240. remapEntry += table_size;
  241. // ------- generate positive scale ----------//
  242. for( scale = 1; scale <= absScale; ++scale)
  243. {
  244. int reservedIndex = 0;
  245. for( int c=0; c < palSize; ++c, ++remapEntry)
  246. {
  247. *remapEntry = c;
  248. // ------ see if it is a reserved color --------//
  249. if( palD.is_reserved(c, reservedIndex) )
  250. continue;
  251. RGBColor rgb = (*fp)(palD.get_rgb(c), scale, absScale);
  252. // ------- scan the closet color, except the reserved color
  253. int cc, dist[NEAREST_COLOR], thisDiff;
  254. BYTE closeColor[NEAREST_COLOR]; // [0] is the closest
  255. for( cc = 0; cc < NEAREST_COLOR; ++cc )
  256. {
  257. closeColor[cc] = c;
  258. dist[cc] = 3*0xff*0xff+1;
  259. }
  260. int dReservedIndex = 0;
  261. int d;
  262. for( d=0; d < palSize; ++d)
  263. {
  264. // ------- skip scanning reserved color ------//
  265. if( palD.is_reserved(d, dReservedIndex) )
  266. continue;
  267. // ------- compare the sqaure distance ----------//
  268. thisDiff = color_dist(rgb, palD.get_rgb(d));
  269. if( thisDiff < dist[NEAREST_COLOR-1])
  270. {
  271. BYTE d1 = (BYTE) d;
  272. for( cc = 0; cc < NEAREST_COLOR; ++cc )
  273. {
  274. if( thisDiff < dist[cc] )
  275. {
  276. // swap thisDiff and dist[cc]
  277. // so that the replaced result will be shifted to next
  278. int tempd;
  279. BYTE tempc;
  280. tempd = dist[cc];
  281. dist[cc] = thisDiff;
  282. thisDiff = tempd;
  283. tempc = closeColor[cc];
  284. closeColor[cc] = d1;
  285. d1 = tempc;
  286. }
  287. }
  288. }
  289. }
  290. // closeColor[] are the closest 8 colours, use hsv comparison to find the nearest
  291. d = closeColor[0];
  292. *remapEntry = d;
  293. int minDiff = color_dist_hsv(rgb, palD.get_rgb(d));
  294. for( cc = 1; cc < NEAREST_COLOR; ++cc)
  295. {
  296. d = closeColor[cc];
  297. thisDiff = color_dist_hsv(rgb, palD.get_rgb(d));
  298. if( thisDiff < minDiff )
  299. {
  300. minDiff = thisDiff;
  301. *remapEntry = d;
  302. }
  303. }
  304. }
  305. }
  306. create_table_array();
  307. }
  308. // ---------- end of function ColorTable::generate_table ----------//
  309. // ---------- begin of function ColorTable::generate_table_fast ----------//
  310. // simplified version, it ignores reserved colors
  311. void ColorTable::generate_table_fast (int absScale, PalDesc &palD, RGBColor (*fp)(RGBColor, int, int))
  312. {
  313. int palSize = palD.pal_size;
  314. err_when(absScale < 0 || palD.pal == NULL);
  315. err_when(palSize > MAX_COLOUR_TABLE_SIZE);
  316. deinit();
  317. abs_scale = absScale;
  318. table_size = palSize;
  319. BYTE *remapEntry = remap_table = (BYTE *)mem_add(table_size * (2*absScale+1) );
  320. remap_table_array = (BYTE **)mem_add(sizeof(BYTE *) * (2*absScale+1) );
  321. int scale;
  322. // ------- generate negative scale ----------//
  323. for( scale = -absScale; scale < 0; ++scale)
  324. {
  325. for( int c=0; c < palSize; ++c, ++remapEntry)
  326. {
  327. *remapEntry = c;
  328. RGBColor rgb = (*fp)(palD.get_rgb(c), scale, absScale);
  329. // ------- scan the closet color, except the reserved color
  330. int cc, dist[NEAREST_COLOR], thisDiff;
  331. BYTE closeColor[NEAREST_COLOR]; // [0] is the closest
  332. for( cc = 0; cc < NEAREST_COLOR; ++cc )
  333. {
  334. closeColor[cc] = c;
  335. dist[cc] = 3*0xff*0xff+1;
  336. }
  337. int d;
  338. for( d=0; d < palSize; ++d)
  339. {
  340. // ------- compare the sqaure distance ----------//
  341. thisDiff = color_dist(rgb, palD.get_rgb(d));
  342. if( thisDiff < dist[NEAREST_COLOR-1])
  343. {
  344. BYTE d1 = (BYTE) d;
  345. for( cc = 0; cc < NEAREST_COLOR; ++cc )
  346. {
  347. if( thisDiff < dist[cc] )
  348. {
  349. // swap thisDiff and dist[cc]
  350. // so that the replaced result will be shifted to next
  351. int tempd;
  352. BYTE tempc;
  353. tempd = dist[cc];
  354. dist[cc] = thisDiff;
  355. thisDiff = tempd;
  356. tempc = closeColor[cc];
  357. closeColor[cc] = d1;
  358. d1 = tempc;
  359. }
  360. }
  361. }
  362. }
  363. // closeColor[] are the closest 8 colours, use hsv comparison to find the nearest
  364. d = closeColor[0];
  365. *remapEntry = d;
  366. int minDiff = color_dist_hsv(rgb, palD.get_rgb(d));
  367. for( cc = 1; cc < NEAREST_COLOR; ++cc)
  368. {
  369. d = closeColor[cc];
  370. thisDiff = color_dist_hsv(rgb, palD.get_rgb(d));
  371. if( thisDiff < minDiff )
  372. {
  373. minDiff = thisDiff;
  374. *remapEntry = d;
  375. }
  376. }
  377. }
  378. }
  379. err_when( remapEntry - remap_table != table_size * abs_scale);
  380. // scale == 0
  381. memcpy( remapEntry, identity_table, palSize);
  382. remapEntry += table_size;
  383. // ------- generate positive scale ----------//
  384. for( scale = 1; scale <= absScale; ++scale)
  385. {
  386. for( int c=0; c < palSize; ++c, ++remapEntry)
  387. {
  388. *remapEntry = c;
  389. RGBColor rgb = (*fp)(palD.get_rgb(c), scale, absScale);
  390. // ------- scan the closet color, except the reserved color
  391. int cc, dist[NEAREST_COLOR], thisDiff;
  392. BYTE closeColor[NEAREST_COLOR]; // [0] is the closest
  393. for( cc = 0; cc < NEAREST_COLOR; ++cc )
  394. {
  395. closeColor[cc] = c;
  396. dist[cc] = 3*0xff*0xff+1;
  397. }
  398. int d;
  399. for( d=0; d < palSize; ++d)
  400. {
  401. // ------- compare the sqaure distance ----------//
  402. thisDiff = color_dist(rgb, palD.get_rgb(d));
  403. if( thisDiff < dist[NEAREST_COLOR-1])
  404. {
  405. BYTE d1 = (BYTE) d;
  406. for( cc = 0; cc < NEAREST_COLOR; ++cc )
  407. {
  408. if( thisDiff < dist[cc] )
  409. {
  410. // swap thisDiff and dist[cc]
  411. // so that the replaced result will be shifted to next
  412. int tempd;
  413. BYTE tempc;
  414. tempd = dist[cc];
  415. dist[cc] = thisDiff;
  416. thisDiff = tempd;
  417. tempc = closeColor[cc];
  418. closeColor[cc] = d1;
  419. d1 = tempc;
  420. }
  421. }
  422. }
  423. }
  424. // closeColor[] are the closest 8 colours, use hsv comparison to find the nearest
  425. d = closeColor[0];
  426. *remapEntry = d;
  427. int minDiff = color_dist_hsv(rgb, palD.get_rgb(d));
  428. for( cc = 1; cc < NEAREST_COLOR; ++cc)
  429. {
  430. d = closeColor[cc];
  431. thisDiff = color_dist_hsv(rgb, palD.get_rgb(d));
  432. if( thisDiff < minDiff )
  433. {
  434. minDiff = thisDiff;
  435. *remapEntry = d;
  436. }
  437. }
  438. }
  439. }
  440. create_table_array();
  441. }
  442. // ---------- end of function ColorTable::generate_table_fast ----------//
  443. // ---------- begin of function ColorTable::generate_table ----------//
  444. //
  445. // match one set of palette with a universal palette
  446. // the set of palette is pointed by sPal, size is sPalSize,
  447. // with some reserved color pointed by sReservedColor and size is sReservedCount
  448. // the univeral palette is pointed by Pal, size is PalSize,
  449. // with some reserved color pointed by reservedColor and size is reservedCount
  450. // note : numbers in reservedColor must be in ascending order
  451. //
  452. // generated map size must be palSize and it has only scale
  453. //
  454. void ColorTable::generate_table(PalDesc &sPalD, PalDesc &palD)
  455. {
  456. int sPalSize = sPalD.pal_size, palSize = palD.pal_size;
  457. err_when(sPalD.pal == NULL || sPalSize <= 0 || sPalD.reserved_count < 0);
  458. err_when(palD.pal == NULL || palSize <= 0 || palD.reserved_count < 0);
  459. err_when(palSize > MAX_COLOUR_TABLE_SIZE || sPalSize > MAX_COLOUR_TABLE_SIZE);
  460. deinit();
  461. abs_scale = 0;
  462. table_size = sPalSize;
  463. BYTE *remapEntry = remap_table = (BYTE *)mem_add(sPalSize);
  464. remap_table_array = (BYTE **)mem_add(sizeof(BYTE *));
  465. int sReservedIndex = 0;
  466. for(int c=0; c < sPalSize; ++c, ++remapEntry)
  467. {
  468. *remapEntry = c; // put a default value (as if it is a reserved color)
  469. // ------ see if it is a reserved color --------//
  470. if( sPalD.is_reserved(c, sReservedIndex))
  471. continue;
  472. RGBColor rgb = sPalD.get_rgb(c);
  473. // ------- scan the closet color, except the reserved color
  474. int cc, dist[NEAREST_COLOR], thisDiff;
  475. BYTE closeColor[NEAREST_COLOR]; // [0] is the closest
  476. for( cc = 0; cc < NEAREST_COLOR; ++cc )
  477. {
  478. closeColor[cc] = c;
  479. dist[cc] = 3*0xff*0xff+1;
  480. }
  481. int dReservedIndex = 0;
  482. int d;
  483. for( d=0; d < palSize; ++d)
  484. {
  485. // ------- skip scanning reserved color ------//
  486. if( palD.is_reserved(d, dReservedIndex) )
  487. continue;
  488. // ------- compare the sqaure distance ----------//
  489. thisDiff = color_dist(rgb, palD.get_rgb(d));
  490. if( thisDiff < dist[NEAREST_COLOR-1])
  491. {
  492. BYTE d1 = (BYTE) d;
  493. for( cc = 0; cc < NEAREST_COLOR; ++cc )
  494. {
  495. if( thisDiff < dist[cc] )
  496. {
  497. // swap thisDiff and dist[cc]
  498. // so that the replaced result will be shifted to next
  499. int tempd;
  500. BYTE tempc;
  501. tempd = dist[cc];
  502. dist[cc] = thisDiff;
  503. thisDiff = tempd;
  504. tempc = closeColor[cc];
  505. closeColor[cc] = d1;
  506. d1 = tempc;
  507. }
  508. }
  509. }
  510. }
  511. // closeColor[] are the closest 8 colours, use hsv comparison to find the nearest
  512. d = closeColor[0];
  513. *remapEntry = d;
  514. int minDiff = color_dist_hsv(rgb, palD.get_rgb(d));
  515. for( cc = 1; cc < NEAREST_COLOR; ++cc)
  516. {
  517. d = closeColor[cc];
  518. thisDiff = color_dist_hsv(rgb, palD.get_rgb(d));
  519. if( thisDiff < minDiff )
  520. {
  521. minDiff = thisDiff;
  522. *remapEntry = d;
  523. }
  524. }
  525. }
  526. create_table_array();
  527. }
  528. // ---------- end of function ColorTable::generate_table ----------//
  529. // ---------- begin of function ColorTable::get_table ----------//
  530. BYTE *ColorTable::get_table(int scale)
  531. {
  532. err_when( !remap_table );
  533. err_when( scale < -abs_scale || scale > abs_scale);
  534. return remap_table + table_size * (scale + abs_scale);
  535. }
  536. // ---------- end of function ColorTable::get_table ----------//
  537. // ---------- begin of function ColorTable::create_table_array ----------//
  538. void ColorTable::create_table_array()
  539. {
  540. err_when( !remap_table );
  541. for( int j = 0; j < 2*abs_scale+1; ++j)
  542. {
  543. remap_table_array[j] = remap_table + table_size * j;
  544. }
  545. }
  546. // ---------- end of function ColorTable::create_table_array ----------//
  547. // ---------- begin of function ColorTable::bright_func ---------//
  548. RGBColor ColorTable::bright_func(RGBColor c, int scale, int absScale)
  549. {
  550. RGBColor ans;
  551. if( scale < 0)
  552. {
  553. double factor = sqrt(double(absScale + scale) / absScale);
  554. ans.red = BYTE(c.red * factor);
  555. ans.green = BYTE(c.green * factor);
  556. ans.blue = BYTE(c.blue * factor);
  557. }
  558. else
  559. {
  560. ans.red = c.red + (MAX_COLOUR - c.red) * scale / absScale;
  561. ans.green = c.green + (MAX_COLOUR - c.green) * scale / absScale;
  562. ans.blue = c.blue + (MAX_COLOUR - c.blue) * scale / absScale;
  563. }
  564. return ans;
  565. }
  566. // ---------- end of function ColorTable::bright_func ---------//
  567. // ---------- begin of function ColorTable::patch_table --------//
  568. void ColorTable::patch_table(BYTE from, BYTE to)
  569. {
  570. err_when(from >= table_size);
  571. for(int s = -abs_scale; s <= abs_scale; ++s)
  572. {
  573. get_table(s)[from] = to;
  574. }
  575. }
  576. // ---------- end of function ColorTable::patch_table --------//
  577. // ---------- begin of function ColorTable::color_dist --------//
  578. int ColorTable::color_dist(RGBColor c1, RGBColor c2)
  579. {
  580. return sq((int)c2.red-c1.red) + sq((int)c2.green-c1.green) + sq((int)c2.blue-c1.blue);
  581. }
  582. // ---------- end of function ColorTable::color_dist --------//
  583. // ---------- begin of function ColorTable::color_dist_hsv --------//
  584. int ColorTable::color_dist_hsv(RGBColor c1, RGBColor c2)
  585. {
  586. // calculate a distance for the colour
  587. // h betweeh 0 and 6
  588. // s between 0 and 1
  589. // v between 0 and 1
  590. HSVColor hsv1(rgb2hsv(c1));
  591. HSVColor hsv2(rgb2hsv(c2));;
  592. double dx = hsv2.saturation * cos(hsv2.hue * M_PI / 3.0) - hsv1.saturation * cos(hsv1.hue * M_PI / 3.0);
  593. double dy = hsv2.saturation * sin(hsv2.hue * M_PI / 3.0) - hsv1.saturation * sin(hsv1.hue * M_PI / 3.0);
  594. double dv = hsv2.brightness - hsv1.brightness;
  595. return int(10000 * ( dx*dx + dy*dy + dv*dv ));
  596. }
  597. // ---------- end of function ColorTable::color_dist_hsv --------//
  598. // -------- begin of function ColorTable::rgb2hsv ---------//
  599. HSVColor ColorTable::rgb2hsv(RGBColor &rgb)
  600. {
  601. if( rgb.red == rgb.green && rgb.red == rgb.blue)
  602. {
  603. return HSVColor(1.0, 0.0, rgb.red / 255.0);
  604. }
  605. // find the smallest colour
  606. if( rgb.red <= rgb.green && rgb.red <= rgb.blue)
  607. {
  608. if( rgb.green >= rgb.blue )
  609. {
  610. // g is the primary, b is secondary
  611. return HSVColor( 2.0 + (double) rgb.blue/ rgb.green,
  612. rgb.blue != 0 ? 1.0 - (double) rgb.red / rgb.blue : 1.0,
  613. rgb.green / 255.0);
  614. }
  615. else
  616. {
  617. // b is the primary, g is secondary
  618. return HSVColor( 4.0 - (double) rgb.green / rgb.blue,
  619. rgb.green != 0 ? 1.0 - (double) rgb.red/ rgb.green : 1.0,
  620. rgb.blue / 255.0);
  621. }
  622. }
  623. else if( rgb.green <= rgb.red && rgb.green <= rgb.blue)
  624. {
  625. if( rgb.red >= rgb.blue)
  626. {
  627. // r is the primary, b is secondary
  628. return HSVColor( 6.0 - (double)rgb.blue/rgb.red,
  629. rgb.blue!=0 ? 1.0 - (double)rgb.green/rgb.blue: 1.0,
  630. rgb.red / 255.0);
  631. }
  632. else
  633. {
  634. // b is the primary, r is secondary
  635. return HSVColor( 4.0 + (double)rgb.red/rgb.blue,
  636. rgb.red!=0 ? 1.0 - (double)rgb.green/rgb.red: 1.0,
  637. rgb.blue / 255.0);
  638. }
  639. }
  640. else if( rgb.blue <= rgb.red && rgb.blue <= rgb.green)
  641. {
  642. if( rgb.red >= rgb.green)
  643. {
  644. // r is the primary, g is secondary
  645. return HSVColor( (double)rgb.green/rgb.red,
  646. rgb.green!=0 ? 1.0 - (double)rgb.blue/rgb.green: 1.0,
  647. rgb.red / 255.0);
  648. }
  649. else
  650. {
  651. // g is the primary, r is secondary
  652. return HSVColor( 2.0 - (double)rgb.red/rgb.green,
  653. rgb.red!=0 ? 1.0 - (double)rgb.blue/rgb.red: 1.0,
  654. rgb.green / 255.0);
  655. }
  656. }
  657. else
  658. {
  659. err_here();
  660. return HSVColor( 1.0, 0.0, rgb.red / 255.0);
  661. }
  662. }
  663. // -------- end of function ColorTable::rgb2hsv ---------//
  664. // -------- begin of function ColorTable::hsv2rgb ---------//
  665. RGBColor ColorTable::hsv2rgb(HSVColor &hsv)
  666. {
  667. while( hsv.hue < 0.0)
  668. hsv.hue += 6.0;
  669. while(hsv.hue >= 6.0)
  670. hsv.hue -= 6.0;
  671. double p = hsv.brightness * 255.0;
  672. err_when( p >= 256.0);
  673. RGBColor ans;
  674. if( hsv.hue < 1.0)
  675. {
  676. // r is primary, g is secondary
  677. ans.red = BYTE(p);
  678. p *= hsv.hue;
  679. ans.green = BYTE(p); // *r * h;
  680. p *= 1.0 - hsv.saturation;
  681. ans.blue = BYTE(p);
  682. }
  683. else if( hsv.hue < 2.0)
  684. {
  685. // g is primary, r is secondary
  686. ans.green = BYTE(p);
  687. p *= 2.0 - hsv.hue;
  688. ans.red = BYTE(p);
  689. p *= 1.0 - hsv.saturation;
  690. ans.blue = BYTE(p);
  691. }
  692. else if( hsv.hue < 3.0)
  693. {
  694. // g is primary, b is secondary
  695. ans.green = BYTE(p);
  696. p *= hsv.hue - 2.0;
  697. ans.blue = BYTE(p);
  698. p *= 1.0 - hsv.saturation;
  699. ans.red = BYTE(p);
  700. }
  701. else if( hsv.hue < 4.0)
  702. {
  703. // b is primary g is secondary
  704. ans.blue = BYTE(p);
  705. p *= 4.0 - hsv.hue;
  706. ans.green = BYTE(p);
  707. p *= 1.0 - hsv.saturation;
  708. ans.red = BYTE(p);
  709. }
  710. else if( hsv.hue < 5.0)
  711. {
  712. // b is primary, r is secondary
  713. ans.blue = BYTE(p);
  714. p *= hsv.hue - 4.0;
  715. ans.red = BYTE(p);
  716. p *= 1.0 - hsv.saturation;
  717. ans.green = BYTE(p);
  718. }
  719. else if( hsv.hue < 6.0)
  720. {
  721. // r is primary, b is secondary
  722. ans.red = BYTE(p);
  723. p *= 6.0 - hsv.hue;
  724. ans.blue = BYTE(p);
  725. p *= 1.0 - hsv.saturation;
  726. ans.green = BYTE(p);
  727. }
  728. return ans;
  729. }
  730. // -------- end of function ColorTable::hsv2rgb ---------//
  731. // -------- begin of function ColorTable::write_file ---------//
  732. int ColorTable::write_file(File *f)
  733. {
  734. return( f->file_put_long(abs_scale) && !f->file_put_long(table_size)
  735. && f->file_write(remap_table, table_size * (2*abs_scale+1)) );
  736. }
  737. // -------- end of function ColorTable::write_file ---------//
  738. // -------- begin of function ColorTable::read_file ---------//
  739. int ColorTable::read_file(File *f)
  740. {
  741. deinit();
  742. abs_scale = f->file_get_long();
  743. table_size = f->file_get_long();
  744. remap_table = (BYTE *)mem_add(table_size * (2*abs_scale+1) );
  745. if(! f->file_read(remap_table, table_size * (2*abs_scale+1)) )
  746. {
  747. mem_del(remap_table);
  748. remap_table = 0;
  749. return 0;
  750. }
  751. remap_table_array = (BYTE **)mem_add(sizeof(BYTE *) * (2*abs_scale+1) );
  752. create_table_array();
  753. return 1;
  754. }
  755. // -------- end of function ColorTable::read_file ---------//