Latest UploadsKing SalmonPakzSnookerJayenkaiYou've ... o have youJayenkaiMerry XmasJayenkaiRCT Classic on iOSJayenkaiOopsJayenkai

### converting colours...

 Page : 1 2

UserMessage
Posted : Sunday, 26 July 2009, 06:58 | Permalink
spinal

 Im trying to reduce regular colours down to a websafe palette... where am i going wrong? ; attempt to convert regular colours to websafe palette. Graphics 640,480,0,2 Dim pal(215,2) For t=0 To 215 Read pal(t,0),pal(t,1),pal(t,2) Next ; r/43, g/43, b/43 For r=0 To 255 For b=0 To 255 col = ((b/43)*36) +((g/43)*6)+ (r/43) Color pal(col,0),pal(col,1),pal(col,2) Plot r,g Next Next While Not KeyHit(1) Wend End ; WebSafe Palette (216 Colours) Data 0,0,0 Data 51,0,0 Data 102,0,0 Data 153,0,0 Data 204,0,0 Data 255,0,0 Data 0,51,0 Data 51,51,0 Data 102,51,0 Data 153,51,0 Data 204,51,0 Data 255,51,0 Data 0,102,0 Data 51,102,0 Data 102,102,0 Data 153,102,0 Data 204,102,0 Data 255,102,0 Data 0,153,0 Data 51,153,0 Data 102,153,0 Data 153,153,0 Data 204,153,0 Data 255,153,0 Data 0,204,0 Data 51,204,0 Data 102,204,0 Data 153,204,0 Data 204,204,0 Data 255,204,0 Data 0,255,0 Data 51,255,0 Data 102,255,0 Data 153,255,0 Data 204,255,0 Data 255,255,0 Data 0,0,51 Data 51,0,51 Data 102,0,51 Data 153,0,51 Data 204,0,51 Data 255,0,51 Data 0,51,51 Data 51,51,51 Data 102,51,51 Data 153,51,51 Data 204,51,51 Data 255,51,51 Data 0,102,51 Data 51,102,51 Data 102,102,51 Data 153,102,51 Data 204,102,51 Data 255,102,51 Data 0,153,51 Data 51,153,51 Data 102,153,51 Data 153,153,51 Data 204,153,51 Data 255,153,51 Data 0,204,51 Data 51,204,51 Data 102,204,51 Data 153,204,51 Data 204,204,51 Data 255,204,51 Data 0,255,51 Data 51,255,51 Data 102,255,51 Data 153,255,51 Data 204,255,51 Data 255,255,51 Data 0,0,102 Data 51,0,102 Data 102,0,102 Data 153,0,102 Data 204,0,102 Data 255,0,102 Data 0,51,102 Data 51,51,102 Data 102,51,102 Data 153,51,102 Data 204,51,102 Data 255,51,102 Data 0,102,102 Data 51,102,102 Data 102,102,102 Data 153,102,102 Data 204,102,102 Data 255,102,102 Data 0,153,102 Data 51,153,102 Data 102,153,102 Data 153,153,102 Data 204,153,102 Data 255,153,102 Data 0,204,102 Data 51,204,102 Data 102,204,102 Data 153,204,102 Data 204,204,102 Data 255,204,102 Data 0,255,102 Data 51,255,102 Data 102,255,102 Data 153,255,102 Data 204,255,102 Data 255,255,102 Data 0,0,153 Data 51,0,153 Data 102,0,153 Data 153,0,153 Data 204,0,153 Data 255,0,153 Data 0,51,153 Data 51,51,153 Data 102,51,153 Data 153,51,153 Data 204,51,153 Data 255,51,153 Data 0,102,153 Data 51,102,153 Data 102,102,153 Data 153,102,153 Data 204,102,153 Data 255,102,153 Data 0,153,153 Data 51,153,153 Data 102,153,153 Data 153,153,153 Data 204,153,153 Data 255,153,153 Data 0,204,153 Data 51,204,153 Data 102,204,153 Data 153,204,153 Data 204,204,153 Data 255,204,153 Data 0,255,153 Data 51,255,153 Data 102,255,153 Data 153,255,153 Data 204,255,153 Data 255,255,153 Data 0,0,204 Data 51,0,204 Data 102,0,204 Data 153,0,204 Data 204,0,204 Data 255,0,204 Data 0,51,204 Data 51,51,204 Data 102,51,204 Data 153,51,204 Data 204,51,204 Data 255,51,204 Data 0,102,204 Data 51,102,204 Data 102,102,204 Data 153,102,204 Data 204,102,204 Data 255,102,204 Data 0,153,204 Data 51,153,204 Data 102,153,204 Data 153,153,204 Data 204,153,204 Data 255,153,204 Data 0,204,204 Data 51,204,204 Data 102,204,204 Data 153,204,204 Data 204,204,204 Data 255,204,204 Data 0,255,204 Data 51,255,204 Data 102,255,204 Data 153,255,204 Data 204,255,204 Data 255,255,204 Data 0,0,255 Data 51,0,255 Data 102,0,255 Data 153,0,255 Data 204,0,255 Data 255,0,255 Data 0,51,255 Data 51,51,255 Data 102,51,255 Data 153,51,255 Data 204,51,255 Data 255,51,255 Data 0,102,255 Data 51,102,255 Data 102,102,255 Data 153,102,255 Data 204,102,255 Data 255,102,255 Data 0,153,255 Data 51,153,255 Data 102,153,255 Data 153,153,255 Data 204,153,255 Data 255,153,255 Data 0,204,255 Data 51,204,255 Data 102,204,255 Data 153,204,255 Data 204,204,255 Data 255,204,255 Data 0,255,255 Data 51,255,255 Data 102,255,255 Data 153,255,255 Data 204,255,255 Data 255,255,255 --> -----Download "Don't Flip Out!" Directly to your OUYA! (Click Here).
