void draw_sprite_scaled_alpha(gfx_context_t * ctx, sprite_t * sprite, int32_t x, int32_t y, uint16_t width, uint16_t height, float alpha) { int32_t _left = max(x, 0); int32_t _top = max(y, 0); int32_t _right = min(x + width, ctx->width - 1); int32_t _bottom = min(y + height, ctx->height - 1); for (uint16_t _y = 0; _y < height; ++_y) { for (uint16_t _x = 0; _x < width; ++_x) { if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom) continue; uint32_t n_color = getBilinearFilteredPixelColor(sprite, (double)_x / (double)width, (double)_y/(double)height); uint32_t f_color = rgb(_ALP(n_color) * alpha, 0, 0); GFX(ctx, x + _x, y + _y) = alpha_blend(GFX(ctx, x + _x, y + _y), n_color, f_color); } } }
void draw_sprite_alpha(gfx_context_t * ctx, sprite_t * sprite, int32_t x, int32_t y, float alpha) { int32_t _left = max(x, 0); int32_t _top = max(y, 0); int32_t _right = min(x + sprite->width, ctx->width - 1); int32_t _bottom = min(y + sprite->height, ctx->height - 1); for (uint16_t _y = 0; _y < sprite->height; ++_y) { for (uint16_t _x = 0; _x < sprite->width; ++_x) { if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom) continue; uint32_t n_color = SPRITE(sprite, _x, _y); uint32_t f_color = rgb(_ALP(n_color) * alpha, 0, 0); GFX(ctx, x + _x, y + _y) = alpha_blend(GFX(ctx, x + _x, y + _y), n_color, f_color); } } }
// This version of Init() is called when creating a print DC nsresult nsDeviceContextOS2::Init( nsNativeDeviceContext aContext, nsIDeviceContext *aOrigContext) { float origscale, newscale; float t2d, a2d; mPrintDC = (HDC)aContext; NS_ASSERTION( mPrintDC, "!ERROR! - Received empty DC for printer"); #ifdef XP_OS2 // Create a print PS now. This is necessary 'cos we need it from // odd places to do font-y things, where the only common reference // point is this DC. We can't just create a new PS because only one // PS can be associated with a given DC, and we can't get that PS from // the DC (really?). And it would be slow :-) SIZEL sizel = { 0 , 0 }; mPrintPS = GFX (::GpiCreatePS ( 0/*hab*/, mPrintDC, &sizel, PU_PELS | GPIT_MICRO | GPIA_ASSOC), GPI_ERROR); #endif CommonInit( mPrintDC); long dpi; GFX (::DevQueryCaps(mPrintDC, CAPS_VERTICAL_FONT_RES, 1, &dpi), FALSE); mPixelsToTwips = ((float)NSIntPointsToTwips(72)) / ((float)dpi); mTwipsToPixels = 1.0 / mPixelsToTwips; newscale = TwipsToDevUnits(); // On OS/2, origscale can be different based on the video resolution. // On 640x480, it's 1/15, on everything else it is 1/12. // For consistent printing, 1/15 is the correct value to use. // It is the closest to 4.x and to Windows. // origscale = aOrigContext->TwipsToDevUnits(); origscale = 1.0/15.0; mCPixelScale = newscale / origscale; t2d = aOrigContext->TwipsToDevUnits(); a2d = aOrigContext->AppUnitsToDevUnits(); mAppUnitsToDevUnits = (a2d / t2d) * mTwipsToPixels; mDevUnitsToAppUnits = 1.0f / mAppUnitsToDevUnits; #ifdef XP_OS2 HCINFO hcinfo; PrnQueryHardcopyCaps( mPrintDC, &hcinfo); mWidth = hcinfo.xPels; mHeight = hcinfo.yPels; // XXX hsb says there are margin problems, must be from here... #ifdef DEBUG printf( "Got surface of size %d x %d pixels (%d Kb)\n", mWidth, mHeight, mWidth * mHeight * mDepth / 8 / 1024); printf( "mCPixelScale = %f\n", mCPixelScale); #endif #endif return NS_OK; }
void print(enum log_level lvl, const char *fmt, ...) { struct timeval tv; va_list ap; va_start(ap, fmt); /* Timestamp */ gettimeofday(&tv, NULL); fprintf(stderr, "%8.3f ", timespec_delta(&epoch, &tv)); switch (lvl) { case DEBUG: fprintf(stderr, BLD("%-5s "), GRY("Debug")); break; case INFO: fprintf(stderr, BLD("%-5s "), WHT(" Info")); break; case WARN: fprintf(stderr, BLD("%-5s "), YEL(" Warn")); break; case ERROR: fprintf(stderr, BLD("%-5s "), RED("Error")); break; } if (_indent) { for (int i = 0; i < _indent-1; i++) fprintf(stderr, GFX("\x78") " "); fprintf(stderr, GFX("\x74") " "); } vfprintf(stderr, fmt, ap); fprintf(stderr, "\n"); va_end(ap); }
nsresult nsDeviceContextOS2::SetDPI(PRInt32 aPrefDPI) { // Set OSVal to what the operating system thinks the logical resolution is. long OSVal; HPS ps = ::WinGetScreenPS(HWND_DESKTOP); HDC hdc = GFX (::GpiQueryDevice (ps), HDC_ERROR); GFX (::DevQueryCaps(hdc, CAPS_HORIZONTAL_FONT_RES, 1, &OSVal), FALSE); ::WinReleasePS(ps); if (aPrefDPI > 0) { // If there's a valid pref value for the logical resolution, // use it. mDpi = aPrefDPI; } else if ((aPrefDPI == 0) || (OSVal > 96)) { // Either if the pref is 0 (force use of OS value) or the OS // value is bigger than 96, use the OS value. mDpi = OSVal; } else { // if we couldn't get the pref or it's negative, and the OS // value is under 96ppi, then use 96. mDpi = 96; } int pt2t = 72; // make p2t a nice round number - this prevents rounding problems mPixelsToTwips = float(NSToIntRound(float(NSIntPointsToTwips(pt2t)) / float(mDpi))); mTwipsToPixels = 1.0f / mPixelsToTwips; // XXX need to reflow all documents return NS_OK; }
static void render_decorations_fancy(yutani_window_t * window, gfx_context_t * ctx, char * title, int decors_active) { int width = window->width; int height = window->height; for (int j = 0; j < decor_top_height; ++j) { for (int i = 0; i < width; ++i) { GFX(ctx,i,j) = 0; } } for (int j = decor_top_height; j < height - decor_bottom_height; ++j) { for (int i = 0; i < decor_left_width; ++i) { GFX(ctx,i,j) = 0; } for (int i = width - decor_right_width; i < width; ++i) { GFX(ctx,i,j) = 0; } } for (int j = height - decor_bottom_height; j < height; ++j) { for (int i = 0; i < width; ++i) { GFX(ctx,i,j) = 0; } } if (decors_active == DECOR_INACTIVE) decors_active = INACTIVE; draw_sprite(ctx, sprites[decors_active + 0], 0, 0); for (int i = 0; i < width - (ul_width + ur_width); ++i) { draw_sprite(ctx, sprites[decors_active + 1], i + ul_width, 0); } draw_sprite(ctx, sprites[decors_active + 2], width - ur_width, 0); for (int i = 0; i < height - (u_height + l_height); ++i) { draw_sprite(ctx, sprites[decors_active + 3], 0, i + u_height); draw_sprite(ctx, sprites[decors_active + 4], width - mr_width, i + u_height); } draw_sprite(ctx, sprites[decors_active + 5], 0, height - l_height); for (int i = 0; i < width - (ll_width + lr_width); ++i) { draw_sprite(ctx, sprites[decors_active + 6], i + ll_width, height - l_height); } draw_sprite(ctx, sprites[decors_active + 7], width - lr_width, height - l_height); set_font_face(FONT_SANS_SERIF_BOLD); set_font_size(12); int title_offset = (width / 2) - (draw_string_width(title) / 2); if (decors_active == 0) { draw_string(ctx, title_offset, TEXT_OFFSET, rgb(226,226,226), title); } else { draw_string(ctx, title_offset, TEXT_OFFSET, rgb(147,147,147), title); } /* Buttons */ draw_sprite(ctx, sprites[decors_active + 8], width - 28, 16); }
void draw_fill(gfx_context_t * ctx, uint32_t color) { for (uint16_t y = 0; y < ctx->height; ++y) { for (uint16_t x = 0; x < ctx->width; ++x) { GFX(ctx, x, y) = color; } } }
void nsDeviceContextOS2 :: CommonInit(HDC aDC) { LONG alArray[CAPS_DEVICE_POLYSET_POINTS]; GFX (::DevQueryCaps(aDC, CAPS_FAMILY, CAPS_DEVICE_POLYSET_POINTS, alArray), FALSE); mDepth = alArray[CAPS_COLOR_BITCOUNT]; mIsPaletteDevice = ((alArray[CAPS_ADDITIONAL_GRAPHICS] & CAPS_PALETTE_MANAGER) == CAPS_PALETTE_MANAGER); mWidth = alArray[CAPS_WIDTH]; mHeight = alArray[CAPS_HEIGHT]; if (alArray[CAPS_TECHNOLOGY] == CAPS_TECH_RASTER_DISPLAY) { // init the screen manager and compute our client rect based on the // screen objects. We'll save the result nsresult ignore; mScreenManager = do_GetService("@mozilla.org/gfx/screenmanager;1", &ignore); if ( !sNumberOfScreens ) mScreenManager->GetNumberOfScreens(&sNumberOfScreens); mSupportsRasterFonts = !!(alArray[CAPS_RASTER_CAPS] & CAPS_RASTER_FONTS); } // if this dc is not a print device DeviceContextImpl::CommonInit(); }
NS_IMETHODIMP nsDeviceContextOS2 :: CheckFontExistence(const nsString& aFontName) { HPS hps = NULL; if (NULL != mPrintDC){ hps = mPrintPS; } else { hps = ::WinGetPS((HWND)mWidget); } nsAutoCharBuffer fontName; PRInt32 fontNameLength; WideCharToMultiByte(0, aFontName.get(), aFontName.Length(), fontName, fontNameLength); long lWant = 0; long lFonts = GFX (::GpiQueryFonts(hps, QF_PUBLIC | QF_PRIVATE, fontName.get(), &lWant, 0, 0), GPI_ALTERROR); if (NULL == mPrintDC) ::WinReleasePS(hps); if (lFonts > 0) return NS_OK; else return NS_ERROR_FAILURE; }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pScmdline, int iCmdshow) { InitWindows(800, 600, false); GFX m_GFX = GFX(m_hwnd); // Main Loop MSG msg_Message; while (Running) { m_GFX.ClearScene(FColour(1.0f, 0.0f, 0.0f, 1.0f)); m_GFX.Draw(); //Check for quit if(PeekMessage(&msg_Message,m_hwnd,0,0,PM_REMOVE)) { if(!IsDialogMessage(m_hwnd,&msg_Message)) { DispatchMessage(&msg_Message); } } } DestroyWindow(m_hwnd); return 0; }
void * draw_thread(void * garbage) { (void)garbage; double time = 0; /* Generate a palette */ uint32_t palette[256]; for (int x = 0; x < 256; ++x) { palette[x] = hsv_to_rgb(x,1.0,1.0); } while (!should_exit) { time += 1.0; int w = win_width; int h = win_height; spin_lock(&draw_lock); for (int x = 0; x < win_width; ++x) { for (int y = 0; y < win_height; ++y) { double value = sin(dist(x + time, y, 128.0, 128.0) / 8.0) + sin(dist(x, y, 64.0, 64.0) / 8.0) + sin(dist(x, y + time / 7, 192.0, 64) / 7.0) + sin(dist(x, y, 192.0, 100.0) / 8.0); GFX(ctx, x + off_x, y + off_y) = palette[(int)((value + 4) * 32)]; } } redraw_borders(); flip(ctx); yutani_flip(yctx, wina); spin_unlock(&draw_lock); syscall_yield(); } }
void draw_line_thick(gfx_context_t * ctx, int32_t x0, int32_t x1, int32_t y0, int32_t y1, uint32_t color, char thickness) { int deltax = abs(x1 - x0); int deltay = abs(y1 - y0); int sx = (x0 < x1) ? 1 : -1; int sy = (y0 < y1) ? 1 : -1; int error = deltax - deltay; while (1) { for (char j = -thickness; j <= thickness; ++j) { for (char i = -thickness; i <= thickness; ++i) { if (x0 + i >= 0 && x0 + i < ctx->width && y0 + j >= 0 && y0 + j < ctx->height) { GFX(ctx, x0 + i, y0 + j) = color; } } } if (x0 == x1 && y0 == y1) break; int e2 = 2 * error; if (e2 > -deltay) { error -= deltay; x0 += sx; } if (e2 < deltax) { error += deltax; y0 += sy; } } }
void context_to_png(FILE * file, gfx_context_t * ctx) { png_structp png_ptr = NULL; png_infop info_ptr = NULL; int32_t x, y; png_byte ** row_pointers = NULL; int status = -1; int depth = 8; png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); info_ptr = png_create_info_struct(png_ptr); if (setjmp(png_jmpbuf(png_ptr))) { goto png_write_failure; } png_set_IHDR(png_ptr, info_ptr, ctx->width, ctx->height, depth, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); row_pointers = png_malloc(png_ptr, ctx->height * sizeof(png_byte *)); for (y = 0; y < ctx->height; ++y) { png_byte * row = png_malloc(png_ptr, sizeof(uint8_t) * ctx->width * sizeof(uint32_t)); row_pointers[y] = row; for (x = 0; x < ctx->width; ++x) { uint32_t pixel = GFX(ctx, x, y); *row++ = _RED(pixel); *row++ = _GRE(pixel); *row++ = _BLU(pixel); *row++ = _ALP(pixel); } } png_init_io(png_ptr, file); png_set_rows(png_ptr, info_ptr, row_pointers); png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL); for (y = 0; y < ctx->height; y++) { png_free(png_ptr, row_pointers[y]); } png_free(png_ptr, row_pointers); fprintf(stderr, "Done writing PNG.\n"); return; png_write_failure: fprintf(stderr, "There was an exception while trying to write out a PNG file :(\n"); return; }
void redraw() { static double r = 0.0; for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { GFX(ctx,x+decor_left_width,y+decor_top_height) = (((y / 10) % 2 == 0) ^ ((x / 10) % 2 == 0)) ? rgb(107,107,107) : rgb(147,147,147); } } draw_sprite(ctx, &img, decor_left_width + width/2 - img.width/2, decor_top_height + height/2 - img.height/2); decors(); flip(ctx); r += 0.02; }
//-------------------- Reed-Solomon ---------------------------- //A Reed-Solomon code is a q^m-ary BCH code of length n = pow(q,m)-1. //k = pow(q,m)-1-t. This class works for q==2. Reed_Solomon::Reed_Solomon(int in_m, int in_t, bool sys, int in_b): m(in_m), t(in_t), b(in_b), systematic(sys) { n = pow2i(m) - 1; k = pow2i(m) - 1 - 2 * t; q = pow2i(m); it_assert( (b >= 0) && (b < n), "Reed_Solomon::Reed_Solomon: narrow-sense parameter restricted to 0 <= b <= n."); GFX x(q, (char *)"-1 0"); ivec alphapow(1); g.set(q, (char *)"0"); for (int i = 1; i <= 2 * t; i++) { alphapow(0) = b + i - 1; g *= (x - GFX(q, alphapow)); } }
nsDeviceContextOS2::~nsDeviceContextOS2() { if(mPrintDC) { GFX (::GpiDestroyPS (mPrintPS), FALSE); ::DevCloseDC(mPrintDC); } else { nsresult rv; nsCOMPtr<nsIPref> prefs = do_GetService(NS_PREF_CONTRACTID, &rv); if (NS_SUCCEEDED(rv)) { prefs->UnregisterCallback("layout.css.dpi", prefChanged, (void *)this); } } NS_IF_RELEASE(mSpec); }
void draw_line(gfx_context_t * ctx, int32_t x0, int32_t x1, int32_t y0, int32_t y1, uint32_t color) { int deltax = abs(x1 - x0); int deltay = abs(y1 - y0); int sx = (x0 < x1) ? 1 : -1; int sy = (y0 < y1) ? 1 : -1; int error = deltax - deltay; while (1) { if (x0 >= 0 && y0 >= 0 && x0 < ctx->width && y0 < ctx->height) { GFX(ctx, x0, y0) = color; } if (x0 == x1 && y0 == y1) break; int e2 = 2 * error; if (e2 > -deltay) { error -= deltay; x0 += sx; } if (e2 < deltax) { error += deltax; y0 += sy; } } }
static void render_decorations_simple(window_t * window, gfx_context_t * ctx, char * title, int decors_active) { for (uint32_t i = 0; i < window->height; ++i) { GFX(ctx, 0, i) = BORDERCOLOR; GFX(ctx, window->width - 1, i) = BORDERCOLOR; } for (uint32_t i = 1; i < decor_top_height; ++i) { for (uint32_t j = 1; j < window->width - 1; ++j) { GFX(ctx, j, i) = BACKCOLOR; } } draw_string(ctx, TEXT_OFFSET_X, TEXT_OFFSET_Y, TEXTCOLOR, title); for (uint32_t i = 0; i < window->width; ++i) { GFX(ctx, i, 0) = BORDERCOLOR; GFX(ctx, i, decor_top_height - 1) = BORDERCOLOR; GFX(ctx, i, window->height - 1) = BORDERCOLOR; } }
void draw_sprite(gfx_context_t * ctx, sprite_t * sprite, int32_t x, int32_t y) { int32_t _left = max(x, 0); int32_t _top = max(y, 0); int32_t _right = min(x + sprite->width, ctx->width - 1); int32_t _bottom = min(y + sprite->height, ctx->height - 1); for (uint16_t _y = 0; _y < sprite->height; ++_y) { for (uint16_t _x = 0; _x < sprite->width; ++_x) { if (x + _x < _left || x + _x > _right || y + _y < _top || y + _y > _bottom) continue; if (sprite->alpha == ALPHA_MASK) { GFX(ctx, x + _x, y + _y) = alpha_blend(GFX(ctx, x + _x, y + _y), SPRITE(sprite, _x, _y), SMASKS(sprite, _x, _y)); } else if (sprite->alpha == ALPHA_EMBEDDED) { GFX(ctx, x + _x, y + _y) = alpha_blend_rgba(GFX(ctx, x + _x, y + _y), SPRITE(sprite, _x, _y)); } else if (sprite->alpha == ALPHA_INDEXED) { if (SPRITE(sprite, _x, _y) != sprite->blank) { GFX(ctx, x + _x, y + _y) = SPRITE(sprite, _x, _y) | 0xFF000000; } } else { GFX(ctx, x + _x, y + _y) = SPRITE(sprite, _x, _y) | 0xFF000000; } } } }
static void drawpage(fz_context *ctx, fz_document *doc, int pagenum) { fz_page *page; fz_display_list *list = NULL; fz_device *dev = NULL; int start; fz_cookie cookie = { 0 }; fz_var(list); fz_var(dev); fz_try(ctx) { page = fz_load_page(doc, pagenum - 1); } fz_catch(ctx) { fz_throw(ctx, "cannot load page %d in file '%s'", pagenum, filename); } float zoom; fz_matrix ctm; fz_rect bounds, bounds2; fz_bbox bbox; fz_pixmap *pix = NULL; int w, h; fz_var(pix); bounds = fz_bound_page(doc, page); zoom = resolution / 72; ctm = fz_scale(zoom, zoom); ctm = fz_concat(ctm, fz_rotate(rotation)); bounds2 = fz_transform_rect(ctm, bounds); bbox = fz_round_rect(bounds2); /* Make local copies of our width/height */ w = width; h = height; /* If a resolution is specified, check to see whether w/h are * exceeded; if not, unset them. */ if (res_specified) { int t; t = bbox.x1 - bbox.x0; if (w && t <= w) w = 0; t = bbox.y1 - bbox.y0; if (h && t <= h) h = 0; } /* Now w or h will be 0 unless then need to be enforced. */ if (w || h) { float scalex = w/(bounds2.x1-bounds2.x0); float scaley = h/(bounds2.y1-bounds2.y0); if (fit) { if (w == 0) scalex = 1.0f; if (h == 0) scaley = 1.0f; } else { if (w == 0) scalex = scaley; if (h == 0) scaley = scalex; } if (!fit) { if (scalex > scaley) scalex = scaley; else scaley = scalex; } ctm = fz_concat(ctm, fz_scale(scalex, scaley)); bounds2 = fz_transform_rect(ctm, bounds); } bbox = fz_round_rect(bounds2); /* TODO: banded rendering and multi-page ppm */ fz_try(ctx) { pix = fz_new_pixmap_with_bbox(ctx, colorspace, bbox); fz_clear_pixmap_with_value(ctx, pix, 255); dev = fz_new_draw_device(ctx, pix); if (list) fz_run_display_list(list, dev, ctm, bbox, &cookie); else fz_run_page(doc, page, dev, ctm, &cookie); fz_free_device(dev); dev = NULL; int size = fz_pixmap_height(ctx, pix) * fz_pixmap_width(ctx, pix); inplace_reorder(fz_pixmap_samples(ctx, pix), size); size_t x_offset = (width - fz_pixmap_width(ctx, pix)) / 2; size_t y_offset = (height - fz_pixmap_height(ctx, pix)) / 2;; if (toggle_decors) { x_offset += decor_left_width; y_offset += decor_top_height; } for (int i = 0; i < fz_pixmap_height(ctx, pix); ++i) { memcpy(&GFX(gfx_ctx, x_offset, y_offset + i), &fz_pixmap_samples(ctx, pix)[fz_pixmap_width(ctx, pix) * i * 4], fz_pixmap_width(ctx, pix) * 4); } } fz_always(ctx) { fz_free_device(dev); dev = NULL; fz_drop_pixmap(ctx, pix); } fz_catch(ctx) { fz_free_display_list(ctx, list); fz_free_page(doc, page); fz_rethrow(ctx); } if (list) fz_free_display_list(ctx, list); fz_free_page(doc, page); fz_flush_warnings(ctx); if (cookie.errors) errored = 1; }
bool Reed_Solomon::decode(const bvec &coded_bits, const ivec &erasure_positions, bvec &decoded_message, bvec &cw_isvalid) { bool decoderfailure, no_dec_failure; int j, i, kk, l, L, foundzeros, iterations = floor_i(static_cast<double>(coded_bits.length()) / (n * m)); bvec mbit(m * k); decoded_message.set_size(iterations * k * m, false); cw_isvalid.set_length(iterations); GFX rx(q, n - 1), cx(q, n - 1), mx(q, k - 1), ex(q, n - 1), S(q, 2 * t), Xi(q, 2 * t), Gamma(q), Lambda(q), Psiprime(q), OldLambda(q), T(q), Omega(q); GFX dummy(q), One(q, (char*)"0"), Omegatemp(q); GF delta(q), tempsum(q), rtemp(q), temp(q), Xk(q), Xkinv(q); ivec errorpos; if ( erasure_positions.length() ) { it_assert(max(erasure_positions) < iterations*n, "Reed_Solomon::decode: erasure position is invalid."); } no_dec_failure = true; for (i = 0; i < iterations; i++) { decoderfailure = false; //Fix the received polynomial r(x) for (j = 0; j < n; j++) { rtemp.set(q, coded_bits.mid(i * n * m + j * m, m)); rx[j] = rtemp; } // Fix the Erasure polynomial Gamma(x) // and replace erased coordinates with zeros rtemp.set(q, -1); ivec alphapow = - ones_i(2); Gamma = One; for (j = 0; j < erasure_positions.length(); j++) { rx[erasure_positions(j)] = rtemp; alphapow(1) = erasure_positions(j); Gamma *= (One - GFX(q, alphapow)); } //Fix the syndrome polynomial S(x). S.clear(); for (j = 1; j <= 2 * t; j++) { S[j] = rx(GF(q, b + j - 1)); } // calculate the modified syndrome polynomial Xi(x) = Gamma * (1+S) - 1 Xi = Gamma * (One + S) - One; // Apply Berlekam-Massey algorithm if (Xi.get_true_degree() >= 1) { //Errors in the received word // Iterate to find Lambda(x), which hold all error locations kk = 0; Lambda = One; L = 0; T = GFX(q, (char*)"-1 0"); while (kk < 2 * t) { kk = kk + 1; tempsum = GF(q, -1); for (l = 1; l <= L; l++) { tempsum += Lambda[l] * Xi[kk - l]; } delta = Xi[kk] - tempsum; if (delta != GF(q, -1)) { OldLambda = Lambda; Lambda -= delta * T; if (2 * L < kk) { L = kk - L; T = OldLambda / delta; } } T = GFX(q, (char*)"-1 0") * T; } // Find the zeros to Lambda(x) errorpos.set_size(Lambda.get_true_degree()); foundzeros = 0; for (j = q - 2; j >= 0; j--) { if (Lambda(GF(q, j)) == GF(q, -1)) { errorpos(foundzeros) = (n - j) % n; foundzeros += 1; if (foundzeros >= Lambda.get_true_degree()) { break; } } } if (foundzeros != Lambda.get_true_degree()) { decoderfailure = true; } else { // Forney algorithm... //Compute Omega(x) using the key equation for RS-decoding Omega.set_degree(2 * t); Omegatemp = Lambda * (One + Xi); for (j = 0; j <= 2 * t; j++) { Omega[j] = Omegatemp[j]; } //Find the error/erasure magnitude polynomial by treating them the same Psiprime = formal_derivate(Lambda*Gamma); errorpos = concat(errorpos, erasure_positions); ex.clear(); for (j = 0; j < errorpos.length(); j++) { Xk = GF(q, errorpos(j)); Xkinv = GF(q, 0) / Xk; // we calculate ex = - error polynomial, in order to avoid the // subtraction when recunstructing the corrected codeword ex[errorpos(j)] = (Xk * Omega(Xkinv)) / Psiprime(Xkinv); if (b != 1) { // non-narrow-sense code needs corrected error magnitudes int correction_exp = ( errorpos(j)*(1-b) ) % n; ex[errorpos(j)] *= GF(q, correction_exp + ( (correction_exp < 0) ? n : 0 )); } } //Reconstruct the corrected codeword. // instead of subtracting the error/erasures, we calculated // the negative error with 'ex' above cx = rx + ex; //Code word validation S.clear(); for (j = 1; j <= 2 * t; j++) { S[j] = cx(GF(q, b + j - 1)); } if (S.get_true_degree() >= 1) { decoderfailure = true; } } } else { cx = rx; decoderfailure = false; } //Find the message polynomial mbit.clear(); if (decoderfailure == false) { if (cx.get_true_degree() >= 1) { // A nonzero codeword was transmitted if (systematic) { for (j = 0; j < k; j++) { mx[j] = cx[j]; } } else { mx = divgfx(cx, g); } for (j = 0; j <= mx.get_true_degree(); j++) { mbit.replace_mid(j * m, mx[j].get_vectorspace()); } } } else { //Decoder failure. // for a systematic code it is better to extract the undecoded message // from the received code word, i.e. obtaining a bit error // prob. p_b << 1/2, than setting all-zero (p_b = 1/2) if (systematic) { mbit = coded_bits.mid(i * n * m, k * m); } else { mbit = zeros_b(k); } no_dec_failure = false; } decoded_message.replace_mid(i * m * k, mbit); cw_isvalid(i) = (!decoderfailure); } return no_dec_failure; }
int main (int argc, char ** argv) { char * _windowed = getenv("DISPLAY"); if (_windowed) { setup_windowing(); resize_window_callback = resize_callback; win_width = 500; win_height = 500; init_decorations(); off_x = decor_left_width; off_y = decor_top_height; /* Do something with a window */ wina = window_create(300, 300, win_width + decor_width(), win_height + decor_height()); assert(wina); ctx = init_graphics_window_double_buffer(wina); } else { ctx = init_graphics_fullscreen_double_buffer(); win_width = ctx->width; win_height = ctx->height; off_x = 0; off_y = 0; } draw_fill(ctx, rgb(0,0,0)); redraw_borders(); flip(ctx); double time; /* Generate a palette */ uint32_t palette[256]; for (int x = 0; x < 256; ++x) { palette[x] = hsv_to_rgb(x,1.0,1.0); } while (1) { if (_windowed) { w_keyboard_t * kbd = poll_keyboard_async(); if (kbd) { char ch = kbd->key; free(kbd); if (ch == 'q') { goto done; break; } } } time += 1.0; int w = win_width; int h = win_height; for (int x = 0; x < win_width; ++x) { for (int y = 0; y < win_height; ++y) { double value = sin(dist(x + time, y, 128.0, 128.0) / 8.0) + sin(dist(x, y, 64.0, 64.0) / 8.0) + sin(dist(x, y + time / 7, 192.0, 64) / 7.0) + sin(dist(x, y, 192.0, 100.0) / 8.0); GFX(ctx, x + off_x, y + off_y) = palette[(int)((value + 4) * 32)]; } } redraw_borders(); flip(ctx); } done: if (_windowed) { teardown_windowing(); } return 0; }
bool NextionON = false; uint8_t NextionPage = 0; char buffer[100] = {0}; uint32_t slidermaxval = 20; char lcd_status_message[30] = WELCOME_MSG; uint8_t lcd_status_message_level = 0; static millis_t next_lcd_update_ms; #if ENABLED(SDSUPPORT) uint8_t SDstatus = 0; // 0 SD not insert, 1 SD insert, 2 SD printing NexUpload Firmware(NEXTION_FIRMWARE_FILE, 57600); #endif #if ENABLED(NEXTION_GFX) GFX gfx = GFX(200, 190); #endif // Page NexPage Pstart = NexPage(0, 0, "start"); NexPage Pinfo = NexPage(1, 0, "info"); NexPage Ptemp = NexPage(2, 0, "temp"); NexPage Pmenu = NexPage(3, 0, "menu"); NexPage Psdcard = NexPage(4, 0, "sdcard"); NexPage Psetup = NexPage(5, 0, "setup"); NexPage Pmove = NexPage(6, 0, "move"); NexPage Pspeed = NexPage(7, 0, "speed"); NexPage Pgcode = NexPage(8, 0, "gcode"); // Page 0 Start NexTimer startimer = NexTimer(0, 1, "tm0");