From de4deb34003317cb697ef43c8faceffce8be91c6 Mon Sep 17 00:00:00 2001 From: Jay Sorg Date: Fri, 12 Jun 2015 01:25:46 -0700 Subject: early work --- include/painter.h | 2 +- src/painter.c | 25 ++-------- src/painter_utils.c | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/painter_utils.h | 65 ++++++++++++++++++++++++++ 4 files changed, 198 insertions(+), 23 deletions(-) create mode 100644 src/painter_utils.c create mode 100644 src/painter_utils.h diff --git a/include/painter.h b/include/painter.h index 301b87c..8cdf32a 100644 --- a/include/painter.h +++ b/include/painter.h @@ -16,7 +16,7 @@ * limitations under the License. */ -#ifndef __PAINTER_H +#if !defined(__PAINTER_H) #define __PAINTER_H #define PT_FORMAT_a8b8g8r8 \ diff --git a/src/painter.c b/src/painter.c index 88a698d..1a82144 100644 --- a/src/painter.c +++ b/src/painter.c @@ -1,5 +1,5 @@ /** - * painter main header + * painter main * * Copyright 2015 Jay Sorg * @@ -21,26 +21,7 @@ #include #include "painter.h" - -struct painter_rect -{ - short x1; - short y1; - short x2; - short y2; -}; - -struct painter -{ - int rop; - int fgcolor; - int bgcolor; - int fill_mode; - int clip_valid; - struct painter_rect clip; - int origin_x; - int origin_y; -}; +#include "painter_utils.h" /*****************************************************************************/ int @@ -60,7 +41,7 @@ painter_create(void **handle) memset(*handle, 0, sizeof(struct painter)); pt = (struct painter *) *handle; - pt->rop = PT_ROP_COPY; + pt->rop = PT_ROP_S; return PT_ERROR_NONE; } diff --git a/src/painter_utils.c b/src/painter_utils.c new file mode 100644 index 0000000..2893cda --- /dev/null +++ b/src/painter_utils.c @@ -0,0 +1,129 @@ +/** + * painter utils + * + * Copyright 2015 Jay Sorg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include "painter.h" +#include "painter_utils.h" + +/*****************************************************************************/ +/* do a raster op */ +int +do_rop(int rop, int src, int dst) +{ + switch (rop) + { + case PT_ROP_0: return 0; + case PT_ROP_DSon: return ~(src | dst); + case PT_ROP_DSna: return (~src) & dst; + case PT_ROP_Sn: return ~src; + case PT_ROP_SDna: return src & (~dst); + case PT_ROP_Dn: return ~(dst); + case PT_ROP_DSx: return src ^ dst; + case PT_ROP_DSan: return ~(src & dst); + case PT_ROP_DSa: return src & dst; + case PT_ROP_DSxn: return ~(src) ^ dst; + case PT_ROP_D: return dst; + case PT_ROP_DSno: return (~src) | dst; + case PT_ROP_S: return src; + case PT_ROP_SDno: return src | (~dst); + case PT_ROP_DSo: return src | dst; + case PT_ROP_1: return ~0; + } + return dst; +} + +/*****************************************************************************/ +char * +bitmap_get_ptr(struct painter_bitmap *bitmap, int x, int y) +{ + char *p; + int Bpp; + + if ((x >= 0) && (x < bitmap->width) && + (y >= 0) && (y < bitmap->height)) + { + Bpp = ((bitmap->format >> 24) + 7) / 8; + p = bitmap->data + (y * bitmap->stride_bytes) + (x * Bpp); + return p; + } + else + { + return NULL; + } +} + +/*****************************************************************************/ +int +pixel_convert(int pixel, int src_format, int dst_format, int *palette) +{ + int a; + int r; + int g; + int b; + int rv; + + if (src_format == dst_format) + { + return pixel; + } + switch (src_format) + { + case PT_FORMAT_a1r5g5b5: + SPLIT_a1r5g5b5(pixel, a, r, g, b); + break; + case PT_FORMAT_r5g6b5: + SPLIT_r5g6b5(pixel, a, r, g, b); + break; + } + rv = 0; + switch (dst_format) + { + case PT_FORMAT_a8r8g8b8: + rv = (a << 24) | (r << 16) | (g << 8) | b; + break; + } + return rv; +} + +/*****************************************************************************/ +int +painter_set_pixel(struct painter *painter, struct painter_bitmap *dst, + int x, int y, int pixel, int pixel_format) +{ + if (painter->clip_valid == 0 || + (x >= painter->clip.x1 && x < painter->clip.x2 && + y >= painter->clip.y1 && y < painter->clip.y2)) + { + if ((x >= 0) && (x < dst->width) && + (y >= 0) && (y < dst->height)) + { + pixel = pixel_convert(pixel, pixel_format, dst->format, + painter->palete); + if (rop != PT_ROP_S) + { + pixel = do_rop(painter->rop, pixel, + bitmap_get_pixel(dst, x, y)); + } + bitmap_set_pixel(dst, x, y, pixel); + } + } +} + diff --git a/src/painter_utils.h b/src/painter_utils.h new file mode 100644 index 0000000..27961cf --- /dev/null +++ b/src/painter_utils.h @@ -0,0 +1,65 @@ +/** + * painter utils header + * + * Copyright 2015 Jay Sorg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#if !defined(__PAINTER_UTILS_H) +#define __PAINTER_UTILS_H + +#define SPLIT_a1r5g5b5(c, a, r, g, b) \ +do { \ + a = (c & 0x8000) ? 0xff : 0; \ + r = ((c >> 7) & 0xf8) | ((c >> 12) & 0x7); \ + g = ((c >> 2) & 0xf8) | ((c >> 8) & 0x7); \ + b = ((c << 3) & 0xf8) | ((c >> 2) & 0x7); \ +} while (0) + +#define SPLIT_r5g6b5(c, a, r, g, b) \ +do { \ + a = 0xff; \ + r = ((c >> 8) & 0xf8) | ((c >> 13) & 0x7); \ + g = ((c >> 3) & 0xfc) | ((c >> 9) & 0x3); \ + b = ((c << 3) & 0xf8) | ((c >> 2) & 0x7); \ +} while (0) + +struct painter_rect +{ + short x1; + short y1; + short x2; + short y2; +}; + +struct painter +{ + int rop; + int fgcolor; + int bgcolor; + int fill_mode; + int clip_valid; + struct painter_rect clip; + int origin_x; + int origin_y; + int palette[256]; +}; + +int +painter_rop(int rop, int src, int dst); +int +painter_set_pixel(struct painter *painter, struct painter_bitmap *dst, + int x, int y, int pixel); + +#endif -- cgit v1.2.3