Posted : Sunday, 26 July 2009, 07:07 | Permalink
Jayenkai

WW Entries : 106
 ; attempt to convert regular colours to websafe palette. Graphics 640,480,0,2 SetBuffer BackBuffer() Dim pal(215,2) For t=0 To 215 Read pal(t,0),pal(t,1),pal(t,2) Next ; r/43, g/43, b/43 six=6 ;switch to 1 for full square For x=0 To 255/six For y=0 To 255 r=x*six:b=y g=((((y*6) Mod 255)/43)*36) col = ((b/43)*36) +((g/43)*6)+ (r/43) Color pal(col,0),pal(col,1),pal(col,2) Plot x,y Next Flip Next While Not KeyHit(1) Wend End ; WebSafe Palette (216 Colours) Data 0,0,0 Data 51,0,0 Data 102,0,0 Data 153,0,0 Data 204,0,0 Data 255,0,0 Data 0,51,0 Data 51,51,0 Data 102,51,0 Data 153,51,0 Data 204,51,0 Data 255,51,0 Data 0,102,0 Data 51,102,0 Data 102,102,0 Data 153,102,0 Data 204,102,0 Data 255,102,0 Data 0,153,0 Data 51,153,0 Data 102,153,0 Data 153,153,0 Data 204,153,0 Data 255,153,0 Data 0,204,0 Data 51,204,0 Data 102,204,0 Data 153,204,0 Data 204,204,0 Data 255,204,0 Data 0,255,0 Data 51,255,0 Data 102,255,0 Data 153,255,0 Data 204,255,0 Data 255,255,0 Data 0,0,51 Data 51,0,51 Data 102,0,51 Data 153,0,51 Data 204,0,51 Data 255,0,51 Data 0,51,51 Data 51,51,51 Data 102,51,51 Data 153,51,51 Data 204,51,51 Data 255,51,51 Data 0,102,51 Data 51,102,51 Data 102,102,51 Data 153,102,51 Data 204,102,51 Data 255,102,51 Data 0,153,51 Data 51,153,51 Data 102,153,51 Data 153,153,51 Data 204,153,51 Data 255,153,51 Data 0,204,51 Data 51,204,51 Data 102,204,51 Data 153,204,51 Data 204,204,51 Data 255,204,51 Data 0,255,51 Data 51,255,51 Data 102,255,51 Data 153,255,51 Data 204,255,51 Data 255,255,51 Data 0,0,102 Data 51,0,102 Data 102,0,102 Data 153,0,102 Data 204,0,102 Data 255,0,102 Data 0,51,102 Data 51,51,102 Data 102,51,102 Data 153,51,102 Data 204,51,102 Data 255,51,102 Data 0,102,102 Data 51,102,102 Data 102,102,102 Data 153,102,102 Data 204,102,102 Data 255,102,102 Data 0,153,102 Data 51,153,102 Data 102,153,102 Data 153,153,102 Data 204,153,102 Data 255,153,102 Data 0,204,102 Data 51,204,102 Data 102,204,102 Data 153,204,102 Data 204,204,102 Data 255,204,102 Data 0,255,102 Data 51,255,102 Data 102,255,102 Data 153,255,102 Data 204,255,102 Data 255,255,102 Data 0,0,153 Data 51,0,153 Data 102,0,153 Data 153,0,153 Data 204,0,153 Data 255,0,153 Data 0,51,153 Data 51,51,153 Data 102,51,153 Data 153,51,153 Data 204,51,153 Data 255,51,153 Data 0,102,153 Data 51,102,153 Data 102,102,153 Data 153,102,153 Data 204,102,153 Data 255,102,153 Data 0,153,153 Data 51,153,153 Data 102,153,153 Data 153,153,153 Data 204,153,153 Data 255,153,153 Data 0,204,153 Data 51,204,153 Data 102,204,153 Data 153,204,153 Data 204,204,153 Data 255,204,153 Data 0,255,153 Data 51,255,153 Data 102,255,153 Data 153,255,153 Data 204,255,153 Data 255,255,153 Data 0,0,204 Data 51,0,204 Data 102,0,204 Data 153,0,204 Data 204,0,204 Data 255,0,204 Data 0,51,204 Data 51,51,204 Data 102,51,204 Data 153,51,204 Data 204,51,204 Data 255,51,204 Data 0,102,204 Data 51,102,204 Data 102,102,204 Data 153,102,204 Data 204,102,204 Data 255,102,204 Data 0,153,204 Data 51,153,204 Data 102,153,204 Data 153,153,204 Data 204,153,204 Data 255,153,204 Data 0,204,204 Data 51,204,204 Data 102,204,204 Data 153,204,204 Data 204,204,204 Data 255,204,204 Data 0,255,204 Data 51,255,204 Data 102,255,204 Data 153,255,204 Data 204,255,204 Data 255,255,204 Data 0,0,255 Data 51,0,255 Data 102,0,255 Data 153,0,255 Data 204,0,255 Data 255,0,255 Data 0,51,255 Data 51,51,255 Data 102,51,255 Data 153,51,255 Data 204,51,255 Data 255,51,255 Data 0,102,255 Data 51,102,255 Data 102,102,255 Data 153,102,255 Data 204,102,255 Data 255,102,255 Data 0,153,255 Data 51,153,255 Data 102,153,255 Data 153,153,255 Data 204,153,255 Data 255,153,255 Data 0,204,255 Data 51,204,255 Data 102,204,255 Data 153,204,255 Data 204,204,255 Data 255,204,255 Data 0,255,255 Data 51,255,255 Data 102,255,255 Data 153,255,255 Data 204,255,255 Data 255,255,255 --> -----"Load, Next List!"
