Grid* load_grid_from_ppm(char *path, unsigned int max_width, unsigned int max_height) { unsigned int i, j; Image *img = load_ppm(path); Grid *grid; if (img == NULL) { print_error("Échec de chargement de la grille."); return NULL; } grid = new_grid(min(max_width, img->width), min(max_height, img->height)); for (i=0; i < grid->width; i++) for (j=0; j <grid->height; j++) { if (img->pixels[i][j].grey < 255) // le pixel n'est pas blanc grid->tiles[i][j] = '+'; else grid->tiles[i][j] = '.'; } delete_image(img); // On libère la mémoire return grid; }
image_id preload_image(char const* path) { if (path == NULL) return B_BAD_VALUE; KTRACE("rld: preload_image(\"%s\")", path); image_t *image = NULL; status_t status = load_image(path, B_LIBRARY_IMAGE, NULL, NULL, &image); if (status < B_OK) { KTRACE("rld: preload_image(\"%s\") failed to load container: %s", path, strerror(status)); return status; } if (image->find_undefined_symbol == NULL) image->find_undefined_symbol = find_undefined_symbol_global; status = load_dependencies(image); if (status < B_OK) goto err; set_image_flags_recursively(image, RTLD_GLOBAL); status = relocate_dependencies(image); if (status < B_OK) goto err; status = add_preloaded_image(image); if (status < B_OK) goto err; inject_runtime_loader_api(image); remap_images(); init_dependencies(image, true); // if the image contains an add-on, register it runtime_loader_add_on* addOnStruct; if (find_symbol(image, SymbolLookupInfo("__gRuntimeLoaderAddOn", B_SYMBOL_TYPE_DATA), (void**)&addOnStruct) == B_OK) { add_add_on(image, addOnStruct); } KTRACE("rld: preload_image(\"%s\") done: id: %" B_PRId32, path, image->id); return image->id; err: KTRACE("rld: preload_image(\"%s\") failed: %s", path, strerror(status)); dequeue_loaded_image(image); delete_image(image); return status; }
image_t* trim_staff(image_t *img){ /* simple function to trim excess space from left and right edges of staff adds two pixels of padding to each side Var Declaration*/ uint16_t height,width; projection_t* proj_onto_x; projection_t* proj_onto_y; uint16_t last_l,last_r,last_t,last_b; uint16_t trsh; uint16_t i,j; image_t* new_img; /*End Var Declaration*/ height=img->height; width=img->width; proj_onto_x = project(img, 1); last_l = 0; last_r = width-1; trsh = height/50;/*was float*/ for (i=1;i<(width+1)/3;i++){/*maybe (width+1)/3-1*/ if (proj_onto_x->data[i] < trsh) last_l = i; if (proj_onto_x->data[width-i] < trsh) last_r = width-i; } delete_flex_array(proj_onto_x); proj_onto_y = project(img, 2); last_t = 0; last_b = height-1; trsh = width /200;/*was float*/ for (i=1;i<(height+1)/3;i++){ if (proj_onto_y->data[i] < trsh) last_t = i; if (proj_onto_y->data[height-i] < trsh) last_b = height-i; } /*crop:*/ new_img=make_image(last_b+5-last_t,last_r+5-last_l); for (i=0;i<last_b+5-last_t;i++){ for (j=0;j<last_r+5-last_l;j++){ if (i<2 ||j<2 ||(i>last_b+1-last_t) ||(j>last_r+1-last_l)){ setPixel(new_img,i,j,0); } else { setPixel(new_img,i,j,getPixel(img,last_t+i-2,last_l+j-2)); } } } delete_image(img); return(new_img); }
bool rgb_image::create_image ( int nrows, int ncols, int alpha ) { delete_image(); this->alpha = alpha; img[0].create(nrows,ncols); img[1].create(nrows,ncols); img[2].create(nrows,ncols); if ( alpha ) img[3].create(nrows,ncols); rows = nrows; cols = ncols; }
image_t* get_staff(const image_t* img, const staff_info* staff,uint16_t count,params* parameters) { /*Var Declarations*/ uint16_t top,bottom; image_t* new_img, *out_img; /*End Var Declarations*/ /*isolate staff*/ top=staff->staff_bounds[count][0]; bottom=staff->staff_bounds[count][1]; new_img=get_sub_img(img,top,bottom,-1,-1); /*trim staff*/ new_img=trim_staff(new_img); out_img=get_sub_img(new_img,-1,-1,parameters->ks_x,-1); delete_image(new_img); return out_img; }
void remove_lines_2(image_t* img,params* parameters,uint32_t numCuts,image_t* lineless_img, uint32_t* stafflines, staff_info* staff){ /*initlize stafflines to a 5 elt array*/ uint32_t line_thickness, line_spacing, line_w, beginCut, endCut, *yprojection, linesFound, *stafflines_tmp, **STAFFLINES, i, j, vertSplice, sum, max_project, loc, eraseTop, eraseBottom, min_thisLineY,max_thisLineY, shift_variable,h,w, **last_stafflines, ii, jj; int32_t min_thisLineY_tmp; image_t *fixThatImage; h = img->height; w = img->width; /*CHANGES: GET RID OF*/ /* line_thickness = (uint32_t)(parameters->thickness); line_spacing = (uint32_t)(parameters->spacing);*/ /*END CHANGES*/ line_w = (uint32_t)(parameters->thickness + parameters->spacing); beginCut = 0; /*CHANGES: W-1 => W*/ endCut = (uint32_t)((w)/numCuts); /*END CHANGES*/ yprojection= (uint32_t *)mget_spc((uint32_t)h, sizeof(uint32_t)); for(i=0;i<h;i++){ sum = 0; for(j=0;j<w;j++){ sum += getPixel(img,i,j); } yprojection[i] = sum; } linesFound = 0; stafflines_tmp = (uint32_t *)mget_spc((uint32_t)10, sizeof(uint32_t)); while(linesFound < 5){ max_project = yprojection[0]; loc = 0; for(i=1; i<h; i++){ if(yprojection[i]>max_project){ max_project = yprojection[i]; loc = i; } } linesFound = linesFound + 1; /*all y coordinates of a line that is part of same staff line:*/ eraseTop = loc-(uint32_t)((line_w+1)/3 ); eraseBottom = loc+(uint32_t)((line_w+1)/3); if (eraseTop < 0){ eraseTop = 0; } /*avoid segfault*/ if (eraseBottom > h-1){ eraseBottom = h-1; } /*avoid segfault*/ min_thisLineY_tmp = -1; for(i=eraseTop; i<=eraseBottom; i++){ if(yprojection[i]>=(9*max_project)/10){ if(min_thisLineY_tmp == -1){ min_thisLineY_tmp = i; } max_thisLineY = i; } /*erase to avoid line in futher iterations*/ yprojection[i] = 0; } min_thisLineY = (uint32_t)(min_thisLineY_tmp); stafflines_tmp[2*(linesFound-1)] = min_thisLineY; stafflines_tmp[2*(linesFound-1)+1] = max_thisLineY; } free(yprojection); quickSort(stafflines_tmp,0,9); last_stafflines = (uint32_t **)multialloc (sizeof(uint32_t),2, 5,2); STAFFLINES = (uint32_t **)multialloc (sizeof (uint32_t),2, 5,2); for(i=0; i<5; i++){ last_stafflines[i][0] = stafflines_tmp[2*i]; last_stafflines[i][1] = stafflines_tmp[2*i + 1]; } free(stafflines_tmp); /*LOOP THRU VERTICAL CUTS*/ shift_variable=0; for(vertSplice =0; vertSplice<numCuts; vertSplice++){ fixThatImage = get_sub_img(img,-1,-1,beginCut,endCut); /*pretty up staff lines*/ fix_lines_and_remove(fixThatImage,parameters,last_stafflines,&shift_variable, vertSplice); for(ii=0;ii<h;ii++){ for(jj=beginCut;jj<=endCut;jj++){ setPixel(img,ii,jj,getPixel(fixThatImage,ii,jj-beginCut)); } } delete_image(fixThatImage); if (vertSplice==0){ for(i=0; i<5; i++){ STAFFLINES[i][0] = last_stafflines[i][0]; STAFFLINES[i][1] = last_stafflines[i][1]; } } beginCut = endCut + 1; /*CHANGED: W-1 TO W*/ endCut = endCut + (uint32_t)((w)/numCuts) + 1; /*END CHANGED*/ if (endCut > w-1){ endCut = w-1; } } /*copy over image*/ for(i=0; i<h;i++){ for(j=0; j<w;j++){ setPixel(lineless_img,i,j,getPixel(img,i,j)); } } for(i=0;i<5;i++){ /*CHANGED 1->0 2->1*/ stafflines[i] = (uint32_t)(((STAFFLINES[i][0]+STAFFLINES[i][1]+1)/2)); /*shift because of zero padding above*/ /*END CHANGE*/ } multifree(STAFFLINES,2); multifree(last_stafflines,2); }
image_t* mini_img_cut(const image_t* img,uint16_t top,uint16_t bottom,uint16_t xbegin,uint16_t xend,const params* parameters, note_cuts* cuts) { /* cuts out small image around stem/measure marker*/ /*Var Declarations*/ uint16_t height, width,line_spacing,extend,count, line_w, count_thresh,sum_thresh; int16_t left,right; int16_t topCut,bottomCut,leftCut,rightCut; int16_t extUp,extDown,extLeft,extRight; image_t* sub_img; projection_t* proj; /*End Var Declarations*/ height=img->height; width=img->width; line_spacing = parameters->spacing; line_w= parameters->thickness + line_spacing; extend = (7*line_w+2)/5; /*left and right cuts*/ count_thresh= (4*line_w+5)/10; extLeft = 1; count = 0; while (extLeft < extend && xbegin-extLeft>=0){ sub_img=get_sub_img(img,top,bottom,xbegin-extLeft,xbegin-extLeft); proj=project(sub_img,1); delete_image(sub_img); if(proj->data[0] == 0) count ++; else count=0; delete_flex_array(proj); if(count > count_thresh) break; extLeft++; } leftCut = xbegin - extLeft + count; extRight = 1; count = 0; while (extRight < extend && xend+extRight<width){ sub_img=get_sub_img(img,top,bottom, xend+extRight, xend+extRight); proj=project(sub_img,1); delete_image(sub_img); if(proj->data[0] == 0) count ++; else count=0; delete_flex_array(proj); if(count > count_thresh) break; extRight++; } rightCut = xend + extRight - count; /*top and bottom cuts*/ count_thresh=0; sum_thresh= (line_w+3)/6+1; extUp = 1; count = 0; while (extUp < line_spacing && top-extUp >= 0){ sub_img=get_sub_img(img,top-extUp,top-extUp,leftCut,rightCut); proj=project(sub_img,2); delete_image(sub_img); if(proj->data[0] <=sum_thresh) count ++; delete_flex_array(proj); if(count > count_thresh) break; extUp++; } topCut = top - extUp + count; extDown = 1; count = 0; while (extDown < line_spacing && bottom+extDown <height){ sub_img=get_sub_img(img,bottom+extDown,bottom+extDown,leftCut,rightCut); proj=project(sub_img,2); delete_image(sub_img); if(proj->data[0] <=sum_thresh) count ++; delete_flex_array(proj); if(count > count_thresh) break; extDown++; } bottomCut = bottom + extDown - count; /*precautions to prevent segfaults:*/ if (topCut < 0) topCut = 0; if (bottomCut >= height) bottomCut = height-1; if (leftCut < 0) leftCut = 0; if (rightCut >= width) rightCut = width-1; cuts->bottom_cut=bottomCut; cuts->left_cut=leftCut; cuts->right_cut=rightCut; cuts->top_cut=topCut; /*output - cut box*/ return get_sub_img(img,topCut,bottomCut,leftCut,rightCut); }
image_t* remove_lines(image_t* img, params* staff_params, uint16_t numCuts, uint16_t **STAFFLINES) { /*returns staff without lines, and STAFFLINES as a 5x2 array*/ uint16_t line_thickness,height,width,line_spacing; uint16_t beginCut,endCut; projection_t* yprojection; projection_t* yprojection2; flex_array_t* ones_array; flex_array_t* ones_array2; flex_array_t* staffLines; uint16_t** last_stafflines; int16_t* array_staff; linked_list* stack; int16_t max_project; uint16_t i,j,lines_found; int16_t shift_variable; uint16_t vertSplice; image_t* fixThatImage; image_t* new_img; /*End Var Declarations*/ line_thickness=staff_params->thickness; line_spacing=staff_params->spacing; height=img->height; width=img->width; beginCut = 0; endCut = (width+numCuts/2)/numCuts; /*/ get inital staff line estimates*/ yprojection2 = project(img,2); ones_array=array_ones(line_thickness); ones_array2=array_ones(1); yprojection = filter(ones_array,ones_array2,yprojection2); /* reduce impact of 'double peaks' (curvy lines)*/ delete_flex_array(ones_array); delete_flex_array(ones_array2); delete_flex_array(yprojection2); max_project = index_of_max(yprojection); for (i=max_project-(line_spacing+1)/2;i<max_project+(line_spacing+1)/2;i++){ yprojection->data[i]=0; } staffLines=find(yprojection,.8*max_project,greater); /*delete staff line, twiddle with the 85% later*/ stack = group(staffLines,3); last_stafflines=multialloc(sizeof(uint16_t),2,5,2); i=0; while (is_list_empty( stack)==0){ array_staff=pop_bottom(stack); last_stafflines[i][0]=(uint16_t)array_staff[0]; last_stafflines[i][1]=(uint16_t)array_staff[1]; free(array_staff); i++; } /* LOOP THRU VERTICAL CUTS*/ shift_variable=0; for (vertSplice = 0;vertSplice<numCuts; vertSplice++){ fixThatImage = get_sub_img(img,-1,-1,beginCut,endCut); /*pretty up staff lines*/ /*implement this once created...*/ /*[img(:,beginCut:endCut),shift_variable,stafflines] = ...*/ /* fix_lines_and_remove_smart(fixThatImage,params,last_stafflines,shift_variable,vertSplice);*/ /*modify last_stafflines in place */ delete_image(fixThatImage); if (vertSplice==1 && STAFFLINES){ for (i=0;i<5;i++){ for (j=0;j<2;j++){ STAFFLINES[i][j]=last_stafflines[i][j]+2;/*create STAFFLINES*/ } } } beginCut = endCut + 1; endCut += (width+numCuts/2)/numCuts; if (endCut >= width) endCut = width-1; } new_img=make_image(img->height +4,img->width +4); for (i=0;i<(img->height +4);i++){ for (j=0;j<(img->width +4);j++){ if (i<2 ||j<2 ||(i>(img->height +1)) ||(j>(img->width +1))){ setPixel(new_img,i,j,0); } else { setPixel(new_img,i,j,getPixel(img,i-2,j-2)); } } } return new_img; }
status_t unload_library(void* handle, image_id imageID, bool addOn) { image_t *image; image_type type = addOn ? B_ADD_ON_IMAGE : B_LIBRARY_IMAGE; if (handle == NULL && imageID < 0) return B_BAD_IMAGE_ID; if (handle == RLD_GLOBAL_SCOPE) return B_OK; rld_lock(); // for now, just do stupid simple global locking if (gInvalidImageIDs) { // After fork, we lazily rebuild the image IDs of all loaded images update_image_ids(); } // we only check images that have been already initialized status_t status = B_BAD_IMAGE_ID; if (handle != NULL) { image = (image_t*)handle; put_image(image); status = B_OK; } else { image = find_loaded_image_by_id(imageID, true); if (image != NULL) { // unload image if (type == image->type) { put_image(image); status = B_OK; } else status = B_BAD_VALUE; } } if (status == B_OK) { while ((image = get_disposable_images().head) != NULL) { // Call the exit hooks that live in this image. // Note: With the Itanium ABI this shouldn't really be done this // way anymore, since global destructors are registered via // __cxa_atexit() (the ones that are registered dynamically) and the // termination routine should call __cxa_finalize() for the image. // The reason why we still do it is that hooks registered with // atexit() aren't associated with the image. We could find out // there which image the hooks lives in and register it // respectively, but since that would be done always, that's // probably more expensive than calling // call_atexit_hooks_for_range() only here, which happens only when // libraries are unloaded dynamically. if (gRuntimeLoader.call_atexit_hooks_for_range) { gRuntimeLoader.call_atexit_hooks_for_range( image->regions[0].vmstart, image->regions[0].vmsize); } image_event(image, IMAGE_EVENT_UNINITIALIZING); if (image->term_routine) ((init_term_function)image->term_routine)(image->id); dequeue_disposable_image(image); unmap_image(image); image_event(image, IMAGE_EVENT_UNLOADING); delete_image(image); } } rld_unlock(); return status; }
image_id load_library(char const *path, uint32 flags, bool addOn, void** _handle) { image_t *image = NULL; image_type type = (addOn ? B_ADD_ON_IMAGE : B_LIBRARY_IMAGE); status_t status; if (path == NULL && addOn) return B_BAD_VALUE; KTRACE("rld: load_library(\"%s\", %#" B_PRIx32 ", %d)", path, flags, addOn); rld_lock(); // for now, just do stupid simple global locking // have we already loaded this library? // Checking it at this stage saves loading its dependencies again if (!addOn) { // a NULL path is fine -- it means the global scope shall be opened if (path == NULL) { *_handle = RLD_GLOBAL_SCOPE; rld_unlock(); return 0; } image = find_loaded_image_by_name(path, APP_OR_LIBRARY_TYPE); if (image != NULL && (flags & RTLD_GLOBAL) != 0) set_image_flags_recursively(image, RTLD_GLOBAL); if (image) { atomic_add(&image->ref_count, 1); rld_unlock(); KTRACE("rld: load_library(\"%s\"): already loaded: %" B_PRId32, path, image->id); *_handle = image; return image->id; } } status = load_image(path, type, NULL, NULL, &image); if (status < B_OK) { rld_unlock(); KTRACE("rld: load_library(\"%s\") failed to load container: %s", path, strerror(status)); return status; } if (image->find_undefined_symbol == NULL) { if (addOn) image->find_undefined_symbol = find_undefined_symbol_add_on; else image->find_undefined_symbol = find_undefined_symbol_global; } status = load_dependencies(image); if (status < B_OK) goto err; // If specified, set the RTLD_GLOBAL flag recursively on this image and all // dependencies. If not specified, we temporarily set // RFLAG_USE_FOR_RESOLVING so that the dependencies will correctly be used // for undefined symbol resolution. if ((flags & RTLD_GLOBAL) != 0) set_image_flags_recursively(image, RTLD_GLOBAL); else set_image_flags_recursively(image, RFLAG_USE_FOR_RESOLVING); status = relocate_dependencies(image); if (status < B_OK) goto err; if ((flags & RTLD_GLOBAL) == 0) clear_image_flags_recursively(image, RFLAG_USE_FOR_RESOLVING); remap_images(); init_dependencies(image, true); rld_unlock(); KTRACE("rld: load_library(\"%s\") done: id: %" B_PRId32, path, image->id); *_handle = image; return image->id; err: KTRACE("rld: load_library(\"%s\") failed: %s", path, strerror(status)); dequeue_loaded_image(image); delete_image(image); rld_unlock(); return status; }
image_id load_program(char const *path, void **_entry) { status_t status; image_t *image; KTRACE("rld: load_program(\"%s\")", path); rld_lock(); // for now, just do stupid simple global locking preload_images(); TRACE(("rld: load %s\n", path)); status = load_image(path, B_APP_IMAGE, NULL, NULL, &gProgramImage); if (status < B_OK) goto err; if (gProgramImage->find_undefined_symbol == NULL) gProgramImage->find_undefined_symbol = find_undefined_symbol_global; status = load_dependencies(gProgramImage); if (status < B_OK) goto err; // Set RTLD_GLOBAL on all libraries including the program. // This results in the desired symbol resolution for dlopen()ed libraries. set_image_flags_recursively(gProgramImage, RTLD_GLOBAL); status = relocate_dependencies(gProgramImage); if (status < B_OK) goto err; inject_runtime_loader_api(gProgramImage); remap_images(); init_dependencies(gProgramImage, true); // Since the images are initialized now, we no longer should use our // getenv(), but use the one from libroot.so find_symbol_breadth_first(gProgramImage, SymbolLookupInfo("getenv", B_SYMBOL_TYPE_TEXT), &image, (void**)&gGetEnv); if (gProgramImage->entry_point == 0) { status = B_NOT_AN_EXECUTABLE; goto err; } *_entry = (void *)(gProgramImage->entry_point); rld_unlock(); gProgramLoaded = true; KTRACE("rld: load_program(\"%s\") done: entry: %p, id: %" B_PRId32 , path, *_entry, gProgramImage->id); return gProgramImage->id; err: KTRACE("rld: load_program(\"%s\") failed: %s", path, strerror(status)); delete_image(gProgramImage); if (report_errors()) { // send error message gErrorMessage.AddInt32("error", status); gErrorMessage.SetDeliveryInfo(gProgramArgs->error_token, -1, 0, find_thread(NULL)); _kern_write_port_etc(gProgramArgs->error_port, 'KMSG', gErrorMessage.Buffer(), gErrorMessage.ContentSize(), 0, 0); } _kern_loading_app_failed(status); rld_unlock(); return status; }