Page 1 of 1

Image reduce colors

Posted: Sun Dec 25, 2011 5:26 pm
by jesperbrannmark
Hi all.
There must be a easier way than this.
This is what I am trying to achieve:
* Take a image from webcam
* Reduce image to 2 or 4 bitplanes
(next step is to take out everything within a span to find a specific marker, like a red circle or something like that)

Right now I have to save as PNG with 4 bitplanes and then load the image again... that is slooooow..

Code: Select all

OpenWindow(0,0,0,640,480,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
AddWindowTimer(0,0, 50)
UsePNGImageDecoder()
UsePNGImageEncoder()
Repeat
  Event = WindowEvent()
  If Event = #PB_Event_Timer
    MX_Release(Image)
    image = MXSnapshot_TakeSnapshot(640)
    tempimage=CreateImage(#PB_Any,640,480)
    StartDrawing(ImageOutput(tempimage))
    DrawImage(image,0,0,320,240)
    StopDrawing()
    SaveImage(tempimage,GetTemporaryDirectory()+"temp.png",#PB_ImagePlugin_PNG,0,2)
    FreeImage(tempimage)
    StartDrawing(WindowOutput(0))
    DrawAlphaImage(ImageID(LoadImage(#PB_Any,GetTemporaryDirectory()+"temp.png")),0,0)
    StopDrawing()
  EndIf
Until Event=#PB_Event_CloseWindow
MXSnapshot_StopSession()

Re: Image reduce colors

Posted: Sun Dec 25, 2011 6:34 pm
by wilbert
You can try manipulating the drawing buffer

Code: Select all

Procedure Reduce4bpp(buffer, numPixels); reduce to 4bpp (4096 colors)
  EnableASM
  MOV ecx, numPixels
  MOV edx, buffer
  !push ebx
  !reduce4bpp_loop:
  !mov eax, [edx]
  !and eax, 0xf0f0f0f0
  !mov ebx, eax
  !shr ebx, 4
  !or eax, ebx
  !mov [edx], eax
  !add edx, 4
  !dec ecx
  !jnz reduce4bpp_loop
  !pop ebx
  DisableASM
EndProcedure

Procedure Reduce2bpp(buffer, numPixels); reduce to 2bpp (64 colors)
  EnableASM
  MOV ecx, numPixels
  MOV edx, buffer
  !push ebx
  !mov ebx, edx
  !reduce2bpp_loop:
  !mov eax, [ebx]
  !shr eax, 6
  !and eax, 0x03030303
  !imul eax, 0x55
  !mov [ebx], eax
  !add ebx, 4
  !dec ecx
  !jnz reduce2bpp_loop
  !pop ebx
  DisableASM
EndProcedure

OpenWindow(0,0,0,640,480,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
AddWindowTimer(0,0, 50)
UsePNGImageDecoder()
UsePNGImageEncoder()
Repeat
  Event = WindowEvent()
  If Event = #PB_Event_Timer
    MX_Release(Image)
    image = MXSnapshot_TakeSnapshot(640)
    tempimage=CreateImage(#PB_Any,640,480)
    StartDrawing(ImageOutput(tempimage))
    DrawImage(image,0,0,320,240)
    Reduce2bpp(DrawingBuffer(), 640 * 480)
    StopDrawing()
    StartDrawing(WindowOutput(0))
    DrawAlphaImage(ImageID(tempimage),0,0)
    StopDrawing()
    FreeImage(tempimage)
  EndIf
Until Event=#PB_Event_CloseWindow
MXSnapshot_StopSession()