Posted : Monday, 27 July 2009, 05:08 | Permalink
spinal

 The Results..... I have managed to use simple pattern dithering to reduce an image from true colour down to 216 websafe colours. ; attempt to convert regular colours to websafe palette. Graphics 640,480,0,2 Dim matrix2(7,7) For y=0 To 7 For x=0 To 7 Read matrix2(x,y) Next Next Dim pal(215,2) For t=0 To 215 Read pal(t,0),pal(t,1),pal(t,2) Next picture = LoadImage("test.bmp") DrawImage picture,0,0 For x=0 To 255 For y=0 To 191 GetColor x,y r=ColorRed() g=ColorGreen() b=ColorBlue() r=r+(matrix2(x Mod 8, y Mod 8)-32) g=g+(matrix2(x Mod 8, y Mod 8)-32) b=b+(matrix2(x Mod 8, y Mod 8)-32) If r>255 Then r=255 If g>255 Then g=255 If b>255 Then b=255 If r<0 Then r=0 If g<0 Then g=0 If b<0 Then b=0 col = ((b/43)*36) +((g/43)*6)+ (r/43) Color pal(col,0),pal(col,1),pal(col,2) Plot x,y+200 Next Next While Not KeyHit(1) Wend End ; dithering threashold Data 1,49,13,61,4,52,16,64 Data 33,17,45,29,36,20,48,32 Data 9,57,5,53,12,60,8,56 Data 41,25,37,21,44,28,40,24 Data 3,51,15,63,2,50,14,62 Data 35,16,47,31,34,18,46,30 Data 11,59,7,55,10,58,6,54 Data 43,27,39,23,42,26,38,22 ; WebSafe Palette (216 Colours) Data 0,0,0 Data 51,0,0 Data 102,0,0 Data 153,0,0 Data 204,0,0 Data 255,0,0 Data 0,51,0 Data 51,51,0 Data 102,51,0 Data 153,51,0 Data 204,51,0 Data 255,51,0 Data 0,102,0 Data 51,102,0 Data 102,102,0 Data 153,102,0 Data 204,102,0 Data 255,102,0 Data 0,153,0 Data 51,153,0 Data 102,153,0 Data 153,153,0 Data 204,153,0 Data 255,153,0 Data 0,204,0 Data 51,204,0 Data 102,204,0 Data 153,204,0 Data 204,204,0 Data 255,204,0 Data 0,255,0 Data 51,255,0 Data 102,255,0 Data 153,255,0 Data 204,255,0 Data 255,255,0 Data 0,0,51 Data 51,0,51 Data 102,0,51 Data 153,0,51 Data 204,0,51 Data 255,0,51 Data 0,51,51 Data 51,51,51 Data 102,51,51 Data 153,51,51 Data 204,51,51 Data 255,51,51 Data 0,102,51 Data 51,102,51 Data 102,102,51 Data 153,102,51 Data 204,102,51 Data 255,102,51 Data 0,153,51 Data 51,153,51 Data 102,153,51 Data 153,153,51 Data 204,153,51 Data 255,153,51 Data 0,204,51 Data 51,204,51 Data 102,204,51 Data 153,204,51 Data 204,204,51 Data 255,204,51 Data 0,255,51 Data 51,255,51 Data 102,255,51 Data 153,255,51 Data 204,255,51 Data 255,255,51 Data 0,0,102 Data 51,0,102 Data 102,0,102 Data 153,0,102 Data 204,0,102 Data 255,0,102 Data 0,51,102 Data 51,51,102 Data 102,51,102 Data 153,51,102 Data 204,51,102 Data 255,51,102 Data 0,102,102 Data 51,102,102 Data 102,102,102 Data 153,102,102 Data 204,102,102 Data 255,102,102 Data 0,153,102 Data 51,153,102 Data 102,153,102 Data 153,153,102 Data 204,153,102 Data 255,153,102 Data 0,204,102 Data 51,204,102 Data 102,204,102 Data 153,204,102 Data 204,204,102 Data 255,204,102 Data 0,255,102 Data 51,255,102 Data 102,255,102 Data 153,255,102 Data 204,255,102 Data 255,255,102 Data 0,0,153 Data 51,0,153 Data 102,0,153 Data 153,0,153 Data 204,0,153 Data 255,0,153 Data 0,51,153 Data 51,51,153 Data 102,51,153 Data 153,51,153 Data 204,51,153 Data 255,51,153 Data 0,102,153 Data 51,102,153 Data 102,102,153 Data 153,102,153 Data 204,102,153 Data 255,102,153 Data 0,153,153 Data 51,153,153 Data 102,153,153 Data 153,153,153 Data 204,153,153 Data 255,153,153 Data 0,204,153 Data 51,204,153 Data 102,204,153 Data 153,204,153 Data 204,204,153 Data 255,204,153 Data 0,255,153 Data 51,255,153 Data 102,255,153 Data 153,255,153 Data 204,255,153 Data 255,255,153 Data 0,0,204 Data 51,0,204 Data 102,0,204 Data 153,0,204 Data 204,0,204 Data 255,0,204 Data 0,51,204 Data 51,51,204 Data 102,51,204 Data 153,51,204 Data 204,51,204 Data 255,51,204 Data 0,102,204 Data 51,102,204 Data 102,102,204 Data 153,102,204 Data 204,102,204 Data 255,102,204 Data 0,153,204 Data 51,153,204 Data 102,153,204 Data 153,153,204 Data 204,153,204 Data 255,153,204 Data 0,204,204 Data 51,204,204 Data 102,204,204 Data 153,204,204 Data 204,204,204 Data 255,204,204 Data 0,255,204 Data 51,255,204 Data 102,255,204 Data 153,255,204 Data 204,255,204 Data 255,255,204 Data 0,0,255 Data 51,0,255 Data 102,0,255 Data 153,0,255 Data 204,0,255 Data 255,0,255 Data 0,51,255 Data 51,51,255 Data 102,51,255 Data 153,51,255 Data 204,51,255 Data 255,51,255 Data 0,102,255 Data 51,102,255 Data 102,102,255 Data 153,102,255 Data 204,102,255 Data 255,102,255 Data 0,153,255 Data 51,153,255 Data 102,153,255 Data 153,153,255 Data 204,153,255 Data 255,153,255 Data 0,204,255 Data 51,204,255 Data 102,204,255 Data 153,204,255 Data 204,204,255 Data 255,204,255 Data 0,255,255 Data 51,255,255 Data 102,255,255 Data 153,255,255 Data 204,255,255 Data 255,255,255 --> -----Download "Don't Flip Out!" Directly to your OUYA! (Click Here).
