1 /****************************************************************************
4 Copyright (c) 2003 Jerome Jacovella-St-Louis
7 pictureEdit.ec - Picture Editor Control
8 ****************************************************************************/
15 #define ID_IMAGE_MODE_COLORTABLE 9
16 #define ID_IMAGE_MODE_INDEXED 10
17 #define ID_IMAGE_MODE_RGB 11
19 static Array<FileFilter> filters
21 { $"Image Files (*.jpg, *.jpeg, *.bmp, *.pcx, *.png, *.gif)", "jpg, jpeg, bmp, pcx, png, gif" },
22 { $"All files", null }
25 static Array<FileType> types
27 { $"Based on extension", null, never },
28 { $"JPG Image", "jpg", always },
29 { $"BMP Image", "bmp", always },
30 { $"PCX Image", "pcx", always },
31 { $"PNG Image", "png", always },
32 { $"GIF Image", "gif", always }
35 FileDialog pictureEditFileDialog { filters = filters.array, sizeFilters = filters.count * sizeof(FileFilter), types = types.array, sizeTypes = types.count * sizeof(FileType) };
37 class PictureEdit : Window
41 isActiveClient = true;
48 char fileName[MAX_LOCATION];
51 //saveDialog = pictureEditFileDialog;
53 Menu fileMenu { menu, $"File", f }
54 MenuItem { fileMenu, $"Save", s, ctrlS, NotifySelect = MenuFileSave };
55 MenuItem { fileMenu, $"Save As...", a, NotifySelect = MenuFileSaveAs };
56 Menu imageMenu { menu, $"Image", i };
57 Menu modeMenu { imageMenu, $"Mode", m };
58 MenuItem imageModeIndexedItem
60 modeMenu, $"Indexed Color...", i, isRadio = true;
61 bool NotifySelect(MenuItem selection, Modifiers mods)
65 ColorAlpha * palette = bitmap.Quantize(0, 255);
67 eBitmap_Convert(null, bitmap, PixelFormat8, palette);
68 bitmap.allocatePalette = true;
71 imageModeColorTableItem.disabled = false;
73 modifiedDocument = true;
78 MenuItem imageModeRGBItem
80 modeMenu, $"RGB Color", r, isRadio = true;
81 bool NotifySelect(MenuItem selection, Modifiers mods)
84 bitmap.Convert(null, pixelFormat888, null);
85 imageModeColorTableItem.disabled = true;
87 modifiedDocument = true;
91 MenuDivider { modeMenu };
92 MenuItem imageModeColorTableItem
94 modeMenu, $"Color Table", r;
95 bool NotifySelect(MenuItem selection, Modifiers mods)
99 PictureEditColorTable colorTable { master = this };
107 MenuDivider { imageMenu };
108 MenuItem adjustHSVItem
110 imageMenu, $"Adjust Hue, Saturation, Value", h;
111 bool NotifySelect(MenuItem selection, Modifiers mods)
115 AdjustHSV adjustHSV { master = this };
124 property char * bitmapFile
130 strcpy(fileName, value);
135 if(bitmap.Load(fileName, null, null))
137 if(bitmap.pixelFormat == pixelFormatRGBA)
138 bitmap.Convert(null, pixelFormat888, null);
140 //if(!eWindow_GetStartWidth(window) || !eWindow_GetStartHeight(window))
142 Size size = initSize; // what's the use of retrieving initSize
143 size.w = bitmap.width;
144 size.h = bitmap.height;
148 Move(eWindow_GetStartX(window), eWindow_GetStartY(window),
149 (!eWindow_GetStartWidth(window)) ? (A_CLIENT|bitmap.width) : eWindow_GetStartWidth(window),
150 (!eWindow_GetStartHeight(window)) ? (A_CLIENT|bitmap.height) : eWindow_GetStartHeight(window));
154 Move(eWindow_GetStartX(window), eWindow_GetStartY(window),
155 (!) ? (A_CLIENT|bitmap.width) : eWindow_GetStartWidth(window),
156 (!eWindow_GetStartHeight(window)) ? (A_CLIENT|bitmap.height) : eWindow_GetStartHeight(window));
159 scrollArea = Size {bitmap.width, bitmap.height };
167 switch(bitmap.pixelFormat)
170 imageModeIndexedItem.checked = true;
173 imageModeRGBItem.checked = true;
174 imageModeColorTableItem.disabled = true;
181 void OnRedraw(Surface surface)
185 int w = (int)(bitmap.width * zoomFactor);
186 int h = (int)(bitmap.height * zoomFactor);
187 if(w == bitmap.width && h == bitmap.height)
190 Max(0, (clientSize.w - w) / 2), Max(0, (clientSize.h - h) / 2),
191 scroll.x, scroll.y, w, h);
195 surface.Filter(bitmap,
196 Max(0, (clientSize.w - w) / 2), Max(0, (clientSize.h - h) / 2),
197 (int)(scroll.x / zoomFactor), (int)(scroll.y / zoomFactor), w, h,
198 bitmap.width, bitmap.height);
203 void OnScroll(ScrollBarAction action, int position, Key key)
208 bool OnKeyHit(Key key, unichar ch)
214 if(bitmap && zoomFactor < 25)
216 float x = 0.5f, y = 0.5f;
217 if(bitmap.width * zoomFactor > clientSize.w)
218 x = scroll.x / (bitmap.width * zoomFactor - clientSize.w);
219 if(bitmap.height * zoomFactor > clientSize.h)
220 y = scroll.y / (bitmap.height * zoomFactor - clientSize.h);
223 scrollArea = Size { bitmap.width * zoomFactor, bitmap.height * zoomFactor };
226 (int)(Max(0, bitmap.width * zoomFactor - clientSize.w) * x),
227 (int)(Max(0, bitmap.height * zoomFactor - clientSize.h) * y) };
234 if(bitmap && zoomFactor > 0.05)
236 float x = 0.5f, y = 0.5f;
237 if(bitmap.width * zoomFactor > clientSize.w)
238 x = scroll.x / (bitmap.width * zoomFactor - clientSize.w);
239 if(bitmap.height * zoomFactor > clientSize.w)
240 y = scroll.y / (bitmap.height * zoomFactor - clientSize.h);
243 scrollArea = Size { bitmap.width * zoomFactor, bitmap.height * zoomFactor };
246 (int)(Max(0, bitmap.width * zoomFactor - clientSize.w) * x),
247 (int)(Max(0, bitmap.height * zoomFactor - clientSize.h) * y) };
256 bool OnSaveFile(char * fileName)
261 if(bitmap.Save(fileName,
262 ((FileType *)pictureEditFileDialog.types)[pictureEditFileDialog.fileType].typeExtension, (void *) bool::true))
264 modifiedDocument = false;
279 class PictureEditColorTable : Window
282 text = $"Color Table";
283 background = formColor;
284 minClientSize = Size { 400, 400 };
288 parent = this, hotKey = escape, size = { 80 }, text = $"Close";
289 anchor = Anchor { right = 10, bottom = 10 };
290 NotifyClicked = ButtonCloseDialog;
293 void OnRedraw(Surface surface)
295 PictureEdit picture = (PictureEdit)master;
296 Bitmap bitmap = picture.bitmap;
298 for(c = 0; c < 256; c++)
300 int x = (c % 16) * 16;
301 int y = (c / 16) * 16;
302 surface.SetBackground(bitmap.palette[c]);
303 surface.Area(10 + x, 30 + y, 10 + x + 15, 30 + y + 15);
308 class ColorBox : Window
311 borderStyle = deepContour;
315 class AdjustHSV : Window
319 background = formColor;
322 replace = { 248, 100, 71 }; //Color { 26, 0, 183 };
323 target = { 207, 61, 71 };
328 this, text = $"Go", position = { 296, 104 }, isDefault = true;
330 bool NotifyClicked(Button button, int x, int y, Modifiers mods)
332 PictureEdit picture = (PictureEdit)master;
333 Bitmap bitmap = picture.bitmap;
334 double h = 1.0f, s = 0.80f, v = 1.24f;
339 h = target.h - replace.h;
340 s = target.s / replace.s;
341 v = target.v / replace.v;
343 for(y = 0; y<bitmap.height; y++)
345 for(x = 0; x<bitmap.width; x++)
347 ColorAlpha color = ((ColorAlpha *)bitmap.picture)[y * bitmap.stride + x];
348 ColorHSV hsv = color;
349 double diffH = Abs(hsv.h/360 - replace.h/360);
350 double diffS = Abs(hsv.s - replace.s)/100.0;
351 double diffV = Abs(hsv.v - replace.v)/100.0;
352 if(diffH <= tolH && diffS <= tolS && diffV <= tolV)
358 ((ColorAlpha *)bitmap.picture)[y * bitmap.stride + x] = color;
362 picture.Update(null);
363 picture.modifiedDocument = true;
369 EditBox hBox { this, text = "H", size = { 78, 19 }, position = { 176, 80 } };
370 EditBox sBox { this, text = "S", size = { 78, 19 }, position = { 176, 120 } };
371 EditBox vBox { this, text = "V", size = { 78, 19 }, position = { 176, 160 } };
372 Label label1 { this, size = { 7, 13 }, position = { 152, 80 }, labeledWindow = hBox };
373 Label label2 { this, size = { 6, 13 }, position = { 152, 120 }, labeledWindow = sBox };
374 Label label3 { this, size = { 6, 13 }, position = { 152, 168 }, labeledWindow = vBox };
375 ColorBox original { this, position = { 10, 10 }, background = replace };
376 ColorBox result { this, position = { 10, 100 }, background = target };