Gehirn sieht graues Bild farbig :)

Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Neue Antwort erstellen

Markus2

Betreff: Gehirn sieht graues Bild farbig :)

BeitragDi, Sep 26, 2006 19:41
Antworten mit Zitat
Benutzer-Profile anzeigen
Zum testen hatte ich ein Bild von einer Burg mit blau/weißem Himmel
und eine Wiese davor genommen , sollte aber mit anderen Bildern auch gehen .

Von einem Farbbild werden die RGB Werte in HSV umgerechnet
und davon werden zwei neue Pixmaps erstellt die nach einander
angezeigt werden .
Das zweite wird in grau angezeigt und das Gehirn sieht es dann farbig Cool

Code: [AUSKLAPPEN]

'Gehirn sieht graues Bild farbig :)

'M.Rauch 26.09.2006

'für BlitzMax V1.20

Strict
'SetGraphicsDriver GLMax2DDriver()
Graphics 1280,1024,16
'Graphics 800,600,16

MainLoop()
End

Function MainLoop()

 '-----------------------------------

 Local img:TImage=LoadImage("Bild.jpg") '<---
 Local pix:TPixmap=LockImage(img,0,True,True)
 'pix=ResizePixmap(pix,GraphicsWidth(),GraphicsHeight()) '<--- ins Fenster einpassen ohne die Ratio des Bildes zu berücksichtigen

 '-----------------------------------

 Local rgb_r:Int,rgb_g:Int,rgb_b:Int
 Local hsv_h:Int,hsv_s:Int,hsv_v:Int
 Local x:Int,y:Int
 Local c:Int

 '-----------------------------------
 Local pix1:TPixmap=CopyPixmap(pix)
 Local pix2:TPixmap=CopyPixmap(pix)

 For x=0 To pix.width-1
 For y=0 To pix.height-1
  c=ReadPixel(pix,x,y) 'ARGB=
  RGB2HSV(ARGB_Red(c),ARGB_Green(c),ARGB_Blue(c),hsv_h,hsv_s,hsv_v)
  WritePixel pix2,x,y,ARGB_Color(255,hsv_v,hsv_v,hsv_v) 'V graues Bild
  hsv_v=128
  HSV2RGB(hsv_h,hsv_s,hsv_v,rgb_r,rgb_g,rgb_b)
  WritePixel pix1,x,y,ARGB_Color(255,255-rgb_r,255-rgb_g,255-rgb_b) 'H & S
 Next
 Next
 UnlockImage img,0

 '-----------------------------------

 'SavePixmapJPeg pix1,"HS.jpg" 
 'SavePixmapJPeg pix2,"V.jpg" 

 '-----------------------------------

 SetColor 255,255,255
 Repeat
  DrawPixmap pix1,0,0
  DrawText "Jetzt ca. 30 Sek. auf einen Punkt im Bild starren ...",0,0
  Flip
  If Nochmal()=0 Then Exit
  DrawPixmap pix2,0,0
  DrawText "Und nun sollte das graue Bild in Farbe gesehen werden fuer eine kurze Zeit :)",0,0
  DrawText "Augen nicht bewegen !",0,14
  Flip
  If Nochmal()=0 Then Exit
 Forever

 '-----------------------------------

 img=Null
 pix=Null
 pix1=Null
 pix2=Null

End Function

Function Nochmal:Int()

 Repeat
  Delay 250
  If KeyHit(KEY_ESCAPE) Then Return 0
  If KeyHit(KEY_RETURN) Then Return 1
  If KeyHit(KEY_SPACE) Then Return 1
  If MouseHit(1) Then Return 1
  If MouseHit(2) Then Return 0
 Forever
 FlushKeys
 FlushMouse

End Function

Function ARGB_Alpha:Int(ARGB:Int)

 Return Int((ARGB & $FF000000:Int) / $1000000:Int)

End Function

Function ARGB_Red:Int(ARGB:Int)
 
 Return Int((ARGB & $00FF0000:Int) / $10000:Int)

End Function

Function ARGB_Green:Int(ARGB:Int)

 Return Int((ARGB & $0000FF00:Int) / $100:Int)
 