Posted : Monday, 27 July 2009, 05:18 | Permalink
Jayenkai

 Neat
Posted : Monday, 27 July 2009, 06:30 | Permalink
Phoenix

WW Entries : 5
 Why would you want to use the websafe palette? Is IE6 being the bottleneck once again? YouTube is phasing out support for that browser, so I think it's fairly safe for everyone to do that by now.
Posted : Monday, 27 July 2009, 07:18 | Permalink
spinal

 It's for a DS project, I'm struggling with one of the graphic modes, so I'm using 8bit instead. However, I need to keep a few palette entries spare for a different part of the display and the websafe palette was the first thing that popped into my head for using less than 256 colours. ...I think the output from the code is quite good considering i'm not searching the palette for the best match colour.-----Download "Don't Flip Out!" Directly to your OUYA! (Click Here).
Posted : Friday, 20 November 2009, 08:43 | Permalink
spinal

Posted : Friday, 20 November 2009, 09:01 | Permalink
HoboBen

WW Entries : 9
 Nice - I think you did a better job than Paint Shop Pro's dithering, too-----
Posted : Saturday, 21 November 2009, 17:54 | Permalink
CodersRule

 Yeah looks nice, just looks like you took the image and added a texture to it
Posted : Sunday, 22 November 2009, 02:07 | Permalink
spinal

 It looks a bit speckly to me, perhaps later I will try some sort of custom palette, maybe use only the 256 most used colours or something.-----Download "Don't Flip Out!" Directly to your OUYA! (Click Here).
Posted : Monday, 30 November 2009, 14:57 | Permalink
spinal

 OK, here's a bash at using 256 most used colours from the image. ; attempt to convert 16bit colour to 8bit Graphics 640,480,0,2 Dim pal(256,2) Dim bitmap(256,192,2) ;R,G,B for each pixel in the image Dim colorList(16777216) ; for counting colours Dim colorValue(16777216) ; for the actual colours picture = LoadImage("test.bmp") ResizeImage picture, 256,192 DrawBlock picture,0,0 For x=0 To 255 For y=0 To 191 GetColor x,y r=ColorRed() g=ColorGreen() b=ColorBlue() bitmap(x,y,0)=r bitmap(x,y,1)=g bitmap(x,y,2)=b r=(r/8)*8 g=(g/8)*8 b=(b/8)*8 colorList( (r Shl 16)+ (g Shl 8)+ b) = colorList( (r Shl 16)+ (g Shl 8)+ b) + 1 Next Next For t = 1 To 16777216 colorValue(t) =t Next Color 255,255,255 Print "Sorting..." quicksort(0,16777215) ; as the sorted sorts smallest to largest, we use the LAST 256 colours For t=0 To 255 pal(t,0) = (colorValue(16777215-t) Shr 16) And 255 pal(t,1) = (colorValue(16777215-t) Shr 8) And 255 pal(t,2) = ColorValue(16777215-t) And 255 Color pal(t,0), pal(t,1), pal(t,2) Rect t*2,0,2,2,1 Next p=0 For t = 1 To 16777216 If colorList(t) >0 Then p=p+1 Next Color 255,255,255 Print p + " colors used" For x=0 To 255 For y=0 To 191 ; col = ((bitmap(x,y,2)/43)*36) +((bitmap(x,y,1)/43)*6)+ (bitmap(x,y,0)/43) col = FindNearestColour(bitmap(x,y,0),bitmap(x,y,1),bitmap(x,y,2)) Color pal(col,0),pal(col,1),pal(col,2) Plot x,y+200 ; compute_disperse(x, y, 0, col) ; compute_disperse(x, y, 1, col) ; compute_disperse(x, y, 2, col) Next Next For t = 1 To 16777216 colorList(t) =0 Next For x=0 To 255 For y=0 To 191 GetColor x,y + 200 r=ColorRed() g=ColorGreen() b=ColorBlue() bitmap(x,y,0)=r bitmap(x,y,1)=g bitmap(x,y,2)=b r=(r/8)*8 g=(g/8)*8 b=(b/8)*8 colorList( (r Shl 16)+ (g Shl 8)+ b) = colorList( (r Shl 16)+ (g Shl 8)+ b) + 1 Next Next p=0 For t = 1 To 16777216 If colorList(t) >0 Then p=p+1 Next Color 255,255,255 Print p + " colors used" Print "Done!" While Not KeyHit(1) Wend End ;We will actually search through the colours. It is much slower, but far more accurate. Function FindNearestColour(r,g,b) minDistanceSquared = 255*255 + 255*255 + 255*255 + 1 ; furthes possible colour difference For i = 0 To 215 ; 215 colours in websafe palette rdiff = r - pal(i,0) gdiff = g - pal(i,1) bdiff = b - pal(i,2) DistanceSquared = rdiff*rdiff + gdiff*gdiff + bdiff*bdiff If DistanceSquared < minDistanceSquared Then minDistanceSquared = DistanceSquared bestIndex = i End If Next Return bestIndex End Function Function plus_truncate(a, b) number = a+b If number <0 Then number =0 If number >255 Then number =255 Return number End Function Function compute_disperse(x, y, channel, index) err = bitmap(x, y, channel) - pal(index, channel) If x+1 < 256 Then ; 256 = image width bitmap(x+1, y, channel) = plus_truncate(bitmap(x+1, y, channel), (err*7)/8) End If If y+1 < 192 Then If x-1 > 0 Then bitmap(x-1, y+1, channel) = plus_truncate(bitmap(x-1, y+1, channel), (err*3)/8) End If bitmap(x, y+1, channel) = plus_truncate(bitmap(x, y+1, channel), (err*5)/8) If x+1 < 256 Then ; 256 = image width bitmap(x+1, y+1, channel) = plus_truncate(bitmap(x+1, y+1, channel), err/8) End If End If End Function Function quicksort(l,r) Local p,q,h p=l q=r x=colorList((l+r)/2) Repeat While colorList(p)q Then Exit ;SWAP------------------ h=colorList(q) colorList(q)=colorList(p) colorList(p)=h h=colorValue(q) colorValue(q)=colorValue(p) colorValue(p)=h ;---------------------- p=p+1 q=q-1 If q<0 Then Exit Forever If l The converted image only uses 215 colours! I had to remove the dithering though, it went a bit crap, not a clue why yet.-----Download "Don't Flip Out!" Directly to your OUYA! (Click Here).
