// This example is provided 'AS IT' // I hope that it will be useful, but WITHOUT ANY WARRANTY. // // By Iri - june 2015. // Web : http://www.scolring.org /* Dependancies to run this example : _load "lib/std/stdlib.pkg" _load "lib/2d/colors.pkg" */ /* To run, create a launcher with these following lines : _load "lib/std/stdlib.pkg" _load "lib/2d/colors.pkg" _load "lib/examples/2d/grayscale.pkg_" main */ /*! \example 2d/grayscale.pkg_ toto */ /* * This example takes an image and apply different greyscale conversion * algorithms. * When the window is displayed, choose an algorithm and click on "Apply". * The conversion can take some seconds. * A "Save" button allow to save the result in your files system. * A "Reset" button allow to display the original image. * For each "Apply", the conversion is always done from the original bitmap. * For the last algorithm, the shaders number can be set in the bottom text field. */ typeof win = ObjWin;; typeof bmp = ObjBitmap;; typeof bmpSrc = ObjBitmap;; typeof combo = ObjBox;; typeof shader = ObjText;; proto makeGray = fun [I I I I I I] I;; proto copyBmp = fun [] I;; // Release the resources and exit fun destroy (o, u)= if !std_objIsNil bmp then _DSbitmap bmp; _DSbitmap bmpSrc; _closemachine;; // Display the bitmap in the main window fun paint (o, u)= _BLTbitmap win bmp 5 5; 0;; // Enable the text field when the "custom" algorithm is choosen fun choice (o, u, iChoice, szChoice)= if iChoice == 10 then _ENtext shader 1 else _ENtext shader 0; 0;; // Apply callback : call the selected algorithm then repaint the window to show the result fun apply (o, u)= copyBmp; let _GETcombo combo -> [iAlgo _] in let _GETbitmapSize bmp -> [w h] in makeGray iAlgo w h atoi _GETtext shader 0 0; _PAINTwindow win; 0;; fun savebmp (o, u, w)= if !std_objIsNil w then _SAVEjpeg bmp w 90; 0;; // Save callback fun save (o, u)= _DLGrflsave _DLGSaveFile _channel win "" "" "jpeg\0*.jpg\0\0" @savebmp 0; 0;; // Reset callback : show again the original bitmap fun reset (o, u)= copyBmp; _PAINTwindow win; 0;; // Main function : load resources, create gui and set callback fun main ()= _showconsole; set win = _CRwindow _channel nil 100 50 800 810 WN_NORMAL "Grayscale"; set bmpSrc = _LDjpeg _channel _checkpack "lib/examples/2d/rscs/fleurs.jpg"; set bmp = _CRbitmap _channel 600 800; copyBmp; _CRtext _channel win 610 5 180 20 0 "Choose an algorithm :"; set combo = _CRcombo _channel win 610 30 180 220 CB_NOEDIT|CB_DOWN "Average"; _ADDcombo combo 0 "Average"; _ADDcombo combo 1 "Human eye A"; _ADDcombo combo 2 "Human eye B"; _ADDcombo combo 3 "Human eye C"; _ADDcombo combo 4 "Desaturation"; _ADDcombo combo 5 "Decomposition Maximum"; _ADDcombo combo 6 "Decomposition Minimum"; _ADDcombo combo 7 "Single gray channel RED"; _ADDcombo combo 8 "Single gray channel GREEN"; _ADDcombo combo 9 "Single gray channel BLUE"; _ADDcombo combo 10 "Custom shaders"; _SELcombo combo 0; _CRtext _channel win 610 675 180 20 0 "Set the shaders number :"; set shader = _CReditLine _channel win 610 700 180 20 ET_DOWN|ET_NUMBER "16"; _ENtext shader 0; _CBbutton _CRbutton _channel win 610 725 180 20 0 "Apply" @apply 0; _CBbutton _CRbutton _channel win 610 750 180 20 0 "Save" @save 0; _CBbutton _CRbutton _channel win 610 775 180 20 0 "Reset" @reset 0; _CBwinPaint win @paint 0; _CBwinDestroy win @destroy 0; _CBcombo combo @choice 0; _PAINTwindow win; 0;; // Apply the selected algorithm for each pixel (so, the entire bitmap) fun makeGray (iAlgo, w, h, iShader, line, col)= if (line >= h) || ((col >= w) && (h <= line+1)) then 0 else ( if col >= w then ( set col = 0; ++line ); let _GETpixel24 bmp col line -> c in let l2d_colors2RGB c -> [r g b] in let if iAlgo == 0 then l2d_colorsGrayAverage r g b else if iAlgo == 1 then l2d_colorsGrayHumanA r g b else if iAlgo == 2 then l2d_colorsGrayHumanB r g b else if iAlgo == 3 then l2d_colorsGrayHumanC r g b else if iAlgo == 4 then l2d_colorsGrayDesaturation r g b else if iAlgo == 5 then l2d_colorsGrayDecomposition r g b 1 else if iAlgo == 6 then l2d_colorsGrayDecomposition r g b 0 else if iAlgo == 7 then l2d_colorsGraySingleChannel r g b 0 else if iAlgo == 8 then l2d_colorsGraySingleChannel r g b 1 else if iAlgo == 9 then l2d_colorsGraySingleChannel r g b 2 else if iAlgo == 10 then l2d_colorsGrayCustom r g b iShader else [0 0 0] -> [a a a] in _PUTpixel24 bmp col line l2d_colorsRGB2C a a a; makeGray iAlgo w h iShader line col+1 );; // Copy the original bitmap in the working bitmap fun copyBmp ()= _CPbitmap24 bmp 0 0 bmpSrc 0 0 600 800 nil; 0;;