static void mk_new_int(JRB l, JRB r, JRB p, int il) { JRB newnode; newnode = (JRB) calloc(1, sizeof(struct jrb_node)); setint(newnode); setred(newnode); setnormal(newnode); newnode->flink = l; newnode->blink = r; newnode->parent = p; setlext(newnode, l); setrext(newnode, r); l->parent = newnode; r->parent = newnode; setleft(l); setright(r); if (ishead(p)) { p->parent = newnode; setroot(newnode); } else if (il) { setleft(newnode); p->flink = newnode; } else { setright(newnode); p->blink = newnode; } recolor(newnode); }
static void mk_new_int(Rb_node l, Rb_node r, Rb_node p, int il) { Rb_node newnode; newnode = (Rb_node) malloc(sizeof(struct rb_node)); setint(newnode); setred(newnode); setnormal(newnode); newnode->c.child.left = l; newnode->c.child.right = r; newnode->p.parent = p; newnode->k.lext = l; newnode->v.rext = r; l->p.parent = newnode; r->p.parent = newnode; setleft(l); setright(r); if (ishead(p)) { p->p.root = newnode; setroot(newnode); } else if (il) { setleft(newnode); p->c.child.left = newnode; } else { setright(newnode); p->c.child.right = newnode; } recolor(newnode); }
//sux void invert_brightness (iImage * source) { int i, j; int r, g, b; //float bright; for (i = 0; i < source->GetWidth (); i++) for (j = 0; j < source->GetHeight (); j++) { r = getred (source, i, j); g = getgreen (source, i, j); b = getblue (source, i, j); r = 255 - r; g = 255 - g; b = 255 - b; setred (source, i, j, r); setgreen (source, i, j, g); setblue (source, i, j, b); } }
void alter_colors (iImage * source, int o_r, int o_g, int o_b, int r2, int g2, int b2) { int i, j; int r, g, b; //float bright; //int red_ratio; //int green_ratio; //int blue_ratio; for (i = 0; i < source->GetWidth (); i++) for (j = 0; j < source->GetHeight (); j++) { r = getred (source, i, j); g = getgreen (source, i, j); b = getblue (source, i, j); //bright=sqrt(r*r+g*g+b*b); setred (source, i, j, (int)(r2 * sqrt (o_r * r + o_g * g + o_b * b) / 255) ); setgreen (source, i, j,(int)( g2 * sqrt (o_r * r + o_g * g + o_b * b) / 255)); setblue (source, i, j,(int)(b2 * sqrt (o_r * r + o_g * g + o_b * b) / 255)); } }
static void recolor(JRB n) { JRB p, gp, s; int done = 0; while (!done) { if (isroot(n)) { setblack(n); return; } p = n->parent; if (isblack(p)) return; if (isroot(p)) { setblack(p); return; } gp = p->parent; s = sibling(p); if (isred(s)) { setblack(p); setred(gp); setblack(s); n = gp; } else { done = 1; } } /* p's sibling is black, p is red, gp is black */ if ((isleft(n) == 0) == (isleft(p) == 0)) { single_rotate(gp, isleft(n)); setblack(p); setred(gp); } else { single_rotate(p, isleft(n)); single_rotate(gp, isleft(n)); setblack(n); setred(gp); } }
//0,200,200 is nice cyan //draws an open rectangle void rect_open (iImage * dest, int x, int y, int end_x, int end_y, int r, int g, int b) { int i, j; //top bar j = y; for (i = x; i < end_x + 1; i++) { setred (dest, i, j, r); setgreen (dest, i, j, g); setblue (dest, i, j, b); setalpha (dest, i, j, 255); } //bottom bar j = end_y; for (i = x; i < end_x + 1; i++) { setred (dest, i, j, r); setgreen (dest, i, j, g); setblue (dest, i, j, b); setalpha (dest, i, j, 255); } i = x; for (j = y; j < end_y + 1; j++) { setred (dest, i, j, r); setgreen (dest, i, j, g); setblue (dest, i, j, b); setalpha (dest, i, j, 255); } i = end_x; for (j = y; j < end_y + 1; j++) { setred (dest, i, j, r); setgreen (dest, i, j, g); setblue (dest, i, j, b); setalpha (dest, i, j, 255); } }
void blit_alpha (iImage * source, iImage * dest, int source_startx, int source_starty, int destx, int desty, int source_endx, int source_endy, int range) { int x; int y; //check if its out of range yourself :P int r, g, b, a; int i, j; //int r2,g2,b2; x = dest->GetWidth (); y = dest->GetHeight (); if (source_endx + destx - source_startx >= x) source_endx = x - destx + source_startx; if (source_endy + desty - source_starty >= y) source_endy = y - desty + source_starty; i = 0; j = 0; for (x = source_startx; x < source_endx; x++) { for (y = source_starty; y < source_endy; y++) { r = getred (source, x, y); g = getgreen (source, x, y); b = getblue (source, x, y); a = getalpha (source, x, y); if (a - range < 1) { } else { setred (dest, i + destx, j + desty, r); setgreen (dest, i + destx, j + desty, g); setblue (dest, i + destx, j + desty, b); } j++; } j = 0; i++; } }
void remove_red (iImage * source) { int x; int y; int i, j; x = source->GetWidth (); y = source->GetHeight (); for (i = 0; i < x; i++) for (j = 0; j < y; j++) setred (source, i, j, 0); }
//the except is where you have a 0... //if o_r ==0 and o_g>0 and o_b>0 rng=10 no colors with red greater than 10 will be replaced void alter_colors_except (iImage * source, int o_r, int o_g, int o_b, int r2, int g2, int b2, int rng) { int i, j; int r, g, b; int ok; for (i = 0; i < source->GetWidth (); i++) for (j = 0; j < source->GetHeight (); j++) { ok = 1; r = getred (source, i, j); g = getgreen (source, i, j); b = getblue (source, i, j); if (o_r == 0) if (r > rng) ok = 0; if (o_g == 0) if (g > rng) ok = 0; if (o_b == 0) if (b > rng) ok = 0; if (ok == 1) { setred (source, i, j,(int)( r2 * sqrt (o_r * r + o_g * g + o_b * b) / 255)); setgreen (source, i, j,(int)( g2 * sqrt (o_r * r + o_g * g + o_b * b) / 255)); setblue (source, i, j,(int)( b2 * sqrt (o_r * r + o_g * g + o_b * b) / 255)); } } }
//This Blit conserves the alpha, Blit_merge blends the alphaed color in with background. void blit (iImage * source, iImage * dest, int source_startx, int source_starty, int destx, int desty, int source_endx, int source_endy) { int x; int y; int r, g, b; int i, j; int a; x = dest->GetWidth (); y = dest->GetHeight (); if (source_endx + destx - source_startx >= x) source_endx = x - destx + source_startx; if (source_endy + desty - source_starty >= y) source_endy = y - desty + source_starty; i = 0; j = 0; for (x = source_startx; x < source_endx; x++) { for (y = source_starty; y < source_endy; y++) { r = getred (source, x, y); g = getgreen (source, x, y); b = getblue (source, x, y); a = getalpha (source, x, y); setred (dest, i + destx, j + desty, r); setgreen (dest, i + destx, j + desty, g); setblue (dest, i + destx, j + desty, b); setalpha (dest, i + destx, j + desty, a); j++; } j = 0; i++; } }
void clear_image (iImage * dest) { int i, j; for (i = 0; i < dest->GetWidth (); i++) for (j = 0; j < dest->GetHeight (); j++) { setred (dest, i, j, 0); setgreen (dest, i, j, 0); setblue (dest, i, j, 0); setalpha (dest, i, j, 0); } }
//draws a rectangle_filled with colors r,g,b void rect (iImage * dest, int x, int y, int end_x, int end_y, int r, int g, int b) { int i, j; //float bright; //int r3,g3,b3; for (i = x; i < end_x + 1; i++) for (j = y; j < end_y + 1; j++) { setred (dest, i, j, r); setgreen (dest, i, j, g); setblue (dest, i, j, b); setalpha (dest, i, j, 255); } }
void swap_blue_to_red (iImage * source) { int i, j; int r, g, b; //float bright; for (i = 0; i < source->GetWidth (); i++) for (j = 0; j < source->GetHeight (); j++) { r = getred (source, i, j); g = getgreen (source, i, j); b = getblue (source, i, j); setred (source, i, j, b); setgreen (source, i, j, g); setblue (source, i, j, r); } }
void jrb_delete_node(JRB n) { JRB s, p, gp; char ir; if (isint(n)) { fprintf(stderr, "Cannot delete an internal node: 0x%p\n", (void *)n); exit(1); } if (ishead(n)) { fprintf(stderr, "Cannot delete the head of an jrb_tree: 0x%p\n", (void *)n); exit(1); } delete_item(n); /* Delete it from the list */ p = n->parent; /* The only node */ if (isroot(n)) { p->parent = p; free(n); return; } s = sibling(n); /* The only node after deletion */ if (isroot(p)) { s->parent = p->parent; s->parent->parent = s; setroot(s); free(p); free(n); return; } gp = p->parent; /* Set parent to sibling */ s->parent = gp; if (isleft(p)) { gp->flink = s; setleft(s); } else { gp->blink = s; setright(s); } ir = isred(p); free(p); free(n); if (isext(s)) { /* Update proper rext and lext values */ p = lprev(s); if (!ishead(p)) setrext(p, s); p = rprev(s); if (!ishead(p)) setlext(p, s); } else if (isblack(s)) { fprintf(stderr, "DELETION PROB -- sib is black, internal\n"); exit(1); } else { p = lprev(s); if (!ishead(p)) setrext(p, s->flink); p = rprev(s); if (!ishead(p)) setlext(p, s->blink); setblack(s); return; } if (ir) return; /* Recolor */ n = s; p = n->parent; s = sibling(n); while(isblack(p) && isblack(s) && isint(s) && isblack(s->flink) && isblack(s->blink)) { setred(s); n = p; if (isroot(n)) return; p = n->parent; s = sibling(n); } if (isblack(p) && isred(s)) { /* Rotation 2.3b */ single_rotate(p, isright(n)); setred(p); setblack(s); s = sibling(n); } { JRB x, z; char il; if (isext(s)) { fprintf(stderr, "DELETION ERROR: sibling not internal\n"); exit(1); } il = isleft(n); x = il ? s->flink : s->blink ; z = sibling(x); if (isred(z)) { /* Rotation 2.3f */ single_rotate(p, !il); setblack(z); if (isred(p)) setred(s); else setblack(s); setblack(p); } else if (isblack(x)) { /* Recoloring only (2.3c) */ if (isred(s) || isblack(p)) { fprintf(stderr, "DELETION ERROR: 2.3c not quite right\n"); exit(1); } setblack(p); setred(s); return; } else if (isred(p)) { /* 2.3d */ single_rotate(s, il); single_rotate(p, !il); setblack(x); setred(s); return; } else { /* 2.3e */ single_rotate(s, il); single_rotate(p, !il); setblack(x); return; } } }
//RNG is the times difference void alter_colors_ratio (iImage * source, int o_r, int o_g, int o_b, int r2, int g2, int b2, int rng) { int i, j; int r, g, b; //int a1; //float bright; //int r3,g3,b3; float rg_rat, gb_rat, br_rat; //Which way do the ratio's face: int rg_way = -1, gb_way = -1, br_way = -1; float rg_fly=0; float gb_fly, br_fly; int ok; //Rat values: 0 - none of one element // greater than 0: green is greater than red // less than 0: red is less than green -inverse 1/ number- greater than... // greater than 0: How many times difference //compare: times difference, against current times difference, and see if within range if (o_r == 0) rg_rat = 0; else { rg_rat = o_g / o_r; if (rg_rat < 0) { rg_rat = 1 / rg_rat; rg_way = 1; } } if (o_g == 0) gb_rat = 0; else { gb_rat = o_b / o_g; if (gb_rat < 0) { gb_rat = 1 / gb_rat; gb_way = 1; } } if (o_b == 0) br_rat = 0; else { br_rat = o_r / o_b; if (br_rat < 0) { br_rat = 1 / br_rat; br_way = 1; } } for (i = 0; i < source->GetWidth (); i++) for (j = 0; j < source->GetHeight (); j++) { ok = 1; r = getred (source, i, j); g = getgreen (source, i, j); b = getblue (source, i, j); //Check which way rg goes if (rg_way == -1) { //everything is cool if 0's.0 stops division by 0 if (r == 0) { if (o_r > 0) ok = 0; } else { rg_fly = g / r; //make sure we got green to red, and not red to green(insta failure for now) //invert and negatize it, so its on a scale goin the other direction if (rg_fly < 0) rg_fly = -(1 / rg_fly); rg_fly = abs ((int)(rg_fly - rg_rat)); if (rg_fly > rng) ok = 0; } } if (rg_way == 1) { rg_fly = r / g; if (rg_fly < 0) rg_fly = -(1 / rg_fly); rg_fly = abs ((int)(rg_fly - rg_rat)); if (rg_fly > rng) ok = 0; } if (gb_way == -1) { //everything is cool if 0's.0 stops division by 0 if (g == 0) { if (o_g > 0) ok = 0; } else { gb_fly = b / g; //make sure we got green to red, and not red to green(insta failure for now) //invert and negatize it, so its on a scale goin the other direction if (gb_fly < 0) gb_fly = -(1 / gb_fly); gb_fly = abs ((int)(gb_fly - gb_rat)); if (gb_fly > rng) ok = 0; } } if (gb_way == 1) { gb_fly = g / b; if (rg_fly < 0) gb_fly = -(1 / gb_fly); gb_fly = abs ((int)(gb_fly - gb_rat)); if (gb_fly > rng) ok = 0; } if (br_way == -1) { //everything is cool if 0's.0 stops division by 0 if (b == 0) { if (o_b > 0) ok = 0; } else { br_fly = r / b; //make sure we got green to red, and not red to green(insta failure for now) //invert and negatize it, so its on a scale goin the other direction if (br_fly < 0) br_fly = -(1 / br_fly); br_fly = abs ((int)(br_fly - br_rat)); if (br_fly > rng) ok = 0; } } if (br_way == 1) { br_fly = b / r; if (rg_fly < 0) br_fly = -(1 / br_fly); br_fly = abs ((int)(br_fly - br_rat)); if (br_fly > rng) ok = 0; } if (ok == 1) { setred (source, i, j,(int)( r2 * sqrt (o_r * r + o_g * g + o_b * b) / 255)); setgreen (source, i, j,(int)( g2 * sqrt (o_r * r + o_g * g + o_b * b) / 255)); setblue (source, i, j,(int)( b2 * sqrt (o_r * r + o_g * g + o_b * b) / 255)); } } }
void blit_transparent (iImage * source, iImage * dest, int source_startx, int source_starty, int destx, int desty, int source_endx, int source_endy, int r0, int g0, int b0, int range) { int x; int y; int r, g, b; int i, j; int r2, g2, b2; x = dest->GetWidth (); y = dest->GetHeight (); if (source_endx + destx - source_startx >= x) source_endx = x - destx + source_startx; if (source_endy + desty - source_starty >= y) source_endy = y - desty + source_starty; i = 0; j = 0; for (x = source_startx; x < source_endx; x++) { for (y = source_starty; y < source_endy; y++) { r = getred (source, x, y); g = getgreen (source, x, y); b = getblue (source, x, y); r2 = abs (r0 - r); g2 = abs (g0 - g); b2 = abs (b0 - b); if (r2 < range && g2 < range && b2 < range) { r = r0; setalpha (dest, i + destx, j + desty, 0); } else { setred (dest, i + destx, j + desty, r); setgreen (dest, i + destx, j + desty, g); setblue (dest, i + destx, j + desty, b); setalpha (dest, i + destx, j + desty, 255); } j++; } j = 0; i++; } }