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)
63 ColorAlpha * palette = bitmap.Quantize(0, 255);
65 eBitmap_Convert(null, bitmap, PixelFormat8, palette);
66 bitmap.allocatePalette = true;
69 imageModeColorTableItem.disabled = false;
71 modifiedDocument = true;
75 MenuItem imageModeRGBItem
77 modeMenu, "RGB Color", r, isRadio = true;
78 bool NotifySelect(MenuItem selection, Modifiers mods)
80 bitmap.Convert(null, pixelFormat888, null);
81 imageModeColorTableItem.disabled = true;
83 modifiedDocument = true;
87 MenuDivider { modeMenu };
88 MenuItem imageModeColorTableItem
90 modeMenu, "Color Table", r;
91 bool NotifySelect(MenuItem selection, Modifiers mods)
93 PictureEditColorTable colorTable { master = this };
100 MenuDivider { imageMenu };
101 MenuItem adjustHSVItem
103 imageMenu, "Adjust Hue, Saturation, Value", h;
104 bool NotifySelect(MenuItem selection, Modifiers mods)
106 AdjustHSV adjustHSV { master = this };
114 property char * bitmapFile
120 strcpy(fileName, value);
125 if(bitmap.Load(fileName, null, null))
127 if(bitmap.pixelFormat == pixelFormatRGBA)
128 bitmap.Convert(null, pixelFormat888, null);
130 //if(!eWindow_GetStartWidth(window) || !eWindow_GetStartHeight(window))
132 Size size = initSize; // what's the use of retrieving initSize
133 size.w = bitmap.width;
134 size.h = bitmap.height;
138 Move(eWindow_GetStartX(window), eWindow_GetStartY(window),
139 (!eWindow_GetStartWidth(window)) ? (A_CLIENT|bitmap.width) : eWindow_GetStartWidth(window),
140 (!eWindow_GetStartHeight(window)) ? (A_CLIENT|bitmap.height) : eWindow_GetStartHeight(window));
144 Move(eWindow_GetStartX(window), eWindow_GetStartY(window),
145 (!) ? (A_CLIENT|bitmap.width) : eWindow_GetStartWidth(window),
146 (!eWindow_GetStartHeight(window)) ? (A_CLIENT|bitmap.height) : eWindow_GetStartHeight(window));
149 scrollArea = Size {bitmap.width, bitmap.height };
157 switch(bitmap.pixelFormat)
160 imageModeIndexedItem.checked = true;
163 imageModeRGBItem.checked = true;
164 imageModeColorTableItem.disabled = true;
171 void OnRedraw(Surface surface)
175 int w = (int)(bitmap.width * zoomFactor);
176 int h = (int)(bitmap.height * zoomFactor);
177 if(w == bitmap.width && h == bitmap.height)
180 Max(0, (clientSize.w - w) / 2), Max(0, (clientSize.h - h) / 2),
181 scroll.x, scroll.y, w, h);
185 surface.Filter(bitmap,
186 Max(0, (clientSize.w - w) / 2), Max(0, (clientSize.h - h) / 2),
187 (int)(scroll.x / zoomFactor), (int)(scroll.y / zoomFactor), w, h,
188 bitmap.width, bitmap.height);
193 void OnScroll(ScrollBarAction action, int position, Key key)
198 bool OnKeyHit(Key key, unichar ch)
206 float x = 0.5f, y = 0.5f;
207 if(bitmap.width * zoomFactor > clientSize.w)
208 x = scroll.x / (bitmap.width * zoomFactor - clientSize.w);
209 if(bitmap.height * zoomFactor > clientSize.h)
210 y = scroll.y / (bitmap.height * zoomFactor - clientSize.h);
213 scrollArea = Size { bitmap.width * zoomFactor, bitmap.height * zoomFactor };
216 (int)(Max(0, bitmap.width * zoomFactor - clientSize.w) * x),
217 (int)(Max(0, bitmap.height * zoomFactor - clientSize.h) * y) };
224 if(zoomFactor > 0.05)
226 float x = 0.5f, y = 0.5f;
227 if(bitmap.width * zoomFactor > clientSize.w)
228 x = scroll.x / (bitmap.width * zoomFactor - clientSize.w);
229 if(bitmap.height * zoomFactor > clientSize.w)
230 y = scroll.y / (bitmap.height * zoomFactor - clientSize.h);
233 scrollArea = Size { bitmap.width * zoomFactor, bitmap.height * zoomFactor };
236 (int)(Max(0, bitmap.width * zoomFactor - clientSize.w) * x),
237 (int)(Max(0, bitmap.height * zoomFactor - clientSize.h) * y) };
246 bool OnSaveFile(char * fileName)
251 if(bitmap.Save(fileName,
252 ((FileType *)pictureEditFileDialog.types)[pictureEditFileDialog.fileType].typeExtension, (void *) bool::true))
254 modifiedDocument = false;
269 class PictureEditColorTable : Window
272 text = "Color Table";
273 background = activeBorder;
274 minClientSize = Size { 400, 400 };
278 parent = this, hotKey = escape, size = { 80 }, text = "Close";
279 anchor = Anchor { right = 10, bottom = 10 };
280 NotifyClicked = ButtonCloseDialog;
283 void OnRedraw(Surface surface)
285 PictureEdit picture = (PictureEdit)master;
286 Bitmap bitmap = picture.bitmap;
288 for(c = 0; c < 256; c++)
290 int x = (c % 16) * 16;
291 int y = (c / 16) * 16;
292 surface.SetBackground(bitmap.palette[c]);
293 surface.Area(10 + x, 30 + y, 10 + x + 15, 30 + y + 15);
298 class ColorBox : Window
301 borderStyle = deepContour;
305 class AdjustHSV : Window
309 background = activeBorder;
312 replace = { 248, 100, 71 }; //Color { 26, 0, 183 };
313 target = { 207, 61, 71 };
318 this, text = "Go", position = { 296, 104 }, isDefault = true;
320 bool NotifyClicked(Button button, int x, int y, Modifiers mods)
322 PictureEdit picture = (PictureEdit)master;
323 Bitmap bitmap = picture.bitmap;
324 double h = 1.0f, s = 0.80f, v = 1.24f;
329 h = target.h - replace.h;
330 s = target.s / replace.s;
331 v = target.v / replace.v;
333 for(y = 0; y<bitmap.height; y++)
335 for(x = 0; x<bitmap.width; x++)
337 ColorAlpha color = ((ColorAlpha *)bitmap.picture)[y * bitmap.stride + x];
338 ColorHSV hsv = color;
339 double diffH = Abs(hsv.h/360 - replace.h/360);
340 double diffS = Abs(hsv.s - replace.s)/100.0;
341 double diffV = Abs(hsv.v - replace.v)/100.0;
342 if(diffH <= tolH && diffS <= tolS && diffV <= tolV)
348 ((ColorAlpha *)bitmap.picture)[y * bitmap.stride + x] = color;
352 picture.Update(null);
353 picture.modifiedDocument = true;
359 EditBox hBox { this, text = "H", size = { 78, 19 }, position = { 176, 80 } };
360 EditBox sBox { this, text = "S", size = { 78, 19 }, position = { 176, 120 } };
361 EditBox vBox { this, text = "V", size = { 78, 19 }, position = { 176, 160 } };
362 Label label1 { this, size = { 7, 13 }, position = { 152, 80 }, labeledWindow = hBox };
363 Label label2 { this, size = { 6, 13 }, position = { 152, 120 }, labeledWindow = sBox };
364 Label label3 { this, size = { 6, 13 }, position = { 152, 168 }, labeledWindow = vBox };
365 ColorBox original { this, position = { 10, 10 }, background = replace };
366 ColorBox result { this, position = { 10, 100 }, background = target };