Simple font resize with blend

but the way is long:
an sdl_surface pixel have 3 colors to blend with nearest

so I have a question:
Is this script too slow?
start<<<
#include
using namespace std;

int main(){

char a[16][10]={
{’ ‘,’ ‘,’ ‘,’ ‘,’#’,’#’,’ ‘,’ ‘,’ ‘,’ ‘},
{’ ‘,’ ‘,’ ‘,’#’,’#’,’#’,’#’,’ ‘,’ ‘,’ ‘},
{’ ‘,’ ‘,’ ‘,’#’,’#’,’#’,’#’,’ ‘,’ ‘,’ ‘},
{’ ‘,’ ‘,’#’,’#’,’#’,’#’,’#’,’#’,’ ‘,’ ‘},
{’ ‘,’ ‘,’#’,’#’,’#’,’#’,’#’,’#’,’ ‘,’ ‘},
{’ ‘,’ ‘,’#’,’#’,’ ‘,’ ‘,’#’,’#’,’ ‘,’ ‘},
{’ ‘,’ ‘,’#’,’#’,’ ‘,’ ‘,’#’,’#’,’ ‘,’ ‘},
{’ ‘,’#’,’#’,’#’,’ ‘,’ ‘,’#’,’#’,’#’,’ ‘},
{’ ‘,’#’,’#’,’#’,’#’,’#’,’#’,’#’,’#’,’ ‘},
{’ ‘,’#’,’#’,’#’,’#’,’#’,’#’,’#’,’#’,’ ‘},
{’ ‘,’#’,’#’,’ ‘,’ ‘,’ ‘,’ ‘,’#’,’#’,’ ‘},
{’#’,’#’,’#’,’ ‘,’ ‘,’ ‘,’ ‘,’#’,’#’,’#’},
{’#’,’#’,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’#’,’#’},
{’#’,’#’,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’#’,’#’},
{’#’,’#’,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’#’,’#’},
{’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’ '},
};
for (int y=0; y<16; y++){

for (int x=0; x<10; x++){

cout << a[y][x]; 

}
cout << ‘\n’;
}

int b[8][5]={{0}};

for (int y=0; y<8; y++){

for (int x=0; x<5; x++){
if (a[y2][x2] == ‘#’)
b[y][x]=10;

if ( x>0 && a[y*2][x*2-1] == '#') { 
  if (b[y][x-1] == 0) 
    b[y][x-1]=1; 
  else 
    b[y][x-1]++; 
} 
if ( x<5 && a[y*2][x*2+1] == '#') { 
  if (b[y][x+1] == 0) 
    b[y][x+1]=1; 
  else 
    b[y][x+1]++; 
} 
if ( y>0 && a[y*2-1][x*2] == '#') { 
  if (b[y-1][x] == 0) 
    b[y-1][x]=1; 
  else 
    b[y-1][x]++; 
} 
if ( y<8 && a[y*2+1][x*2] == '#') { 
  if (b[y+1][x] == 0) 
    b[y+1][x]=1; 
  else 
    b[y+1][x]++; 
} 

}
}

for (int y=0; y<8; y++){
for (int x=0; x<5; x++){
if (b[y][x]>7)
cout << ‘#’;
else if (b[y][x]>1)
cout << ‘+’;
else
cout << ’ ';
}
cout << ‘\n’;
}

}

end
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20030128/425e35c2/attachment.pgp

Well, I guess… I’m not sure what you mean. A pixel in an SDL_surface
(just as in a h/w display buffer) is either 8 bit palletized, or some
form of RGB TrueColor or DirectColor. (The distinction between the
latter is related to gamma correction - let’s ignore that for now.)

In palletized modes, each pixel is just an index into a color look-up
table, so there’s no fixed relation between pixel values and
displayed color.

In TrueColor and DirectColor modes, you have separate fields for red,
green and blue. The sizes of the fields vary. Common formats are
5:5:5, 5:6:5 and 8:8:8. If you don’t want to mess with it, you can
just pick one and have SDL convert for you, but that’ll slow things
down, of course. (If you’re just doing some init time processing and
then convert SDL_DisplayFormat(Alpha)() everything, it’s no big deal,
unless you have lots of data to process.)

Basic additive color blending:
R G B ResultOn Tuesday 28 January 2003 12.00, Riccardo wrote:

but the way is long:
an sdl_surface pixel have 3 colors to blend with nearest

-------------------
0 0 0	black
0 0 1	blue
0 1 0	green
0 1 1	cyan
1 0 0	red
1 0 1	magenta
1 1 0	yellow
1 1 1	white

so I have a question:
Is this script too slow?

That would depend on what you want to do and when. Haven’t read
carefully (I’m “working” now… ;-), but it doesn’t seem lightning
fast, so you’d probably not want to do this kind of stuff in real
time. For generating graphics when starting up the game, anything
goes, basically, as long as it doesn’t take “forever” to start the
game on a minimum spec system. That is, it should be “fast enough” on
the slowest machine the game/application is supposed to run on.
Making it faster than that is a waste of time, that would be better
spent on design and useful features.

Oh well… I have I feeling I’m not making as much sense as I would
like to. :slight_smile: Guess I’m getting tired after working all night. (As
well as implementing a nice reverb DSP effect… Ahem.)

//David Olofson - Programmer, Composer, Open Source Advocate

.- The Return of Audiality! --------------------------------.
| Free/Open Source Audio Engine for use in Games or Studio. |
| RT and off-line synth. Scripting. Sample accurate timing. |
`---------------------------> http://olofson.net/audiality -’
http://olofson.nethttp://www.reologica.se

It’s probably far easier to simply build a font engine that blts letters… but if you want to get crazy:

You might want to try bit manipulation of a one dimensional array of 16 bit integers. Since you’re scanning through the arrays looking for a flag or not, a 1 or 0 bit would do the same. I’d use a Uint16 to keep things simple, and use the remaining 6 bits to hold the resized font info. You should then be able to manipulate the integers with bit opperations and can store the new size in the remaining 6 bits. Create another array that will be your fade affect array, and in it just store the pixels that should be shaded. A simple bit shifting to the right and left ANDed together and then the original XORed (cut out… which ever that is… I always get my opperations confused). You should be left with the halo of the fade, effectively. You can then use a routine to scan the two arrays and fill in the correct colors into your surface… though pixel by pixel is a bit slow I would assume.

0000110000000000 = 0x0C00
0001111000000000 = 0x1E00
0001111000000000 = 0x1E00
0011111100000000 = etc…
0011111100000000
0011001100000000
0011001100000000
0111001110000000
0111111110000000
0111111110000000
0110000110000000
1110000111000000
1100000011000000
1100000011000000
1100000011000000
0000000000000000

Uint16 a[16]={0x0C00,0x1E00,0x1E00,etc…}

Then it’s just a matter of resizing and creating another array for the fade, which rather than doing the logic like you have, can be done faster by simple bit shifting. I’d do this during startup and simply refer to them later, rather than calculating every time.

Again, bmp image blts of letters is probably easier.

  • Joby
    http://jobybednar.com
    Give someone a program - frustrate them for a day.
    Teach them how to program - frustrate them for a lifetime.> ----- Original Message -----

From: t1t0@tiscali.it (Riccardo)
Reply-To: sdl at libsdl.org
Date: 28 Jan 2003 12:00:54 +0100

but the way is long:
an sdl_surface pixel have 3 colors to blend with nearest

so I have a question:
Is this script too slow?
start<<<
#include
using namespace std;

int main(){

char a[16][10]={
{’ ‘,’ ‘,’ ‘,’ ‘,’#’,’#’,’ ‘,’ ‘,’ ‘,’ ‘},
{’ ‘,’ ‘,’ ‘,’#’,’#’,’#’,’#’,’ ‘,’ ‘,’ ‘},
{’ ‘,’ ‘,’ ‘,’#’,’#’,’#’,’#’,’ ‘,’ ‘,’ ‘},
{’ ‘,’ ‘,’#’,’#’,’#’,’#’,’#’,’#’,’ ‘,’ ‘},
{’ ‘,’ ‘,’#’,’#’,’#’,’#’,’#’,’#’,’ ‘,’ ‘},
{’ ‘,’ ‘,’#’,’#’,’ ‘,’ ‘,’#’,’#’,’ ‘,’ ‘},
{’ ‘,’ ‘,’#’,’#’,’ ‘,’ ‘,’#’,’#’,’ ‘,’ ‘},
{’ ‘,’#’,’#’,’#’,’ ‘,’ ‘,’#’,’#’,’#’,’ ‘},
{’ ‘,’#’,’#’,’#’,’#’,’#’,’#’,’#’,’#’,’ ‘},
{’ ‘,’#’,’#’,’#’,’#’,’#’,’#’,’#’,’#’,’ ‘},
{’ ‘,’#’,’#’,’ ‘,’ ‘,’ ‘,’ ‘,’#’,’#’,’ ‘},
{’#’,’#’,’#’,’ ‘,’ ‘,’ ‘,’ ‘,’#’,’#’,’#’},
{’#’,’#’,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’#’,’#’},
{’#’,’#’,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’#’,’#’},
{’#’,’#’,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’#’,’#’},
{’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’ '},
};
for (int y=0; y<16; y++){

for (int x=0; x<10; x++){

cout << a[y][x];
}
cout << ‘\n’;
}

int b[8][5]={{0}};

for (int y=0; y<8; y++){

for (int x=0; x<5; x++){
if (a[y2][x2] == ‘#’)
b[y][x]=10;

if ( x>0 && a[y2][x2-1] == ‘#’) {
if (b[y][x-1] == 0)
b[y][x-1]=1;
else
b[y][x-1]++;
}
if ( x<5 && a[y2][x2+1] == ‘#’) {
if (b[y][x+1] == 0)
b[y][x+1]=1;
else
b[y][x+1]++;
}
if ( y>0 && a[y2-1][x2] == ‘#’) {
if (b[y-1][x] == 0)
b[y-1][x]=1;
else
b[y-1][x]++;
}
if ( y<8 && a[y2+1][x2] == ‘#’) {
if (b[y+1][x] == 0)
b[y+1][x]=1;
else
b[y+1][x]++;
}
}
}

for (int y=0; y<8; y++){
for (int x=0; x<5; x++){
if (b[y][x]>7)
cout << ‘#’;
else if (b[y][x]>1)
cout << ‘+’;
else
cout << ’ ';
}
cout << ‘\n’;
}

}

end

first of all
please for my english
:-(> Message: 24

Date: Tue, 28 Jan 2003 10:19:06 -0800
From: “Joby Bednar”
To:
Subject: Re: [SDL] Simple font resize with blend
Reply-To: sdl at libsdl.org

It’s probably far easier to simply build a font engine that blts letters… but if you want to get crazy:

You might want to try bit manipulation of a one dimensional array of 16 bit integers. Since you’re scanning through the arrays looking for a flag or not, a 1 or 0 bit would do the same. I’d use a Uint16 to keep things simple, and use the remaining 6 bits to hold the resized font info. You should then be able to manipulate the integers with bit opperations and can store the new size in the remaining 6 bits. Create another array that will be your fade affect array, and in it just store the pixels that should be shaded. A simple bit shifting to the right and left ANDed together and then the original XORed (cut out… which ever that is… I always get my opperations confused). You should be left with the halo of the fade, effectively. You can then use a routine to scan the two arrays and fill in the correct colors into your surface… though pixel by pixel is a bit slow I would assume.

0000110000000000 = 0x0C00
0001111000000000 = 0x1E00
0001111000000000 = 0x1E00
0011111100000000 = etc…
0011111100000000
0011001100000000
0011001100000000
0111001110000000
0111111110000000
0111111110000000
0110000110000000
1110000111000000
1100000011000000
1100000011000000
1100000011000000
0000000000000000

Uint16 a[16]={0x0C00,0x1E00,0x1E00,etc…}

This is for reduce the font size?
It’s right, but this is only an example:
I want to resize an SDL_Surface, add a simple ‘blend’ for a better
graphic result.
Using it for this char resize is only an example,
I want add a member function on a Image object that can do it for any
Image (an image object contain a sdl_surface member object)

Having a simple small font with 3 color (#=black +=gray ’ '=trasparent)

########
########

In fact,
the output of this program is
#+
+##

#+

+# +#
#+ #

with 3 color.

But I have to use a similar script for resize an sdl_surface, if it
isn’t too slow.

I can get a determinate pixel of a surface, but how can I get the 3
color of pixel (red, green, blue)?!?

However, the script became slowest because I have to calcolate 3 color
blend for every pixel

Then it’s just a matter of resizing and creating another array for the fade, which rather than doing the logic like you have, can be done faster by simple bit shifting. I’d do this during startup and simply refer to them later, rather than calculating every time.
I don’t understand how i can resize the font using a Uint16…
I have to reconvert 0x0C00 to 0000110000000000, calculating and revert
it to 0x0C00 ?
Again, bmp image blts of letters is probably easier.

  • Joby
    http://jobybednar.com
    Give someone a program - frustrate them for a day.
    Teach them how to program - frustrate them for a lifetime.

-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20030128/3581637c/attachment.pgp

This is for reduce the font size?
It’s right, but this is only an example:
I want to resize an SDL_Surface, add a simple ‘blend’ for a better
graphic result.
Using it for this char resize is only an example,
I want add a member function on a Image object that can do it for any
Image (an image object contain a sdl_surface member object)

Any image? And having your destination rect being a smaller size than your source rect when blitting doesn’t cut it? When I’ve played around with it, as long as you keep the ratios fairly normal between to two rects, it usually looks pretty sharp to me. Different ratios for a reduced size do better than others, so try playing around with that and see if it works for you.

Otherwise, yes, the logic you are using will have to be done for RGB values… you just have to remember that color info is stored in different ways so double check your settings and you’ll have to manipulate the color by seperately adding each color channel and then combining them to your single Uint that stores the color. Something like:

If you have RGBs for pixel 1, and RGB for pixel 2… then…

Uint24 R = (R1>>1)+(R2>>1); //bit shift x right 1 == x/2
Uint24 G = (G1>>1)+(R2>>1);
Uint24 B = (B1>>1)+(R2>>1);

RGB = (R<<16)&(G<<8)&(B); //if color is in 24 bit 0x®(G)(B) format

I’d just play around with it and make sure you manipulate the right bits with the right formats, but that should do it… or something like that.

  • Joby
    http://jobybednar.com
    Give someone a program - frustrate them for a day.
    Teach them how to program - frustrate them for a lifetime.

thank for your patience

Any image? And having your destination rect being a smaller size than your source rect when blitting doesn’t cut it?

I will get the right ratio for soure image resizing…

When I’ve played around with it, as long as you keep the ratios fairly normal between to two rects, it usually looks pretty sharp to me. Different ratios for a reduced size do better than others, so try playing around with that and see if it works for you.

Otherwise, yes, the logic you are using will have to be done for RGB values… you just have to remember that color info is stored in different ways so double check your settings and you’ll have to manipulate the color by seperately adding each color channel and then combining them to your single Uint that stores the color. Something like:

If you have RGBs for pixel 1, and RGB for pixel 2… then…

Uint24 R = (R1>>1)+(R2>>1); //bit shift x right 1 == x/2
Uint24 G = (G1>>1)+(R2>>1);
Uint24 B = (B1>>1)+(R2>>1);

RGB = (R<<16)&(G<<8)&(B); //if color is in 24 bit 0x®(G)(B) format

An Uint24 is equal to html color code, is right?
It have 8 bit for each color (#ffffff is 255 red, 255 green and 255
blue, and with 8 bit i have 256 char…)

well,
but how can I work with a Uint24?

If I have a Uint24 pixel
is this a right example for a red color?
111111110000000000000000
24 bit
8 for red,
8 for greeen and
8 for blue?

then

Uint24 pixel = the pixel that i have

Uint8 Red = pixel<<16; // I can’t use an Uint8? a single color have only
8 bits? with this shift is Red==000000000000000011111111 ?

I understand?

I’d just play around with it and make sure you manipulate the right bits with the right formats, but that should do it… or something like that.

  • Joby
    http://jobybednar.com
    Give someone a program - frustrate them for a day.
    Teach them how to program - frustrate them for a lifetime.

-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20030129/aa242c8e/attachment.pgp

If you have RGBs for pixel 1, and RGB for pixel 2… then…

Uint24 R = (R1>>1)+(R2>>1); //bit shift x right 1 == x/2
Uint24 G = (G1>>1)+(R2>>1);
Uint24 B = (B1>>1)+(R2>>1);

RGB = (R<<16)&(G<<8)&(B); //if color is in 24 bit 0x®(G)(B) format

An Uint24 is equal to html color code, is right?
It have 8 bit for each color (#ffffff is 255 red, 255 green and 255
blue, and with 8 bit i have 256 char…)

I’m assuming it is. When working with Color Keys the color format for a 24bit BMP was in RGB format like HTML, so I could do “0xRRGGBB” to knock out the background color when blitting.

well,
but how can I work with a Uint24?

If I have a Uint24 pixel
is this a right example for a red color?
111111110000000000000000
24 bit
8 for red,
8 for greeen and
8 for blue?

then

Uint24 pixel = the pixel that i have

Uint8 Red = pixel<<16; // I can’t use an Uint8? a single color have only
8 bits? with this shift is Red==000000000000000011111111 ?

I understand?

You would think so, but all the variables need to be the same type. If you bit shift an 8 bit number over 16 bits you’ll be left with 00000000 since the data will be stored in 8 bits when its all said and done. What you want to do is store the 8 bits of info within 24 bit blocks. To get the RGB values from Uint24 C == 0xFF8844 for example:

Uint24 R = C>>16; //R=00000000 00000000 11111111;
Uint24 G = (C<<8)>>16; //G=00000000 00000000 10001000;
Uint24 B = (C<<16)>>16; //B=00000000 00000000 01000100;

Then when you put them back together, after you found the new color you need to fill with, you just shift them over again and bit AND them together:

R<<16 == 11111111 00000000 00000000;
G<<8 == 00000000 10001000 00000000;
B == 00000000 00000000 01000100;

New C = (R<<16) & (G<<8) & B; //C=11111111 10001000 01000100

If you tried to bit AND a 24 bit and an 8 bit, theoretically you are trying to do:

00000000 00000000 11111111
& 11111111

Which won’t work too well. Hopefully that helps… and hopefully I got it right. I personally wouldn’t trust me, so try it out and see what happens. :wink:

  • Joby
    http://jobybednar.com
    Give someone a program - frustrate them for a day.
    Teach them how to program - frustrate them for a lifetime.

Message: 14

Looking at it again, I’m sorry to say that I got my bit manipulation mixed up… the opperations should be OR (|) rather than AND (&) when putting it all back together. Ooops, sorry:

New C = (R<<16) | (G<<8) | B;

In your example, I could be wrong, but I thought I read somewhere that SDL.h should be the first include. In any event it looks like it thinks Uint24 is an undeclared function. I’m pretty sure Uint24 is a valid type. Either it isn’t and you need to try Uint32, or your SDL library isn’t loading properly.

I had fill #include <SDL.h> on a first line, but
a.c: In function int main()': a.c:7:Uint24’ undeclared (first use this function)
a.c:7: (Each undeclared identifier is reported only once for each
function it appears in.)
a.c:7: parse error before =' token a.c:10:color’ undeclared (first use this function)

If I use an Uint32,
no errors were found,
but how i can use a Uint32 color?

ps:
i didn’t find any “Uint24” word on the english SDL guide:
[dick at t1t0 html]$ cat find . | grep Uint24
cat: .: Is a directory
[dick at t1t0 html]$

if you try with Uint32,
a lot of rows was find.> Date: Thu, 30 Jan 2003 17:36:37 -0800

From: “Joby Bednar”
To:
Subject: Re: [SDL] Re: Simple font resize with blend : Uint24 doesn’t work.
Reply-To: sdl at libsdl.org

  • Joby
    http://jobybednar.com
    Give someone a program - frustrate them for a day.
    Teach them how to program - frustrate them for a lifetime.

Message: 15
Date: Thu, 30 Jan 2003 20:39:36 -0500
From: Glenn Maynard <g_sdl at zewt.org>
To: sdl at libsdl.org
Subject: Re: [SDL] Re: Simple font resize with blend : Uint24 doesn’t work.
Reply-To: sdl at libsdl.org

On Fri, Jan 31, 2003 at 02:13:35AM +0100, Riccardo wrote:

a.c: In function int main()': a.c:6:Uint24’ undeclared (first use this function)
a.c:6: (Each undeclared identifier is reported only once for each
function it appears in.)
a.c:6: parse error before =' token a.c:9:color’ undeclared (first use this function)

There’s no such thing as “Uint24”. I know of no modern system with a
native 24-bit type. If you want 24 bits, use Uint32 and ignore the high
byte.

what is the hight byte?
the 8 first or 8 last bits?

however,
how the color format is stored in a Uint32


Glenn Maynard
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20030131/b782da65/attachment.pgp

but how i can use a Uint32 color?

look in the SDL docs for more info about pixel formats for surfaces and such. There’s a number of ways to access them and manipulate them. In 32bit use of 24bit colors, the first 8 bits are unused and the remaining follow RGB format. Read the docs and go through the header files and look at the prototypes… you should get some ideas.

  • Joby
    http://jobybednar.com
    Give someone a program - frustrate them for a day.
    Teach them how to program - frustrate them for a lifetime.

well,
I’ll do that,

but i can’t find a Uint24 format on the sdl reference

thank for your time,
you teach me how use the << >> operators
i didn’t know before them> Message: 1

Date: Mon, 3 Feb 2003 12:10:08 -0800
From: “Joby Bednar”
To:
Subject: Re: [SDL] Re: Simple font resize with blend
Reply-To: sdl at libsdl.org

but how i can use a Uint32 color?

look in the SDL docs for more info about pixel formats for surfaces and such. There’s a number of ways to access them and manipulate them. In 32bit use of 24bit colors, the first 8 bits are unused and the remaining follow RGB format. Read the docs and go through the header files and look at the prototypes… you should get some ideas.

I already told you that there’s no such thing as “Uint24” …

24-bit surface formats have BytesPerPixel == 3, but you can’t reference
them like a regular type, they have to be unpacked.On Tue, Feb 04, 2003 at 11:57:36PM +0100, Riccardo wrote:

well,
I’ll do that,

but i can’t find a Uint24 format on the sdl reference


Glenn Maynard