[be9162]: PaletteEdit.cpp Maximize Restore History

Download this file

PaletteEdit.cpp    505 lines (451 with data), 10.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
// PaletteEdit.cpp : implementation file
//
#include "stdafx.h"
#include "chitem.h"
#include "PaletteEdit.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPaletteEdit dialog
CPaletteEdit::CPaletteEdit(CWnd* pParent /*=NULL*/)
: CDialog(CPaletteEdit::IDD, pParent)
{
//{{AFX_DATA_INIT(CPaletteEdit)
m_green = 0;
m_blue = 0;
m_red = 0;
m_from = 1;
m_to = 255;
m_function = -1;
m_keepgray = FALSE;
m_xr = 0;
m_xg = 0;
m_xb = 0;
//}}AFX_DATA_INIT
mypoint=0;
palette=0;
m_pSteps=NULL;
m_max=256;
}
void CPaletteEdit::DoDataExchange(CDataExchange* pDX)
{
BYTE myidx;
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPaletteEdit)
DDX_Control(pDX, IDC_PALETTE, m_palette_control);
DDX_Text(pDX, IDC_GREEN, m_green);
DDX_Text(pDX, IDC_BLUE, m_blue);
DDX_Text(pDX, IDC_RED, m_red);
DDX_Text(pDX, IDC_FROM, m_from);
DDX_Text(pDX, IDC_TO, m_to);
DDX_CBIndex(pDX, IDC_FUNCTION, m_function);
DDX_Check(pDX, IDC_KEEPGRAY, m_keepgray);
DDX_Text(pDX, IDC_BLUE2, m_xr);
DDX_Text(pDX, IDC_GREEN2, m_xg);
DDX_Text(pDX, IDC_RED2, m_xb);
//}}AFX_DATA_MAP
if(pDX->m_bSaveAndValidate==UD_DISPLAY)
{
myidx=(BYTE) (mypoint.x+mypoint.y*16);
DDX_Text(pDX, IDC_IDX, myidx);
}
else
{
DDX_Text(pDX, IDC_IDX, myidx);
mypoint.x=myidx&15;
mypoint.y=myidx>>4;
}
}
BEGIN_MESSAGE_MAP(CPaletteEdit, CDialog)
//{{AFX_MSG_MAP(CPaletteEdit)
ON_STN_CLICKED(IDC_PALETTE, OnPalette2)
ON_STN_DBLCLK(IDC_PALETTE, OnPalette)
ON_WM_MOUSEMOVE()
ON_EN_KILLFOCUS(IDC_BLUE, OnKillfocusBlue)
ON_EN_KILLFOCUS(IDC_GREEN, OnKillfocusGreen)
ON_EN_KILLFOCUS(IDC_RED, OnKillfocusRed)
ON_EN_KILLFOCUS(IDC_IDX, OnKillfocusIdx)
ON_EN_KILLFOCUS(IDC_FROM, OnKillfocusFrom)
ON_EN_KILLFOCUS(IDC_TO, OnKillfocusTo)
ON_BN_CLICKED(IDC_SMOOTH, OnSmooth)
ON_CBN_KILLFOCUS(IDC_FUNCTION, OnKillfocusFunction)
ON_BN_CLICKED(IDC_RGB, OnRgb)
ON_BN_CLICKED(IDC_ALL, OnAll)
ON_BN_CLICKED(IDC_KEEPGRAY, OnKeepgray)
ON_EN_KILLFOCUS(IDC_BLUE2, OnKillfocusBlue2)
ON_EN_KILLFOCUS(IDC_GREEN2, OnKillfocusGreen2)
ON_EN_KILLFOCUS(IDC_RED2, OnKillfocusRed2)
ON_BN_CLICKED(IDC_SWAP, OnSwap)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPaletteEdit message handlers
void CPaletteEdit::OnPalette2()
{
COLORREF mycolor;
mypoint=mousepoint;
MapWindowPoints(GetDlgItem(IDC_PALETTE),&mypoint,1);
mypoint.x/=16;
mypoint.y/=16;
if(mypoint.x<0 || mypoint.y<0 || mypoint.x>15 || mypoint.y>15) return;
mycolor=palette[mypoint.x+mypoint.y*16];
m_red=GetRValue(mycolor);
m_green=GetGValue(mycolor);
m_blue=GetBValue(mycolor);
InitPaletteBitmap();
UpdateData(UD_DISPLAY);
}
void CPaletteEdit::OnPalette()
{
COLORREF mycolor;
mypoint=mousepoint;
MapWindowPoints(GetDlgItem(IDC_PALETTE),&mypoint,1);
mypoint.x/=16;
mypoint.y/=16;
if(mypoint.x<0 || mypoint.y<0 || mypoint.x>15 || mypoint.y>15) return;
if(dlg.DoModal()==IDOK)
{
palette[mypoint.x+mypoint.y*16]=dlg.GetColor();
}
mycolor=palette[mypoint.x+mypoint.y*16];
m_red=GetRValue(mycolor);
m_green=GetGValue(mycolor);
m_blue=GetBValue(mycolor);
InitPaletteBitmap();
UpdateData(UD_DISPLAY);
}
void CPaletteEdit::InitPaletteBitmap()
{
BYTE Red,Green,Blue;
HBITMAP hb;
BITMAPINFO bmi;
int i,j;
int idx, myidx;
CDC *pDC = AfxGetMainWnd()->GetDC();
if (!pDC)
return;
myidx=mypoint.x+mypoint.y*16;
for(j=0;j<m_max;j++)
{
Red=((BYTE *) (palette+j))[2];
Green=((BYTE *) (palette+j))[1];
Blue=((BYTE *) (palette+j))[0];
for(i=0;i<256;i++)
{
idx=((j&0xf0)<<8)|((i&0xf0)<<4)|((j&15)<<4)|(i&15);
if((myidx==j) && ((i&3)==((i>>4)&3)) )
{
colorbits[idx].rgbRed=(BYTE) (Red^0xaa);
colorbits[idx].rgbGreen=(BYTE) (Green^0xaa);
colorbits[idx].rgbBlue=(BYTE) (Blue^0xaa);
}
else
{
colorbits[idx].rgbRed=Red;
colorbits[idx].rgbGreen=Green;
colorbits[idx].rgbBlue=Blue;
}
colorbits[idx].rgbReserved=0;
}
}
memset(&bmi.bmiHeader,0,sizeof(BITMAPINFOHEADER));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = 16*16;
bmi.bmiHeader.biHeight = -m_max;// The graphics are stored as bottom up DIBs.
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
hb = ::CreateDIBitmap(pDC->GetSafeHdc(), &bmi.bmiHeader,CBM_INIT,colorbits,
&bmi,DIB_RGB_COLORS);
AfxGetMainWnd()->ReleaseDC(pDC);
m_palette_control.SetBitmap(hb);
}
BOOL CPaletteEdit::OnInitDialog()
{
COLORREF mycolor;
CDialog::OnInitDialog();
//tooltips
{
m_tooltip.Create(this,TTS_NOPREFIX);
m_tooltip.SetMaxTipWidth(200);
m_tooltip.SetTipBkColor(RGB(240,224,160));
m_tooltip.AddTool(GetDlgItem(IDCANCEL), IDS_CANCEL);
}
mypoint=0;
palettehwnd=GetDlgItem(IDC_PALETTE)->m_hWnd;
mycolor=palette[0];
m_red=GetRValue(mycolor);
m_green=GetGValue(mycolor);
m_blue=GetBValue(mycolor);
InitPaletteBitmap();
UpdateData(UD_DISPLAY);
return TRUE;
}
void CPaletteEdit::OnMouseMove(UINT nFlags, CPoint point)
{
mousepoint=point;
CDialog::OnMouseMove(nFlags, point);
}
BOOL CPaletteEdit::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->hwnd==palettehwnd)
{
if(pMsg->message==WM_MOUSEMOVE)
{
mousepoint=pMsg->pt;
ScreenToClient(&mousepoint);
}
}
else m_tooltip.RelayEvent(pMsg);
return CDialog::PreTranslateMessage(pMsg);
}
void CPaletteEdit::OnKillfocusBlue()
{
int myidx;
UpdateData(UD_RETRIEVE);
myidx=mypoint.x+mypoint.y*16;
*((BYTE *) (palette+myidx)+2)=m_blue;
InitPaletteBitmap();
UpdateData(UD_DISPLAY);
}
void CPaletteEdit::OnKillfocusGreen()
{
int myidx;
UpdateData(UD_RETRIEVE);
myidx=mypoint.x+mypoint.y*16;
*((BYTE *) (palette+myidx)+1)=m_green;
InitPaletteBitmap();
UpdateData(UD_DISPLAY);
}
void CPaletteEdit::OnKillfocusRed()
{
int myidx;
UpdateData(UD_RETRIEVE);
myidx=mypoint.x+mypoint.y*16;
*((BYTE *) (palette+myidx))=m_red;
InitPaletteBitmap();
UpdateData(UD_DISPLAY);
}
void CPaletteEdit::OnKillfocusIdx()
{
UpdateData(UD_RETRIEVE);
InitPaletteBitmap();
UpdateData(UD_DISPLAY);
}
void CPaletteEdit::OnKillfocusFrom()
{
UpdateData(UD_RETRIEVE);
UpdateData(UD_DISPLAY);
}
void CPaletteEdit::OnKillfocusTo()
{
UpdateData(UD_RETRIEVE);
UpdateData(UD_DISPLAY);
}
void CPaletteEdit::OnAll() //all except transparent color
{
m_from=1;
m_to=255;
UpdateData(UD_DISPLAY);
}
void CPaletteEdit::OnSmooth()
{
int from, to, div, pos;
BYTE fr, fg, fb;
BYTE tr, tg, tb;
BYTE val;
if(m_to<m_from) { from=m_to; to=m_from; }
else { from=m_from; to=m_to; }
div=to-from;
if(div<3) return; //no way
fr=GetRValue(palette[from]);
fg=GetGValue(palette[from]);
fb=GetBValue(palette[from]);
tr=GetRValue(palette[to]);
tg=GetGValue(palette[to]);
tb=GetBValue(palette[to]);
pos=0;
while(from++<to)
{
pos++;
val=(BYTE) (0.5+fr+(tr-fr)*pos/div);
*((BYTE *) (palette+from))=val;
val=(BYTE) (0.5+fg+(tg-fg)*pos/div);
*((BYTE *) (palette+from)+1)=val;
val=(BYTE) (0.5+fb+(tb-fb)*pos/div);
*((BYTE *) (palette+from)+2)=val;
}
}
void CPaletteEdit::OnKillfocusFunction()
{
UpdateData(UD_RETRIEVE);
UpdateData(UD_DISPLAY);
}
void CPaletteEdit::OnKeepgray()
{
UpdateData(UD_RETRIEVE);
UpdateData(UD_DISPLAY);
}
void CPaletteEdit::ExecuteStep(int &fr, int &fg, int &fb, int &ftmp, int function)
{
switch(function)
{
case 100: ftmp=fr; break;
case 101: ftmp=fg; break;
case 102: ftmp=fb; break;
case 103: fr=ftmp; break;
case 104: fg=ftmp; break;
case 105: fb=ftmp; break;
case 106: ftmp/=2; break;
case 107: ftmp*=2; break;
case 0: fr=fg; break;
case 1: fr=fb; break;
case 2: fg=fr; break;
case 3: fg=fb; break;
case 4: fb=fr; break;
case 5: fb=fg; break;
case 6: ftmp=fr; fr=fg; fg=ftmp; break;
case 7: ftmp=fg; fg=fb; fb=ftmp; break;
case 8: ftmp=fb; fb=fr; fr=ftmp; break;
case 9: fr/=2; break;
case 10: fg/=2; break;
case 11: fb/=2; break;
case 12: fr*=2; break;
case 13: fg*=2; break;
case 14: fb*=2; break;
case 15: //blue red
if(fr>(fb+fg)*1.111)
{
ftmp=fb; fb=fr; fr=ftmp;
}
break;
case 16: //red green
if(fg>(fr+fb)*1.111)
{
ftmp=fr; fr=fg; fg=ftmp;
}
break;
case 17: //green blue
if(fb>(fg+fr)*1.111)
{
ftmp=fg; fg=fb; fb=ftmp;
}
break;
case 18:
fr+=m_xr; fg+=m_xg; fb+=m_xb;
break;
case 19:
fr-=m_xr; fg-=m_xg; fb-=m_xb;
break;
case 20: //redshift
if(fb+fg>fr*1.35)
{
ftmp=(int) ((fb>fg?fg:fb)/1.5);
fb-=ftmp;
fg-=ftmp;
}
break;
case 21: //darken
fr = fr*fr/255;
fg = fg*fg/255;
fb = fb*fb/255;
break;
case 22: //contrast
fr = fr*fr/128;
fg = fg*fg/128;
fb = fb*fb/128;
break;
}
if(fr>255) fr=255;
else if(fr<0) fr=0;
if(fg>255) fg=255;
else if(fg<0) fg=0;
if(fb>255) fb=255;
else if(fb<0) fb=0;
}
#define SCRIPT 23
int CPaletteEdit::ExecuteScript(int &fr, int &fg, int &fb, int &ftmp)
{
int i;
if(!m_pSteps)
{
MessageBox("Sorry... Not implemented.","Warning",MB_OK);
return -1;
}
for(i=0;i<m_nStep;i++)
{
ExecuteStep(fr,fg,fb,ftmp,m_pSteps[i]);
}
return 0;
}
void CPaletteEdit::OnRgb()
{
int from, to;
int tmp, isgray;
int fr, fg, fb, ftmp;
if(m_to<m_from) { from=m_to; to=m_from; }
else { from=m_from; to=m_to; }
for(;from<=to;from++)
{
fb=GetRValue(palette[from]);
fg=GetGValue(palette[from]);
fr=GetBValue(palette[from]);
if(m_keepgray)
{
tmp=(fb-fg);
tmp*=tmp;
isgray=tmp;
tmp=(fb-fr);
tmp*=tmp;
isgray+=tmp;
if(isgray<GREY_TRESHOLD) continue;
}
ftmp=0;
if(m_function==SCRIPT)
{
if(ExecuteScript(fr,fg,fb,ftmp))
{
break;
}
}
else
{
ExecuteStep(fr,fg,fb,ftmp,m_function);
}
*(BYTE *) (palette+from)=(BYTE) fb;
*((BYTE *) (palette+from)+1)=(BYTE) fg;
*((BYTE *) (palette+from)+2)=(BYTE) fr;
}
InitPaletteBitmap();
UpdateData(UD_DISPLAY);
}
void CPaletteEdit::OnKillfocusBlue2()
{
UpdateData(UD_RETRIEVE);
UpdateData(UD_DISPLAY);
}
void CPaletteEdit::OnKillfocusGreen2()
{
UpdateData(UD_RETRIEVE);
UpdateData(UD_DISPLAY);
}
void CPaletteEdit::OnKillfocusRed2()
{
UpdateData(UD_RETRIEVE);
UpdateData(UD_DISPLAY);
}
void CPaletteEdit::OnSwap()
{
int myidx;
myidx=mypoint.x+mypoint.y*16;
if(myidx==0)
{
MessageBox("Can't swap the transparent color here.","Palette editor",MB_OK);
return;
}
SwapPalette(palette,myidx,1);
UpdateData(UD_DISPLAY);
}