Page 1 of 1

Anaglyph image

Posted: Sat Jun 19, 2010 7:18 pm
by kvitaliy
Wikipedia:
Anaglyph images are used to provide a stereoscopic 3D effect, when viewed with 2 color glasses (each lens a chromatically opposite color, usually red and cyan). Images are made up of two color layers, superimposed, but offset with respect to each other to produce a depth effect. Usually the main subject is in the center, while the foreground and background are shifted laterally in opposite directions. The picture contains two differently filtered colored images, one for each eye. When viewed through the "color coded" "anaglyph glasses", they reveal an integrated stereoscopic image.
You can use this code to create your own Anaglyph images using a standard digital camera.

Code: Select all

;You can use this code to create your own Anaglyph  images using a standard digital camera.
;Line up a subject And take a picture of it, move the camera about 2 inches To the right And take another picture. 

;{- Enumerations / DataSections
;{ Windows
Enumeration
  #Window_0
EndEnumeration
;}
;{ Gadgets
Enumeration
  #Image_0
  #Image_2
  #Button_3
  #Button_4
  #Button_5
 
EndEnumeration
;}
Define.l Event, EventWindow, EventGadget, EventType, EventMenu
;}

UseJPEGImageDecoder() 
UseTGAImageDecoder() 
UseTIFFImageDecoder() 
UsePNGImageDecoder() 
UseJPEGImageEncoder()

Procedure OpenWindow_Window_0()
  If OpenWindow(#Window_0, 431, 128, 678, 300, "2D to Anaglyph ", #PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
    ImageGadget(#Image_0,  0, 40, 310, 210, 0, #PB_Image_Border)
    ImageGadget(#Image_2,  350, 40, 310, 210, 0, #PB_Image_Border)
    ButtonGadget(#Button_3, 5, 10, 115, 20, "Left image")
    ButtonGadget(#Button_4, 535, 10, 115, 20, "Right image")
    ButtonGadget(#Button_5, 250, 10, 160, 20, " Create Anaglyph")
    
  EndIf
EndProcedure

OpenWindow_Window_0()

;{- Event loop
Repeat
  Event = WaitWindowEvent()
  Select Event
    ; ///////////////////
    Case #PB_Event_Gadget
      EventGadget = EventGadget()
      EventType = EventType()
    If EventGadget = #Button_3
         FileName$ = OpenFileRequester("Open left image", "", "All supported formats|*.bmp;*.jpg;*.tif;*.tga;*.png", 0) 
           If FileName$ 
               CurrentDir$ = GetPathPart(FileName$) 
               LoadImage(0, FileName$) 
               wid = ImageWidth(0)
               hei = ImageHeight(0)
               Mass1=wid*hei
               Dim pixels1(Mass1)
             
               CopyImage(0, 1)
               ResizeImage(1, 310, 210)
               SetGadgetState(#Image_0, ImageID(1)) 
               
           EndIf
           
           
     ElseIf EventGadget = #Button_4
           FileName$ = OpenFileRequester("Open right image", "", "All supported formats|*.bmp;*.jpg;*.tif;*.tga;*.png", 0) 
           If FileName$ 
               CurrentDir$ = GetPathPart(FileName$) 
               LoadImage(2, FileName$) 
               wid1 = ImageWidth(2)
               hei1 = ImageHeight(2)
               CopyImage(2, 3)
               ResizeImage(3, 310, 210)
               SetGadgetState(#Image_2, ImageID(3))
              
           EndIf
     ElseIf EventGadget = #Button_5
            p1=0
         ;*********** Left image************
          If  IsImage(0) And  IsImage(2)
             If wid > wid1: wid=wid1:EndIf
             If hei > hei1: hei=hei1:EndIf
           If StartDrawing(ImageOutput(0))
           
               For h= 0 To hei - 1 
                For w= 0 To wid - 1 
                  
                  redPoint=Red(Point(w, h))
                  Plot(w,h,RGB(redPoint,0,0))
                  pixels1(p1)=redPoint
                  p1+1
                  
                Next
             
              Next
              Debug p1
           StopDrawing()
          EndIf 
               CopyImage(0, 1)
               ResizeImage(1, 310, 210)
               SetGadgetState(#Image_0, ImageID(1)) ;
     ;**********Right image ************
     
           If StartDrawing(ImageOutput(2))
             For h= 0 To hei1 - 1 
                For w= 0 To wid1 - 1 
                 
                  GrPoint=Green(Point(w, h))
                  BlPoint=Blue(Point(w, h))
                  Plot(w,h,RGB(0,GrPoint,BlPoint))
                
                Next
             
              Next
              
           StopDrawing()
          EndIf 
               CopyImage(2, 3)
               ResizeImage(3, 310, 210)
               SetGadgetState(#Image_2, ImageID(3))
    
    ;*********** Create Anaglyph  ***********
    p1=0
    
    
    If StartDrawing(ImageOutput(2))
      DrawingMode(#PB_2DDrawing_AlphaBlend)
             For h= 0 To hei - 1 
                For w= 0 To wid - 1 
                   Plot(w,h,RGBA(pixels1(p1),0,0,125))
                  p1+1
                Next
            Next
           StopDrawing()
          EndIf 
               CopyImage(2, 3)
               ResizeImage(3, 310, 210)
               SetGadgetState(#Image_2, ImageID(3))
              If  SaveImage(2,GetTemporaryDirectory()+"Anagliph.jpg",#PB_ImagePlugin_JPEG,8)
                 MessageRequester("Create Anaglyph","Ok!")
                 RunProgram(GetTemporaryDirectory()+"Anaglyph.jpg")
               EndIf
   
      EndIf
   
    Else
      MessageRequester("Error!"," No image!")
  EndIf
      
    ; ////////////////////////
    Case #PB_Event_CloseWindow
      EventWindow = EventWindow()
      If EventWindow = #Window_0
        CloseWindow(#Window_0)
        Break
      EndIf
  EndSelect
ForEver
;
;}