/// Calculate the Lagarias-Miller-Odlyzko alpha tuning factor. /// alpha = a log(x)^2 + b log(x) + c /// a, b and c are constants that should be determined empirically. /// @see ../doc/alpha-factor-tuning.pdf /// double get_alpha_lmo(int128_t x) { double alpha = get_alpha(); // use default alpha if no command-line alpha provided if (alpha < 1) { double a = 0.00156512; double b = -0.0261411; double c = 0.990948; double logx = log((double) x); alpha = a * pow(logx, 2) + b * logx + c; } return in_between(1, alpha, iroot<6>(x)); }
maxint_t S1(maxint_t x, int threads) { if (x < 1) return 0; if (print_status()) set_print_variables(true); double alpha = get_alpha(x, 0.0017154, -0.0508992, 0.483613, 0.0672202); int64_t y = (int64_t) (iroot<3>(x) * alpha); int64_t c = PhiTiny::get_c(y); if (x <= numeric_limits<int64_t>::max()) return S1((int64_t) x, y, c, threads); else return S1(x, y, c, threads); }
void test_colors() { printf("testing colors\n"); int r = 25; int g = 225; int b = 200; int a = 255; color c = get_color(r,g,b,a); assert(c == 0x19E1C8FF); assert(get_red(c) == r); assert(get_green(c) == g); assert(get_blue(c) == b); assert(get_alpha(c) == a); assert(set_red(c,0xAA) == 0xAAE1C8FF); assert(set_green(c,0xAA) == 0x19AAC8FF); assert(set_blue(c,0xAA) == 0x19E1AAFF); assert(set_alpha(c,0xAA) == 0x19E1C8AA); }
/// Calculate the Deleglise-Rivat alpha tuning factor. /// alpha = a log(x)^3 + b log(x)^2 + c log(x) + d /// a, b, c and d are constants that should be determined empirically. /// @see ../doc/alpha-tuning-factor.pdf /// double get_alpha_deleglise_rivat(int128_t x) { double alpha = get_alpha(); double x2 = (double) x; // use default alpha if no command-line alpha provided if (alpha < 1) { double a = 0.000356618; double b = 0.00263762; double c = -0.125227; double d = 1.39952; double logx = log(x2); alpha = a * pow(logx, 3) + b * pow(logx, 2) + c * logx + d; } return in_between(1, alpha, iroot<6>(x)); }
/** * Draws a curve for the speed graph. */ void GtkGraph::draw(std::queue<double> q, double height, double increment, double maxValue, const Cairo::RefPtr<Cairo::Context>& cr) { // wizards use computers // computers use numbers // no magic double offset = increment * (m_displaySize - q.size()); cr->move_to(0, height); for(unsigned i = 0; i< (m_displaySize - q.size());++i) cr->line_to(i*increment, height); double oldy; if(q.empty()) return; oldy = height - (q.front() * height / maxValue); cr->line_to(offset, oldy); q.pop(); double x = increment + offset; while(!q.empty()) { double y = height - (q.front() * height / maxValue); cr->curve_to(x - increment/2, oldy, x - increment/2, y, x, y); q.pop(); oldy = y; x += increment; } if(gt::Settings::settings["GraphStyle"] == "Fill") { cr->stroke_preserve(); Gdk::Cairo::set_source_rgba(cr, Gdk::RGBA(gt::Settings::settings[(upl) ? "GraphUploadFillColor" : "GraphDownloadFillColor"])); cr->line_to(x - increment, height); cr->line_to(0,height); auto k = Gdk::RGBA(gt::Settings::settings[(upl) ? "GraphUploadFillColor" : "GraphDownloadFillColor"]); cr->set_source_rgba(k.get_red(), k.get_green(), k.get_blue(), k.get_alpha() * 0.5); cr->fill(); } else cr->stroke(); }
maxint_t S2_hard(maxint_t x, int threads) { if (x < 1) return 0; if (print_status()) set_print_variables(true); double alpha = get_alpha(x, 0.0017154, -0.0508992, 0.483613, 0.0672202); int64_t y = (int64_t) (iroot<3>(x) * alpha); int64_t z = (int64_t) (x / y); int64_t c = PhiTiny::get_c(y); // TODO: find better S2_hard approximation formula maxint_t s2_hard_approx = Li(x); if (x <= numeric_limits<int64_t>::max()) return S2_hard((int64_t) x, y, z, c, (int64_t) s2_hard_approx, threads); else return S2_hard(x, y, z, c, s2_hard_approx, threads); }
/// Calculate the number of primes below x using the /// Deleglise-Rivat algorithm. /// Run time: O(x^(2/3) / (log x)^2) operations, O(x^(1/3) * (log x)^3) space. /// int64_t pi_deleglise_rivat_parallel1(int64_t x, int threads) { if (x < 2) return 0; double alpha = get_alpha(x, 0.0017154, -0.0508992, 0.483613, 0.0672202); int64_t x13 = iroot<3>(x); int64_t y = (int64_t) (x13 * alpha); int64_t z = x / y; int64_t p2 = P2(x, y, threads); vector<int32_t> mu = generate_moebius(y); vector<int32_t> lpf = generate_least_prime_factors(y); vector<int32_t> primes = generate_primes(y); int64_t pi_y = pi_bsearch(primes, y); int64_t c = PhiTiny::get_c(y); int64_t s1 = S1(x, y, c, threads); int64_t s2 = S2(x, y, z, c, primes, lpf, mu, threads); int64_t phi = s1 + s2; int64_t sum = phi + pi_y - 1 - p2; return sum; }
int bmp_image::output_to_file(const std::string& file_name) const { std::ofstream out(file_name.c_str(), std::ios_base::binary | std::ios_base::out); if ( !out ) { return 1; } //full header size 122 /* BMP structure * File Header * 0 Magic number 0x42, 0x4d (2 bytes) * 2 File size = w*h + h*(w%4) + 122 ? (4 bytes) * 6 Unused (4 bytes) * 10 Pixel array offset = 122 (4 bytes) * DIB Header * 14 Bytes in DIB Header = 108 (4 bytes) * 18 Bitmap width (4 bytes) * 22 Bitmap width (4 bytes) * 26 Color planes = 1 (2 bytes) * 28 Bits/pixel = 32 (2 bytes) * 30 BI_BITFIELDS = 3 (no compression used) (4 bytes) * 34 Size of the raw data in the Pixel Array = w*h + h*(w%4) ? (incl padding) (4 bytes) * 38 horizonal pixels/meter = 2835 (4 bytes) * 42 vertival pixels/meter = 2835 (4 bytes) * 46 Number of colors in the palette = 0 (4 bytes) * 50 Important colors = 0 (4 bytes) * 54 Red channel bit mask = 0x00FF0000 (4 bytes) * 58 Green channel bit mask = 0x0000FF00 (4 bytes) * 62 Blue channel bit mask = 0x000000FF (4 bytes) * 66 Alpha channel bit mask = 0xFF000000 (4 bytes) * 70 Color space type = 0x206E6957 ?? (4 bytes) //LCS_WINDOWS_COLOR_SPACE * 74 CIEXYZTRIPLE Color Space (unused) (36 bytes) * 110 red gamma = unused (4 bytes) * 114 green gamma = unused (4 bytes) * 118 blue gamma = unused (4 bytes) * 122 <Pixel Data> */ //TODO endianess //const boost::uint8_t unused_8 = 0; //const boost::uint16_t unused_16 = 0; const boost::uint32_t unused_32 = 0; //File Header out.write("\x42\x4d", 2); //magic number const boost::uint32_t file_size = width*height*4 + 122; out.write( (const char *)(&file_size), 4); //file_size out.write( (const char *)(&unused_32), 4); //unused const boost::uint32_t pixel_array_offset = 122; out.write( (const char *)(&pixel_array_offset), 4); //pixel_array_offset const boost::uint32_t dib_header_size = 108; out.write( (const char *)(&dib_header_size), 4); //dib_header_size const boost::uint32_t bitmap_width = width; const boost::uint32_t bitmap_height = height; out.write( (const char *)(&bitmap_width), 4); //bitmap_width out.write( (const char *)(&bitmap_height), 4); //bitmap_height const boost::uint16_t color_planes = 1; out.write( (const char *)(&color_planes), 2); //color_planes const boost::uint16_t bits_per_pixel = 32; out.write( (const char *)(&bits_per_pixel), 2); //bits_per_pixel const boost::uint32_t bitfields = 3; out.write( (const char *)(&bitfields), 4); //bitfields const boost::uint32_t pixel_array_size = width*height*4; out.write( (const char *)(&pixel_array_size), 4); //pixel_array_size const boost::uint32_t horizontal_physical_resolution = 2835; const boost::uint32_t vertical_physical_resolution = 2835; out.write( (const char *)(&horizontal_physical_resolution), 4); //horizontal_physical_resolution out.write( (const char *)(&vertical_physical_resolution), 4); //vertical_physical_resolution out.write( (const char *)(&unused_32), 4); //num of colors on palette out.write( (const char *)(&unused_32), 4); //num of important colors const boost::uint32_t red_channel_bit_mask = bmp_impl::endian_swap( 0x00FF0000U ); const boost::uint32_t green_channel_bit_mask = bmp_impl::endian_swap( 0x0000FF00U ); const boost::uint32_t blue_channel_bit_mask = bmp_impl::endian_swap( 0x000000FFU ); const boost::uint32_t alpha_channel_bit_mask = bmp_impl::endian_swap( 0xFF000000U ); out.write( (const char *)(&red_channel_bit_mask), 4); //red_channel_bit_mask out.write( (const char *)(&green_channel_bit_mask), 4); //green_channel_bit_mask out.write( (const char *)(&blue_channel_bit_mask), 4); //blue_channel_bit_mask out.write( (const char *)(&alpha_channel_bit_mask), 4); //alpha_channel_bit_mask const boost::uint32_t color_space_type = bmp_impl::endian_swap( 0x206E6957U ); out.write( (const char *)(&color_space_type), 4); //color_space_type //CIEXYZTRIPLE Color Space (unused) (36 bytes) for(unsigned i = 0; i < 36/4; ++i) { out.write( (const char *)(&unused_32), 4); } out.write( (const char *)(&unused_32), 4); //red gamma out.write( (const char *)(&unused_32), 4); //green gamma out.write( (const char *)(&unused_32), 4); //blue gamma for(unsigned cy = 0; cy < pixels.height(); ++cy) { for(unsigned x = 0; x < pixels.width(); ++x) { unsigned y = pixels.height() - cy - 1; const single_color_t alpha = get_alpha(pixels(x, y)); const single_color_t red = get_red(pixels(x, y)); const single_color_t green = get_green(pixels(x, y)); const single_color_t blue = get_blue(pixels(x, y)); out.write((const char *)&alpha, 1); out.write((const char *)&red, 1); out.write((const char *)&green, 1); out.write((const char *)&blue, 1); } //no padding required as the pixels themselves are 4 bytes long } out.close(); return 0; }
picture erode (picture pic, picture pen) { raster<true_color> ras= as_raster<true_color> (pic); raster<double> alpha= get_alpha (as_raster<true_color> (pen)); return raster_picture (erode (ras, alpha)); }
picture outline (picture pic, picture pen) { raster<true_color> ras= as_raster<true_color> (pic); raster<double> alpha= get_alpha (as_raster<true_color> (pen)); return raster_picture (variation (ras, alpha)); }
static gboolean matting_process (GeglOperation *operation, GeglBuffer *input_buf, GeglBuffer *aux_buf, GeglBuffer *output_buf, const GeglRectangle *result, int level) { const GeglProperties *o = GEGL_PROPERTIES (operation); gfloat *input = NULL; guchar *trimap = NULL; gfloat *output = NULL; BufferRecord *buffer = NULL; gboolean success = FALSE; int w, h, i, x, y, xdiff, ydiff, neighbour_mask; GArray *foreground_samples, *background_samples; GArray *unknown_positions; g_return_val_if_fail (babl_format_get_n_components (babl_format (FORMAT_INPUT )) == COMPONENTS_INPUT, FALSE); g_return_val_if_fail (babl_format_get_n_components (babl_format (FORMAT_AUX )) == COMPONENTS_AUX, FALSE); g_return_val_if_fail (babl_format_get_n_components (babl_format (FORMAT_OUTPUT)) == COMPONENTS_OUTPUT, FALSE); g_return_val_if_fail (operation, FALSE); g_return_val_if_fail (input_buf, FALSE); g_return_val_if_fail (aux_buf, FALSE); g_return_val_if_fail (output_buf, FALSE); g_return_val_if_fail (result, FALSE); w = result->width; h = result->height; input = g_new (gfloat, w * h * COMPONENTS_INPUT); trimap = g_new (guchar, w * h * COMPONENTS_AUX); output = g_new0 (gfloat, w * h * COMPONENTS_OUTPUT); buffer = g_new0 (BufferRecord, w * h); gegl_buffer_get (input_buf, result, 1.0, babl_format (FORMAT_INPUT), input, GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE); gegl_buffer_get ( aux_buf, result, 1.0, babl_format (FORMAT_AUX), trimap, GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE); foreground_samples = g_array_new(FALSE, FALSE, sizeof(ColorSample)); background_samples = g_array_new(FALSE, FALSE, sizeof(ColorSample)); unknown_positions = g_array_new(FALSE, FALSE, sizeof(Position)); // Get mask for (y = 0; y < h; y++) { for (x = 0; x < w; x++) { int mask = trimap[y * w + x]; for (ydiff = -1; ydiff <= 1; ydiff++) { // Borders if (y+ydiff < 0 || y+ydiff >= h) continue; for (xdiff = -1; xdiff <= 1; xdiff++) { // Borders if (x+xdiff < 0 || x+xdiff >= w) continue; neighbour_mask = trimap[(y + ydiff) * w + x + xdiff]; if (neighbour_mask != mask && (mask == 0 || mask == 255)) { int index = y*w+x; ColorSample s; s.pos.x = x; s.pos.y = y; COLOR(s.color[c] = input[index*3 + c]); if (mask == 255) { g_array_append_val(foreground_samples, s); buffer[index].fg_distance = 0; buffer[index].bg_distance = FLT_MAX; } else { g_array_append_val(background_samples, s); buffer[index].fg_distance = 0; buffer[index].bg_distance = FLT_MAX; } // Go to next pixel xdiff = 1; ydiff = 1; } } } } } /* If we have no information to work with, there is nothing to process. */ if (foreground_samples->len == 0 || background_samples->len == 0) { success = FALSE; goto cleanup; } // Initialize unknowns for (y = 0; y < h; y++) { for (x = 0; x < w; x++) { int index = y * w + x; if (trimap[index] != 0 && trimap[index] != 255) { Position p; p.x = x; p.y = y; g_array_append_val(unknown_positions, p); buffer[index].fg_distance = FLT_MAX; buffer[index].bg_distance = FLT_MAX; buffer[index].fg_index = rand() % foreground_samples->len; buffer[index].bg_index = rand() % background_samples->len; } } } g_array_sort(foreground_samples, color_compare); g_array_sort(background_samples, color_compare); // Do real iterations for (i = 0; i < o->iterations; i++) { unsigned j; GEGL_NOTE (GEGL_DEBUG_PROCESS, "Iteration %i", i); for (j=0; j<unknown_positions->len; j++) { Position p = g_array_index(unknown_positions, Position, j); do_random_search(foreground_samples, background_samples, input, buffer, p.x, p.y, w); } for (j=0; j<unknown_positions->len; j++) { Position p = g_array_index(unknown_positions, Position, j); do_propagate(foreground_samples, background_samples, input, buffer, trimap, p.x, p.y, w, h); } } // Fill results in for (y = 0; y < h; y++) { for (x = 0; x < w; x++) { int index = y * w + x; if (trimap[index] == 0 || trimap[index] == 255) { if (trimap[index] == 0) { output[index] = 0; } else if (trimap[index] == 255) { output[index] = 1; } } else { ColorSample background, foreground; foreground = g_array_index(foreground_samples, ColorSample, buffer[index].fg_index); background = g_array_index(background_samples, ColorSample, buffer[index].bg_index); output[index] = get_alpha(foreground.color, background.color, &input[index * 3]); } } } // Save to buffer gegl_buffer_set (output_buf, result, 0, babl_format (FORMAT_OUTPUT), output, GEGL_AUTO_ROWSTRIDE); success = TRUE; cleanup: g_free (input); g_free (trimap); g_free (output); g_free (buffer); g_array_free (foreground_samples, TRUE); g_array_free (background_samples, TRUE); g_array_free (unknown_positions, TRUE); return success; }