Re: AW: AW: Implementing PseudoColor support on TrueColor screens

2020-06-02 Thread Sleep

This should be slightly easier to read:

Display*X_display = NULL;
Window  X_mainWindow;
ColormapX_cmap;
Visual* X_visual;
GC  X_gc;
int X_screen;
XVisualInfo X_visualinfo;
XImage* image = NULL;
int X_width;
int X_height;

unsigned long st2d_8to24table[256];
int32_t  shiftmask_fl = 0;
int32_t  r_shift, g_shift, b_shift;
uint32_t r_mask, g_mask, b_mask;

//
// I_InitGraphics
//
// Creates and maps window
void I_InitGraphics (void)
{
char* displayname = NULL;

X_width = SCREENWIDTH;
X_height = SCREENHEIGHT;

// open the display
X_display = XOpenDisplay(displayname);

// use the default visual
X_screen = DefaultScreen(X_display);
XMatchVisualInfo(X_display, X_screen, 24, TrueColor, _visualinfo);
X_visual = X_visualinfo.visual;

// create the main window
X_mainWindow = XCreateWindow(X_display,
 RootWindow(X_display, X_screen),
 0, 0,
 X_width, X_height,
 0, // borderwidth
 X_visualinfo.depth, // depth
 InputOutput,
 X_visual,
 0,
 0);

// create the GC
X_gc = XCreateGC(X_display,
 X_mainWindow,
 0,
 NULL);

// map the window
XMapWindow(X_display, X_mainWindow);

image = XCreateImage(X_display,
 X_visual,
 X_visualinfo.depth,
 ZPixmap,
 0,
 malloc((X_width * 4) * X_height),
 X_width, X_height,
 32,
 0);

screens[0] = (byte *)image->data;
}

// Convert colors to 24 bits, called by I_FinishUpdate()
void st3_fixup (int width, int height)
{
uint8_t *row_src;

for (int y = 0; y < height; y++) {
row_src = (uint8_t *)>data [y * image->bytes_per_line];
for (int x = width-1; x >= 0; x--) {
XPutPixel(image, x, y, st2d_8to24table[row_src[x]]);
}
}
}

// Called by xlib_rgb24()
void shiftmask_init (void)
{
unsigned int x;

r_mask = X_visual->red_mask;
g_mask = X_visual->green_mask;
b_mask = X_visual->blue_mask;

for (r_shift = -8, x = 1; x < r_mask; x = x << 1)
r_shift++;

for (g_shift = -8, x = 1; x < g_mask; x = x << 1)
g_shift++;

for (b_shift = -8, x = 1; x < b_mask; x = x << 1)
b_shift++;

shiftmask_fl = 1;
}

// Called by UploadNewPalette()
unsigned long xlib_rgb24 (int r, int g, int b)
{
unsigned long p;

if (shiftmask_fl == 0)
shiftmask_init();

p = 0;

if (r_shift > 0) {
p = (r << (r_shift)) & r_mask;
} else if (r_shift < 0) {
p = (r >> (-r_shift)) & r_mask;
} else p |= (r & r_mask);

if (g_shift > 0) {
p |= (g << (g_shift)) & g_mask;
} else if (g_shift < 0) {
p |= (g >> (-g_shift)) & g_mask;
} else p |= (g & g_mask);

if (b_shift > 0) {
p |= (b << (b_shift)) & b_mask;
} else if (b_shift < 0) {
p |=(b >> (-b_shift)) & b_mask;
} else p |= (b & b_mask);

return p;
}

//
// Palette stuff.
//
void UploadNewPalette (Colormap cmap, byte *palette)
{
(void)cmap; // UNUSED
for (int i = 0; i < 256; i++)
{
st2d_8to24table[i] = xlib_rgb24(palette[i * 3], palette[i * 3 + 
1],palette[i * 3 + 2]);

}
}

//
// I_SetPalette
//
void I_SetPalette (byte* palette)
{
UploadNewPalette(X_cmap, palette);
}

//
// I_FinishUpdate
//
// Display the image on the window
void I_FinishUpdate (void)
{
// Convert colors of the image to 24 bits color depth
st3_fixup(X_width, X_height);

// draw the image
XPutImage(X_display,
  X_mainWindow,
  X_gc,
  image,
  0, 0,
  0, 0,
  X_width, X_height );

// sync up with server
XSync(X_display, False);
}

___
xorg@lists.x.org: X.Org support
Archives: http://lists.freedesktop.org/archives/xorg
Info: https://lists.x.org/mailman/listinfo/xorg
Your subscription address: %(user_address)s


Re: AW: AW: Implementing PseudoColor support on TrueColor screens

2020-06-02 Thread Sleep
No, that is not intentional, the original function had x = width-1 and I 
forgot to change that for clarity, and yes, the colors are converted 
perfectly, by "squashed" I mean what is happening in this image (that is 
Doom).

https://postimg.cc/MfRz0Dk7
You see, only 1/4 of the window is being filled, instead of drawing on 
the entire window; I just thought the problem was in that function but 
that seems to be a problem unrelated to the colors. Here is the (super 
messy) original code: 
https://github.com/so-sleepy/DOOM/blob/bb4382c111ff0a83e52dd7e043884bf41c89daa0/linuxdoom-1.10/i_video.c#L769


I_InitGraphics() creates the window, I_FinishUpdate() draws the image on 
the window (you can ignore all if statements with 'multiply' inside, 
those are used to scale), UploadNewPalette() takes the game's palette 
and use that to build the lookup table.


> To make things more easy to debug, can you save image->data
> where you know what who that should look like ? then you can play
> with a simple display code instead of that.

Thank you, I will do exactly that! I will try my best to narrow down the 
issue.

___
xorg@lists.x.org: X.Org support
Archives: http://lists.freedesktop.org/archives/xorg
Info: https://lists.x.org/mailman/listinfo/xorg
Your subscription address: %(user_address)s


AW: AW: Implementing PseudoColor support on TrueColor screens

2020-06-02 Thread Walter Harms
a assume this is intentional backwards ?
for (int x = width-1; x >= 0; x--) {

if st2d_8to24table contains no rubbish the colors should be ok now, are they ?

what means "squashed image" ? a scaling problem ?

To make things more easy to debug, can you save image->data
where you know what who that should look like ? then you can play
with a simple display code instead of that.

re,
 wh

Von: xorg  im Auftrag von Sleep 

Gesendet: Dienstag, 2. Juni 2020 16:09:20
An: xorg@lists.x.org
Betreff: Re: AW: Implementing PseudoColor support on TrueColor screens

Thank you very much, Walter. st3_fixup() (adapted for my use) is a lot
more readable now:

void st3_fixup(XImage *image, int width, int height)
{
 uint8_t *row_src;

 for (int y = 0; y < height; y++) {
 row_src = (uint8_t *)>data [y * image->bytes_per_line];
 for (int x = width-1; x >= 0; x--) {
 XPutPixel(image, x, y, st2d_8to24table[row_src[x]]);
 }
 }
}

The issue I am having persists, just letting you know that you have
helped me on something, again, thank you a lot.
___
xorg@lists.x.org: X.Org support
Archives: http://lists.freedesktop.org/archives/xorg
Info: https://lists.x.org/mailman/listinfo/xorg
Your subscription address: %(user_address)s
___
xorg@lists.x.org: X.Org support
Archives: http://lists.freedesktop.org/archives/xorg
Info: https://lists.x.org/mailman/listinfo/xorg
Your subscription address: %(user_address)s