Posted : Monday, 30 November 2009, 15:18 | Permalink
Jayenkai

WW Entries : 106
 That's lovely, although you have lost the house's redness...-----"Load, Next List!"
Posted : Monday, 30 November 2009, 23:52 | Permalink
spinal

 yup, its using the most used 256 colours, which, isn't such a good idea now that I think of it. However, I can't think of any other way to do it, other than a fixed palette, which can look worse -----Download "Don't Flip Out!" Directly to your OUYA! (Click Here).
Posted : Monday, 07 December 2009, 15:15 | Permalink
spinal

 Woohoo!!!!!! I do believe I have done an ok job on this now. I cheated a lot on the palette reduction, instead of using a whole bunch of crazy analysis on the image, I just averaged out the colours and chose 256 of them. ; attempt to convert 16bit colour to 8bit Graphics 657,480,0,2 Dim pal(256,2) Dim bitmap(256,192,2) ;R,G,B for each pixel in the image Dim colorList(16777216) ; for counting colours Dim colorValue(16777216) ; for the actual colours Dim distance_list(16777216) picture = LoadImage("test.bmp") ResizeImage picture, 256,192 DrawBlock picture,0,0 Color 255,255,255 Print " Counting colours" For x=0 To 255 For y=0 To 191 GetColor x,y r=ColorRed() g=ColorGreen() b=ColorBlue() colorList( (r Shl 16)+ (g Shl 8)+ b) = colorList( (r Shl 16)+ (g Shl 8)+ b) + 1 Next Next p=0 For t = 0 To 16777215 colorValue(t) =t If colorList(t) >0 Then colorList(t) =0 ; reset the colours p=p+1 End If Next totalcolours = p Color 255,255,255 Print " "+ totalcolours + " colors" For x=0 To 255 For y=0 To 191 GetColor x,y r=ColorRed() g=ColorGreen() b=ColorBlue() bitmap(x,y,0)=r bitmap(x,y,1)=g bitmap(x,y,2)=b ;PSP uses only 5bits per channel, so thats what i'll do r=r/8*8 g=g/8*8 b=b/8*8 colorList( (r Shl 16)+ (g Shl 8)+ b) = colorList( (r Shl 16)+ (g Shl 8)+ b) + 1 Next Next Color 255,255,255 Print " Sorting palette" quicksort(0,16777215) p=0 offset = -1 For t = 1 To 16777215 If colorList(t) >0 Then If offset=-1 Then offset = t p=p+1 End If Next ;quicksort2(offset,16777215) x = p/248 For t = 0 To 255 Step 50 pal(t/50,0) = t pal(t/50,1) = t pal(t/50,2) = t Next pal(6,0) = 255 : pal(6,1) = 255 : pal(6,2) = 255 pal(7,0) = 255 : pal(7,1) = 0 : pal(7,2) = 255 For t = 0 To 248 s = offset + (x*t) pal(t+8,0) = (colorValue(s) Shr 16) And 255 pal(t+8,1) = (colorValue(s) Shr 8) And 255 pal(t+8,2) = ColorValue(s) And 255 Next ; sort the palette a little, make it look nice, not actually needed though For x=0 To 255 For y=0 To 255 br = (pal(y,0)+ pal(y,0)+ pal(y,1)+ pal(y,1)+ pal(y,1)+ pal(y,2))/6 br1 = (pal(y+1,0)+pal(y+1,0)+pal(y+1,1)+pal(y+1,1)+pal(y+1,1)+pal(y+1,2))/6 If br < br1 Then temp0 = pal(y+1,0) temp1 = pal(y+1,1) temp2 = pal(y+1,2) pal(y+1,0) = pal(y,0) pal(y+1,1) = pal(y,1) pal(y+1,2) = pal(y,2) pal(y,0) = temp0 pal(y,1) = temp1 pal(y,2) = temp2 End If Next Next Color 128,128,128 Rect 264,62,129,129,1 t=0 For y = 0 To 15 For x = 0 To 15 Color pal(t,0), pal(t,1), pal(t,2) Rect 265 + (x*8), 63 + (y*8),7,7,1 t=t+1 Next Next For x=0 To 255 For y=0 To 191 ; col = ((bitmap(x,y,2)/43)*36) +((bitmap(x,y,1)/43)*6)+ (bitmap(x,y,0)/43) ; for websafe palette col = FindNearestColour(bitmap(x,y,0),bitmap(x,y,1),bitmap(x,y,2)) Color pal(col,0),pal(col,1),pal(col,2) Plot x+401,y compute_disperse2(x, y, 0, col) compute_disperse2(x, y, 1, col) compute_disperse2(x, y, 2, col) Next Next Color 255,255,255 Print " Done!" While Not KeyHit(1) Delay 2 Wend End ;We will actually search through the colours. It is much slower, but far more accurate. Function FindNearestColour(r,g,b) minDistanceSquared = 255*255 + 255*255 + 255*255 + 1 ; furthes possible colour difference For i = 0 To 255 ; 215 colours in websafe palette rdiff = r - pal(i,0) gdiff = g - pal(i,1) bdiff = b - pal(i,2) DistanceSquared = rdiff*rdiff + gdiff*gdiff + bdiff*bdiff If DistanceSquared < minDistanceSquared Then minDistanceSquared = DistanceSquared bestIndex = i End If Next Return bestIndex End Function Function plus_truncate(a, b) number = a+b If number <0 Then number =0 If number >255 Then number =255 Return number End Function Function compute_disperse(x, y, channel, index) err = bitmap(x, y, channel) - pal(index, channel) If x+1 < 256 Then ; 256 = image width bitmap(x+1, y, channel) = plus_truncate(bitmap(x+1, y, channel), (err*7)/16) End If If y+1 < 192 Then If x-1 > 0 Then bitmap(x-1, y+1, channel) = plus_truncate(bitmap(x-1, y+1, channel), (err*3)/16) End If bitmap(x, y+1, channel) = plus_truncate(bitmap(x, y+1, channel), (err*5)/16) If x+1 < 256 Then ; 256 = image width bitmap(x+1, y+1, channel) = plus_truncate(bitmap(x+1, y+1, channel), err/16) End If End If End Function Function compute_disperse2(x, y, channel, index) err = bitmap(x, y, channel) - pal(index, channel) If x+1 < 256 Then ; 256 = image width bitmap(x+1, y, channel) = plus_truncate(bitmap(x+1, y, channel), (err*7)/16) End If If y+1 < 192 Then If x-2 > 0 Then bitmap(x-2, y+1, channel) = plus_truncate(bitmap(x-2, y+1, channel), err/16) End If bitmap(x, y+1, channel) = plus_truncate(bitmap(x, y+1, channel), (err*5)/16) If x+1 < 256 Then ; 256 = image width bitmap(x-1, y+1, channel) = plus_truncate(bitmap(x-1, y+1, channel), (err*3)/16) End If End If End Function Function quicksort(l,r) Local p,q,h p=l q=r x=colorList((l+r)/2) Repeat While colorList(p)q Then Exit ;SWAP------------------ h=colorList(q) colorList(q)=colorList(p) colorList(p)=h h=colorValue(q) colorValue(q)=colorValue(p) colorValue(p)=h ;---------------------- p=p+1 q=q-1 If q<0 Then Exit Forever If lq Then Exit ;SWAP------------------ h=colorList(q) colorList(q)=colorList(p) colorList(p)=h h=colorValue(q) colorValue(q)=colorValue(p) colorValue(p)=h ;---------------------- p=p+1 q=q-1 If q<0 Then Exit Forever If l -----Download "Don't Flip Out!" Directly to your OUYA! (Click Here).
