CGA

Share your advanced PureBasic knowledge/code with the community.
User avatar
J. Baker
Addict
Addict
Posts: 2185
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

CGA

Post by J. Baker »

Code: Select all

Procedure CGA(color)
  
  DivideR.i = Red(color)
  DivideG.i = Green(color)
  DivideB.i = Blue(color)
  
      If DivideR.i > 0 And DivideR.i < 128 And DivideG.i > 0 And DivideG.i < 128 And DivideB.i > 0 And DivideB.i < 128
          DivideR.i = DivideR.i * 1.33
          DivideG.i = DivideG.i * 1.33
          DivideB.i = DivideB.i * 1.33
      EndIf
      
      If DivideR.i >= 170
        cgaR = 255
      ElseIf DivideR.i > 127 And DivideB.i < 170 Or DivideG.i < 170
        cgaR = 170
      Else
        If DivideG.i >= 170 Or DivideB.i >= 170
          cgaR = 85
        Else
          cgaR = 0
        EndIf
      EndIf
      
      If DivideG.i >= 170
        cgaG = 255
      ElseIf DivideG.i > 127 And DivideR.i < 170 Or DivideB.i < 170
       cgaG = 170
      Else
        If DivideR.i >= 170 Or DivideB.i >= 170
          cgaG = 85
        Else
          cgaG = 0
        EndIf
      EndIf
      
      If DivideB.i >= 170
        cgaB = 255
      ElseIf DivideB.i > 127 And DivideR.i < 170 Or DivideG.i < 170
       cgaB = 170
      Else
        If DivideR.i >= 170 Or DivideG.i >= 170
          cgaB = 85
        Else
          cgaB = 0
        EndIf
      EndIf     
      
  If cgaR = 170 And cgaG = 170 And cgaB = 0
     cgaG = 85
  EndIf
  
  ProcedureReturn RGB(cgaR, cgaG, cgaB)
  
EndProcedure

UseJPEGImageDecoder()
UsePNGImageDecoder()
UsePNGImageEncoder()

FileName$ = GetHomeDirectory() + "Desktop/image.png"

LoadImage(0, FileName$)

