- Revert 47615. Please fix actual sysreg instead of adding inconsistency between...
[reactos.git] / rosapps / templates / imagesoft / brightness.c
1 #include "precomp.h"
2
3 #define BASECOLOUR 100
4
5
6 VOID
7 AdjustBrightness(HBITMAP hOrigBitmap,
8 HBITMAP hNewBitmap,
9 HWND hwnd,
10 HDC hdcMem,
11 INT RedVal,
12 INT GreenVal,
13 INT BlueVal)
14 {
15 BITMAPINFO bi;
16 BITMAP bitmap;
17 BOOL bRes;
18 DWORD Count = 0;
19 INT i, j;
20 PBYTE pBits;
21 RECT rc;
22
23 GetObject(hNewBitmap,
24 sizeof(BITMAP),
25 &bitmap);
26
27 /* Bitmap header */
28 bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
29 bi.bmiHeader.biWidth = bitmap.bmWidth;
30 bi.bmiHeader.biHeight = bitmap.bmHeight;
31 bi.bmiHeader.biPlanes = 1;
32 bi.bmiHeader.biBitCount = 32;
33 bi.bmiHeader.biCompression = BI_RGB;
34 bi.bmiHeader.biSizeImage = bitmap.bmWidth * bitmap.bmHeight * 4;
35 bi.bmiHeader.biClrUsed = 0;
36 bi.bmiHeader.biClrImportant = 0;
37
38 /* Buffer */
39 pBits = (PBYTE)HeapAlloc(ProcessHeap,
40 0,
41 bitmap.bmWidth * bitmap.bmHeight * 4);
42 if (!pBits)
43 return;
44
45 /* get the bits from the original bitmap */
46 bRes = GetDIBits(hdcMem,
47 hOrigBitmap,
48 0,
49 bitmap.bmHeight,
50 pBits,
51 &bi,
52 DIB_RGB_COLORS);
53
54 for (i = 0; i < bitmap.bmHeight; i++)
55 {
56 for (j = 0; j < bitmap.bmWidth; j++)
57 {
58 DWORD Val = 0;
59 INT b, g, r;
60
61 CopyMemory(&Val,
62 &pBits[Count],
63 4);
64
65 /* Get pixels in reverse order */
66 b = GetRValue(Val);
67 g = GetGValue(Val);
68 r = GetBValue(Val);
69
70 /* Red */
71 r += RedVal;
72 if (r > 255) r = 255;
73 else if (r < 0) r = 0;
74
75 /* Green */
76 g += GreenVal;
77 if (g > 255) g = 255;
78 else if (g < 0) g = 0;
79
80 /* Blue */
81 b += BlueVal;
82 if (b > 255) b = 255;
83 else if (b < 0) b = 0;
84
85 /* Store in reverse order */
86 Val = RGB(b, g, r);
87 CopyMemory(&pBits[Count],
88 &Val,
89 4);
90
91 /* RGB color take 4 bytes.The high-order byte must be zero */
92 Count += 4;
93 }
94 }
95
96 /* Set the new pixel bits */
97 SetDIBits(hdcMem,
98 hNewBitmap,
99 0,
100 bRes,
101 pBits,
102 &bi,
103 DIB_RGB_COLORS);
104
105 HeapFree(ProcessHeap,
106 0,
107 pBits);
108
109 GetClientRect(hwnd,
110 &rc);
111
112 InvalidateRect(hwnd,
113 &rc,
114 FALSE);
115 }
116
117
118 static PIMAGEADJUST
119 Bri_OnInitDialog(PIMAGEADJUST pImgAdj,
120 HWND hDlg,
121 LPARAM lParam)
122 {
123 pImgAdj = (IMAGEADJUST*) HeapAlloc(ProcessHeap,
124 0,
125 sizeof(IMAGEADJUST));
126 if (!pImgAdj)
127 return NULL;
128
129
130 pImgAdj->Info = (PMAIN_WND_INFO)lParam;
131 if (!pImgAdj->Info->ImageEditors)
132 goto fail;
133
134
135 pImgAdj->hPicPrev = GetDlgItem(hDlg, IDC_PICPREVIEW);
136 GetClientRect(pImgAdj->hPicPrev,
137 &pImgAdj->ImageRect);
138
139 /* Make a static copy of the main image */
140 pImgAdj->hBitmap = (HBITMAP) CopyImage(pImgAdj->Info->ImageEditors->hBitmap,
141 IMAGE_BITMAP,
142 pImgAdj->ImageRect.right,
143 pImgAdj->ImageRect.bottom,
144 LR_CREATEDIBSECTION);
145 if (!pImgAdj->hBitmap)
146 goto fail;
147
148 /* Make a copy which will be updated */
149 pImgAdj->hPreviewBitmap = (HBITMAP) CopyImage(pImgAdj->Info->ImageEditors->hBitmap,
150 IMAGE_BITMAP,
151 pImgAdj->ImageRect.right,
152 pImgAdj->ImageRect.bottom,
153 LR_CREATEDIBSECTION);
154 if (!pImgAdj->hPreviewBitmap)
155 goto fail;
156
157
158 pImgAdj->RedVal = pImgAdj->BlueVal = pImgAdj->GreenVal = 0;
159
160 /* setup dialog */
161 SendDlgItemMessage(hDlg,
162 IDC_BRI_FULL,
163 BM_SETCHECK,
164 BST_CHECKED,
165 0);
166 SendDlgItemMessage(hDlg,
167 IDC_BRI_TRACKBAR,
168 TBM_SETRANGE,
169 TRUE,
170 (LPARAM)MAKELONG(0, 200));
171 SendDlgItemMessage(hDlg,
172 IDC_BRI_TRACKBAR,
173 TBM_SETPOS,
174 TRUE,
175 (LPARAM)BASECOLOUR);
176 SetDlgItemText(hDlg,
177 IDC_BRI_EDIT,
178 _T("100"));
179
180 return pImgAdj;
181
182 fail:
183 HeapFree(ProcessHeap,
184 0,
185 pImgAdj);
186 return NULL;
187 }
188
189
190 static VOID
191 Bri_OnDrawItem(PIMAGEADJUST pImgAdj,
192 LPARAM lParam)
193 {
194 LPDRAWITEMSTRUCT lpDrawItem;
195 HDC hdcMem;
196
197 lpDrawItem = (LPDRAWITEMSTRUCT)lParam;
198
199 hdcMem = CreateCompatibleDC(lpDrawItem->hDC);
200
201 if(lpDrawItem->CtlID == IDC_PICPREVIEW)
202 {
203 SelectObject(hdcMem,
204 pImgAdj->hPreviewBitmap);
205
206 BitBlt(lpDrawItem->hDC,
207 pImgAdj->ImageRect.left,
208 pImgAdj->ImageRect.top,
209 pImgAdj->ImageRect.right,
210 pImgAdj->ImageRect.bottom,
211 hdcMem,
212 0,
213 0,
214 SRCCOPY);
215
216 DeleteDC(hdcMem);
217 }
218 }
219
220
221 static VOID
222 Bri_OnTrackBar(PIMAGEADJUST pImgAdj,
223 HWND hDlg)
224 {
225 HDC hdcMem;
226 DWORD TrackPos;
227
228 TrackPos = (DWORD)SendDlgItemMessage(hDlg,
229 IDC_BRI_TRACKBAR,
230 TBM_GETPOS,
231 0,
232 0);
233
234 SetDlgItemInt(hDlg,
235 IDC_BRI_EDIT,
236 TrackPos,
237 FALSE);
238
239 if (IsDlgButtonChecked(hDlg, IDC_BRI_FULL) == BST_CHECKED)
240 {
241 pImgAdj->RedVal = pImgAdj->GreenVal = pImgAdj->BlueVal = TrackPos - BASECOLOUR;
242 }
243 else if (IsDlgButtonChecked(hDlg, IDC_BRI_RED) == BST_CHECKED)
244 {
245 pImgAdj->RedVal = TrackPos - BASECOLOUR;
246 }
247 else if (IsDlgButtonChecked(hDlg, IDC_BRI_GREEN) == BST_CHECKED)
248 {
249 pImgAdj->GreenVal = TrackPos - BASECOLOUR;
250 }
251 else if (IsDlgButtonChecked(hDlg, IDC_BRI_BLUE) == BST_CHECKED)
252 {
253 pImgAdj->BlueVal = TrackPos - BASECOLOUR;
254 }
255
256 hdcMem = GetDC(pImgAdj->hPicPrev);
257
258 AdjustBrightness(pImgAdj->hBitmap,
259 pImgAdj->hPreviewBitmap,
260 pImgAdj->hPicPrev,
261 hdcMem,
262 pImgAdj->RedVal,
263 pImgAdj->GreenVal,
264 pImgAdj->BlueVal);
265
266 ReleaseDC(pImgAdj->hPicPrev, hdcMem);
267 }
268
269
270 static BOOL
271 Bri_OnCommand(PIMAGEADJUST pImgAdj,
272 HWND hDlg,
273 UINT uID)
274 {
275 switch (uID)
276 {
277 case IDOK:
278 {
279 HDC hdcMem;
280
281 hdcMem = GetDC(pImgAdj->Info->ImageEditors->hSelf);
282
283 AdjustBrightness(pImgAdj->Info->ImageEditors->hBitmap,
284 pImgAdj->Info->ImageEditors->hBitmap,
285 pImgAdj->Info->ImageEditors->hSelf,
286 hdcMem,
287 pImgAdj->RedVal,
288 pImgAdj->GreenVal,
289 pImgAdj->BlueVal);
290
291 ReleaseDC(pImgAdj->Info->ImageEditors->hSelf,
292 hdcMem);
293
294 EndDialog(hDlg,
295 uID);
296
297 return TRUE;
298 }
299
300 case IDCANCEL:
301 {
302 EndDialog(hDlg,
303 uID);
304 return TRUE;
305 }
306 }
307
308 return FALSE;
309 }
310
311
312 INT_PTR CALLBACK
313 BrightnessProc(HWND hDlg,
314 UINT message,
315 WPARAM wParam,
316 LPARAM lParam)
317 {
318 static PIMAGEADJUST pImgAdj = NULL;
319
320 switch (message)
321 {
322 case WM_INITDIALOG:
323 {
324 pImgAdj = Bri_OnInitDialog(pImgAdj,
325 hDlg,
326 lParam);
327 if (!pImgAdj)
328 {
329 EndDialog(hDlg, -1);
330 return FALSE;
331 }
332
333 return TRUE;
334 }
335
336 case WM_DRAWITEM:
337 {
338 Bri_OnDrawItem(pImgAdj,
339 lParam);
340 return TRUE;
341 }
342
343 case WM_HSCROLL:
344 {
345 if (LOWORD(wParam) == TB_THUMBTRACK ||
346 LOWORD(wParam) == TB_ENDTRACK)
347 {
348 Bri_OnTrackBar(pImgAdj,
349 hDlg);
350 }
351
352 return TRUE;
353 }
354
355 case WM_COMMAND:
356 {
357 return Bri_OnCommand(pImgAdj,
358 hDlg,
359 LOWORD(wParam));
360 }
361
362 case WM_DESTROY:
363 {
364 if (pImgAdj)
365 {
366 if (pImgAdj->hBitmap)
367 DeleteObject(pImgAdj->hBitmap);
368 if (pImgAdj->hPreviewBitmap)
369 DeleteObject(pImgAdj->hPreviewBitmap);
370
371 HeapFree(ProcessHeap,
372 0,
373 pImgAdj);
374 }
375 }
376 }
377
378 return FALSE;
379 }