Posted : Monday, 07 December 2009, 16:34 | Permalink
Nolan

WW Entries : 2
 Looks great except for a few white artifacts near the house.-----nolandc.com
Posted : Monday, 07 December 2009, 17:54 | Permalink
Jayenkai

 Fantastic! Best I've seen
Posted : Wednesday, 09 December 2009, 13:04 | Permalink
spinal

 OK, cleaned the code up a little, added a couple of extra dithering patterns. I'm getting slightly different palettes now, now sure exactly why, perhaps something to do with the sorting. Anyway, the output still looks good and its a bit faster. ; Colour reduction by Spinal Graphics 800,600,0,2 Style = 2 ; 0 = nearest colour ; 1 = floyd stienberg ; 2 = stucki ; 3 = burkes ; 4 = sierra lite picture = LoadImage("test.bmp") Global pwidth = ImageWidth(picture) Global pheight = ImageHeight(picture) Dim pal(256,2) Dim bitmap(pwidth,pheight,2) ;R,G,B for each pixel in the image Dim colorList(32768) ; for counting colours Dim colorValue(32768) ; for the actual colours For t = 0 To 32768 colorValue(t) =t Next DrawBlock picture,0,0 SetBuffer ImageBuffer(picture) For x=0 To pwidth For y=0 To pheight GetColor x,y r=ColorRed() g=ColorGreen() b=ColorBlue() bitmap(x,y,0)=r bitmap(x,y,1)=g bitmap(x,y,2)=b r=r/8 g=g/8 b=b/8 colorList( (r Shl 10)+ (g Shl 5)+ b) = colorList( (r Shl 10)+ (g Shl 5)+ b) + 1 Next Next quicksort(0,32768) ; sort the colour data into assending order p=0 offset = -1 For t = 1 To 32768 If colorList(t) >0 Then If offset=-1 Then offset = t p=p+1 End If Next ; I forced some colours here, I want to make sure there are some greys ; in each palette, it helps the output, also, magenta because I need it sometimes. pal(0,0) = 0 : pal(0,1) = 0 : pal(0,2) = 0 pal(1,0) = 42 : pal(1,1) = 42 : pal(1,2) = 42 pal(2,0) = 85 : pal(2,1) = 85 : pal(2,2) = 85 pal(3,0) = 127 : pal(3,1) = 127 : pal(3,2) = 127 pal(4,0) = 170 : pal(4,1) = 170 : pal(4,2) = 170 pal(5,0) = 213 : pal(5,1) = 213 : pal(5,2) = 213 pal(6,0) = 255 : pal(6,1) = 255 : pal(6,2) = 255 pal(7,0) = 255 : pal(7,1) = 0 : pal(7,2) = 255 If p > 248 Then useDither = 1 x = p/248 For t = 0 To 248 s = offset + (x*t) pal(t+8,0) = ((colorValue(s) Shr 10) And 31)*8 pal(t+8,1) = ((colorValue(s) Shr 5) And 31)*8 pal(t+8,2) = (ColorValue(s) And 31)*8 Next Else useDither = 0 For t = 0 To p s = offset +t pal(t+8,0) = ((colorValue(s) Shr 10) And 31)*8 pal(t+8,1) = ((colorValue(s) Shr 5) And 31)*8 pal(t+8,2) = (ColorValue(s) And 31)*8 Next End If ; sort the palette a little, make it look nice, not actually needed though ; simple bubble sort will do Repeat x=0 For y=0 To 254 br = (pal(y,0)+ pal(y,0)+ pal(y,1)+ pal(y,1)+ pal(y,1)+ pal(y,2))/6 br1 = (pal(y+1,0)+pal(y+1,0)+pal(y+1,1)+pal(y+1,1)+pal(y+1,1)+pal(y+1,2))/6 If br < br1 Then temp0 = pal(y+1,0) temp1 = pal(y+1,1) temp2 = pal(y+1,2) pal(y+1,0) = pal(y,0) pal(y+1,1) = pal(y,1) pal(y+1,2) = pal(y,2) pal(y,0) = temp0 pal(y,1) = temp1 pal(y,2) = temp2 x=1 End If Next Until x=0 SetBuffer FrontBuffer() Color 128,128,128 Rect pwidth + 7,62,129,129,1 t=0 For y = 0 To 15 For x = 0 To 15 Color pal(t,0), pal(t,1), pal(t,2) Rect pwidth + 8 + (x*8), 63 + (y*8),7,7,1 t=t+1 Next Next For x=0 To pwidth For y=0 To pheight col = FindNearestColour(bitmap(x,y,0),bitmap(x,y,1),bitmap(x,y,2)) Color pal(col,0),pal(col,1),pal(col,2) Plot x,y + pheight + 8 If useDither = 1 Then Select style Case 1 compute_disperse(x, y, 0, col) compute_disperse(x, y, 1, col) compute_disperse(x, y, 2, col) Case 2 compute_disperse2(x, y, 0, col) compute_disperse2(x, y, 1, col) compute_disperse2(x, y, 2, col) Case 3 compute_disperse3(x, y, 0, col) compute_disperse3(x, y, 1, col) compute_disperse3(x, y, 2, col) Case 4 compute_disperse4(x, y, 0, col) compute_disperse4(x, y, 1, col) compute_disperse4(x, y, 2, col) End Select End If Next Next While Not KeyHit(1) Delay 2 Wend End Function FindNearestColour(r,g,b) minDistanceSquared = 255*255 + 255*255 + 255*255 + 1 ; furthes possible colour difference For i = 0 To 255 rdiff = r - pal(i,0) gdiff = g - pal(i,1) bdiff = b - pal(i,2) DistanceSquared = rdiff*rdiff + gdiff*gdiff + bdiff*bdiff If DistanceSquared < minDistanceSquared Then minDistanceSquared = DistanceSquared bestIndex = i End If Next Return bestIndex End Function Function quicksort(l,r) Local p,q,h p=l q=r x=colorList((l+r)/2) Repeat While colorList(p)q Then Exit ;SWAP------------------ h=colorList(q) colorList(q)=colorList(p) colorList(p)=h h=colorValue(q) colorValue(q)=colorValue(p) colorValue(p)=h ;---------------------- p=p+1 q=q-1 If q<0 Then Exit Forever If l255 Then number =255 Return number End Function Function compute_disperse(x, y, channel, index) ;Floyd Steinberg ; * 7 ; 3 5 1 (1/16) err = bitmap(x, y, channel) - pal(index, channel) If x+1 < pwidth Then bitmap(x+1, y, channel) = plus_truncate(bitmap(x+1, y, channel), (err*7)/16) End If If y+1 < pheight Then If x-1 > 0 Then bitmap(x-1, y+1, channel) = plus_truncate(bitmap(x-1, y+1, channel), (err*3)/16) End If bitmap(x, y+1, channel) = plus_truncate(bitmap(x, y+1, channel), (err*5)/16) If x+1 < pwidth Then bitmap(x+1, y+1, channel) = plus_truncate(bitmap(x+1, y+1, channel), err/16) End If End If End Function Function compute_disperse2(x, y, channel, index) ;Stucki ; * 8 4 ; 2 4 8 4 2 ; 1 2 4 2 1 (1/42) err = bitmap(x, y, channel) - pal(index, channel) If x+1 < pwidth Then bitmap(x+1, y, channel) = plus_truncate(bitmap(x+1, y, channel), (err*8)/42) If x+2 < pwidth Then bitmap(x+2, y, channel) = plus_truncate(bitmap(x+2, y, channel), (err*4)/42) If y+1 < pheight Then If x-2 > 0 Then bitmap(x-2, y+1, channel) = plus_truncate(bitmap(x-2, y+1, channel), (err*2)/42) If x-1 > 0 Then bitmap(x-1, y+1, channel) = plus_truncate(bitmap(x-1, y+1, channel), (err*4)/42) bitmap(x, y+1, channel) = plus_truncate(bitmap(x, y+1, channel), (err*8)/42) If x+1 < pwidth Then bitmap(x+1, y+1, channel) = plus_truncate(bitmap(x+1, y+1, channel), (err*4)/42) If x+2 < pwidth Then bitmap(x+2, y+1, channel) = plus_truncate(bitmap(x+2, y+1, channel), (err*2)/42) End If If y+2 < pheight Then If x-2 > 0 Then bitmap(x-2, y+2, channel) = plus_truncate(bitmap(x-2, y+2, channel), (err*1)/42) If x-1 > 0 Then bitmap(x-1, y+2, channel) = plus_truncate(bitmap(x-1, y+2, channel), (err*2)/42) bitmap(x, y+2, channel) = plus_truncate(bitmap(x, y+2, channel), (err*4)/42) If x+1 < pwidth Then bitmap(x+1, y+2, channel) = plus_truncate(bitmap(x+1, y+2, channel), (err*2)/42) If x+2 < pwidth Then bitmap(x+2, y+2, channel) = plus_truncate(bitmap(x+2, y+2, channel), (err*1)/42) End If End Function Function compute_disperse3(x, y, channel, index) ;Burkes ; * 8 4 ; 2 4 8 4 2 (1/32) err = bitmap(x, y, channel) - pal(index, channel) If x+1 < pwidth Then bitmap(x+1, y, channel) = plus_truncate(bitmap(x+1, y, channel), (err*8)/32) If x+2 < pwidth Then bitmap(x+2, y, channel) = plus_truncate(bitmap(x+2, y, channel), (err*4)/32) If y+1 < pheight Then If x-2 > 0 Then bitmap(x-2, y+1, channel) = plus_truncate(bitmap(x-2, y+1, channel), (err*2)/32) If x-1 > 0 Then bitmap(x-1, y+1, channel) = plus_truncate(bitmap(x-1, y+1, channel), (err*4)/32) bitmap(x, y+1, channel) = plus_truncate(bitmap(x, y+1, channel), (err*8)/32) If x+1 < pwidth Then bitmap(x+1, y+1, channel) = plus_truncate(bitmap(x+1, y+1, channel), (err*4)/32) If x+2 < pwidth Then bitmap(x+2, y+1, channel) = plus_truncate(bitmap(x+2, y+1, channel), (err*2)/32) End If End Function Function compute_disperse4(x, y, channel, index) ;Sierra - Filter Lite ; * 2 ; 1 1 (1/4) err = bitmap(x, y, channel) - pal(index, channel) If x+1 < pwidth Then bitmap(x+1, y, channel) = plus_truncate(bitmap(x+1, y, channel), (err*2)/4) If y+1 < pheight Then If x-1 > 0 Then bitmap(x-1, y+1, channel) = plus_truncate(bitmap(x-1, y+1, channel), err/4) bitmap(x, y+1, channel) = plus_truncate(bitmap(x, y+1, channel), err/4) End If End Function --> -----Download "Don't Flip Out!" Directly to your OUYA! (Click Here).
