int InitNextImage(IplImage* img) { CvSize sz(img->width, img->height); ReallocImage(&imgGray, sz, 1); ReallocImage(&imgThresh, sz, 1); ptRotate = face[MOUTH].ptCenter; float m[6]; CvMat mat = cvMat( 2, 3, CV_32FC1, m ); if (NULL == imgGray || NULL == imgThresh) return 0; /*m[0] = (float)cos(-dbRotateAngle*CV_PI/180.); m[1] = (float)sin(-dbRotateAngle*CV_PI/180.); m[2] = (float)ptRotate.x; m[3] = -m[1]; m[4] = m[0]; m[5] = (float)ptRotate.y;*/ cv2DRotationMatrix( cvPointTo32f(ptRotate), -dbRotateAngle, 1., &mat ); cvWarpAffine( img, imgGray, &mat ); if (NULL == mstgContours) mstgContours = cvCreateMemStorage(); else cvClearMemStorage(mstgContours); if (NULL == mstgContours) return 0; return 1; }
void FaceDetection::FindContours(IplImage* imgGray) { ReallocImage(&m_imgThresh, cvGetSize(imgGray), 1); if (NULL == m_imgThresh) return; // int iNumLayers = m_iNumLayers; int iMinLevel = 0, iMaxLevel = 255, iStep = 255 / iNumLayers; ThresholdingParam(imgGray, iNumLayers, iMinLevel, iMaxLevel, iStep); // init cvReleaseMemStorage(&m_mstgContours); m_mstgContours = cvCreateMemStorage(); if (NULL == m_mstgContours) return; memset(m_seqContours, 0, sizeof(CvSeq*) * MAX_LAYERS); cvReleaseMemStorage(&m_mstgRects); m_mstgRects = cvCreateMemStorage(); if (NULL == m_mstgRects) return; m_seqRects = cvCreateSeq(0, sizeof(CvSeq), sizeof(CvContourRect), m_mstgRects); if (NULL == m_seqRects) return; // find contours for (int l = iMinLevel, i = 0; l < iMaxLevel; l += iStep, i++) { cvThreshold(imgGray, m_imgThresh, (double)l, (double)255, CV_THRESH_BINARY); if (cvFindContours(m_imgThresh, m_mstgContours, &m_seqContours[i], sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE)) AddContours2Rect(m_seqContours[i], l, i); } // sort rects cvSeqSort(m_seqRects, CompareContourRect, NULL); }// void FaceDetection::FindContours(IplImage* imgGray)
Elf32_Phdr * CELFFile::AddProgramHeader() { size_t old_size = size; ReallocImage(((GetProgramCount() + 1) * sizeof(Elf32_Phdr)) + size); Elf32_Phdr * new_phdr = (Elf32_Phdr*)((size_t)GetImage() + old_size); for (int i = 0 ; i < GetProgramCount(); i++) { Elf32_Phdr cur_phdr = *GetProgramHeader(i); /*if (i == GetProgramCount() - 1) { Elf32_Phdr *last_phdr = new_phdr+1; *last_phdr = cur_phdr; } else*/ { *new_phdr = cur_phdr; new_phdr++; } } GetElfHeader()->e_phnum++; GetElfHeader()->e_phoff = old_size; return new_phdr; }
void CELFFile::AddSegmentSection(char * new_section_name,size_t new_section_size,long new_section_flag) { printf("new base:%x\n",GetNewSegmentSectionBase() ); long new_section_addr = GetNewSegmentSectionBase(); Elf32_Phdr * new_phdr_header = AddProgramHeader(); new_phdr_header->p_align = 0x1000;//new_section_size + (0x1000 - (new_section_size % 0x1000)); new_phdr_header->p_filesz = new_section_size; new_phdr_header->p_memsz = new_section_size; new_phdr_header->p_offset = size; new_phdr_header->p_flags = new_section_flag;//PF_X|PF_W|PF_R; new_phdr_header->p_vaddr = new_section_addr; new_phdr_header->p_type = PT_LOAD; new_phdr_header->p_paddr = new_section_addr; //asm("int3"); ReallocImage(size+new_section_size); return ; if (buffer == NULL || new_section_size == 0) return; size_t old_size = size; Elf32_Shdr * t_shdr_header = GetFirstSectionHeader() + GetSectionCount(); if (reinterpret_cast<size_t>(t_shdr_header) - reinterpret_cast<size_t>(GetImage()) == old_size) { ReallocImage(size + new_section_size + sizeof(Elf32_Shdr)); } else { ReallocImage(size + new_section_size ); } Elf32_Ehdr * elf_header = GetElfHeader(); Elf32_Shdr * string_section_header = GetSectionHeader( elf_header->e_shstrndx ); size_t str_table_last_offset = string_section_header->sh_size + string_section_header->sh_offset; char * new_string_begin_addr = reinterpret_cast<char*>(GetImage()) + str_table_last_offset; strcpy(new_string_begin_addr,new_section_name); string_section_header->sh_size += strlen(new_section_name); //Elf32_Shdr * new_shdr_header = GetSectionHeader(GetSectionCount() - 1); //Elf32_Shdr * new_shdr_header = GetSectionHeader(0) + GetSectionCount(); //printf ("last section:%s\n",GetStringTableStr( new_shdr_header->sh_name)); Elf32_Shdr * new_shdr_header = reinterpret_cast<Elf32_Shdr*>( (elf_header->e_shoff + reinterpret_cast<Elf32_Off>(GetImage()))); new_shdr_header += GetSectionCount(); //new_shdr_header++; printf("Elf32_Shdr Size:%x\n",sizeof(Elf32_Shdr)); if (reinterpret_cast<size_t>(new_shdr_header) - reinterpret_cast<size_t>(GetImage()) == old_size) { new_shdr_header->sh_offset = old_size + sizeof(Elf32_Shdr); } else { new_shdr_header->sh_offset = old_size; } printf("offset :%x\n",reinterpret_cast<size_t>(new_shdr_header) - reinterpret_cast<size_t>(GetImage())); new_shdr_header->sh_name = str_table_last_offset - string_section_header->sh_offset; printf ("last section:%s\n",GetStringTableStr( new_shdr_header->sh_name)); new_shdr_header->sh_addr = GetNewSectionBase(); new_shdr_header->sh_size = new_section_size; printf("size :%#x\n",new_shdr_header->sh_size); new_shdr_header->sh_flags = new_section_flag; //new_shdr_header->sh_offset = old_size; new_shdr_header->sh_type = 1; new_shdr_header->sh_link = 0; new_shdr_header->sh_info = 0; new_shdr_header->sh_addralign = 16; new_shdr_header->sh_entsize = 0; ++elf_header->e_shnum; /* Elf32_Phdr * new_phdr_header = GetProgramHeader(GetProgramCount() - 1); new_phdr_header++; printf("new_phdr_header offset:%#x\n",(size_t)new_phdr_header - (size_t)GetImage()); new_phdr_header->p_align = 0x1000;//new_section_size + (0x1000 - (new_section_size % 0x1000)); new_phdr_header->p_filesz = new_section_size; new_phdr_header->p_memsz = new_section_size; new_phdr_header->p_offset = new_shdr_header->sh_offset; new_phdr_header->p_flags = 5; new_phdr_header->p_vaddr = new_shdr_header->sh_addr; new_phdr_header->p_type = 1; new_phdr_header->p_paddr = new_phdr_header->p_vaddr; elf_header->e_phnum += 1;*/ }
void CELFFile::AddSection(char * new_section_name,size_t new_section_size,long new_section_flag) { if (buffer == NULL || new_section_size == 0) return; size_t old_size = size; Elf32_Shdr * t_shdr_header = GetFirstSectionHeader() + GetSectionCount(); if (reinterpret_cast<size_t>(t_shdr_header) - reinterpret_cast<size_t>(GetImage()) == old_size) { ReallocImage(size + new_section_size + sizeof(Elf32_Shdr)); } else { ReallocImage(size + new_section_size ); } Elf32_Ehdr * elf_header = GetElfHeader(); Elf32_Shdr * string_section_header = GetSectionHeader( elf_header->e_shstrndx ); size_t str_table_last_offset = string_section_header->sh_size + string_section_header->sh_offset; char * new_string_begin_addr = reinterpret_cast<char*>(GetImage()) + str_table_last_offset; strcpy(new_string_begin_addr,new_section_name); string_section_header->sh_size += strlen(new_section_name); //Elf32_Shdr * new_shdr_header = GetSectionHeader(GetSectionCount() - 1); //Elf32_Shdr * new_shdr_header = GetSectionHeader(0) + GetSectionCount(); //printf ("last section:%s\n",GetStringTableStr( new_shdr_header->sh_name)); Elf32_Shdr * new_shdr_header = reinterpret_cast<Elf32_Shdr*>( (elf_header->e_shoff + reinterpret_cast<Elf32_Off>(GetImage()))); new_shdr_header += GetSectionCount(); //new_shdr_header++; printf("Elf32_Shdr Size:%x\n",sizeof(Elf32_Shdr)); if (reinterpret_cast<size_t>(new_shdr_header) - reinterpret_cast<size_t>(GetImage()) == old_size) { new_shdr_header->sh_offset = old_size + sizeof(Elf32_Shdr); } else { new_shdr_header->sh_offset = old_size; } printf("offset :%x\n",reinterpret_cast<size_t>(new_shdr_header) - reinterpret_cast<size_t>(GetImage())); new_shdr_header->sh_name = str_table_last_offset - string_section_header->sh_offset; printf ("last section:%s\n",GetStringTableStr( new_shdr_header->sh_name)); new_shdr_header->sh_addr = GetNewSectionBase(); new_shdr_header->sh_size = new_section_size; printf("size :%#x\n",new_shdr_header->sh_size); new_shdr_header->sh_flags = new_section_flag; //new_shdr_header->sh_offset = old_size; new_shdr_header->sh_type = 1; new_shdr_header->sh_link = 0; new_shdr_header->sh_info = 0; new_shdr_header->sh_addralign = 16; new_shdr_header->sh_entsize = 0; ++elf_header->e_shnum; }