int HaarDetector::init(unsigned int image_width, unsigned int image_height) { if (m_object_sizes.size() == 0) return -1; if (m_ai_classifiers.size() == 0) return -2; AiClassifier* pai = m_ai_classifiers[m_ai_classifiers.size() - 1]; if (pai->ai_type() != AiClassifier::SIGMOID_ANN) return -3; m_image_width = image_width; m_image_height = image_height; m_integral_image = new vec2Di(get_image_height(), get_image_width()); for (unsigned int i = 0; i < (unsigned int)m_object_sizes.size(); i++) { const ObjectSize& osize = m_object_sizes[i]; ObjectMap* omap = new ObjectMap(get_image_width(), get_image_height(), osize.width, osize.height); m_object_maps.push_back(omap); } m_search_mask = new vec2Di(get_image_height(), get_image_width()); m_tmp_search_mask = new vec2Di(get_image_height(), get_image_width()); m_status = 0; return 0; }
__kernel void resize_image(__read_only image2d_t input, const sampler_t sampler, __write_only image2d_t output) { const uint x = get_global_id(0); const uint y = get_global_id(1); const float w = get_image_width(output); const float h = get_image_height(output); float2 coord = { ((float) x / w) * get_image_width(input), ((float) y / h) * get_image_height(input) }; float4 pixel = read_imagef(input, sampler, coord); write_imagef(output, (int2)(x, h - y - 1), pixel); };
static void autovscale(DDLSymbolTable *st, float *vmin, float *vmax) { int i; int n; float min; float max; // Find max and min values in the data int npts = (get_image_width(st) * get_image_height(st) * get_image_depth(st)); get_min_max(st, &min, &max); // Get intensity distribution int nbins = 10000; int *histogram = new int[nbins]; get_histogram(st, min, max, histogram, nbins); // Estimate percentile points from distribution int percentile = (int)(0.01 * npts); if (percentile < 1) percentile = 1; float scale = (nbins - 1) / (max - min); for (i=n=0; n<percentile && i<nbins; n += histogram[i++]); *vmin = min + (i-1) / scale; for (i=nbins, n=0; n<percentile && i>0; n += histogram[--i]); *vmax = min + i / scale; // Set vmin to 0 if it looks plausible if (*vmin > 0 && *vmin / *vmax < 0.05){ *vmin = 0; } delete [] histogram; }
void HaarDetector::compute_integral_image(const vec2Di& image) { if (status() < 0) return; //copy image -> m_integral_image vec2Di& v = *m_integral_image; v = image; for (unsigned int i = 1; i < get_image_height(); i++) v(i, 0) += v(i - 1, 0); for (unsigned int j = 1; j < get_image_width(); j++) v(0, j) += v(0, j - 1); for (unsigned int i = 1; i < get_image_height(); i++) for (unsigned int j = 1; j < get_image_width(); j++) v(i, j) += (v(i, j - 1) + v(i - 1, j)) - v(i - 1, j - 1); }
int2 OVERLOAD get_image_dim(image2d_t image) { int2 result; result.x = get_image_width(image); result.y = get_image_height(image); return result; }
int4 OVERLOAD get_image_dim(image3d_t image) { int4 result; result.x = get_image_width(image); result.y = get_image_height(image); result.z = get_image_depth(image); result.w = 0; return result; }
void set_glyph_image(font_t* font, int codepoint, image_t* image) { image_t* old_image; struct font_glyph* p_glyph; p_glyph = &font->glyphs[codepoint]; old_image = p_glyph->image; p_glyph->image = ref_image(image); p_glyph->width = get_image_width(image); p_glyph->height = get_image_height(image); free_image(old_image); }
kernel void sobel_rgb(read_only image2d_t src, write_only image2d_t dst) { int x = (int)get_global_id(0); int y = (int)get_global_id(1); if (x >= get_image_width(src) || y >= get_image_height(src)) return; // [(x-1, y+1), (x, y+1), (x+1, y+1)] // [(x-1, y ), (x, y ), (x+1, y )] // [(x-1, y-1), (x, y-1), (x+1, y-1)] // [p02, p12, p22] // [p01, pixel, p21] // [p00, p10, p20] //Basically finding influence of neighbour pixels on current pixel float4 p00 = read_imagef(src, sampler, (int2)(x - 1, y - 1)); float4 p10 = read_imagef(src, sampler, (int2)(x, y - 1)); float4 p20 = read_imagef(src, sampler, (int2)(x + 1, y - 1)); float4 p01 = read_imagef(src, sampler, (int2)(x - 1, y)); //pixel that we are working on float4 p21 = read_imagef(src, sampler, (int2)(x + 1, y)); float4 p02 = read_imagef(src, sampler, (int2)(x - 1, y + 1)); float4 p12 = read_imagef(src, sampler, (int2)(x, y + 1)); float4 p22 = read_imagef(src, sampler, (int2)(x + 1, y + 1)); //Find Gx = kernel + 3x3 around current pixel // Gx = [-1 0 +1] [p02, p12, p22] // [-2 0 +2] + [p01, pixel, p21] // [-1 0 +1] [p00, p10, p20] float3 gx = -p00.xyz + p20.xyz + 2.0f * (p21.xyz - p01.xyz) -p02.xyz + p22.xyz; //Find Gy = kernel + 3x3 around current pixel // Gy = [-1 -2 -1] [p02, p12, p22] // [ 0 0 0] + [p01, pixel, p21] // [+1 +2 +1] [p00, p10, p20] float3 gy = p00.xyz + p20.xyz + 2.0f * (- p12.xyz + p10.xyz) - p02.xyz - p22.xyz; //Find G float3 g = native_sqrt(gx * gx + gy * gy); // we could also approximate this as g = fabs(gx) + fabs(gy) write_imagef(dst, (int2)(x, y), (float4)(g.x, g.y, g.z, 1.0f)); }
static gpointer rectangle_create_info_widget(GtkWidget *frame) { RectangleProperties_t *props = g_new(RectangleProperties_t, 1); GtkWidget *table, *label, *chain_button; gint max_width = get_image_width(); gint max_height = get_image_height(); table = gtk_table_new(4, 4, FALSE); gtk_container_add(GTK_CONTAINER(frame), table); gtk_table_set_row_spacings(GTK_TABLE(table), 6); gtk_table_set_col_spacings(GTK_TABLE(table), 6); gtk_widget_show(table); label = create_label_in_table(table, 0, 0, _("Upper left _x:")); props->x = create_spin_button_in_table(table, label, 0, 1, 1, 0, max_width - 1); g_signal_connect(props->x, "value-changed", G_CALLBACK(x_changed_cb), (gpointer) props); create_label_in_table(table, 0, 3, _("pixels")); label = create_label_in_table(table, 1, 0, _("Upper left _y:")); props->y = create_spin_button_in_table(table, label, 1, 1, 1, 0, max_height - 1); g_signal_connect(props->y, "value-changed", G_CALLBACK(y_changed_cb), (gpointer) props); create_label_in_table(table, 1, 3, _("pixels")); label = create_label_in_table(table, 2, 0, _("_Width:")); props->width = create_spin_button_in_table(table, label, 2, 1, 1, 1, max_width); g_signal_connect(props->width, "value-changed", G_CALLBACK(width_changed_cb), (gpointer) props); create_label_in_table(table, 2, 3, _("pixels")); label = create_label_in_table(table, 3, 0, _("_Height:")); props->height = create_spin_button_in_table(table, label, 3, 1, 1, 1, max_height); g_signal_connect(props->height, "value-changed", G_CALLBACK(height_changed_cb), (gpointer) props); create_label_in_table(table, 3, 3, _("pixels")); chain_button = gimp_chain_button_new(GIMP_CHAIN_RIGHT); props->chain_button = chain_button; gtk_table_attach_defaults(GTK_TABLE(table), chain_button, 2, 3, 2, 4); gtk_widget_show(chain_button); return props; }
static void update_font_metrics(font_t* font) { int max_x = 0, max_y = 0; int min_width = INT_MAX; uint32_t i; for (i = 0; i < font->num_glyphs; ++i) { font->glyphs[i].width = get_image_width(font->glyphs[i].image); font->glyphs[i].height = get_image_height(font->glyphs[i].image); min_width = fmin(font->glyphs[i].width, min_width); max_x = fmax(font->glyphs[i].width, max_x); max_y = fmax(font->glyphs[i].height, max_y); } font->min_width = min_width; font->max_width = max_x; font->height = max_y; }
void mean_image(image *img1, image *img2, image *out) { mblock_t b1, b2; /* macrobblocks to be averaged */ int width, /* width of image */ height, /* height of image */ x, y, /* block loop indexes */ i, j; /* pixel loop indexes */ /* Get image width and height */ width = get_image_width(img1); height = get_image_height(img1); /* Scan through the images and process blocks */ for(y = 0; y < height; y+=16) { /* block width is 16 pixels */ for(x = 0; x < width; x+=16) { /* block height is 16 pixels */ /* get a block from the first input image */ get_mblock(img1, x, y, b1); /* get a block from the second input image */ get_mblock(img2, x, y, b2); /* Scan through the blocks and interpolate */ /* i.e. pixel_out = (pixel_in + pixel_out) / 2 */ for(i = 0; i < 16; i++) { for(j = 0; j < 16; j++) { /* store the result in block 1 */ b1[i][j] = (b1[i][j] + b2[i][j]) / 2; } } /* put the block in the output image */ put_mblock(out, x, y, b1); } } return; }
static gpointer circle_create_info_widget(GtkWidget *frame) { CircleProperties_t *props = g_new(CircleProperties_t, 1); GtkWidget *table, *label; gint max_width = get_image_width(); gint max_height = get_image_height(); table = gtk_table_new(3, 3, FALSE); gtk_container_add(GTK_CONTAINER(frame), table); gtk_table_set_row_spacings(GTK_TABLE(table), 6); gtk_table_set_col_spacings(GTK_TABLE(table), 6); gtk_widget_show(table); label = create_label_in_table(table, 0, 0, _("Center _x:")); props->x = create_spin_button_in_table(table, label, 0, 1, 1, 0, max_width - 1); g_signal_connect(props->x, "value-changed", G_CALLBACK (x_changed_cb), (gpointer) props); create_label_in_table(table, 0, 2, _("pixels")); label = create_label_in_table(table, 1, 0, _("Center _y:")); props->y = create_spin_button_in_table(table, label, 1, 1, 1, 0, max_height - 1); g_signal_connect(props->y, "value-changed", G_CALLBACK (y_changed_cb), (gpointer) props); create_label_in_table(table, 1, 2, _("pixels")); label = create_label_in_table(table, 2, 0, _("_Radius:")); props->r = create_spin_button_in_table(table, label, 2, 1, 1, 1, G_MAXINT); g_signal_connect(props->r, "value-changed", G_CALLBACK (r_changed_cb), (gpointer) props); create_label_in_table(table, 2, 2, _("pixels")); return props; }
static void get_min_max(DDLSymbolTable *st, float *vmin, float *vmax) { char *errmsg; int npts = (get_image_width(st) * get_image_height(st) * get_image_depth(st)); int type = get_data_type(st, &errmsg); switch (type){ case DDL_INT8: { u_char *data = (u_char *)get_ddl_data(st); u_char *end = data + npts; u_char min = *data; u_char max = *data++; u_char x; while (data < end){ if ((x=*data++) < min){ min = x; }else if (x > max){ max = x; } } *vmin = (float)min; *vmax = (float)max; } break; case DDL_INT16: { u_short *data = (u_short *)get_ddl_data(st); u_short *end = data + npts; u_short min = *data; u_short max = *data++; u_short x; while (data < end){ if ((x=*data++) < min){ min = x; }else if (x > max){ max = x; } } *vmin = (float)min; *vmax = (float)max; } break; case DDL_INT32: { int *data = (int *)get_ddl_data(st); int *end = data + npts; int min = *data; int max = *data++; int x; while (data < end){ if ((x=*data++) < min){ min = x; }else if (x > max){ max = x; } } *vmin = (float)min; *vmax = (float)max; } break; case DDL_FLOAT32: { float *data = (float *)get_ddl_data(st); float *end = data + npts; float min = *data; float max = *data++; float x; while (data < end){ if ((x=*data++) < min){ min = x; }else if (x > max){ max = x; } } *vmin = (float)min; *vmax = (float)max; } break; case DDL_FLOAT64: { double *data = (double *)get_ddl_data(st); double *end = data + npts; double min = *data; double max = *data++; double x; while (data < end){ if ((x=*data++) < min){ min = x; }else if (x > max){ max = x; } } *vmin = (float)min; *vmax = (float)max; } break; } }
static void get_histogram(DDLSymbolTable *st, float min, float max, int *histogram, int nbins) { int i; char *errmsg; int npts = (get_image_width(st) * get_image_height(st) * get_image_depth(st)); float scale = (nbins - 1) / (max - min); for (i=0; i<nbins; i++){ histogram[i] = 0; } int type = get_data_type(st, &errmsg); switch (type){ case DDL_INT8: { u_char *data = (u_char *)get_ddl_data(st); u_char *end = data + npts; while (data < end){ histogram[(int)((*data++ - min) * scale)]++; } } break; case DDL_INT16: { u_short *data = (u_short *)get_ddl_data(st); u_short *end = data + npts; while (data < end){ histogram[(int)((*data++ - min) * scale)]++; } } break; case DDL_INT32: { int *data = (int *)get_ddl_data(st); int *end = data + npts; while (data < end){ histogram[(int)((*data++ - min) * scale)]++; } } break; case DDL_FLOAT32: { float *data = (float *)get_ddl_data(st); float *end = data + npts; while (data < end){ histogram[(int)((*data++ - min) * scale)]++; } } break; case DDL_FLOAT64: { double *data = (double *)get_ddl_data(st); double *end = data + npts; while (data < end){ histogram[(int)((*data++ - min) * scale)]++; } } break; } }
int main(int argc, char* argv) { turn_on_debug_output(); create_image_world(); // Write your code here int v = load_bmp("plane.bmp"); put_image(v, (get_screen_width() / 2) - (get_image_width(v) / 2), (get_screen_height() / 2) - (get_image_height(v) / 2)); return p1world_shutdown(); }
__kernel void convolution(__read_only image2d_t sourceImage, __write_only image2d_t outputImage, __constant float* filter, int filterWidth) { const sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST; // Store each work-item’s unique row and column int x = get_global_id(0); int y = get_global_id(1); // Half the width of the filter is needed for indexing // memory later int halfWidth = (int)(filterWidth/2); // All accesses to images return data as four-element vector // (i.e., float4). float4 sum = {0.0f, 0.0f, 0.0f, 0.0f}; // Iterator for the filter int filterIdx = 0; // Each work-item iterates around its local area based on the // size of the filter int2 coords; // Coordinates for accessing the image // Iterate the filter rows for(int i = -halfWidth; i <= halfWidth; i++) { coords.y = y + i; // Iterate over the filter columns for(int j = -halfWidth; j <= halfWidth; j++) { coords.x = x + j; float4 pixel; // Read a pixel from the image. // Work on a channel pixel = read_imagef(sourceImage, sampler, coords); sum.x += pixel.x * filter[filterIdx++]; //sum.y += pixel.y * filter[filterIdx++]; //sum.z += pixel.z * filter[filterIdx++]; } } barrier(CLK_GLOBAL_MEM_FENCE); // Copy the data to the output image if the // work-item is in bounds if(y < get_image_height(sourceImage) && x < get_image_width(sourceImage)) { coords.x = x; coords.y = y; //Same channel is copied in all three channels //write_imagef(outputImage, coords, // (float4)(sum.x,sum.x,sum.x,1.0f)); write_imagef(outputImage, coords, sum); } }
int main(int argc, char* argv[]) { create_image_world(); int image = create_image(100, 100); for(int x = 0; x < 100; x++) { for(int y = 0; y < 100; y++) { if (x == y) { set_pixel(image, x, y, 255, 0, 0, 255); } else if (x+y == 100) { set_pixel(image, x, y, 255, 255, 0, 0); } else { set_pixel(image, x, y, 255, 0, 255, 0); } } } put_image(image, get_screen_width()/2-get_image_width(image)/2, get_screen_height()/2-get_image_height(image)/2); return p1world_shutdown(); }
void Player::player_move(Map *map) { Actor::move(map); check_water(map); int input = get_input(); const Tmx::Tileset *tileset = map->get_tileset(0); const Tmx::PropertySet prop = tileset->GetProperties(); // Check if on catapult int catid = prop.GetNumericProperty("catapult"); if (catid && check_below(map, 1, catid, catid) == 0) { if (input & PRESS_RIGHT) { set_vx(get_attribute("move_speed")); } else if (input & PRESS_LEFT) { set_vx(-get_attribute("move_speed")); } set_jump(map, true); } switch(m_action) { case Still: set_vx(0); case Move: // Check for crouch or move if (input & PRESS_DOWN) { set_action(Crouch); } else if (input & PRESS_RIGHT) { animate_move(); set_action(Move); set_vx(get_attribute("move_speed")); } else if (input & PRESS_LEFT) { animate_move(); set_action(Move); set_vx(-get_attribute("move_speed")); } else { set_action(Still); } // Check for jump if (input & PRESS_JUMP) { if (m_jump_ready && m_hit_ground) { if (input & PRESS_RIGHT) { set_vx(get_attribute("move_speed")); } else if (input & PRESS_LEFT) { set_vx(-get_attribute("move_speed")); } set_jump(map, false); } m_jump_ready = false; } else { // Restore jump lock m_jump_ready = true; } Body::move(map); if (get_fall()) { set_action(Fall); } break; case Fall: // Check for change of direction during fall if (input & PRESS_RIGHT) { set_vx(get_attribute("move_speed")); } else if (input & PRESS_LEFT) { set_vx(-get_attribute("move_speed")); } Body::move(map); if (!get_fall()) { m_hit_ground = true; set_action(Still); } break; case Jump: case Catapult: Body::move(map); if (get_fall()) { set_action(Fall); } break; case Crouch: set_vx(0); Body::move(map); if (!get_fall()) { m_hit_ground = true; } if (!(input & PRESS_DOWN)) { set_action(Still); } break; case Hit: if (m_hit_timer.expired(get_attribute("hit_time"))) { set_vx(0); set_lock_direction(false); reset_hit(); } Body::move(map); break; case HitPerish: animate_perish(); set_speed(0, -get_attribute("move_speed")); Body::move(map); if (m_y < -get_image_height()) { set_solid(true); set_invisible(false); set_action(HitPerished); } break; case Attack: case AttackLow: if (animate_attack()) { reset_attack(); } Body::move(map); if (!get_fall()) { m_hit_ground = true; } break; default: Body::move(map); break; } }
int mathexpr(ParmList inparms, ParmList *outparms) { float x, y, z; /* User variables */ float r[100]; /* Many user variables */ int ii, jj, kk; /* Integer user variables */ int n[100]; /* Many integer user variables */ int i, j, k; /* Pixel position in row, column, depth */ int width, height, depth; /* Size of all images */ int indx; /* Running pixel number */ char msg[128]; DDLSymbolTable *st; DDLSymbolTable *out; float **img; /* Vector of pointers to input data */ float *iout; /* Pointer to output data */ int nsrcs; /* Number of input images */ ParmList src_ddls; ParmList dst_ddls; /*fprintf(stderr,"mathexpr(0x%x, 0x%x)\n", inparms, outparms);/*CMP*/ /*printParm(inparms);/*CMP*/ /* Grab the input args */ src_ddls = findParm(inparms, "src_ddls"); if (!src_ddls){ ib_errmsg("MATH: \"src_ddls\" not passed"); return FALSE; } nsrcs = countParm(src_ddls); img = (float **)malloc(nsrcs * sizeof(float *)); fdfhandle = (DDLSymbolTable **)malloc(nsrcs * sizeof(DDLSymbolTable *)); if (!img || !fdfhandle){ ib_errmsg("MATH: out of memory"); return FALSE; } /* Check image sizes */ width = height = depth = 0; for (indx=0; indx<nsrcs; indx++){ getPtrParm(src_ddls, &st, indx); i = get_image_width(st); j = get_image_height(st); k = get_image_depth(st); if (!i || !j){ sprintf(msg,"MATH: image size is %dx%d\n", i, j); ib_errmsg(msg); return FALSE; } if ((width && i != width) || (height && j != height) || (depth && k != depth)) { ib_errmsg("MATH: images are different sizes\n"); return FALSE; } width = i; height = j; depth = k; /* Point the working source image pointers to the appropriate data */ img[indx] = get_ddl_data(st); fdfhandle[indx] = st; } /*fprintf(stderr,"MATH: width=%d, height=%d, depth=%d\n", width, height, depth);/*DBG*/ /* Copy the first input object (for storing the output image) */ getPtrParm(src_ddls, &st, 0); out = clone_ddl(st, 1); /*fprintf(stderr,"MATH: out width=%d, data=0x%x, *data=%g\n", get_image_width(out), get_ddl_data(out), *get_ddl_data(out));/*CMP*/ /*fprintf(stderr,"indata=0x%x, *indata=%g\n", img[0], img[0][0]);/*CMP*/ iout = get_ddl_data(out); /* * NOTE: IB_EXPRESSION will be expanded into something like * img[0][indx]+img[1][indx] */ interrupt_begin(); for (indx=k=0; k<depth; k++){ for (j=0; j<height; j++){ for (i=0; i<width && !interrupt(); i++, indx++){ iout[indx] = IB_EXPRESSION; } } } interrupt_end(); /*fprintf(stderr,"MATH: out width=%d, data=0x%x, *data=%g\n", get_image_width(out), get_ddl_data(out), *get_ddl_data(out));/*CMP*/ /* Pass back the output image */ *outparms = allocParm("dst_ddls", PL_PTR, 1); setPtrParm(*outparms, out, 0); return TRUE; }