Posted : Wednesday, 09 December 2009, 14:06 | Permalink
JL235

WW Entries : 7
 They look awesome. At first I was confused because I couldn't tell if the ones on the right were meant to be the 256 colour versions or the originals. At a glance some of those really do look the same. One small point, some of pallettes seem to contain a bright pink colour even though it's not used in the picture. Is that a bug?-----PlayMyCode.com - build and play in your browser, Blog, Twitter.
Posted : Wednesday, 09 December 2009, 14:39 | Permalink
Afr0

WW Entries : 3
 Diablos: That's probably the default masking colour.-----Afr0 Games Project Dollhouse on Github - Please fork!
Posted : Wednesday, 09 December 2009, 14:47 | Permalink
spinal

 I forced a few grey values and bright pink (255,0,255) into the palette, I need it for a project i'm working on, it doesn't hurt the image at all.-----Download "Don't Flip Out!" Directly to your OUYA! (Click Here).
Posted : Monday, 08 August 2011, 05:06 | Permalink
Jayenkai

WW Entries : 106
 Super useful for rerendering iOS backgrounds for DS Thanks again, Spinal!-----"Load, Next List!"
Posted : Monday, 08 August 2011, 10:25 | Permalink
CodersRule

Posted : Monday, 08 August 2011, 13:37 | Permalink
spinal

 woohoo! Two yeas later and still working
