/* * This file is part of firk's window manager library (libfwm) * Copyright (C) 2023 firk * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include #include #include #include #include #include #include #include #include #include #include #include #define LIBFWM_INTERNAL #include "libfwm.h" static void putU16LE(unsigned char *dst, unsigned short v) { dst[0] = v; dst[1] = (v >> 8); } static void putU16BE(unsigned char *dst, unsigned short v) { dst[0] = (v >> 8); dst[1] = v; } static void putU24LE(unsigned char *dst, unsigned long v) { dst[0] = v; dst[1] = (v >> 8); dst[2] = (v >> 16); } static void putU24BE(unsigned char *dst, unsigned long v) { dst[0] = (v >> 16); dst[1] = (v >> 8); dst[2] = v; } static void putU32LE(unsigned char *dst, unsigned long v) { dst[0] = v; dst[1] = (v >> 8); dst[2] = (v >> 16); dst[3] = (v >> 24); } static void putU32BE(unsigned char *dst, unsigned long v) { dst[0] = (v >> 24); dst[1] = (v >> 16); dst[2] = (v >> 8); dst[3] = v; } static void fill_imgdata_1LE(unsigned char *dst, unsigned char const *src, unsigned short w, unsigned short h, unsigned short bytes_per_line) { unsigned int x, y; unsigned char c1, c2; unsigned char *d; for(y=0; y> 4]; src++; *d = (c1 & 1); x++; if(x==w) break; *d |= (c2 << 1) & 1; x++; if(x==w) break; c1 = colors[src[0] & 15]; c2 = colors[src[0] >> 4]; src++; *d |= (c1 << 2) & 1; x++; if(x==w) break; *d |= (c2 << 3) & 1; x++; c1 = colors[src[0] & 15]; c2 = colors[src[0] >> 4]; src++; *d |= (c1 << 4) & 1; x++; if(x==w) break; *d |= (c2 << 5) & 1; x++; if(x==w) break; c1 = colors[src[0] & 15]; c2 = colors[src[0] >> 4]; src++; *d |= (c1 << 6) & 1; x++; if(x==w) break; *d |= (c2 << 7); x++; d++; } dst += bytes_per_line; } } static void fill_imgdata_1BE(unsigned char *dst, unsigned char const *src, unsigned short w, unsigned short h, unsigned short bytes_per_line) { unsigned int x, y; unsigned char c1, c2; unsigned char *d; for(y=0; y> 4]; src++; *d = (c1 << 7); x++; if(x==w) break; *d |= (c2 << 6) & 1; x++; if(x==w) break; c1 = colors[src[0] & 15]; c2 = colors[src[0] >> 4]; src++; *d |= (c1 << 5) & 1; x++; if(x==w) break; *d |= (c2 << 4) & 1; x++; c1 = colors[src[0] & 15]; c2 = colors[src[0] >> 4]; src++; *d |= (c1 << 3) & 1; x++; if(x==w) break; *d |= (c2 << 2) & 1; x++; if(x==w) break; c1 = colors[src[0] & 15]; c2 = colors[src[0] >> 4]; src++; *d |= (c1 << 1) & 1; x++; if(x==w) break; *d |= (c2 & 1); x++; d++; } dst += bytes_per_line; } } static void fill_imgdata_2LE(unsigned char *dst, unsigned char const *src, unsigned short w, unsigned short h, unsigned short bytes_per_line) { unsigned int x, y; unsigned char c1, c2; unsigned char *d; for(y=0; y> 4]; src++; *d = (c1 & 3); x++; if(x==w) break; *d |= (c2 << 2) & 3; x++; if(x==w) break; c1 = colors[src[0] & 15]; c2 = colors[src[0] >> 4]; src++; *d |= (c1 << 4) & 3; x++; if(x==w) break; *d |= (c2 << 6); x++; d++; } dst += bytes_per_line; } } static void fill_imgdata_2BE(unsigned char *dst, unsigned char const *src, unsigned short w, unsigned short h, unsigned short bytes_per_line) { unsigned int x, y; unsigned char c1, c2; unsigned char *d; for(y=0; y> 4]; src++; *d = (c1 << 6); x++; if(x==w) break; *d |= (c2 << 4) & 3; x++; if(x==w) break; c1 = colors[src[0] & 15]; c2 = colors[src[0] >> 4]; src++; *d |= (c1 << 2) & 3; x++; if(x==w) break; *d |= (c2 & 3); x++; d++; } dst += bytes_per_line; } } static void fill_imgdata_4LE(unsigned char *dst, unsigned char const *src, unsigned short w, unsigned short h, unsigned short bytes_per_line) { unsigned int x, y; unsigned char c1, c2; unsigned char *d; for(y=0; y> 4]; src++; *d = (c1 & 15); x++; if(x==w) break; *d |= (c2 << 4); x++; d++; } dst += bytes_per_line; } } static void fill_imgdata_4BE(unsigned char *dst, unsigned char const *src, unsigned short w, unsigned short h, unsigned short bytes_per_line) { unsigned int x, y; unsigned char c1, c2; unsigned char *d; for(y=0; y> 4]; src++; *d = (c1 << 4); x++; if(x==w) break; *d |= (c2 & 15); x++; d++; } dst += bytes_per_line; } } static void fill_imgdata_8(unsigned char *dst, unsigned char const *src, unsigned short w, unsigned short h, unsigned short bytes_per_line) { unsigned int x, y; unsigned char c1, c2; unsigned char *d; for(y=0; y> 4]; src++; *(d++) = c1; x++; if(x==w) break; *(d++) = c2; x++; } dst += bytes_per_line; } } static void fill_imgdata_16LE(unsigned char *dst, unsigned char const *src, unsigned short w, unsigned short h, unsigned short bytes_per_line) { unsigned int x, y; unsigned short c1, c2; unsigned char *d; for(y=0; y> 4]; src++; putU16LE(d,c1); x++; d+=2; if(x==w) break; putU16LE(d,c2); x++; d+=2; } dst += bytes_per_line; } } static void fill_imgdata_16BE(unsigned char *dst, unsigned char const *src, unsigned short w, unsigned short h, unsigned short bytes_per_line) { unsigned int x, y; unsigned short c1, c2; unsigned char *d; for(y=0; y> 4]; src++; putU16BE(d,c1); x++; d+=2; if(x==w) break; putU16BE(d,c2); x++; d+=2; } dst += bytes_per_line; } } static void fill_imgdata_24LE(unsigned char *dst, unsigned char const *src, unsigned short w, unsigned short h, unsigned short bytes_per_line) { unsigned int x, y; unsigned long c1, c2; unsigned char *d; for(y=0; y> 4]; src++; putU24LE(d,c1); x++; d+=3; if(x==w) break; putU24LE(d,c2); x++; d+=3; } dst += bytes_per_line; } } static void fill_imgdata_24BE(unsigned char *dst, unsigned char const *src, unsigned short w, unsigned short h, unsigned short bytes_per_line) { unsigned int x, y; unsigned long c1, c2; unsigned char *d; for(y=0; y> 4]; src++; putU24BE(d,c1); x++; d+=3; if(x==w) break; putU24BE(d,c2); x++; d+=3; } dst += bytes_per_line; } } static void fill_imgdata_32LE(unsigned char *dst, unsigned char const *src, unsigned short w, unsigned short h, unsigned short bytes_per_line) { unsigned int x, y; unsigned long c1, c2; unsigned char *d; for(y=0; y> 4]; src++; putU32LE(d,c1); x++; d+=4; if(x==w) break; putU32LE(d,c2); x++; d+=4; } dst += bytes_per_line; } } static void fill_imgdata_32BE(unsigned char *dst, unsigned char const *src, unsigned short w, unsigned short h, unsigned short bytes_per_line) { unsigned int x, y; unsigned long c1, c2; unsigned char *d; for(y=0; y> 4]; src++; putU32BE(d,c1); x++; d+=4; if(x==w) break; putU32BE(d,c2); x++; d+=4; } dst += bytes_per_line; } } static int fill_imgdata(unsigned char *dst, unsigned char const *src, unsigned short w, unsigned short h, unsigned short bits_per_pixel, unsigned short bytes_per_line, int bit_order, int byte_order) { if(bits_per_pixel<1 || bits_per_pixel>32) return -1; if(byte_order==MSBFirst && bits_per_pixel>8) bits_per_pixel += 5; if(bit_order==MSBFirst && bits_per_pixel<8) bits_per_pixel += 5; switch(bits_per_pixel) { case 1: fill_imgdata_1LE(dst, src, w, h, bytes_per_line); return 0; case 5+ 1: fill_imgdata_1BE(dst, src, w, h, bytes_per_line); return 0; case 2: fill_imgdata_2LE(dst, src, w, h, bytes_per_line); return 0; case 5+ 2: fill_imgdata_2BE(dst, src, w, h, bytes_per_line); return 0; case 4: fill_imgdata_4LE(dst, src, w, h, bytes_per_line); return 0; case 5+ 4: fill_imgdata_4BE(dst, src, w, h, bytes_per_line); return 0; case 8: fill_imgdata_8(dst, src, w, h, bytes_per_line); return 0; case 16: fill_imgdata_16LE(dst, src, w, h, bytes_per_line); return 0; case 5+16: fill_imgdata_16BE(dst, src, w, h, bytes_per_line); return 0; case 24: fill_imgdata_24LE(dst, src, w, h, bytes_per_line); return 0; case 5+24: fill_imgdata_24BE(dst, src, w, h, bytes_per_line); return 0; case 32: fill_imgdata_32LE(dst, src, w, h, bytes_per_line); return 0; case 5+32: fill_imgdata_32BE(dst, src, w, h, bytes_per_line); return 0; } return -1; } extern Pixmap LIBFWM_CreateIconPixmap(unsigned char const *data, unsigned short w, unsigned short h) { Pixmap p; XImage *img; GC gc; unsigned int depth, bpl; unsigned char *idata; size_t sz; if(!w || !h) return 0; depth = DefaultDepthOfScreen(screen); p = XCreatePixmap(display, rootwin, w, h, depth); if(!(img = XCreateImage(display, NULL, depth, ZPixmap, 0, NULL, w, h, 8, 0))) goto err0; bpl = img->bytes_per_line; sz = bpl*h; if(sz/bpl!=h || !(idata = malloc(sz))) goto err1; if(fill_imgdata(idata, data, w, h, img->bits_per_pixel, img->bytes_per_line, img->bitmap_bit_order, img->byte_order)<0) goto err2; gc = XCreateGC(display, p, 0, NULL); img->data = (char*)idata; XPutImage(display, p, gc, img, 0, 0, 0, 0, w, h); img->data = NULL; XFreeGC(display, gc); free(idata); XDestroyImage(img); return p; err2: free(idata); err1: XDestroyImage(img); err0: XFreePixmap(display, p); return 0; }