;ResizeImage(0, 320, 200)

  StartDrawing(ImageOutput(0))
   DrawingMode(#PB_2DDrawing_Default)
    For x = 0 To ImageWidth(0) - 1
      For y = 0 To ImageHeight(0) - 1
        c = Point(x, y)
          Plot(x, y , CGA(c))
      Next y
    Next x

SaveImage(0, FileName$ + "_cga.png", #PB_ImagePlugin_PNG, 0, 24)

FreeImage(0)

End
Last edited by J. Baker on Mon Feb 25, 2013 10:14 am, edited 6 times in total.
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
dige
Addict
Addict
Posts: 1410
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: CGA

Post by dige »

What is CGA? Looks like a posterize effect...
"Daddy, I'll run faster, then it is not so far..."
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: CGA

Post by wilbert »

dige wrote:What is CGA? Looks like a posterize effect...
It's a very old type of graphics card that had only 16 colors.
http://en.wikipedia.org/wiki/Color_Graphics_Adapter
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 539
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: CGA

Post by BasicallyPure »

I like it.
I see that it can be made to work using only integers with almost identical results.

Code: Select all

Procedure CGA(color)
   
   DivideR.i = Red(color)
   DivideG.i = Green(color)
   DivideB.i = Blue(color)
   
   If DivideR > 170 Or DivideG > 170 Or DivideB > 170
      cgaR = DivideR / 128 * 255
      cgaG = DivideG / 128 * 255
      cgaB = DivideB / 128 * 255
      If cgaR = 0
         cgaR = 85
      EndIf
      If cgaG = 0
         cgaG = 85
      EndIf
      If cgaB = 0
         cgaB = 85
      EndIf
   Else
      If DivideR > 85 And DivideR <= 170
         cgaR = 170
      Else
         cgaR = 0
      EndIf
      If DivideG > 85 And DivideG <= 170
         cgaG = 170
      Else
         cgaG = 0
      EndIf
      If DivideB > 85 And DivideB <= 170
         cgaB = 170
      Else
         cgaB = 0
      EndIf
   EndIf
   
   If cgaR = 170 And cgaG = 170 And cgaB = 0
      cgaG = 85
   EndIf
   
   ProcedureReturn RGB(cgaR, cgaG, cgaB)
   
EndProcedure

;UseJPEGImageDecoder()
UsePNGImageDecoder()
UsePNGImageEncoder()

FileName$ = GetHomeDirectory() + "Desktop/image.png"

LoadImage(0, FileName$)

;ResizeImage(0, 320, 200)

xMax = ImageWidth(0) - 1
yMax = ImageHeight(0) - 1

StartDrawing(ImageOutput(0))
   DrawingMode(#PB_2DDrawing_Default)
   For x = 0 To xMax
      For y = 0 To yMax
         c = Point(x, y)
         Plot(x, y , CGA(c))
      Next y
   Next x
StopDrawing()

SaveImage(0, FileName$ + "_cga.png", #PB_ImagePlugin_PNG, 0, 4)

FreeImage(0)

End
BP
Last edited by BasicallyPure on Thu Feb 21, 2013 6:13 pm, edited 1 time in total.
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
User avatar
J. Baker
Addict
Addict
Posts: 2185
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: CGA

Post by J. Baker »

It does work nice with integers. Thanks BasicallyPure! ;)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: CGA

Post by wilbert »

Sorry to tell you but the conversion is incorrect.
If the input value is $FF0000, the output value still is $FF0000 while this is not a valid CGA color.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
J. Baker
Addict
Addict
Posts: 2185
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: CGA

Post by J. Baker »

wilbert wrote:Sorry to tell you but the conversion is incorrect.
If the input value is $FF0000, the output value still is $FF0000 while this is not a valid CGA color.
Nice catch wilbert. I think I fixed it. Just updated the code. ;)

EDIT: I just changed the values that were 0.75 to 0.66 as that was incorrect too. :oops:
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: CGA

Post by wilbert »

Did you consider using a lookup table or is speed not that important ?
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
J. Baker
Addict
Addict
Posts: 2185
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: CGA

Post by J. Baker »

wilbert wrote:Did you consider using a lookup table or is speed not that important ?
I like speed but accuracy come first.

Also, there's still something wrong with the code. A pixel on the original image has a very low red and blue value but a high green value. But for some reason, the converted image is brown. Where or when did the red value turn to a much higher one is beyond me.
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: CGA

Post by wilbert »

I tried a lookup table but I doubt if it is faster (might as well be slower).
I also don't know if I got the table exactly right.

Code: Select all

Procedure CGA(color)
  !mov eax, [p.v_color]
  !and eax, 0xe0e0e0; convert b8g8r8 > r3g3b3
  
  !mov edx, eax
  !shr edx, 21
  
  !movzx ecx, ah
  !ror ecx, 2
  
  !or edx, ecx
  
  !movzx ecx, al
  !rol ecx, 1
  
  !or edx, ecx
  
  ; lookup in table r8g8b8[r3g3b3]
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
    !lea eax, [cga_table512rgb]
    !mov eax, [eax + edx * 4]
  CompilerElse
    !lea rax, [cga_table512rgb]
    !mov eax, [rax + rdx * 4]
  CompilerEndIf
  
  !bswap eax; convert r8g8b8 > b8g8r8
  !shr eax, 8
  ProcedureReturn
  !cga_table512rgb:
  !dd 0x000000,0x000000,0x000000,0x0000AA,0x0000AA,0x0000AA,0x0000AA,0x0000AA
  !dd 0x000000,0x000000,0x000000,0x0000AA,0x0000AA,0x0000AA,0x0000AA,0x0000AA
  !dd 0x000000,0x000000,0x555555,0x555555,0x0000AA,0x0000AA,0x0000AA,0x5555FF
  !dd 0x00AA00,0x00AA00,0x555555,0x00AAAA,0x00AAAA,0x00AAAA,0x00AAAA,0x5555FF
  !dd 0x00AA00,0x00AA00,0x00AA00,0x00AAAA,0x00AAAA,0x00AAAA,0x00AAAA,0x00AAAA
  !dd 0x00AA00,0x00AA00,0x00AA00,0x00AAAA,0x00AAAA,0x00AAAA,0x00AAAA,0x00AAAA
  !dd 0x00AA00,0x00AA00,0x00AA00,0x00AAAA,0x00AAAA,0x00AAAA,0x00AAAA,0x55FFFF
  !dd 0x00AA00,0x00AA00,0x55FF55,0x55FF55,0x00AAAA,0x00AAAA,0x55FFFF,0x55FFFF
  !dd 0x000000,0x000000,0x000000,0x0000AA,0x0000AA,0x0000AA,0x0000AA,0x0000AA
  !dd 0x000000,0x000000,0x555555,0x555555,0x0000AA,0x0000AA,0x0000AA,0x5555FF
  !dd 0x000000,0x555555,0x555555,0x555555,0x555555,0x0000AA,0x5555FF,0x5555FF
  !dd 0x00AA00,0x555555,0x555555,0x555555,0x00AAAA,0x00AAAA,0x5555FF,0x5555FF
  !dd 0x00AA00,0x00AA00,0x555555,0x00AAAA,0x00AAAA,0x00AAAA,0x00AAAA,0x5555FF
  !dd 0x00AA00,0x00AA00,0x00AA00,0x00AAAA,0x00AAAA,0x00AAAA,0x00AAAA,0x55FFFF
  !dd 0x00AA00,0x00AA00,0x55FF55,0x55FF55,0x00AAAA,0x00AAAA,0x55FFFF,0x55FFFF
  !dd 0x00AA00,0x55FF55,0x55FF55,0x55FF55,0x55FF55,0x55FFFF,0x55FFFF,0x55FFFF
  !dd 0x000000,0x000000,0x555555,0x555555,0x0000AA,0x0000AA,0x0000AA,0x5555FF
  !dd 0x000000,0x555555,0x555555,0x555555,0x555555,0x0000AA,0x5555FF,0x5555FF
  !dd 0x555555,0x555555,0x555555,0x555555,0x555555,0x5555FF,0x5555FF,0x5555FF
  !dd 0x555555,0x555555,0x555555,0x555555,0x555555,0x5555FF,0x5555FF,0x5555FF
  !dd 0x00AA00,0x555555,0x555555,0x555555,0x00AAAA,0x00AAAA,0x5555FF,0x5555FF
  !dd 0x00AA00,0x00AA00,0x55FF55,0x55FF55,0x00AAAA,0x00AAAA,0x55FFFF,0x55FFFF
  !dd 0x00AA00,0x55FF55,0x55FF55,0x55FF55,0x55FF55,0x55FFFF,0x55FFFF,0x55FFFF
  !dd 0x55FF55,0x55FF55,0x55FF55,0x55FF55,0x55FF55,0x55FFFF,0x55FFFF,0x55FFFF
  !dd 0xAA0000,0xAA0000,0x555555,0xAA00AA,0xAA00AA,0xAA00AA,0xAA00AA,0x5555FF
  !dd 0xAA0000,0x555555,0x555555,0x555555,0xAA00AA,0xAA00AA,0x5555FF,0x5555FF
  !dd 0xAA5500,0x555555,0x555555,0x555555,0x555555,0x5555FF,0x5555FF,0x5555FF
  !dd 0xAA5500,0x555555,0x555555,0x555555,0x555555,0x5555FF,0x5555FF,0x5555FF
  !dd 0xAA5500,0x555555,0x555555,0x555555,0xAAAAAA,0xAAAAAA,0x5555FF,0x5555FF
  !dd 0x00AA00,0x55FF55,0x55FF55,0x55FF55,0xAAAAAA,0xAAAAAA,0xAAAAAA,0x55FFFF
  !dd 0x55FF55,0x55FF55,0x55FF55,0x55FF55,0x55FF55,0xAAAAAA,0x55FFFF,0x55FFFF
  !dd 0x55FF55,0x55FF55,0x55FF55,0x55FF55,0x55FF55,0x55FFFF,0x55FFFF,0x55FFFF
  !dd 0xAA0000,0xAA0000,0xAA0000,0xAA00AA,0xAA00AA,0xAA00AA,0xAA00AA,0xAA00AA
  !dd 0xAA0000,0xAA0000,0x555555,0xAA00AA,0xAA00AA,0xAA00AA,0xAA00AA,0x5555FF
  !dd 0xAA5500,0xAA5500,0x555555,0x555555,0xAA00AA,0xAA00AA,0x5555FF,0x5555FF
  !dd 0xAA5500,0xAA5500,0x555555,0x555555,0xAAAAAA,0xAAAAAA,0x5555FF,0x5555FF
  !dd 0xAA5500,0xAA5500,0x555555,0xAAAAAA,0xAAAAAA,0xAAAAAA,0xAAAAAA,0x5555FF
  !dd 0xAA5500,0xAA5500,0x55FF55,0xAAAAAA,0xAAAAAA,0xAAAAAA,0xAAAAAA,0xAAAAAA
  !dd 0x55FF55,0x55FF55,0x55FF55,0x55FF55,0xAAAAAA,0xAAAAAA,0xAAAAAA,0x55FFFF
  !dd 0x55FF55,0x55FF55,0x55FF55,0x55FF55,0x55FF55,0xAAAAAA,0x55FFFF,0x55FFFF
  !dd 0xAA0000,0xAA0000,0xAA0000,0xAA00AA,0xAA00AA,0xAA00AA,0xAA00AA,0xAA00AA
  !dd 0xAA0000,0xAA0000,0xAA0000,0xAA00AA,0xAA00AA,0xAA00AA,0xAA00AA,0xFF55FF
  !dd 0xAA5500,0xAA5500,0xAA5500,0xFF5555,0xAA00AA,0xAA00AA,0xFF55FF,0xFF55FF
  !dd 0xAA5500,0xAA5500,0xAA5500,0xFF5555,0xAAAAAA,0xAAAAAA,0xAAAAAA,0xFF55FF
  !dd 0xAA5500,0xAA5500,0xAA5500,0xAAAAAA,0xAAAAAA,0xAAAAAA,0xAAAAAA,0xAAAAAA
  !dd 0xAA5500,0xAA5500,0xAAAAAA,0xAAAAAA,0xAAAAAA,0xAAAAAA,0xAAAAAA,0xAAAAAA
  !dd 0xFFFF55,0xFFFF55,0xFFFF55,0xAAAAAA,0xAAAAAA,0xAAAAAA,0xAAAAAA,0xFFFFFF
  !dd 0xFFFF55,0xFFFF55,0xFFFF55,0xFFFF55,0xAAAAAA,0xAAAAAA,0xFFFFFF,0xFFFFFF
  !dd 0xAA0000,0xAA0000,0xAA0000,0xAA00AA,0xAA00AA,0xAA00AA,0xAA00AA,0xFF55FF
  !dd 0xAA0000,0xAA0000,0xFF5555,0xFF5555,0xAA00AA,0xAA00AA,0xFF55FF,0xFF55FF
  !dd 0xAA5500,0xFF5555,0xFF5555,0xFF5555,0xFF5555,0xFF55FF,0xFF55FF,0xFF55FF
  !dd 0xAA5500,0xFF5555,0xFF5555,0xFF5555,0xFF5555,0xAAAAAA,0xFF55FF,0xFF55FF
  !dd 0xAA5500,0xFF5555,0xFF5555,0xFF5555,0xAAAAAA,0xAAAAAA,0xAAAAAA,0xFF55FF
  !dd 0xAA5500,0xFFFF55,0xFFFF55,0xAAAAAA,0xAAAAAA,0xAAAAAA,0xAAAAAA,0xFFFFFF
  !dd 0xFFFF55,0xFFFF55,0xFFFF55,0xFFFF55,0xAAAAAA,0xAAAAAA,0xFFFFFF,0xFFFFFF
  !dd 0xFFFF55,0xFFFF55,0xFFFF55,0xFFFF55,0xFFFF55,0xFFFFFF,0xFFFFFF,0xFFFFFF
  !dd 0xAA0000,0xAA0000,0xFF5555,0xFF5555,0xAA00AA,0xAA00AA,0xFF55FF,0xFF55FF
  !dd 0xAA0000,0xFF5555,0xFF5555,0xFF5555,0xFF5555,0xFF55FF,0xFF55FF,0xFF55FF
  !dd 0xAA5500,0xFF5555,0xFF5555,0xFF5555,0xFF5555,0xFF55FF,0xFF55FF,0xFF55FF
  !dd 0xAA5500,0xFF5555,0xFF5555,0xFF5555,0xFF5555,0xFF55FF,0xFF55FF,0xFF55FF
  !dd 0xAA5500,0xFF5555,0xFF5555,0xFF5555,0xFF5555,0xAAAAAA,0xFF55FF,0xFF55FF
  !dd 0xFFFF55,0xFFFF55,0xFFFF55,0xFFFF55,0xAAAAAA,0xAAAAAA,0xFFFFFF,0xFFFFFF
  !dd 0xFFFF55,0xFFFF55,0xFFFF55,0xFFFF55,0xFFFF55,0xFFFFFF,0xFFFFFF,0xFFFFFF
  !dd 0xFFFF55,0xFFFF55,0xFFFF55,0xFFFF55,0xFFFF55,0xFFFFFF,0xFFFFFF,0xFFFFFF
EndProcedure
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 539
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: CGA

Post by BasicallyPure »

My assumptions are as follows:
for any color byte value CGA can only be 0 or 85 or 170 or 255.
so original byte values translate like this.
0 to 63 becomes 0
64 to 127 becomes 85
128 to 191 becomes 170
192 to 255 becomes 255

Try this. ( should be easy to do in assembly this way)

Code: Select all

Procedure CGA(color)
   
   R.i = Red(color)
   G.i = Green(color)
   B.i = Blue(color)
   
   cgaR.i = R / 64 * 85
   cgaG.i = G / 64 * 85
   cgaB.i = B / 64 * 85
   
   ProcedureReturn RGB(cgaR, cgaG, cgaB)
   
EndProcedure

UsePNGImageDecoder()
UsePNGImageEncoder()

FileName$ = GetHomeDirectory() + "Desktop/image.png"

LoadImage(0, FileName$)

xMax = ImageWidth(0) - 1
yMax = ImageHeight(0) - 1

StartDrawing(ImageOutput(0))
   DrawingMode(#PB_2DDrawing_Default)
   For x = 0 To xMax
      For y = 0 To yMax
         c = Point(x, y)
         Plot(x, y , CGA(c))
      Next y
   Next x
StopDrawing()

SaveImage(0, FileName$ + "_cga.png", #PB_ImagePlugin_PNG, 0, 4)

FreeImage(0)

End
As fast as I know how without assembly code.

Code: Select all

UsePNGImageDecoder()
UsePNGImageEncoder()

FileName$ = GetHomeDirectory() + "Desktop/image.png"

LoadImage(0, FileName$)

xMax = ImageWidth(0) - 1
yMax = ImageHeight(0) - 1

StartDrawing(ImageOutput(0))
   DrawingMode(#PB_2DDrawing_Default)
   For x = 0 To xMax
      For y = 0 To yMax
         color = Point(x, y)
         CGA = color & $0000FF / $000040 * $000055
         CGA + color & $00FF00 / $004000 * $005500
         CGA + color & $FF0000 / $400000 * $550000
         Plot(x, y , CGA)
      Next y
   Next x
StopDrawing()

SaveImage(0, FileName$ + "_cga.png", #PB_ImagePlugin_PNG, 0, 4)
FreeImage(0)
BP
Last edited by BasicallyPure on Fri Feb 22, 2013 5:11 pm, edited 1 time in total.
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
User avatar
J. Baker
Addict
Addict
Posts: 2185
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: CGA

Post by J. Baker »

All these codes seem to work, it's just accuracy of the conversion. So far, as close as I can get with accuracy is this. As you can see, I'm trying to find a way that if the original color is not truly black, to give it some kind of color value.

Image I've been testing.
Image

Code: Select all

Procedure CGA(color)
  
  DivideR.i = Red(color)
  DivideG.i = Green(color)
  DivideB.i = Blue(color)
  
      If DivideR.i > 170
        cgaR = 255
      ElseIf DivideR.i > 127 ;And DivideR.i < 171
        cgaR = 170
      Else
        If DivideG.i > 170 Or DivideB.i > 170
          cgaR = 85
        Else
          cgaR = 0
        EndIf
      EndIf
      
      If DivideG.i > 170
        cgaG = 255
      ElseIf DivideG.i > 127 ;And DivideG.i < 171
       cgaG = 170
      Else
        If DivideR.i > 170 Or DivideB.i > 170
          cgaG = 85
        Else
          cgaG = 0
        EndIf
      EndIf
      
      If DivideB.i > 170
        cgaB = 255
      ElseIf DivideB.i > 127 ;And DivideB.i < 171
       cgaB = 170
      Else
        If DivideR.i > 170 Or DivideG.i > 170
          cgaB = 85
        Else
          cgaB = 0
        EndIf
      EndIf
      
    MaxBlack = 64  
      
   If cgaR = 0 And cgaG = 0 And cgaB = 0
     If DivideR.i > MaxBlack Or DivideG.i > MaxBlack Or DivideB.i > MaxBlack
       If DivideB.i - DivideR.i > MaxBlack And DivideB.i - DivideG.i > MaxBlack
         cgaR = 0
         cgaG = 0
         cgaB = 170
       EndIf
       If DivideG.i - DivideR.i > MaxBlack And DivideG.i - DivideB.i > MaxBlack
         cgaR = 0
         cgaG = 170
         cgaB = 0
       EndIf
       If DivideG.i - DivideR.i > MaxBlack And DivideB.i - DivideR.i > MaxBlack
         cgaR = 0
         cgaG = 170
         cgaB = 170
       EndIf
       If DivideR.i - DivideG.i > MaxBlack And DivideR.i - DivideB.i > MaxBlack
         cgaR = 170
         cgaG = 0
         cgaB = 0
       EndIf
       If DivideR.i - DivideG.i > MaxBlack And DivideB.i - DivideG.i > MaxBlack
         cgaR = 170
         cgaG = 0
         cgaB = 170
       EndIf
       If DivideR.i - DivideG.i > MaxBlack / 2 And DivideR.i - DivideB.i > MaxBlack
         cgaR = 170
         cgaG = 85
         cgaB = 0
       EndIf
       If DivideR.i < MaxBlack And DivideR.i > 0 Or DivideG.i < MaxBlack And DivideG.i > 0 Or DivideB.i < MaxV And DivideB.i > 0
         cgaR = 170
         cgaG = 170
         cgaB = 170
       EndIf
     EndIf
   EndIf    
      
  If cgaR = 170 And cgaG = 170 And cgaB = 0
     cgaG = 85
  EndIf
  
  ProcedureReturn RGB(cgaR, cgaG, cgaB)
  
EndProcedure

UseJPEGImageDecoder()
UsePNGImageDecoder()
UsePNGImageEncoder()

FileName$ = GetHomeDirectory() + "Desktop/kq5.png"

LoadImage(0, FileName$)

;ResizeImage(0, 320, 240)

  StartDrawing(ImageOutput(0))
   DrawingMode(#PB_2DDrawing_Default)
    For x = 0 To ImageWidth(0) - 1
      For y = 0 To ImageHeight(0) - 1
        c = Point(x, y)
            Plot(x, y , CGA(c))
      Next y
    Next x

SaveImage(0, FileName$ + "_cga.png", #PB_ImagePlugin_PNG, 0, 4)

FreeImage(0)

End
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 539
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: CGA

Post by BasicallyPure »

nothing black except when original color = 0

Code: Select all

UsePNGImageDecoder()
UsePNGImageEncoder()

FileName$ = GetHomeDirectory() + "Desktop/image.png"

LoadImage(0, FileName$)

xMax = ImageWidth(0) - 1
yMax = ImageHeight(0) - 1

StartDrawing(ImageOutput(0))
   DrawingMode(#PB_2DDrawing_Default)
   For x = 0 To xMax
      For y = 0 To yMax
         color = Point(x, y)
         If color = 0
            Plot(x, y, 0)
         Else
            R = Red(color)
            G = Green(color)
            B = Blue(color)
            CGA_R = R / $40 * $55
            CGA_G = G / $40 * $55
            CGA_B = B / $40 * $55
            
            If RGB(CGA_R, CGA_G, CGA_B) = 0
               If R > G And R > B
                  CGA_R = $55
               ElseIf G > R And G > B
                  CGA_G = $55
               Else
                  CGA_B = $55
               EndIf
            EndIf
            
            Plot(x, y , RGB(CGA_R, CGA_G, CGA_B))
         EndIf
      Next y
   Next x
StopDrawing()

SaveImage(0, FileName$ + "_cga.png", #PB_ImagePlugin_PNG, 0, 4)
FreeImage(0)
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: CGA

Post by wilbert »

J. Baker wrote:All these codes seem to work, it's just accuracy of the conversion. So far, as close as I can get with accuracy is this. As you can see, I'm trying to find a way that if the original color is not truly black, to give it some kind of color value.
I used the method described here http://en.wikipedia.org/wiki/Color_quantization for a static palette to build the color table I used. It finds the closest palette entry.
But looking at the image you posted, that might not be what you want to do. The image is so colorful that you probably prefer a green pixel for a green pixel even if the brightness is off a lot and a gray pixel might in fact be closer.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
J. Baker
Addict
Addict
Posts: 2185
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: CGA

Post by J. Baker »

wilbert wrote:
J. Baker wrote:All these codes seem to work, it's just accuracy of the conversion. So far, as close as I can get with accuracy is this. As you can see, I'm trying to find a way that if the original color is not truly black, to give it some kind of color value.
I used the method described here http://en.wikipedia.org/wiki/Color_quantization for a static palette to build the color table I used. It finds the closest palette entry.
But looking at the image you posted, that might not be what you want to do. The image is so colorful that you probably prefer a green pixel for a green pixel even if the brightness is off a lot and a gray pixel might in fact be closer.
Yeah, that is what I'm talking about. I'm trying to write a procedure to do this but so far it's only half right.

Also, I don't need this code for anything. This is just a "challenge" really. Well, that and I do like 4bit graphics, Dos, ect. ;)

@BasicallyPure
Looks good but still some minor issues that I think could still be corrected with code. ;)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
Post Reply