int main(int ac, char** av) { #if 1 { fixed_t fu = float_to_fixed(1.2); fixed_t bar = float_to_fixed(2.4); fixed_t baz = fixed_add(fu, bar); printf("%f + ", fixed_to_float(fu)); printf("%f = ", fixed_to_float(bar)); printf("%f\n", fixed_to_float(baz)); } { fixed_t fu = float_to_fixed(1.5); fixed_t bar = int_to_fixed(2); fixed_t baz = fixed_mul(fu, bar); printf("%f * ", fixed_to_float(fu)); printf("%f = ", fixed_to_float(bar)); printf("%f\n", fixed_to_float(baz)); } { fixed_t fu = int_to_fixed(1); fixed_t bar = int_to_fixed(2); fixed_t baz = fixed_div(fu, bar); printf("%f / ", fixed_to_float(fu)); printf("%f = ", fixed_to_float(bar)); printf("%f\n", fixed_to_float(baz)); } { fixed_t fu = int_to_fixed(-1); fixed_t bar = int_to_fixed(2); fixed_t baz = fixed_div(fu, bar); printf("%f / ", fixed_to_float(fu)); printf("%f = ", fixed_to_float(bar)); printf("%f\n", fixed_to_float(baz)); } #endif #if 0 { fixed_t alpha = float_to_fixed(0.0); fixed_t step = float_to_fixed(0.01); for (; alpha < FIXED_TWO_PI; alpha = fixed_add(alpha, step)) { printf("%f ", fixed_to_float(alpha)); printf("%f\n", fabsf(sinf(fixed_to_float(alpha)) - fixed_to_float(fixed_sin(alpha)))); } } #endif return 0; }
void mat44_perspective(mat44 *m, fixed fov, fixed aspect, fixed near_z, fixed far_z) { fixed _fov = fixed_div(fixed_mul(FIXED_PI, fov), int_to_fixed(180)); fixed _f = fixed_div(FIXED_ONE, fixed_tan(fixed_mul(_fov, double_to_fixed(0.5)))); fixed nf = fixed_div((far_z + near_z), (near_z - far_z)); fixed nfr = fixed_div(fixed_mul(fixed_mul(int_to_fixed(2), far_z), near_z), (near_z - far_z)); mat44_set(m, fixed_div(_f, aspect), 0 , 0 , 0 , 0 , _f, 0 , 0 , 0 , 0 , nf , nfr, 0 , 0 , FIXED_NEGONE, 0); }
/** Calculates the result of plugging in @c value into the linear equation described by the @c offset and @c coeff as a fixed point value. Used in converting between units. @code retval = (coeff * value) + offset @endcode @param offset The offset of the linear equation. Often called @c b. @param coeff The coefficient of the linear equation. Often called @c m. @param value The value to be plugged into the linear equation. Often called @c x. @return The fixed point result of plugging @c value into the linear equation. */ fixed linear_to_fixed(int offset, float coeff, int value){ int a, b, c, d, e; a = value; b = a - offset; //subtract the offset c = -1 * int_to_fixed(b); //convert to fixed point d = float_to_fixed(coeff); //convert coefficient to fixed point e = fixed_mult(c,d); //multiply gain to get amps in fixed point return e; }
void draw_line_antialias_(GBitmap* img, int16_t x1, int16_t y1, int16_t x2, int16_t y2, GColor8 color) { uint8_t* img_pixels = gbitmap_get_data(img); int16_t w = gbitmap_get_bounds(img).size.w; int16_t h = gbitmap_get_bounds(img).size.h; fixed dx = int_to_fixed(abs_(x1 - x2)); fixed dy = int_to_fixed(abs_(y1 - y2)); bool steep = dy > dx; if(steep){ swap_(x1, y1); swap_(x2, y2); } if(x1 > x2){ swap_(x1, x2); swap_(y1, y2); } dx = x2 - x1; dy = y2 - y1; fixed intery; int x; for(x=x1; x <= x2; x++) { intery = int_to_fixed(y1) + (int_to_fixed(x - x1) * dy / dx); if(x>=0){ if(steep){ _plot(img_pixels, w, h, ipart_(intery) , x, color, rfpart_(intery)); _plot(img_pixels, w, h, ipart_(intery) + 1, x, color, fpart_(intery)); } else { _plot(img_pixels, w, h, x, ipart_(intery) , color, rfpart_(intery)); _plot(img_pixels, w, h, x, ipart_(intery) + 1, color, fpart_(intery)); } } } }
void image_downsize_gd_fixed_point(image *im) { int x, y; fixed_t sy1, sy2, sx1, sx2; int dstX = 0, dstY = 0, srcX = 0, srcY = 0; fixed_t width_scale, height_scale; int dstW = im->target_width; int dstH = im->target_height; int srcW = im->width; int srcH = im->height; if (im->height_padding) { dstY = im->height_padding; dstH = im->height_inner; } if (im->width_padding) { dstX = im->width_padding; dstW = im->width_inner; } width_scale = fixed_div(int_to_fixed(srcW), int_to_fixed(dstW)); height_scale = fixed_div(int_to_fixed(srcH), int_to_fixed(dstH)); for (y = dstY; (y < dstY + dstH); y++) { sy1 = fixed_mul(int_to_fixed(y - dstY), height_scale); sy2 = fixed_mul(int_to_fixed((y + 1) - dstY), height_scale); for (x = dstX; (x < dstX + dstW); x++) { fixed_t sx, sy; fixed_t spixels = 0; fixed_t red = 0, green = 0, blue = 0, alpha = 0; if (!im->has_alpha) alpha = FIXED_255; sx1 = fixed_mul(int_to_fixed(x - dstX), width_scale); sx2 = fixed_mul(int_to_fixed((x + 1) - dstX), width_scale); sy = sy1; /* DEBUG_TRACE("sx1 %f, sx2 %f, sy1 %f, sy2 %f\n", fixed_to_float(sx1), fixed_to_float(sx2), fixed_to_float(sy1), fixed_to_float(sy2)); */ do { fixed_t yportion; //DEBUG_TRACE(" yportion(sy %f, sy1 %f, sy2 %f) = ", fixed_to_float(sy), fixed_to_float(sy1), fixed_to_float(sy2)); if (fixed_floor(sy) == fixed_floor(sy1)) { yportion = FIXED_1 - (sy - fixed_floor(sy)); if (yportion > sy2 - sy1) { yportion = sy2 - sy1; } sy = fixed_floor(sy); } else if (sy == fixed_floor(sy2)) { yportion = sy2 - fixed_floor(sy2); } else { yportion = FIXED_1; } //DEBUG_TRACE("%f\n", fixed_to_float(yportion)); sx = sx1; do { fixed_t xportion; fixed_t pcontribution; pix p; //DEBUG_TRACE(" xportion(sx %f, sx1 %f, sx2 %f) = ", fixed_to_float(sx), fixed_to_float(sx1), fixed_to_float(sx2)); if (fixed_floor(sx) == fixed_floor(sx1)) { xportion = FIXED_1 - (sx - fixed_floor(sx)); if (xportion > sx2 - sx1) { xportion = sx2 - sx1; } sx = fixed_floor(sx); } else if (sx == fixed_floor(sx2)) { xportion = sx2 - fixed_floor(sx2); } else { xportion = FIXED_1; } //DEBUG_TRACE("%f\n", fixed_to_float(xportion)); pcontribution = fixed_mul(xportion, yportion); p = get_pix(im, fixed_to_int(sx + srcX), fixed_to_int(sy + srcY)); /* DEBUG_TRACE(" merging with pix %d, %d: src %x (%d %d %d %d), pcontribution %f\n", fixed_to_int(sx + srcX), fixed_to_int(sy + srcY), p, COL_RED(p), COL_GREEN(p), COL_BLUE(p), COL_ALPHA(p), fixed_to_float(pcontribution)); */ red += fixed_mul(int_to_fixed(COL_RED(p)), pcontribution); green += fixed_mul(int_to_fixed(COL_GREEN(p)), pcontribution); blue += fixed_mul(int_to_fixed(COL_BLUE(p)), pcontribution); if (im->has_alpha) alpha += fixed_mul(int_to_fixed(COL_ALPHA(p)), pcontribution); spixels += pcontribution; sx += FIXED_1; } while (sx < sx2); sy += FIXED_1; } while (sy < sy2); // If rgba get too large for the fixed-point representation, fallback to the floating point routine // This should only happen with very large images if (red < 0 || green < 0 || blue < 0 || alpha < 0) { warn("fixed-point overflow: %d %d %d %d\n", red, green, blue, alpha); return image_downsize_gd(im); } if (spixels != 0) { /* DEBUG_TRACE(" rgba (%f %f %f %f) spixels %f\n", fixed_to_float(red), fixed_to_float(green), fixed_to_float(blue), fixed_to_float(alpha), fixed_to_float(spixels)); */ spixels = fixed_div(FIXED_1, spixels); red = fixed_mul(red, spixels); green = fixed_mul(green, spixels); blue = fixed_mul(blue, spixels); if (im->has_alpha) alpha = fixed_mul(alpha, spixels); } /* Clamping to allow for rounding errors above */ if (red > FIXED_255) red = FIXED_255; if (green > FIXED_255) green = FIXED_255; if (blue > FIXED_255) blue = FIXED_255; if (im->has_alpha && alpha > FIXED_255) alpha = FIXED_255; /* DEBUG_TRACE(" -> %d, %d %x (%d %d %d %d)\n", x, y, COL_FULL(fixed_to_int(red), fixed_to_int(green), fixed_to_int(blue), fixed_to_int(alpha)), fixed_to_int(red), fixed_to_int(green), fixed_to_int(blue), fixed_to_int(alpha)); */ if (im->orientation != ORIENTATION_NORMAL) { int ox, oy; // new destination pixel coordinates after rotating image_get_rotated_coords(im, x, y, &ox, &oy); if (im->orientation >= 5) { // 90 and 270 rotations, width/height are swapped so we have to use alternate put_pix method put_pix_rotated( im, ox, oy, im->target_height, COL_FULL(fixed_to_int(red), fixed_to_int(green), fixed_to_int(blue), fixed_to_int(alpha)) ); } else { put_pix( im, ox, oy, COL_FULL(fixed_to_int(red), fixed_to_int(green), fixed_to_int(blue), fixed_to_int(alpha)) ); } } else { put_pix( im, x, y, COL_FULL(fixed_to_int(red), fixed_to_int(green), fixed_to_int(blue), fixed_to_int(alpha)) ); } } } }
void menu_input( void ) { switch (last_key) { #if defined(TARGET_GP2X) || defined(TARGET_UNIX) case SDLK_ESCAPE: quit = true; break; #endif case GP2X_KB_SELECT: toggle_file_selector(); case GP2X_KB_START: case GP2X_KB_Y: case GP2X_KB_X: toggle_menu(); need_redraw = true; break; case GP2X_KB_A: case GP2X_KB_B: switch (menu_select) { case 2: set_scale(int_to_fixed(1)); break; case 3: set_scale(fit_inside); fit_pref = 0; break; case 4: set_scale(fit_outside); fit_pref = 1; break; case 6: auto_fit = !auto_fit; break; case 7: auto_rotate = !auto_rotate; break; case 9: toggle_file_selector(); break; case 10: show_hidden = !show_hidden; toggle_file_visibility(); break; case 11: toggle_menu(); break; case 12: quit = true; break; } need_redraw = true; break; default: break; } if (input[0]) { switch (--menu_select) { case 1: menu_select = 12; break; case 5: case 8: menu_select--; break; } delay += 150; need_redraw = true; } if (input[2]) { switch (++menu_select) { case 5: case 8: menu_select++; break; case 13: menu_select = 2; break; } delay += 150; need_redraw = true; } }