| 1 | /* |
|---|
| 2 | * libpipi Proper image processing implementation library |
|---|
| 3 | * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org> |
|---|
| 4 | * 2008 Jean-Yves Lamoureux <jylam@lnxscene.org |
|---|
| 5 | * All Rights Reserved |
|---|
| 6 | * |
|---|
| 7 | * $Id$ |
|---|
| 8 | * |
|---|
| 9 | * This library is free software. It comes without any warranty, to |
|---|
| 10 | * the extent permitted by applicable law. You can redistribute it |
|---|
| 11 | * and/or modify it under the terms of the Do What The Fuck You Want |
|---|
| 12 | * To Public License, Version 2, as published by Sam Hocevar. See |
|---|
| 13 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | /* |
|---|
| 17 | * line.c: line rendering functions |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #include "config.h" |
|---|
| 21 | #include "common.h" |
|---|
| 22 | |
|---|
| 23 | #include <stdio.h> |
|---|
| 24 | #include <stdlib.h> |
|---|
| 25 | #include <string.h> |
|---|
| 26 | |
|---|
| 27 | #include "pipi.h" |
|---|
| 28 | #include "pipi_internals.h" |
|---|
| 29 | |
|---|
| 30 | struct line |
|---|
| 31 | { |
|---|
| 32 | int x1, y1; |
|---|
| 33 | int x2, y2; |
|---|
| 34 | void (*draw) (pipi_image_t*, struct line*); |
|---|
| 35 | union { |
|---|
| 36 | uint32_t color32; |
|---|
| 37 | float colorf[3]; |
|---|
| 38 | }; |
|---|
| 39 | |
|---|
| 40 | union { |
|---|
| 41 | uint32_t *buf_u32; |
|---|
| 42 | float *buf_f; |
|---|
| 43 | }; |
|---|
| 44 | |
|---|
| 45 | }; |
|---|
| 46 | static void clip_line(pipi_image_t*, struct line*); |
|---|
| 47 | static uint8_t clip_bits(pipi_image_t*, int, int); |
|---|
| 48 | static void draw_aliased_line_u32(pipi_image_t*, struct line*); |
|---|
| 49 | static void draw_aliased_line_gray(pipi_image_t *img, struct line* s); |
|---|
| 50 | static void draw_aliased_line_float(pipi_image_t *img, struct line* s); |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | int pipi_draw_line(pipi_image_t *img , int x1, int y1, int x2, int y2, uint32_t c) |
|---|
| 55 | { |
|---|
| 56 | struct line s; |
|---|
| 57 | s.x1 = x1; |
|---|
| 58 | s.y1 = y1; |
|---|
| 59 | s.x2 = x2; |
|---|
| 60 | s.y2 = y2; |
|---|
| 61 | |
|---|
| 62 | if(img->last_modified == PIPI_PIXELS_RGBA_C) { |
|---|
| 63 | uint32_t *dstdata; |
|---|
| 64 | dstdata = (uint32_t *)pipi_getpixels(img, PIPI_PIXELS_RGBA_C)->pixels; |
|---|
| 65 | s.color32 = c; |
|---|
| 66 | s.buf_u32 = dstdata; |
|---|
| 67 | s.draw = draw_aliased_line_u32; |
|---|
| 68 | } else if(img->last_modified == PIPI_PIXELS_Y_F) { |
|---|
| 69 | float *dstdata; |
|---|
| 70 | dstdata = (float *)pipi_getpixels(img, PIPI_PIXELS_Y_F)->pixels; |
|---|
| 71 | s.colorf[0] = c/255.0f; /* XXX FIXME */ |
|---|
| 72 | s.buf_f = dstdata; |
|---|
| 73 | s.draw = draw_aliased_line_gray; |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | } else { |
|---|
| 77 | float *dstdata; |
|---|
| 78 | dstdata = (float *)pipi_getpixels(img, PIPI_PIXELS_RGBA_F)->pixels; |
|---|
| 79 | s.colorf[0] = (c&0x00FF0000)/255.0f; /* XXX FIXME */ |
|---|
| 80 | s.colorf[1] = (c&0x0000FF00)/255.0f; /* XXX FIXME */ |
|---|
| 81 | s.colorf[2] = (c&0x000000FF)/255.0f; /* XXX FIXME */ |
|---|
| 82 | s.buf_f = dstdata; |
|---|
| 83 | s.draw = draw_aliased_line_float; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | clip_line(img, &s); |
|---|
| 87 | return 0; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | int pipi_draw_polyline(pipi_image_t *img, int const x[], int const y[], |
|---|
| 92 | int n, uint32_t c) |
|---|
| 93 | { |
|---|
| 94 | int i; |
|---|
| 95 | struct line s; |
|---|
| 96 | |
|---|
| 97 | if(img->last_modified == PIPI_PIXELS_RGBA_C) { |
|---|
| 98 | uint32_t *dstdata; |
|---|
| 99 | dstdata = (uint32_t *)pipi_getpixels(img, PIPI_PIXELS_RGBA_C)->pixels; |
|---|
| 100 | s.color32 = c; |
|---|
| 101 | s.buf_u32 = dstdata; |
|---|
| 102 | s.draw = draw_aliased_line_u32; |
|---|
| 103 | } else { |
|---|
| 104 | float *dstdata; |
|---|
| 105 | dstdata = (float *)pipi_getpixels(img, PIPI_PIXELS_RGBA_F)->pixels; |
|---|
| 106 | s.colorf[0] = (c&0x00FF0000)/255.0f; /* XXX FIXME */ |
|---|
| 107 | s.colorf[1] = (c&0x0000FF00)/255.0f; /* XXX FIXME */ |
|---|
| 108 | s.colorf[2] = (c&0x000000FF)/255.0f; /* XXX FIXME */ |
|---|
| 109 | s.buf_f = dstdata; |
|---|
| 110 | s.draw = draw_aliased_line_float; |
|---|
| 111 | img->last_modified = PIPI_PIXELS_RGBA_F; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | for(i = 0; i < n; i++) |
|---|
| 115 | { |
|---|
| 116 | s.x1 = x[i]; |
|---|
| 117 | s.y1 = y[i]; |
|---|
| 118 | s.x2 = x[i+1]; |
|---|
| 119 | s.y2 = y[i+1]; |
|---|
| 120 | clip_line(img, &s); |
|---|
| 121 | } |
|---|
| 122 | return 0; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | /* |
|---|
| 130 | * XXX: The following functions are local. |
|---|
| 131 | */ |
|---|
| 132 | /* Generic Cohen-Sutherland line clipping function. */ |
|---|
| 133 | static void clip_line(pipi_image_t *img, struct line* s) |
|---|
| 134 | { |
|---|
| 135 | uint8_t bits1, bits2; |
|---|
| 136 | |
|---|
| 137 | bits1 = clip_bits(img, s->x1, s->y1); |
|---|
| 138 | bits2 = clip_bits(img, s->x2, s->y2); |
|---|
| 139 | |
|---|
| 140 | if(bits1 & bits2) |
|---|
| 141 | return; |
|---|
| 142 | |
|---|
| 143 | if(bits1 == 0) |
|---|
| 144 | { |
|---|
| 145 | if(bits2 == 0) |
|---|
| 146 | s->draw(img, s); |
|---|
| 147 | else |
|---|
| 148 | { |
|---|
| 149 | int tmp; |
|---|
| 150 | tmp = s->x1; s->x1 = s->x2; s->x2 = tmp; |
|---|
| 151 | tmp = s->y1; s->y1 = s->y2; s->y2 = tmp; |
|---|
| 152 | clip_line(img, s); |
|---|
| 153 | } |
|---|
| 154 | return; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | if(bits1 & (1<<0)) |
|---|
| 158 | { |
|---|
| 159 | s->y1 = s->y2 - (s->x2 - 0) * (s->y2 - s->y1) / (s->x2 - s->x1); |
|---|
| 160 | s->x1 = 0; |
|---|
| 161 | } |
|---|
| 162 | else if(bits1 & (1<<1)) |
|---|
| 163 | { |
|---|
| 164 | int xmax = img->w - 1; |
|---|
| 165 | s->y1 = s->y2 - (s->x2 - xmax) * (s->y2 - s->y1) / (s->x2 - s->x1); |
|---|
| 166 | s->x1 = xmax; |
|---|
| 167 | } |
|---|
| 168 | else if(bits1 & (1<<2)) |
|---|
| 169 | { |
|---|
| 170 | s->x1 = s->x2 - (s->y2 - 0) * (s->x2 - s->x1) / (s->y2 - s->y1); |
|---|
| 171 | s->y1 = 0; |
|---|
| 172 | } |
|---|
| 173 | else if(bits1 & (1<<3)) |
|---|
| 174 | { |
|---|
| 175 | int ymax = img->h - 1; |
|---|
| 176 | s->x1 = s->x2 - (s->y2 - ymax) * (s->x2 - s->x1) / (s->y2 - s->y1); |
|---|
| 177 | s->y1 = ymax; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | clip_line(img, s); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | /* Helper function for clip_line(). */ |
|---|
| 184 | static uint8_t clip_bits(pipi_image_t *img, int x, int y) |
|---|
| 185 | { |
|---|
| 186 | uint8_t b = 0; |
|---|
| 187 | |
|---|
| 188 | if(x < 0) |
|---|
| 189 | b |= (1<<0); |
|---|
| 190 | else if(x >= (int)img->w) |
|---|
| 191 | b |= (1<<1); |
|---|
| 192 | |
|---|
| 193 | if(y < 0) |
|---|
| 194 | b |= (1<<2); |
|---|
| 195 | else if(y >= (int)img->h) |
|---|
| 196 | b |= (1<<3); |
|---|
| 197 | |
|---|
| 198 | return b; |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | /* Solid line drawing function, using Bresenham's mid-point line |
|---|
| 204 | * scan-conversion algorithm. */ |
|---|
| 205 | static void draw_aliased_line_u32(pipi_image_t *img, struct line* s) |
|---|
| 206 | { |
|---|
| 207 | #undef ASSIGN |
|---|
| 208 | #define ASSIGN(x, y, w) s->buf_u32[x+y*w] = s->color32; |
|---|
| 209 | #include "line_template.c" |
|---|
| 210 | } |
|---|
| 211 | static void draw_aliased_line_float(pipi_image_t *img, struct line* s) |
|---|
| 212 | { |
|---|
| 213 | #undef ASSIGN |
|---|
| 214 | #define ASSIGN(x, y, w) s->buf_f[(x*4)+y*(w*4)] = s->colorf[0]; \ |
|---|
| 215 | s->buf_f[1 + (x*4)+y*(w*4)] = s->colorf[1]; \ |
|---|
| 216 | s->buf_f[2 + (x*4)+y*(w*4)] = s->colorf[2]; |
|---|
| 217 | #include "line_template.c" |
|---|
| 218 | } |
|---|
| 219 | static void draw_aliased_line_gray(pipi_image_t *img, struct line* s) |
|---|
| 220 | { |
|---|
| 221 | #undef ASSIGN |
|---|
| 222 | #define ASSIGN(x, y, w) s->buf_f[x+y*w] = s->colorf[0]; |
|---|
| 223 | #include "line_template.c" |
|---|
| 224 | } |
|---|