Programatically determine if an image is colour or grayscale

Windows specific forum
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Programatically determine if an image is colour or grays

Post by Saki »

Welcome back Keya

My two cent - Simple and very fast.

The way to do this is to abuse the FilterCallback to analyze an image.
FilterCallback is much faster than reading the lines manually, no matter how you do it.
The method of direct access without Point() has practically never shown a speed advantage for me, which is why I stopped using it a long time ago.
Bit based operations are very fast.
Then it is only necessary to avoid as sample "IF, AND" queries, which naturally act as a little handbrake.

Code: Select all

; Is a image grey scaled - By Saki
Global result
UsePNGImageDecoder() : UseTIFFImageDecoder() : UseJPEGImageDecoder()
Procedure CallBack_GreyCheck(x, y, source_color, destination_color)
  result+Bool(Red(source_color)!(Red(source_color)!Green(source_color)!Blue(source_color)))
  ProcedureReturn destination_color
EndProcedure
file$=OpenFileRequester("Select a image", "", "", 0)
window_ID=OpenWindow(#PB_Any, 0, 0, 800, 800, "", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
canvas_ID=CanvasGadget(#PB_Any, 0, 0, WindowWidth(window_ID), WindowHeight(window_ID))
image_ID=LoadImage(#PB_Any, file$)
ms=ElapsedMilliseconds()
If IsImage(image_ID)
  StartDrawing(ImageOutput(image_ID))
  DrawingMode(#PB_2DDrawing_CustomFilter)
  CustomFilterCallback(@CallBack_GreyCheck())
  DrawAlphaImage(ImageID(image_ID), 0, 0)
  StopDrawing()
  ms=ElapsedMilliseconds()-ms
Else
  DrawText(10*DesktopResolutionX(), WindowHeight(window_ID)*DesktopResolutionY()-25, "Can not use this image")
EndIf
StartDrawing(CanvasOutput(canvas_ID))
DrawText(10*DesktopResolutionX(), WindowHeight(window_ID)*DesktopResolutionY()-25, "colored points "+result+" // Elapsedmilliseconds : "+ Str(ms))
ResizeImage(image_ID, WindowWidth(window_ID)*DesktopResolutionX(), WindowHeight(window_ID)*DesktopResolutionY()-40)
DrawAlphaImage(ImageID(image_ID), 0, 0)
StopDrawing()
Repeat
Until WaitWindowEvent()=#PB_Event_CloseWindow
Last edited by Saki on Mon Jan 25, 2021 5:47 pm, edited 2 times in total.
地球上の平和
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: Programatically determine if an image is colour or grays

Post by Keya »

Saki wrote:FilterCallback is much faster than reading the lines manually, no matter how you do it.
Are you sure? Your method requires calling a (callback) function with EVERY SINGLE PIXEL.
That is, every single pixel must go through a CALL to a PROLOUGE with EPILOGUE function, and return.
Mine doesnt. And im not sure how you can say it's faster than reading lines manually, when your code also requires reading lines manually.
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Programatically determine if an image is colour or grays

Post by IdeasVacuum »

You can't just assume that if the image palette only has 2 colours that it's B&W - it might be red & blue for example.
Hi Keya - Not if the image is identified as Grayscale first :mrgreen:
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: Programatically determine if an image is colour or grays

Post by Keya »

IdeasVacuum wrote:
You can't just assume that if the image palette only has 2 colours that it's B&W - it might be red & blue for example.
Hi Keya - Not if the image is identified as Grayscale first :mrgreen:
If you're lucky enough that you have a file format that tells you in advance yes :)
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Programatically determine if an image is colour or grays

Post by IdeasVacuum »

....It's not down to luck - if you read the Topic from the start you see that we first determine if the image is colour or grayscale. If it is grayscale and has only two colours, we check to see if the colours are black and white.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply