1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
|
/*
* Pure Data Packet. common image processing routines.
* Copyright (c) by Tom Schouten <tom@zwizwa.be>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/*
This file contains common code for (portable) low level image processing objects
pdp_imageproc_* methods
The rest is int pdp_imageproc_<platform>.c
There are also highlevel dispatcher methods that operate on packets:
pdp_imageproc_dispatch_* methods
*/
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "pdp_imageproc.h"
#include "pdp_image.h"
#include "pdp_mem.h"
#include "pdp_packet.h"
#define CLAMP16(x) (((x) > 0x7fff) ? 0x7fff : (((x) < -0x7fff) ? -0x7fff : (x)))
u32 pdp_imageproc_legalwidth(int i)
{
// sevy : we don't see no need for that limitation
// has been tested on linux with 1280x1024 without problems
// if (i>1024) return 1024;
if (i>0) return ((((i-1)>>3)+1)<<3);
return 8;
}
u32 pdp_imageproc_legalheight(int i)
{
// if (i>1024) return 1024;
if (i>0) return ((((i-1)>>3)+1)<<3);
return 8;
}
u32 pdp_imageproc_legalwidth_round_down(int i)
{
// if (i>1024) return 1024;
if (i>8) return ((i>>3)<<3);
return 8;
}
u32 pdp_imageproc_legalheight_round_down(int i)
{
// if (i>1024) return 1024;
if (i>8) return ((i>>3)<<3);
return 8;
}
/* check if two packets are allocated and of the same type */
int pdp_packet_compat(int packet0, int packet1)
{
t_pdp *header0 = pdp_packet_header(packet0);
t_pdp *header1 = pdp_packet_header(packet1);
if (!(header1)) return 0;
if (!(header0)) return 0;
if (header0->type != header1->type) return 0;
return 1;
}
/* some operations */
/* logic operators */
void pdp_imageproc_xor_process(void *x, u32 width, u32 height, s16 *image, s16 *image2)
{
u32 *plane = (u32 *)image;
u32 *plane2 = (u32 *)image2;
int count = (width * height) >> 1;
int i;
for (i=0; i<count; i++){
plane[i] ^= plane2[i];
}
}
void pdp_imageproc_and_process(void *x, u32 width, u32 height, s16 *image, s16 *image2)
{
u32 *plane = (u32 *)image;
u32 *plane2 = (u32 *)image2;
int count = (width * height) >> 1;
int i;
for (i=0; i<count; i++){
plane[i] &= plane2[i];
}
}
void pdp_imageproc_or_process(void *x, u32 width, u32 height, s16 *image, s16 *image2)
{
u32 *plane = (u32 *)image;
u32 *plane2 = (u32 *)image2;
int count = (width * height) >> 1;
int i;
for (i=0; i<count; i++){
plane[i] |= plane2[i];
}
}
void pdp_imageproc_not_process(void *x, u32 width, u32 height, s16 *image)
{
u32 *plane = (u32 *)image;
int count = (width * height) >> 1;
int i;
for (i=0; i<count; i++){
plane[i] ^= 0xffffffff;
}
}
void pdp_imageproc_mask_process(void *x, u32 width, u32 height, s16 *image)
{
uptr mask = (uptr)x;
u32 *plane = (u32 *)image;
int count = (width * height) >> 1;
int i;
mask = (mask & 0xffff) | (mask << 16);
for (i=0; i<count; i++){
plane[i] &= mask;
}
}
// produce a plasma image
// note: random number generator can be platform specific
// however, it should be seeded. (same seed produces the same result)
typedef struct
{
u32 seed;
s32 scale;
} t_plasma;
static inline s16 _rand_s16(void)
{
return (s16)(random()<<0);
}
static inline s16 _new_color(s32 one, s32 two, s32 scale)
{
return CLAMP16((one >> 1) + (two >> 1) + ((scale * _rand_s16()) >> 16));
//return (one >> 1) + (two >> 1);
}
void *pdp_imageproc_plasma_new(void){return pdp_alloc(sizeof(t_plasma));}
void pdp_imageproc_plasma_delete(void *x){pdp_dealloc(x);}
void pdp_imageproc_plasma_setseed(void *x, float seed)
{
*((float *)x) = seed;
}
void pdp_imageproc_plasma_setturbulence(void *x, float f)
{
((t_plasma *)x)->scale = CLAMP16(f * ((float)0x7fff));
}
static void _plasma_subdiv(u32 w, u32 h, u32 s, s16 *image, int calc_left, int calc_top, s32 scale)
{
int w0 = ((w-1)>>1); // width of left segments
int h0 = ((h-1)>>1); // heigth of top segments
int w1 = w - w0;
int h1 = h - h0;
/* conditions: w0 <= w1, h0 <= h1 */
/* original coordinates */
int topleft = 0;
int topright = w-1;
int bottomleft = s * (h-1);
int bottomright = bottomleft + topright;
/* new subdivision coordinates */
int top = w0;
int left = s * h0;
int bottom = bottomleft + w0;
int right = topright + left;
int center = left + top;
if (w0 && h0){ /* left-right and top-bottom subdivide */
/* calculate corner pixel colours */
if (calc_top) image[top] = _new_color(image[topleft], image[topright], scale);
if (calc_left) image[left] = _new_color(image[topleft], image[bottomleft], scale);
image[right] = _new_color(image[topright], image[bottomright], scale);
image[bottom] = _new_color(image[bottomleft], image[bottomright], scale);
image[center] = (_new_color(image[top], image[bottom], scale) >> 1)
+(_new_color(image[left], image[right], scale) >> 1);
/* subdivide (with overlap) */
_plasma_subdiv(w0+1, h0+1, s, &image[topleft], 1, 1, scale);
_plasma_subdiv(w1, h0+1, s, &image[top], 0, 1, scale);
_plasma_subdiv(w0+1, h1, s, &image[left], 1, 0, scale);
_plasma_subdiv(w1, h1, s, &image[center], 0, 0, scale);
}
else if(h0) { /* top-bottom subdivide */
//post("h:%d", h);
/* calculate corner pixel colours */
if(calc_left) image[left] = _new_color(image[topleft], image[bottomleft], scale);
image[right] = _new_color(image[topright], image[bottomright], scale);
/* subdivide (without overlap) */
_plasma_subdiv(w, h0+1, s, &image[topleft], 1, 0, scale);
_plasma_subdiv(w, h1, s, &image[left], 1, 0, scale);
}
else if (w0){ /* left-right subdivide */
/* calculate corner pixel colours */
if (calc_top) image[top] = _new_color(image[topleft], image[topright], scale);
image[bottom] = _new_color(image[bottomleft], image[bottomright],scale);
/* subdivide with overlap */
_plasma_subdiv(w0+1, h, s, &image[topleft], 0, 1, scale);
_plasma_subdiv(w1, h, s, &image[top], 0, 1, scale);
}
}
void pdp_imageproc_plasma_process(void *x, u32 width, u32 height, s16 *image)
{
s32 scale = (((t_plasma *)x)->scale);
srandom (((t_plasma *)x)->seed);
/* set initial border colours */
image[0] = _rand_s16();
image[width-1] = _rand_s16();
image[width * (height-1)] = _rand_s16();
image[width * height - 1] = _rand_s16();
/* subdivide */
_plasma_subdiv(width, height, width, image, 1, 1, scale);
((t_plasma *)x)->seed = random();
}
void pdp_imageproc_zero_process(void *x, u32 width, u32 height, s16 *image)
{
int bytesize = (width * height) << 1;
memset(image, 0, bytesize);
}
void pdp_imageproc_constant_process(void *x, u32 width, u32 height, s16 *image)
{
int i;
uptr value = (uptr)x;
u32 *plane = (u32 *)image;
int wordsize = (width * height) >> 1;
value = (value & 0xffff) | (value << 16);
for (i=0; i<wordsize; i++){
plane[i] = value;
}
}
/* other stateless operators */
/* some 2x16bit vector ops */
/* some bit shuffling to ensure 32 bit accesses
get the sign bit extended as a mask: - : 0xffff +: 0x0000 */
static inline u32 _sign(s32 invec)
{
s32 mask_top = invec;
s32 mask_bot = invec;
mask_top &= 0x80000000; /* isolate top sign bit */
mask_bot <<= 16; /* shift bottom word to top word */
mask_bot &= 0x80000000; /* isolate bottom sign bit */
mask_top >>= 15; /* shift sign bit into top word */
mask_bot >>= 15;
mask_bot = (s32)(((u32)mask_bot) >> 16); /* shift top word into bottom word */
return mask_top |mask_bot;
}
/* clear the least significant bit of the top word
to ensure a decoupled vector add */
static inline void _decouple(s32 *invec)
{
*invec &= 0xfffeffff;
}
void pdp_imageproc_abs_process(void *x, u32 width, u32 height, s16 *image)
{
int i;
s32 *wimage = (s32 *)image;
int wsize = (width * height) >> 1;
for (i=0; i<wsize; i++){
/* this computes c = (c >= 0) ? (c) : (~c) */
/* not is used instead of neg to prevent overflow on 0x8000 */
/* this maps both 0 and -1 to 0 */
wimage[i] ^= _sign(wimage[i]);
}
}
void pdp_imageproc_zthresh_process(void *x, u32 width, u32 height, s16 *image)
{
int i;
s32 *wimage = (s32 *)image;
int wsize = (width * height) >> 1;
for (i=0; i<wsize; i++){
/* this computes c = (c >= 0) ? (c) : (0) */
wimage[i] &= ~_sign(wimage[i]);
}
}
/* hard thresholding: x contains a positive unsigned short int */
void pdp_imageproc_hardthresh_process(void *x, u32 width, u32 height, s16 *image)
{
int i;
uptr thresh = (uptr)x;
s32 sign1, isign2, a;
s32 *wimage = (s32 *)image;
int wsize = (width * height) >> 1;
thresh |= (thresh << 16);
for (i=0; i<wsize; i++){
a = wimage[i];
sign1 = _sign(a);
a ^= sign1; /* take abs */
_decouple(&a);
a -= thresh; /* subtract threshold */
isign2 = ~ _sign(a);
a &= isign2; /* zero thresh */
_decouple(&a);
a += thresh & isign2; /* add threshold (if not zero thresholded)*/
a ^= sign1;
wimage[i] = a;
}
}
/* soft thresholding: x contains a positive unsigned short int */
void pdp_imageproc_softthresh_process(void *x, u32 width, u32 height, s16 *image)
{
int i;
sptr thresh = (sptr)x;
s32 sign1, sign2, a;
s32 *wimage = (s32 *)image;
int wsize = (width * height) >> 1;
thresh |= thresh << 16;
for (i=0; i<wsize; i++){
a = wimage[i];
sign1 = _sign(a);
a ^= sign1; /* take abs */
_decouple(&a);
a -= thresh; /* subtract threshold */
sign2 = _sign(a);
a &= ~ sign2; /* zero thresh */
_decouple(&a);
//a += thresh; /* add threshold */
a ^= sign1;
wimage[i] = a;
}
}
/* turns an image into a positive andmask */
void pdp_imageproc_ispositive_process(void *x, u32 width, u32 height, s16 *image)
{
int i;
s32 *wimage = (s32 *)image;
int wsize = (width * height) >> 1;
for (i=0; i<wsize; i++){
wimage[i] = ~_sign(wimage[i]);
}
}
/* get sign */
void pdp_imageproc_sign_process(void *x, u32 width, u32 height, s16 *image)
{
int i;
s32 *wimage = (s32 *)image;
int wsize = (width * height) >> 1;
for (i=0; i<wsize; i++){
wimage[i] = _sign(wimage[i]) ^ 0x7fff7fff;
}
}
/* flip left <-> right */
void pdp_imageproc_flip_lr_process(void *dummy, u32 width, u32 height, s16 *image)
{
u32 y;
s16 tmp, *l, *r;
for (y=0; y<height; y++){
l = image;
r = image + width - 1;
while (l < r){
tmp = *l;
*l = *r;
*r = tmp;
l++;
r--;
}
image += width;
}
}
void pdp_llconv_flip_top_bottom(s16 *data, int width, int height, int pixelsize);
void pdp_imageproc_flip_tb_process(void *dummy, u32 width, u32 height, s16 *image)
{
pdp_llconv_flip_top_bottom(image, width, height, 2);
}
/* image processing dispatcher methods */
/* if the first packet contains a nonzero channel mask, it will be used instead
of the one supplied as argument to the dispatcher functions.
the packet's channel mask will be reset to 0 */
void pdp_imageproc_dispatch_1buf(void (*process_routine)(void*, u32, u32, s16*), void *x, u32 chanmask, int packet0)
{
t_pdp *header0;
t_image *image0;
s16 *idata0;
unsigned int w,h,d,plane_size,mask;
/* if packet is not a valid image return without doing anything */
if (!(pdp_packet_image_isvalid(packet0))) return;
header0 = pdp_packet_header(packet0);
image0 = pdp_packet_image_info(packet0);
idata0 = pdp_packet_data (packet0);
w = image0->width;
h = image0->height;
d = image0->depth;
plane_size = w*h;
if (image0->chanmask) chanmask = image0->chanmask;
image0->chanmask = 0;
switch(image0->encoding){
case PDP_IMAGE_GREY:
if (chanmask & 1) (*process_routine)(x, w, h, idata0);
break;
case PDP_IMAGE_YV12:
if (chanmask & 1) (*process_routine)(x, w, h, idata0);
idata0 += plane_size;
plane_size >>= 2;
w >>= 1;
h >>= 1;
if (chanmask & 2) (*process_routine)(x, w, h, idata0);
idata0 += plane_size;
if (chanmask & 4) (*process_routine)(x, w, h, idata0);
break;
case PDP_IMAGE_MCHP:
mask = 1;
while (d--){
if (chanmask & mask) (*process_routine)(x, w, h, idata0);
idata0 += plane_size;
mask <<= 1;
}
break;
default:
break;
}
}
void pdp_imageproc_dispatch_2buf(void (*process_routine)(void*, u32, u32, s16*, s16 *), void *x, u32 chanmask, int packet0, int packet1)
{
t_pdp *header0;
t_image *image0;
s16 *idata0, *idata1;
unsigned int w,h,d,plane_size,mask;
/* if packets are not compatible images, return without doing anything */
if (!(pdp_packet_image_compat(packet0, packet1))) return;
header0 = pdp_packet_header(packet0);
image0 = pdp_packet_image_info(packet0);
idata0 = pdp_packet_data (packet0);
idata1 = pdp_packet_data (packet1);
w = image0->width;
h = image0->height;
d = image0->depth;
plane_size = w*h;
if (image0->chanmask) chanmask = image0->chanmask;
image0->chanmask = 0;
switch(image0->encoding){
case PDP_IMAGE_GREY:
if (chanmask & 1) (*process_routine)(x, w, h, idata0, idata1);
break;
case PDP_IMAGE_YV12:
if (chanmask & 1) (*process_routine)(x, w, h, idata0, idata1);
idata0 += plane_size;
idata1 += plane_size;
plane_size >>= 2;
w >>= 1;
h >>= 1;
if (chanmask & 2) (*process_routine)(x, w, h, idata0, idata1);
idata0 += plane_size;
idata1 += plane_size;
if (chanmask & 4) (*process_routine)(x, w, h, idata0, idata1);
break;
case PDP_IMAGE_MCHP:
mask = 1;
while (d--){
if (chanmask & mask) (*process_routine)(x, w, h, idata0, idata1);
idata0 += plane_size;
idata1 += plane_size;
mask <<= 1;
}
break;
default:
break;
}
}
void pdp_imageproc_dispatch_3buf(void (*process_routine)(void*, u32, u32, s16*, s16 *, s16 *), void *x, u32 chanmask, int packet0, int packet1, int packet2)
{
t_pdp *header0;
t_image *image0;
s16 *idata0, *idata1, *idata2;
unsigned int w,h,d,plane_size, mask;
/* if packets are not compatible images, return without doing anything */
if (!((pdp_packet_image_compat(packet0, packet1))
&&(pdp_packet_image_compat(packet0, packet1)))) return;
header0 = pdp_packet_header(packet0);
image0 = pdp_packet_image_info(packet0);
idata0 = pdp_packet_data (packet0);
idata1 = pdp_packet_data (packet1);
idata2 = pdp_packet_data (packet2);
w = image0->width;
h = image0->height;
d = image0->depth;
plane_size = w*h;
if (image0->chanmask) chanmask = image0->chanmask;
image0->chanmask = 0;
switch(image0->encoding){
case PDP_IMAGE_GREY:
if (chanmask & 1)(*process_routine)(x, w, h, idata0, idata1, idata2);
break;
case PDP_IMAGE_YV12:
if (chanmask & 1)(*process_routine)(x, w, h, idata0, idata1, idata2);
idata0 += plane_size;
idata1 += plane_size;
idata2 += plane_size;
plane_size >>= 2;
w >>= 1;
h >>= 1;
if (chanmask & 2)(*process_routine)(x, w, h, idata0, idata1, idata2);
idata0 += plane_size;
idata1 += plane_size;
idata2 += plane_size;
if (chanmask & 4)(*process_routine)(x, w, h, idata0, idata1, idata2);
break;
case PDP_IMAGE_MCHP:
mask = 1;
while (d--){
if (chanmask & mask) (*process_routine)(x, w, h, idata0, idata1, idata2);
idata0 += plane_size;
idata1 += plane_size;
idata2 += plane_size;
mask <<= 1;
}
break;
default:
break;
}
}
|