int c_tools_frame::write_grayscale_imgs(const wxString& path) { std::vector<std::string> left_names = get_ocv_img_mgr()->get_left_grayscale_img_names(); std::vector<std::string> mid_names = get_ocv_img_mgr()->get_mid_grayscale_img_names(); std::vector<std::string> right_names = get_ocv_img_mgr()->get_right_grayscale_img_names(); for (unsigned int i = 0; i < left_names.size(); ++i) { std::string name = left_names[i]; ocv_mat_ptr img = get_ocv_img_mgr()->find_image_by_name(name); if (is_image_valid(img)) { std::string std_path = wxstr_to_std(path) + "\\" + name + img_ext; std::vector<int> compression_params; compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION); compression_params.push_back(9); //cv::imshow("hehe", *img); cv::imwrite(std_path, *img, compression_params); } } for (unsigned int i = 0; i < mid_names.size(); ++i) { std::string name = mid_names[i]; ocv_mat_ptr img = get_ocv_img_mgr()->find_image_by_name(name); if (is_image_valid(img)) { std::string std_path = wxstr_to_std(path) + "\\" + name + img_ext; std::vector<int> compression_params; compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION); compression_params.push_back(9); cv::imwrite(std_path, *img, compression_params); } } for (unsigned int i = 0; i < right_names.size(); ++i) { std::string name = right_names[i]; ocv_mat_ptr img = get_ocv_img_mgr()->find_image_by_name(name); if (is_image_valid(img)) { std::string std_path = wxstr_to_std(path) + "\\" + name + img_ext; std::vector<int> compression_params; compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION); compression_params.push_back(9); cv::imwrite(std_path, *img, compression_params); } } return 0; }
void *image_load (char *elf_start, unsigned int size) { Elf64_Ehdr *hdr = NULL; Elf64_Phdr *phdr = NULL; Elf64_Shdr *shdr = NULL; Elf64_Sym *syms = NULL; char *strings = NULL; char *start = NULL; char *taddr = NULL; void *entry = NULL; int i = 0; char *exec = NULL; hdr = (Elf64_Ehdr *) elf_start; if(!is_image_valid(hdr)) { printk("image_load:: invalid ELF image\n"); return 0; } taddr = exec = (char*) mmap(NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); if(!exec) { printk("image_load:: error allocating memory\n"); return 0; } // Start with clean memory. memset(exec,0x0,size); phdr = (Elf64_Phdr *)(elf_start + hdr->e_phoff); for(i=0; i < hdr->e_phnum; ++i) { if(phdr[i].p_type != PT_LOAD) { continue; } if(phdr[i].p_filesz > phdr[i].p_memsz) { printk("image_load:: p_filesz > p_memsz\n"); munmap(exec, size); return 0; } if(!phdr[i].p_filesz) { continue; } // p_filesz can be smaller than p_memsz, // the difference is zeroe'd out. start = elf_start + phdr[i].p_offset; //taddr = exec + phdr[i].p_vaddr; //memmove(taddr,start,phdr[i].p_filesz); if(!(phdr[i].p_flags & PF_W)) { // Read-only. mprotect((unsigned char *) taddr, phdr[i].p_memsz, PROT_READ); } if(phdr[i].p_flags & PF_X) { // Executable. mprotect((unsigned char *) taddr, phdr[i].p_memsz, PROT_EXEC); } for(unsigned n = 0; n < phdr[i].p_filesz; ++n) { *taddr = start[n]; ++taddr; } } shdr = (Elf64_Shdr *)(elf_start + hdr->e_shoff); for(i=0; i < hdr->e_shnum; ++i) { if (shdr[i].sh_type == SHT_DYNSYM) { syms = (Elf64_Sym*)(elf_start + shdr[i].sh_offset); strings = elf_start + shdr[shdr[i].sh_link].sh_offset; entry = find_sym("main", shdr + i, strings, elf_start, exec); break; } } return entry; }/* image_load */
/* * image_load: * * Given an ELF file loaded in memory with given * size, parse the file and convert it into an * executable. Return the process start routine. * * NOTES: Currently supports statically linked executables * that are entirely position independent code (-fpic). * This means that current linux gcc code must be compiled * this way: * * gcc -c -static -nostdlib -fpic code.c * ld -elf_i386 test5.o -o code.exe -static -Ttext 0x100000 * * The relocation to address 0x100000 is arbitrary, the process * will be relocated to the address the kernel sees fit. * The entry point to the process will be a C function * called int lf_i386() and it may call _start(). * In this environment, the code for _start needs to be developed * and is not the standard C start routine. * Compiling regular gcc code as pic or pie implies that * it will be dynamically linked which this exec function * does not currently support. * */ void *image_load (char *elf_start, unsigned int size, unsigned char **exec_mem, unsigned int *exec_size) { Elf32_Ehdr *hdr = NULL; Elf32_Phdr *phdr = NULL; unsigned char *start = NULL; Elf32_Addr taddr = 0; Elf32_Addr offset = 0; int i = 0; unsigned char *exec = NULL; Elf32_Addr estart = 0; unsigned int esize = 0; hdr = (Elf32_Ehdr *) elf_start; if(!is_image_valid(hdr)) { printk("image_load:: invalid ELF image\n"); return 0; } LINE(); phdr = (Elf32_Phdr *)(elf_start + hdr->e_phoff); LINE(); // Calculate the process size in memory. for(i=0; i < hdr->e_phnum; i++) { if(phdr[i].p_type != PT_LOAD) { continue; } if(!estart) { estart = phdr[i].p_paddr; } esize += ((phdr[i].p_paddr-estart) + phdr[i].p_memsz); } estart = 0; #ifdef _TEST_EXEC exec = (unsigned char *)mmap(NULL, esize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); #else // Kernel exec = (unsigned char *)malloc(esize); #endif if(!exec) { printk("image_load:: error allocating memory\n"); return 0; } // Start with clean memory. memset(exec,0x0,esize); LINE(); phdr = (Elf32_Phdr *)(elf_start + hdr->e_phoff); LINE(); #ifdef _TEST_EXEC printk("esize=%d size=%d=\n",esize,size); printk("hdr->e_phnum=[%x]\n", hdr->e_phnum); #endif for(i=0; i < hdr->e_phnum; i++) { LINE(); #ifdef _TEST_EXEC printk("p_type=[%x]\n",phdr[i].p_type); #endif if(phdr[i].p_type != PT_LOAD) { continue; } if(phdr[i].p_filesz > phdr[i].p_memsz) { printk("image_load:: p_filesz > p_memsz\n"); #ifdef _TEST_EXEC munmap(exec, esize); #else // Kernel free((void *)exec); #endif return 0; } if(!phdr[i].p_filesz) { #ifdef _TEST_EXEC LINE(); printk("p_filesz=[%d]\n",phdr[i].p_filesz); printk("p_offset=[%d]\n",phdr[i].p_offset); printk("p_memsz=[%d]\n",phdr[i].p_memsz); #endif memset((char *) ((Elf32_Addr)exec + (phdr[i].p_paddr-estart)), 0x0,phdr[i].p_memsz); continue; } // p_filesz can be smaller than p_memsz, // the difference is zeroe'd out. LINE(); start = (unsigned char *) (elf_start + phdr[i].p_offset); if(!estart) { estart = phdr[i].p_paddr; } taddr = (Elf32_Addr)exec + offset; #ifdef _TEST_EXEC printk("taddr=[%x] phdr[i].p_paddr=[%x] exec=[%x] " "offset=[%x] phdr[i].p_memsz=[%x] phdr[i].p_filesz=[%x] " "size=[%d] start=[%d]\n", taddr, phdr[i].p_paddr, exec, offset, phdr[i].p_memsz, phdr[i].p_filesz,esize,start); #endif memmove((unsigned char *)taddr, (unsigned char *)start,phdr[i].p_filesz); offset += ((phdr[i].p_paddr - estart) + phdr[i].p_memsz); LINE(); } // Mark the sections read-only as described. for(i=0; i < hdr->e_phnum; i++) { LINE(); #ifdef _TEST_EXEC printk("p_type=[%x]\n",phdr[i].p_type); #endif if(phdr[i].p_type != PT_LOAD) { continue; } if(phdr[i].p_filesz > phdr[i].p_memsz) { printk("image_load:: p_filesz > p_memsz\n"); #ifdef _TEST_EXEC munmap(exec, esize); #else free((void *)exec); #endif return 0; } if(!phdr[i].p_filesz) { continue; } // p_filesz can be smaller than p_memsz, // the difference is zeroe'd out. LINE(); start = (unsigned char *) (elf_start + phdr[i].p_offset); if(!estart) { estart = phdr[i].p_paddr; } taddr = (Elf32_Addr)exec + offset; #ifdef _TEST_EXEC printk("taddr=[%x] phdr[i].p_paddr=[%x] exec=[%x] " "offset=[%x] phdr[i].p_memsz=[%x] phdr[i].p_filesz=[%x] " "size=[%d] start=[%d]\n", taddr, phdr[i].p_paddr, exec, offset, phdr[i].p_memsz, phdr[i].p_filesz,esize,start); #endif offset += ((phdr[i].p_paddr - estart) + phdr[i].p_memsz); LINE(); if(!(phdr[i].p_flags & PF_W)) { #ifdef _TEST_EXEC LINE(); // Read-only. mprotect((unsigned char *) taddr, phdr[i].p_filesz, // RGD was memsz PROT_READ | PROT_EXEC); #else mem_set_read_only(taddr, phdr[i].p_filesz / PAGE_SIZE); #endif } } LINE(); #ifdef _TEST_EXEC printk("i=[%x] hdr->e_entry=[%x] exec=[%x]\n", i,hdr->e_entry,exec); #endif (*exec_mem) = exec; (*exec_size) = esize; return (void *)((hdr->e_entry - estart) + (Elf32_Addr)exec); }/* image_load */