End Function

Function ARGB_Blue:Int(ARGB:Int)

 Return (ARGB & $000000FF:Int)

End Function

Function ARGB_Color:Int(Alpha:Int,Red:Int,Green:Int,Blue:Int)

 Return (Alpha*$1000000:Int+Red*$10000:Int+Green*$100:Int+Blue)

End Function

Function RGB2HSV(r:Int,g:Int,b:Int,h:Int Var,s:Int Var,v:Int Var) 'hsv byref

 'RGB nach HSV

  Local vMin:Int
  Local vMax:Int
  Local Minus:Int
  Local Plus:Int
  Local r_delta:Int
  Local g_delta:Int
  Local b_delta:Int

  vMin = Min3(r, g, b)
  vMax = Max3(r, g, b)
  Minus = vMax - vMin
  Plus = vMax + vMin
  v = ((Plus * 240) + 255) / (2 * 255)
  If vMax = vMin Then
      h = 0
      s = 0
  Else
      If v <= (240 / 2) Then
         s = ((Minus * 240) + 0.5) / Plus
      Else
         s = ((Minus * 240) + 0.5) / (2 * 255 - Plus)
      End If
      r_delta = (((vMax - r) * 40) + 0.5) / Minus
      g_delta = (((vMax - g) * 40) + 0.5) / Minus
      b_delta = (((vMax - b) * 40) + 0.5) / Minus
      Select vMax
         Case r
            h = b_delta - g_delta
         Case g
            h = 80 + r_delta - b_delta
         Case b
            h = 160 + g_delta - r_delta
      End Select
      While h < 0
         h = h + 240
      Wend
      h = h Mod 240
  End If
End Function

Function HSV2RGB(H:Int,S:Int,V:Int,R:Int Var,G:Int Var,B:Int Var) 'rgb byref

 'HSV nach RGB

  Local Magic1:Int
  Local Magic2:Int

  If S = 0 Then
   R = (V * 255) / 240
   G = R
   B = R
  Else
   If V <= 240 / 2 Then
    Magic2 = (V * (240 + S) + 120) / 240
   Else
    Magic2 = V + S - ((V * S) + 120) / 240
   End If
   Magic1 = 2 * V - Magic2
   R = (H2RGB(Magic1, Magic2, H + 80) * 255 + 120) / 240
   G = (H2RGB(Magic1, Magic2, H) * 255 + 120) / 240
   B = (H2RGB(Magic1, Magic2, H - 80) * 255 + 120) / 240
  End If

  'nur 0-255 bei RGB
  If R<0 Then
   R=0
  ElseIf R>255 Then
   R=255
  EndIf
  If G<0 Then
   G=0
  ElseIf G>255 Then
   G=255
  EndIf
  If B<0 Then
   B=0
  ElseIf B>255 Then
   B=255
  EndIf

End Function

Function H2RGB:Int(mag1:Int,mag2:Int,Hue:Int)

 'für HSV2RGB

 Local Ret:Int

 If Hue < 0 Then
  Hue = Hue + 240
 Else
  If Hue > 240 Then Hue = Hue - 240
 End If
 
 If Hue < 40 Then
  Ret = (mag1 + (((mag2 - mag1) * Hue + 20) / 40))
 Else
  If Hue < 120 Then
   Ret = mag2
  Else
   If Hue < 160 Then
    Ret = (mag1 + (((mag2 - mag1) * (160 - Hue) + 20) / 40))
   Else
    Ret = mag1
   End If
  End If
 End If
 Return Ret
 
End Function

Function Min3:Int(a:Int,b:Int,c:Int)
 Return Min(a,Min(b,c))
End Function

Function Max3:Int(a:Int,b:Int,c:Int)
 Return Max(a,Max(b,c))
End Function

hamZta

Administrator

BeitragDi, Sep 26, 2006 20:28
Antworten mit Zitat
Benutzer-Profile anzeigen
Verrückt Smile

Funktioniert sehr gut!

hamZta
Blog.

Neue Antwort erstellen


Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group