?login_element?

Subversion Repositories NedoOS

Rev

Rev 40 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. //---------------------------------------------------------------------------
  2.  
  3. #include <vcl.h>
  4. #include <math.h>
  5. #include <stdio.h>
  6. #pragma hdrstop
  7.  
  8. //#include "nedodefs.h"
  9.  
  10. #include "Unit1.h"
  11. //---------------------------------------------------------------------------
  12. #pragma package(smart_init)
  13. #pragma resource "*.dfm"
  14.  
  15. int chunkpixelnumber[16] = {
  16. /*  0x0, 0xc, 0x2, 0xe,
  17.   0x8, 0x4, 0xa, 0x6,
  18.   0x3, 0xf, 0x1, 0xd,
  19.   0xb, 0x7, 0x9, 0x5*/
  20.   0x0, 0xc, 0x6, 0xa,
  21.   0x9, 0x4, 0x1, 0xd,
  22.   0xe, 0x7, 0xb, 0x3,
  23.   0x5, 0x2, 0x8, 0xf
  24. };
  25.  
  26. int chunkpixelnumberdiamond[16] = {
  27.   0x1, 0xd, 0x3, 0xf,
  28.   0x9, 0x5, 0xb, 0x7,
  29.   0x4, 0x10, 0x2, 0xe,
  30.   0xc, 0x8, 0xa, 0x6
  31. };
  32.  
  33. #define PALCOEFF 85/3
  34. unsigned char pal[768];
  35.  
  36. unsigned char zxpal[16*2];
  37.  
  38. unsigned char maxdistdiv_fromattr[256];
  39. unsigned char min_fromattr[256];
  40. unsigned char maxaxis_fromattr[256];
  41.  
  42. unsigned char tdiv[16384];
  43.  
  44. #define PALEXTRA 5
  45. char palextra[3*PALEXTRA] = {
  46.   //18*PALCOEFF/2, 15*PALCOEFF/2, 13*PALCOEFF/2,
  47.   17*PALCOEFF/2, 13*PALCOEFF/2, 10*PALCOEFF/2,
  48.   16*PALCOEFF/2, 11*PALCOEFF/2,  9*PALCOEFF/2,
  49.   15*PALCOEFF/2,  9*PALCOEFF/2,  8*PALCOEFF/2,
  50.   17*PALCOEFF/2, 11*PALCOEFF/2,  6*PALCOEFF/2,
  51.   11*PALCOEFF/2,  7*PALCOEFF/2,  5*PALCOEFF/2,
  52. };
  53.  
  54. unsigned char pal16[16*3]; //act(r,g,b)
  55.  
  56. unsigned char t64to16[64];
  57.  
  58. unsigned char t64to16ink[64];
  59.  
  60. unsigned char t64to16paper[64];
  61.  
  62. char colorexist[1000] = {
  63. //x=g(0..9), y=b(9..0)
  64. //R=9:
  65. 1,0,0,1,0,0,1,0,0,1, //W
  66. 0,0,0,0,0,0,0,0,1,1,
  67. 0,0,0,0,0,0,0,1,1,1,
  68. 1,0,0,1,0,0,1,1,1,1, //YW
  69. 0,0,0,0,0,0,1,1,1,1,
  70. 0,0,0,0,0,0,1,1,0,0,
  71. 1,0,0,1,0,0,1,1,0,1, //Y
  72. 0,0,0,0,0,0,0,0,0,0,
  73. 0,0,0,0,0,0,0,0,0,0,
  74. 1,0,0,1,0,0,1,0,0,1,
  75.  
  76. //R=8:
  77. 0,0,0,0,0,0,0,0,0,1,
  78. 0,0,0,0,0,0,0,0,1,1,
  79. 0,0,0,0,0,0,0,1,1,1,
  80. 0,0,0,0,0,0,1,1,1,1,
  81. 0,0,0,0,0,1,1,1,1,1,
  82. 0,0,0,0,0,1,1,1,1,0,
  83. 0,0,0,0,0,1,1,1,1,0,
  84. 0,0,0,0,0,0,0,0,0,0,
  85. 0,0,0,0,0,0,0,0,0,0,
  86. 0,0,0,0,0,0,0,0,0,0,
  87.  
  88. //R=7:
  89. 0,0,0,0,0,0,0,0,0,1,
  90. 0,0,0,0,0,0,0,1,1,1,
  91. 0,0,0,0,0,0,1,1,1,1,
  92. 0,0,0,0,0,1,1,1,1,0,
  93. 0,0,0,0,0,1,1,1,1,0,
  94. 0,0,0,0,1,1,1,1,0,0,
  95. 0,0,0,0,1,1,1,1,0,0,
  96. 0,0,0,0,0,0,0,0,0,0,
  97. 0,0,0,0,0,0,0,0,0,0,
  98. 0,0,0,0,0,0,0,0,0,0,
  99.  
  100. //R=6:
  101. 1,0,0,1,0,0,1,0,0,1, //CW
  102. 0,0,0,0,0,0,0,1,1,0,
  103. 0,0,0,0,0,0,1,1,1,0,
  104. 1,0,0,1,0,1,1,1,0,1,
  105. 0,0,0,0,0,1,1,1,0,0,
  106. 0,0,0,0,1,1,1,0,0,0,
  107. 1,0,0,1,1,1,1,0,0,1,
  108. 0,0,0,0,0,0,0,0,0,0,
  109. 0,0,0,0,0,0,0,0,0,0,
  110. 1,0,0,1,0,0,1,0,0,1,
  111.  
  112. //R=5:
  113. 0,0,0,0,0,0,0,0,0,0,
  114. 0,0,0,0,0,0,1,1,0,0,
  115. 0,0,0,0,0,1,1,1,0,0,
  116. 0,0,0,0,1,1,1,1,1,0,
  117. 0,0,0,0,1,1,1,1,0,0,
  118. 0,0,0,1,1,1,1,1,0,0,
  119. 0,0,0,1,1,1,1,0,0,0,
  120. 0,0,1,1,1,1,1,0,0,0,
  121. 0,0,0,0,0,0,0,0,0,0,
  122. 0,0,0,0,0,0,0,0,0,0,
  123.  
  124. //R=4:
  125. 0,0,0,0,0,0,0,0,0,0,
  126. 0,0,0,0,0,0,0,0,0,0,
  127. 0,0,0,0,0,1,0,0,0,0,
  128. 0,0,0,0,1,1,0,0,0,0,
  129. 0,0,0,1,1,1,1,0,0,0,
  130. 0,0,0,1,1,1,1,0,0,0,
  131. 0,0,1,1,1,1,1,1,0,0,
  132. 0,0,1,1,1,1,1,0,0,0,
  133. 0,1,1,1,1,1,1,0,0,0,
  134. 0,0,0,0,0,0,0,0,0,0,
  135.  
  136. //R=3:
  137. 1,0,0,1,0,0,1,0,0,1,
  138. 0,0,0,0,0,0,0,0,0,0,
  139. 0,0,0,0,0,0,0,0,0,0,
  140. 1,0,0,1,0,0,1,0,0,1,
  141. 0,0,0,1,0,0,0,0,0,0,
  142. 0,0,1,1,1,0,0,0,0,0,
  143. 1,0,1,1,1,0,1,0,0,1,
  144. 0,1,1,1,1,1,0,0,0,0,
  145. 0,1,0,0,0,1,0,0,0,0,
  146. 1,1,0,1,0,1,1,0,0,1,
  147.  
  148. //R=2:
  149. 0,0,0,0,0,0,0,0,0,0,
  150. 0,0,0,0,0,0,0,0,0,0,
  151. 0,0,0,0,0,0,0,0,0,0,
  152. 0,0,0,0,0,0,0,0,0,0,
  153. 0,0,1,0,0,0,0,0,0,0,
  154. 0,0,1,1,0,0,0,0,0,0,
  155. 0,1,1,1,0,0,0,0,0,0,
  156. 0,1,1,1,1,0,0,0,0,0,
  157. 1,1,1,0,1,0,0,0,0,0,
  158. 1,1,0,0,0,1,0,0,0,0,
  159.  
  160. //R=1:
  161. 0,0,0,0,0,0,0,0,0,0,
  162. 0,0,0,0,0,0,0,0,0,0,
  163. 0,0,0,0,0,0,0,0,0,0,
  164. 0,0,0,0,0,0,0,0,0,0,
  165. 0,0,0,0,0,0,0,0,0,0,
  166. 0,1,0,0,0,0,0,0,0,0,
  167. 0,1,1,0,0,0,0,0,0,0,
  168. 1,1,1,1,0,0,0,0,0,0,
  169. 1,1,1,1,0,0,0,0,0,0,
  170. 1,1,1,1,1,0,0,0,0,0,
  171.  
  172. //R=0:
  173. 1,0,0,1,0,0,1,0,0,1,
  174. 0,0,0,0,0,0,0,0,0,0,
  175. 0,0,0,0,0,0,0,0,0,0,
  176. 1,0,0,1,0,0,1,0,0,1,
  177. 0,0,0,0,0,0,0,0,0,0,
  178. 0,0,0,0,0,0,0,0,0,0,
  179. 1,0,0,1,0,0,1,0,0,1,
  180. 1,1,0,0,0,0,0,0,0,0,
  181. 1,1,1,0,0,0,0,0,0,0,
  182. 1,1,1,1,0,0,1,0,0,1
  183. };
  184.  
  185. int tsin[256];
  186.  
  187.  
  188. TForm1 *Form1;
  189. //---------------------------------------------------------------------------
  190. __fastcall TForm1::TForm1(TComponent* Owner)
  191.         : TForm(Owner)
  192. {
  193. }
  194. //---------------------------------------------------------------------------
  195.  
  196. void TForm1::setColorChunkyR(int x, int y, int n)
  197. {
  198.   curColorChunky->Canvas->Pixels[x][y] = (curColorChunky->Canvas->Pixels[x][y] & 0xffff00) + n;
  199. }
  200.  
  201. void TForm1::setColorChunkyG(int x, int y, int n)
  202. {
  203.   curColorChunky->Canvas->Pixels[x][y] = (curColorChunky->Canvas->Pixels[x][y] & 0xff00ff) + (n<<8);
  204. }
  205.  
  206. void TForm1::setColorChunkyB(int x, int y, int n)
  207. {
  208.   curColorChunky->Canvas->Pixels[x][y] = (curColorChunky->Canvas->Pixels[x][y] & 0x00ffff) + (n<<16);
  209. }
  210.  
  211. void TForm1::setHSpaletteR(int x, int y, int n)
  212. {
  213.   Image1->Canvas->Pixels[x][y] = (Image1->Canvas->Pixels[x][y] & 0xffff00) + n;
  214. }
  215.  
  216. void TForm1::setHSpaletteG(int x, int y, int n)
  217. {
  218.   Image1->Canvas->Pixels[x][y] = (Image1->Canvas->Pixels[x][y] & 0xff00ff) + (n<<8);
  219. }
  220.  
  221. void TForm1::setHSpaletteB(int x, int y, int n)
  222. {
  223.   Image1->Canvas->Pixels[x][y] = (Image1->Canvas->Pixels[x][y] & 0x00ffff) + (n<<16);
  224. }
  225.  
  226. void TForm1::showHSpalette()
  227. {
  228. //#define BYTE int
  229. #define REPEAT do
  230. #define UNTIL(x) while(!(x))
  231.   int x;
  232.   int xsize = Image1->Width; //128;
  233.   int y;
  234.   int ysize = Image1->Height; //192;
  235.  
  236.   float h,s;
  237.  
  238.   y = 0x00;
  239.   REPEAT {
  240.     h = (float)y/ysize;
  241.     x = 0x00;
  242.     REPEAT {
  243.       s = (float)(x&0xfffffffc)/xsize;
  244.  
  245.       TColor color = calcHSVtoRGB(h,s,curV);
  246.       //Image1->Canvas->Pixels[x][y] = color;
  247.  
  248.       int chunkpixel = (x&3) + ((y&3)<<2);
  249.       int ir = (color&0xff)/15;
  250.       int ig = ((color>>8)&0xff)/15;
  251.       int ib = ((color>>16)&0xff)/15;
  252.  
  253.       setHSpaletteR(x, y, (ir > chunkpixelnumber[chunkpixel])*255&0xff );
  254.       setHSpaletteG(x, y, (ig > chunkpixelnumber[chunkpixel])*255&0xff );
  255.       setHSpaletteB(x, y, (ib > chunkpixelnumber[chunkpixel])*255&0xff );
  256.  
  257.       x++;
  258.     }UNTIL (x == xsize);
  259.     y++;
  260.   }UNTIL (y == ysize);
  261. }
  262.  
  263. int getR(TColor rgb)
  264. {
  265.   return (rgb)&0xff;
  266. }
  267.  
  268. int getG(TColor rgb)
  269. {
  270.   return (rgb>>8)&0xff;
  271. }
  272.  
  273. int getB(TColor rgb)
  274. {
  275.   return (rgb>>16)&0xff;
  276. }
  277.  
  278. void __fastcall TForm1::FormCreate(TObject *Sender)
  279. {
  280.   int colindex;
  281.  
  282.   Image2->Picture->LoadFromFile("../lanscape.bmp");
  283.   //Image2->Picture->LoadFromFile("seversta.bmp");
  284.   //Image2->Picture->LoadFromFile("melnchud.bmp");
  285.   //Image2->Picture->LoadFromFile("hippiman.bmp");
  286.  
  287.   FILE* fin;
  288.   fin = fopen("softe16.act", "rb");
  289.   fread(pal16, 16*3, 1, fin);
  290.   fclose(fin);
  291.  
  292.   for (int i = 0; i<64; i++) {
  293.     int r = (i>>0)&0x03;
  294.     int g = (i>>2)&0x03;
  295.     int b = (i>>4)&0x03; //BBGGRR
  296.     int mindist = 10000;
  297.     for (int j = 0; j<16; j++) {
  298.       int r16 = (pal16[j*3+0]>>6)&0x03;
  299.       int g16 = (pal16[j*3+1]>>6)&0x03;
  300.       int b16 = (pal16[j*3+2]>>6)&0x03;
  301.       int dist = 3*abs(r-r16) +4*abs(g-g16) + 2*abs(b-b16);
  302.       //шчсхурЄ№ чрьхэ√ ёхЁ√ї ЎтхЄют юъЁр°хээ√ьш:
  303.       if ((r==g)&&(g==b)&&!((r16==g16)&&(g16==b16))) dist = 10000;
  304.       if (dist < mindist) {
  305.         mindist = dist;
  306.         t64to16[i] = j;
  307.       };
  308.  
  309.     };
  310.   };
  311.  
  312.   for (int i = 0; i<64; i++) { //BBGGRR
  313.     //Memo1->Lines->Add(IntToStr(t64to16[i]));
  314.     t64to16paper[i] = ((t64to16[i]&8)<<4) | ((t64to16[i]&7)<<3);
  315.     int rdown = (i&3) - 1; if (rdown<0) rdown = 0;
  316.     int gdown = ((i>>2)&3) - 1; if (gdown<0) gdown = 0;
  317.     int bdown = ((i>>4)&3) - 1; if (bdown<0) bdown = 0;
  318.     int j = (bdown<<4) + (gdown<<2) + rdown;
  319.     //t64to16ink[i] = ((t64to16[j]&8)<<3) | (t64to16[j]&7);
  320.     t64to16ink[i] = ((t64to16[i]&8)<<3) | (t64to16[i]&7);
  321.   };
  322.  
  323.   FILE* foutink;
  324.   foutink = fopen("t64to16i", "wb");
  325.   fwrite(t64to16ink, 64, 1, foutink);
  326.   fclose(foutink);
  327.  
  328.   FILE* foutpaper;
  329.   foutpaper = fopen("t64to16p", "wb");
  330.   fwrite(t64to16paper, 64, 1, foutpaper);
  331.   fclose(foutpaper);
  332.  
  333.   for (int i = 0; i<16; i++) {
  334.     unsigned char b;
  335.     int r16 = (pal16[i*3+0]>>6)&0x03;
  336.     int g16 = (pal16[i*3+1]>>6)&0x03;
  337.     int b16 = (pal16[i*3+2]>>6)&0x03;
  338.     //DDp palette: %grbG11RB(low),%grbG11RB(high), шэтхЁёэ√х
  339.     b = ((g16&1)<<7) | ((g16&2)<<3);
  340.     b |= ((r16&1)<<6) | ((r16&2)<<0);
  341.     b |= ((b16&1)<<5) | ((b16&2)>>1);
  342.     zxpal[i*2] = 255-b;
  343.     zxpal[i*2+1] = 255-b;
  344.   };
  345.  
  346.   FILE* foutzxpal;
  347.   foutzxpal = fopen("zxpal", "wb");
  348.   fwrite(zxpal, 16*2, 1, foutzxpal);
  349.   fclose(foutzxpal);
  350.  
  351.   for (int attr = 0; attr<256; attr++) {
  352.     int paper = ((attr&0x80)>>4) + ((attr&0x38)>>3);
  353.     int ink = ((attr&0x40)>>3) + (attr&0x07);
  354.     int Rmin = pal16[ink*3+0];
  355.     int Gmin = pal16[ink*3+1];
  356.     int Bmin = pal16[ink*3+2];
  357.     int Rmax = pal16[paper*3+0];
  358.     int Gmax = pal16[paper*3+1];
  359.     int Bmax = pal16[paper*3+2];
  360.  
  361.     int dist,maxdist;
  362.     int maxaxis;
  363.       //ш∙хь юё№ ё ьръёшьры№э√ь (max-min) ш схЁ╕ь юЄЄєфр maxcolor ш mincolor
  364.       dist = Rmax-Rmin;
  365.       maxdist = dist; maxaxis = 0;
  366.       dist = Gmax-Gmin;
  367.       if (dist > maxdist) {maxdist = dist; maxaxis = 1;}
  368.       dist = Bmax-Bmin;
  369.       if (dist > maxdist) {maxdist = dist; maxaxis = 2;}
  370.  
  371.     if (maxaxis == 0) {min_fromattr[attr] = Rmin;};
  372.     if (maxaxis == 1) {min_fromattr[attr] = Gmin;};
  373.     if (maxaxis == 2) {min_fromattr[attr] = Bmin;};
  374.  
  375.     maxdistdiv_fromattr[attr] = maxdist/4 + 0x40; //tdiv addr = 0x4000
  376.  
  377.     maxaxis_fromattr[attr] = maxaxis*8 + 64;
  378.   };
  379.  
  380.   FILE* foutmaxaxis;
  381.   foutmaxaxis = fopen("tmaxaxis", "wb");
  382.   fwrite(maxdistdiv_fromattr, 256, 1, foutmaxaxis);
  383.   fwrite(min_fromattr, 256, 1, foutmaxaxis);
  384.   fwrite(maxaxis_fromattr, 256, 1, foutmaxaxis);
  385.   fclose(foutmaxaxis);
  386.  
  387.   for (int dist = 0; dist<64; dist++) {
  388.     for (int delta = 0; delta<256; delta++) {
  389.       float dist_f = dist/64.;
  390.       float delta_f = delta/128.;
  391.       float result = 0;
  392.       if (dist != 0) result = delta_f/dist_f;
  393.       if (result > 1) result = 1;
  394.       if (delta >= 128) result = 0; //negative
  395.       tdiv[dist*256 + delta] = (unsigned char)(int)(result*16+.5);
  396.     };
  397.   };
  398.  
  399.   FILE* fdiv;
  400.   fdiv = fopen("tdiv", "wb");
  401.   fwrite(tdiv, 16384, 1, fdiv);
  402.   fclose(fdiv);
  403.  
  404.  
  405.  
  406. #define PICHGT 200
  407. #define PICWID8 80
  408.  
  409.   TColor rgb,rgb1,rgb2,rgb1attr,rgb2attr; //BBGGRR
  410.   //int color64;
  411.   //int color1;
  412.   //int color2;
  413.   int lowx;
  414.   int paper;
  415.   int ink;
  416.   int dist,maxdist;
  417.   int maxaxis; //0..2
  418.   int Rmin,Rmax;
  419.   int Gmin,Gmax;
  420.   int Bmin,Bmax;
  421.   int Rmincolor,Rmaxcolor;
  422.   int Gmincolor,Gmaxcolor;
  423.   int Bmincolor,Bmaxcolor;
  424.   int nRmincolor,nRmaxcolor;
  425.   int nGmincolor,nGmaxcolor;
  426.   int nBmincolor,nBmaxcolor;
  427.  
  428.   for (int y = 0; y < PICHGT; y++) {
  429.     for (int x8 = 0; x8 < PICWID8; x8++) {
  430.       paper = -1;
  431.       ink = -1;
  432.          //эрщЄш фтр color64, ёрь√х фры№эшх фЁєу юЄ фЁєур
  433.          //фы  ¤Єюую ш∙хь яю тёхь юё ь min,max,mincolor,maxcolor
  434.          //яюЄюь ш∙хь юё№ ё ьръёшьры№э√ь (max-min) ш схЁ╕ь юЄЄєфр maxcolor ш mincolor
  435.       Rmin = 0xff; Rmax = 0x00;
  436.       Gmin = 0xff; Gmax = 0x00;
  437.       Bmin = 0xff; Bmax = 0x00;
  438.       for (lowx = 0; lowx < 8; lowx++) {
  439.          rgb = Image2->Canvas->Pixels[x8*8+lowx][y*2];
  440.           //rgb = (rgb&0xff)*0x010101;
  441.          //color64 = ((rgb>>(16+6-4))&0x30) | ((rgb>>(8+6-2))&0x0c) | ((rgb>>(0+6-0))&0x03);
  442.          if (getR(rgb) < Rmin) {Rmin = getR(rgb); Rmincolor = rgb; nRmincolor = lowx;};
  443.          if (getR(rgb) >= Rmax) {Rmax = getR(rgb); Rmaxcolor = rgb; nRmaxcolor = lowx;};
  444.          if (getG(rgb) < Gmin) {Gmin = getG(rgb); Gmincolor = rgb; nGmincolor = lowx;};
  445.          if (getG(rgb) >= Gmax) {Gmax = getG(rgb); Gmaxcolor = rgb; nGmaxcolor = lowx;};
  446.          if (getB(rgb) < Bmin) {Bmin = getB(rgb); Bmincolor = rgb; nBmincolor = lowx;};
  447.          if (getB(rgb) >= Bmax) {Bmax = getB(rgb); Bmaxcolor = rgb; nBmaxcolor = lowx;};
  448.       };
  449.       //ш∙хь юё№ ё ьръёшьры№э√ь (max-min) ш схЁ╕ь юЄЄєфр maxcolor ш mincolor
  450.       dist = Bmax-Bmin;
  451.       maxdist = dist; maxaxis = 2;
  452.       dist = Gmax-Gmin;
  453.       if (dist >= maxdist) {maxdist = dist; maxaxis = 1;}
  454.       dist = Rmax-Rmin;
  455.       if (dist >= maxdist) {maxdist = dist; maxaxis = 0;}
  456.       if (maxaxis == 0) {rgb1 = Rmincolor; rgb2 = Rmaxcolor;};
  457.       if (maxaxis == 1) {rgb1 = Gmincolor; rgb2 = Gmaxcolor;};
  458.       if (maxaxis == 2) {rgb1 = Bmincolor; rgb2 = Bmaxcolor;};
  459.     if (y==1) {
  460.       Memo1->Lines->Add(IntToStr(y)+':'+IntToStr(x8)+":Bmax="+IntToStr(Bmax)+",Bmin="+IntToStr(Bmin)+",Gmax="+IntToStr(Gmax)+",Gmin="+IntToStr(Gmin)+",Rmax="+IntToStr(Rmax)+",Rmin="+IntToStr(Rmin));
  461.       Memo1->Lines->Add(IntToStr(y)+':'+IntToStr(x8)+":nBmax="+IntToStr(nBmaxcolor)+",nBmin="+IntToStr(nBmincolor)+",nGmax="+IntToStr(nGmaxcolor)+",nGmin="+IntToStr(nGmincolor)+",nRmax="+IntToStr(nRmaxcolor)+",nRmin="+IntToStr(nRmincolor));
  462.       Memo1->Lines->Add(IntToStr(y)+':'+IntToStr(x8)+":maxaxis="+IntToStr(maxaxis));
  463.     };
  464.  
  465.       int r,g,b;
  466.       int i,p;
  467. //#define ROUNDDOWN 64
  468. #define ROUNDDOWN 32
  469.       r = (((int)getR(rgb1)-16/*-ROUNDDOWN*/)>>6);
  470.       g = (((int)getG(rgb1)-ROUNDDOWN)>>6);
  471.       b = (((int)getB(rgb1)-ROUNDDOWN)>>6);
  472.       if (r<0) r = 0;
  473.       if (g<0) g = 0;
  474.       if (b<0) b = 0;
  475.       //i = t64to16[(b<<4) | (g<<2) | (r<<0)];
  476.     if (y==1) {
  477.       Memo1->Lines->Add(IntToStr(y)+':'+IntToStr(x8)+":r2="+IntToStr(getR(rgb2))+",g2="+IntToStr(getG(rgb2))+",b2="+IntToStr(getB(rgb2)));
  478.       Memo1->Lines->Add(IntToStr(y)+':'+IntToStr(x8)+":inkindex64="+IntToStr((b<<4) | (g<<2) | (r<<0)));
  479.     };
  480.       int reali = t64to16ink[(b<<4) | (g<<2) | (r<<0)];
  481.       i = ((reali&0x40)>>3) | (reali&7);
  482.       r = pal16[i*3+0];
  483.       g = pal16[i*3+1];
  484.       b = pal16[i*3+2];
  485.       rgb1attr = (b<<16) | (g<<8) | r;
  486.  
  487. #define ROUNDUP 32
  488.       r = (((int)getR(rgb2)+16/*ROUNDUP*/)>>6);
  489.       g = (((int)getG(rgb2)+ROUNDUP)>>6);
  490.       b = (((int)getB(rgb2)+ROUNDUP)>>6);
  491.       if (r>=4) r = 3;
  492.       if (g>=4) g = 3;
  493.       if (b>=4) b = 3;
  494.       p = t64to16[(b<<4) | (g<<2) | (r<<0)];
  495.       int realp = t64to16paper[(b<<4) | (g<<2) | (r<<0)];
  496.       r = pal16[p*3+0];
  497.       g = pal16[p*3+1];
  498.       b = pal16[p*3+2];
  499.       rgb2attr = (b<<16) | (g<<8) | r;
  500.  
  501.     //int realattr = ((p&8)<<4) | ((i&8)<<3) | ((p&7)<<3) | (i&7);
  502.     int realattr = reali | realp;
  503.     if (y==1) {
  504.       Memo1->Lines->Add(IntToStr(y)+':'+IntToStr(x8)+':'+IntToStr(p)+','+IntToStr(i)+'='+IntToStr(realattr));
  505.     };
  506.  
  507.       rgb1 = rgb1attr;
  508.       rgb2 = rgb2attr;
  509. /*
  510.     dist = abs(getR(rgb2)-getR(rgb1));
  511.     maxdist = dist; maxaxis = 0;
  512.     dist = abs(getG(rgb2)-getG(rgb1));
  513.     if (dist > maxdist) {maxdist = dist; maxaxis = 1;}
  514.     dist = abs(getB(rgb2)-getB(rgb1));
  515.     if (dist > maxdist) {maxdist = dist; maxaxis = 2;}
  516. */
  517.     maxdist = (maxdistdiv_fromattr[realattr]&0x3f)*4;
  518.     maxaxis = (maxaxis_fromattr[realattr]&0x3f) /8;
  519.     int min = min_fromattr[realattr];
  520.     //maxaxis = 2-maxaxis;
  521.  
  522.     /**/
  523.     /*
  524.     if (maxaxis == 0) maxdist = getR(rgb2)-getR(rgb1);
  525.     if (maxaxis == 1) maxdist = getG(rgb2)-getG(rgb1);
  526.     if (maxaxis == 2) maxdist = getB(rgb2)-getB(rgb1);
  527.     /**/
  528.  
  529.       for (lowx = 0; lowx < 8; lowx++) {
  530.          rgb = Image2->Canvas->Pixels[x8*8+lowx][y*2];
  531.           //rgb = (rgb&0xff)*0x010101;
  532.  
  533.          float intlevel = 0;
  534.          if (maxdist > 10)
  535.          {
  536.            if (maxaxis == 0) intlevel = (getR(rgb)-getR(rgb1))/(float)(maxdist);
  537.            if (maxaxis == 1) intlevel = (getG(rgb)-getG(rgb1))/(float)(maxdist);
  538.            if (maxaxis == 2) intlevel = (getB(rgb)-getB(rgb1))/(float)(maxdist);
  539.          };
  540.          //if (intlevel < 0) intlevel = 0;
  541.          //if (intlevel > 1) intlevel = 1;
  542.  
  543.       int delta;
  544.       if (maxaxis == 0) delta = (getR(rgb)-min/*getR(rgb1)*/);
  545.       if (maxaxis == 1) delta = (getG(rgb)-min/*getG(rgb1)*/);
  546.       if (maxaxis == 2) delta = (getB(rgb)-min/*getB(rgb1)*/);
  547.  
  548.       delta = delta / 2;
  549.       //if (delta < 0) delta = 0;
  550.       delta = delta&0xff;
  551.  
  552.       /*float dist_f = maxdist/256.;
  553.       float delta_f = delta/128.;
  554.       float result = 0;
  555.       if (dist > 10) result = delta_f/dist_f;
  556.       if (result > 1) result = 1;
  557.       if (delta >= 128) result = 0; //negative
  558.       int intintlevel = (unsigned char)(int)(result*16+.5);
  559.       */
  560.       int intintlevel = tdiv[256*(maxdist/4) + delta];
  561.  
  562.       int chunkpixel = (lowx&3) + ((y&3)<<2);
  563.       //if ((int)(intlevel*16+.5) >= chunkpixelnumberdiamond[chunkpixel]) {
  564.       if (intintlevel >= chunkpixelnumberdiamond[chunkpixel]) {
  565.         rgb = rgb2attr;
  566.       }else {
  567.         rgb = rgb1attr;
  568.       };
  569.           //rgb =            (unsigned char)((1-intlevel)*getB(rgb1attr) + (intlevel)*getB(rgb2attr));
  570.           //rgb = (rgb<<8) + (unsigned char)((1-intlevel)*getG(rgb1attr) + (intlevel)*getG(rgb2attr));
  571.           //rgb = (rgb<<8) + (unsigned char)((1-intlevel)*getR(rgb1attr) + (intlevel)*getR(rgb2attr));
  572.          ImageOut->Canvas->Pixels[x8*8+lowx][y*2] = rgb;
  573.          ImageOut->Canvas->Pixels[x8*8+lowx][y*2+1] = rgb;
  574.  
  575.          if (realattr == 0xf1) {
  576.            Memo1->Lines->Add(IntToStr(intintlevel));
  577.          };
  578.       };
  579.     };
  580.   };
  581.  
  582.  
  583.  
  584.   for (int i = 0; i<3*PALEXTRA ;i++) {
  585.     pal[i] = palextra[i];
  586.   };
  587.  
  588.   colindex = 3*PALEXTRA;
  589.  
  590.   for (int r = 0; r<10 ;r++) {
  591.   for (int g = 0; g<10 ;g++) {
  592.   for (int b = 0; b<10 ;b++) {
  593.     if (colorexist[(9-r)*100 + (9-b)*10 + g]) {
  594.       pal[colindex++] = r*PALCOEFF;
  595.       pal[colindex++] = g*PALCOEFF;
  596.       pal[colindex++] = b*PALCOEFF;
  597.     };
  598.   };
  599.   };
  600.   };
  601.  
  602.   Memo1->Lines->Add(IntToStr(colindex));
  603.  
  604.   FILE* fout;
  605.   fout = fopen("pal256.act", "wb");
  606.   fwrite(pal, 768, 1, fout);
  607.   fclose(fout);
  608.  
  609.   curH = .2;
  610.   curS = .2;
  611.   curV = .4;
  612.  
  613. #define PI 3.14159
  614.   for (int i=0; i<256; i++) {
  615.     float a = (float)i/128*PI;
  616.     tsin[i] = 127*sin(a);
  617.   }
  618.  
  619.   showHSpalette();
  620.   showVpalette();
  621.   showcolor();
  622.  
  623. }
  624.  
  625. float fabs(float a)
  626. {
  627.   if (a>0) return a; else return -a;
  628. }
  629.  
  630. int TForm1::calcHSVtoRGB(float h,float s,float v){
  631. #define minC 64
  632. #define maxC 96
  633. #define kC 8
  634. //#define BYTE unsigned char
  635.   unsigned char ir,ig,ib;
  636.  
  637.   int ih,is,iv;
  638.   ih = h*256;
  639.   is = s*256;
  640.   //v = 1;
  641.   iv = v*256/kC;
  642.  
  643.   int scoeff = (1 - fabs((v-.5)*2) )*256;
  644.  
  645.   is = is*scoeff/256;
  646.  
  647.   int isi = 2*is*tsin[ih]/kC/256;
  648.   int ic = 2*is*tsin[(64-ih)&0xff]/kC/256;
  649.  
  650.   //h=0..32
  651.   //si,c=+-0..32
  652.  
  653.   ir = iv + 2*isi + ic + minC;
  654.   ig = iv +       - ic + minC;
  655.   ib = iv - 2*isi + ic + minC;
  656.  
  657. //  if (ig >= 256) {ir = ir + (ig-256); ib = ib + (ig-256); }
  658.  
  659.   if (ir<minC) ir=minC; if (ir>=maxC) ir=maxC-1;
  660.   if (ig<minC) ig=minC; if (ig>=maxC) ig=maxC-1;
  661.   if (ib<minC) ib=minC; if (ib>=maxC) ib=maxC-1;
  662.  
  663.   ir = (ir-minC)*kC;
  664.   ig = (ig-minC)*kC;
  665.   ib = (ib-minC)*kC;
  666.  
  667.   return (ib<<16) + (ig<<8) + (ir);
  668. }
  669.  
  670. void TForm1::showcolor()
  671. {
  672.   int x,y;
  673.  
  674.   TColor color = calcHSVtoRGB(curH,curS,curV);
  675.   lbR->Caption = IntToStr(color & 0xff);
  676.   lbG->Caption = IntToStr((color>>8) & 0xff);
  677.   lbB->Caption = IntToStr((color>>16) & 0xff);
  678.  
  679.   int hgt = curColor->Height-1;
  680.   int wid = curColor->Width-1;
  681.  
  682.   for (y=0; y<=hgt; y++)
  683.        for (x=0; x<=wid; x++)
  684.           curColor->Canvas->Pixels[x][y] = color;
  685.  
  686.   hgt = curColorChunky->Height-1;
  687.   wid = curColorChunky->Width-1;
  688.  
  689.   for (y=0; y<=hgt; y++)
  690.        for (x=0; x<=wid; x++)
  691.        {
  692.           int chunkpixel = (x&3) + ((y&3)<<2);
  693.           int ir = (color&0xff)/15;
  694.           int ig = ((color>>8)&0xff)/15;
  695.           int ib = ((color>>16)&0xff)/15;
  696.  
  697.           setColorChunkyR(x, y, (ir > chunkpixelnumber[chunkpixel])*255&0xff );
  698.           setColorChunkyG(x, y, (ig > chunkpixelnumber[chunkpixel])*255&0xff );
  699.           setColorChunkyB(x, y, (ib > chunkpixelnumber[chunkpixel])*255&0xff );
  700. //          setColorChunkyR(x, y, (ir >= chunkpixelnumber[(x&3) + ((y&3)<<2)])*255&0xff );
  701. //          setColorChunkyG(x, y, (ig >= chunkpixelnumber[((x+1)&3) + ((y&3)<<2)])*255&0xff );
  702. //          setColorChunkyB(x, y, (ib >= chunkpixelnumber[(x&3) + (((y+1)&3)<<2)])*255&0xff );
  703.        }
  704. }
  705.  
  706. void TForm1::showVpalette()
  707. {
  708.   int hgt = changV->Height-1;
  709.   int wid = changV->Width-1;
  710.   for (int y=0;y<=hgt;y++)
  711.   {
  712.        for (int x=0;x<=wid;x++)
  713.        {
  714.           changV->Canvas->Pixels[x][y] = calcHSVtoRGB(curH,curS,(float)(hgt-1-y)/hgt);
  715.        }
  716.   }
  717. }
  718.  
  719. //---------------------------------------------------------------------------
  720. void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift,
  721.       int X, int Y)
  722. {
  723.  
  724.   curmouseH = (float)Y/(Image1->Height);
  725.   curmouseS = (float)X/(Image1->Width);
  726.  
  727.   TColor color = calcHSVtoRGB(curmouseH,curmouseS,curV); //Image1->Canvas->Pixels[X][Y];
  728.  
  729.   lbR->Caption = IntToStr(color & 0xff);
  730.   lbG->Caption = IntToStr((color>>8) & 0xff);
  731.   lbB->Caption = IntToStr((color>>16) & 0xff);
  732.  
  733. }
  734. //---------------------------------------------------------------------------
  735.  
  736.  
  737. void __fastcall TForm1::Image1Click(TObject *Sender)
  738. {
  739.   curH = curmouseH;
  740.   curS = curmouseS;
  741.  
  742.   showcolor();
  743.   showVpalette();
  744.  
  745. }
  746. //---------------------------------------------------------------------------
  747.  
  748.  
  749. void __fastcall TForm1::changVMouseMove(TObject *Sender, TShiftState Shift,
  750.       int X, int Y)
  751. {
  752.   curmouseV = 1-(float)(Y)/(changV->Height-1);
  753. }
  754. //---------------------------------------------------------------------------
  755.  
  756. void __fastcall TForm1::changVClick(TObject *Sender)
  757. {
  758.   curV = curmouseV;
  759.   //showHSpalette();
  760.   showcolor();
  761. }
  762. //---------------------------------------------------------------------------
  763.  
  764. void __fastcall TForm1::curColorClick(TObject *Sender)
  765. {
  766.   TColor color = calcHSVtoRGB(curH,curS,curV); //Image1->Canvas->Pixels[X][Y];
  767. //#define PI 3.14159
  768.   float si,c;
  769.   float h,s,v;
  770.   float r,g,b;
  771.   r = (float)(color & 0xff)/256;
  772.   g = (float)((color>>8) & 0xff)/256;
  773.   b = (float)((color>>16) & 0xff)/256;
  774.   si = r/2-b/2;
  775.   c = r/2+b/2-g;
  776.   float rad = sqrt(si*si + c*c);
  777.   Memo1->Lines->Add("r="+FloatToStr(r));
  778.   Memo1->Lines->Add("g="+FloatToStr(g));
  779.   Memo1->Lines->Add("b="+FloatToStr(b));
  780.   Memo1->Lines->Add("si="+FloatToStr(si));
  781.   Memo1->Lines->Add("c="+FloatToStr(c));
  782.   Memo1->Lines->Add("rad="+FloatToStr(rad));
  783.  
  784.   //TODO if (rad==0) ...
  785.  
  786.   //float a=atan2(si,c);
  787.   float a = asin(si/rad);
  788.   if (c<0) a = PI - a;
  789.   Memo1->Lines->Add("a="+FloatToStr(a));
  790.   h=a/(2*PI);
  791.   if (h<0)
  792.      {h=h+1;}
  793.   Memo1->Lines->Add("oldH="+FloatToStr(curH));
  794.   Memo1->Lines->Add("H="+FloatToStr(h));
  795.   curH=h;
  796.  
  797.   v = g/2 + r/4 + b/4;
  798.  
  799.   Memo1->Lines->Add("oldV="+FloatToStr(curV));
  800.   Memo1->Lines->Add("V="+FloatToStr(v));
  801.   curV=v;
  802.  
  803.   float scoeff = (1 - fabs((v-.5)*2) );
  804.   Memo1->Lines->Add("scoeff="+FloatToStr(scoeff));
  805.   s = rad/scoeff/1.7;
  806.   //if (s>1) s = 1;
  807.   Memo1->Lines->Add("oldS="+FloatToStr(curS));
  808.   Memo1->Lines->Add("S="+FloatToStr(s));
  809.   curS=s;
  810.  
  811.   showVpalette();
  812.   showcolor();
  813. }
  814. //---------------------------------------------------------------------------
  815.  
  816.