Posted : Tuesday, 09 August 2011, 12:30 | Permalink
rockford

 Has it really been two years! Time flies.
Posted : Tuesday, 09 August 2011, 12:59 | Permalink
Jayenkai

WW Entries : 106
 lol, same thought from me the other day.. "oh, yeah, Spinal made something for that a few months ago... *searches*... How long!!!!?!!"-----"Load, Next List!"

 Page : 1 2
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 549|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
 Latest PostsHurricane Doris vs Shed Roof steve_ancell Wed 20:23 PIP Crap Jayenkai Wed 16:45 What Have You Done? - Feb 2017 rockford Wed 12:23 SSL Options Jayenkai Wed 11:31 AGameAWeek : 2017 - Part One Jayenkai Mon 08:35 Nintendo Switch Indies rychan Mon 02:48 UWP Languages rskgames Sun 05:05 Switchmas rockford Sun 04:24 Happy Birthday, Mrs Rockford rockford Fri 05:30 Pokemon Go Jayenkai Fri 04:46 More

 Latest Items Snippet : Wall Tracing on Random Maps (rpg)Pakz Wed 18:56 Blog : mini projectJayenkai Tue 06:23 Snippet : Path FollowingPakz Mon 16:25 Dev-Diary : PS2 to N64 Adapterspinal Mon 01:53 Snippet : Flowers (Jan 2017)Kuron Thu 01:13 Showcase : Clusters of Hextherevillsgames Mon 15:01 Article : Maths 101 - Episode 1: Basic Trigonometryshroom_monk Sun 14:07 Article : Maths 101 - Episode 5: Line Intersectionshroom_monk Sun 14:02 Dev-Diary : Normals and Dot productsPakz Sat 20:37 Pets : Molly On The CouchJayenkai Thu 04:32 Showcase : Between SpacePakz Sun 12:55 Snippet : Additive Particle FlamesPakz Fri 18:12 Snippet : Maze 2d - Recursive BacktrackerPakz Sun 07:34 Snippet : Bush fire mapsPakz Wed 09:43 Woot : My MusicJayenkai Sat 14:20More

Who's Online
steve_ancell
Wed, at 20:27
rskgames
Wed, at 20:21
Pakz
Wed, at 19:43
Jayenkai
Wed, at 18:27