double Stack_Util_Voxel_Distance(size_t index1, size_t index2, int width, int height) { if (index1 == index2) { return 0; } int tmp; if (index1 < index2) { SWAP2(index1, index2, tmp); } int area = width * height; int dz = (index1 - index2) / area; index1 %= area; index2 %= area; if (index1 == index2) { #ifdef _DEBUG_ printf("%d, %d, %d\n", 0, 0, dz); #endif return dz; } int dx, dy; if (index1 < index2) { dz++; SWAP2(index1, index2, tmp); } dy = (index1 - index2) / width; index1 %= width; index2 %= width; if (index1 == index2) { #ifdef _DEBUG_ printf("%d, %d, %d\n", 0, dy, dz); #endif return sqrt(dz * dz + dy * dy); } if (index1 < index2) { dy++; } dx = index2 - index1; #ifdef _DEBUG_ printf("%d, %d, %d\n", dx, dy, dz); #endif return sqrt(dx * dx + dy * dy + dz * dz); }
void swapn(unsigned char *b, int N, int n) { int i, j; for (i = 0; i < (n)*(N); i += N) for (j = 0; j < N/2; j ++) SWAP2(b[i + j], b[i + N - j - 1]); return; }
/* * Byte swap in place an array b of n of elements each one N bytes long. * A good compiler should unroll the inner loops. Letting the compiler do it * gives us portability. Note that we might want to isolate the * cases N = 2, 4, 8 (and 16 for long double and perhaps long long) * */ void F77_FUNC(swap1,SWAP1)(unsigned char *b, int *N, int *n) { int i, j; for (i = 0; i < (*n)*(*N); i += *N) for (j = 0; j < *N/2; j ++) SWAP2(b[i + j], b[i + *N - j - 1]); }
void F77_FUNC(swap,SWAP)(char *c, char *b, int *N, int *n) { int i, j; memcpy((void *)b, (void *)c,(*n)*(*N)); for (i = 0; i < (*n)*(*N); i += *N) for (j = 0; j < *N/2; j ++) SWAP2(b[i + j], b[i + *N - j - 1]); }
/* A simple version of quicksort; taken from Kernighan and Ritchie, page 87. Assumes 0 origin for v, number of elements = right+1 (right is index of right-most member). */ static PetscErrorCode PetscSortMPIIntWithArray_Private(PetscMPIInt *v,PetscMPIInt *V,PetscMPIInt right) { PetscErrorCode ierr; PetscMPIInt i,vl,last,tmp; PetscFunctionBegin; if (right <= 1) { if (right == 1) { if (v[0] > v[1]) SWAP2(v[0],v[1],V[0],V[1],tmp); } PetscFunctionReturn(0); } SWAP2(v[0],v[right/2],V[0],V[right/2],tmp); vl = v[0]; last = 0; for (i=1; i<=right; i++) { if (v[i] < vl) {last++; SWAP2(v[last],v[i],V[last],V[i],tmp);} } SWAP2(v[0],v[last],V[0],V[last],tmp); ierr = PetscSortMPIIntWithArray_Private(v,V,last-1);CHKERRQ(ierr); ierr = PetscSortMPIIntWithArray_Private(v+last+1,V+last+1,right-(last+1));CHKERRQ(ierr); PetscFunctionReturn(0); }
void Stack_Graph_Workspace_Set_Range(Stack_Graph_Workspace *sgw, int x0, int x1, int y0, int y1, int z0, int z1) { if (sgw->range == NULL) { sgw->range = iarray_malloc(6); } int tmp; sgw->range[0] = x0; sgw->range[1] = x1; if (sgw->range[0] > sgw->range[1]) { SWAP2(sgw->range[0], sgw->range[1], tmp); } sgw->range[2] = y0; sgw->range[3] = y1; if (sgw->range[2] > sgw->range[3]) { SWAP2(sgw->range[2], sgw->range[3], tmp); } sgw->range[4] = z0; sgw->range[5] = z1; if (sgw->range[4] > sgw->range[5]) { SWAP2(sgw->range[4], sgw->range[5], tmp); } }
/** * Writes a 16 bit integer to a StreamWrite. * * @param stream The StreamWrite to write to. * @param value The value to write. */ EXPORT void streamWriteUShort(StreamWrite* stream, uint16_t value) { if (!stream) { return; } if (stream->index + 2 >= stream->bufferLength) { streamWriteExpand(stream); } if (stream->endianness != ENDIANNESS_NATIVE) { value = SWAP2(value); } *(uint32_t*)(stream->data + stream->index) = value; streamWriteSkip(stream, 2); }
int main(void) { int x = 5; int y = 9; printf("x = %d y = %d\n", x, y); printf("\n"); SWAP1(x, y); printf("x = %d y = %d\n", x, y); printf("\n"); SWAP2(x, y); printf("x = %d y = %d\n", x, y); printf("\n"); SWAP3(x, y); printf("x = %d y = %d\n", x, y); return 0; }
//Realiza a leitura dos dados dos arquivos de fitas geradas e passa esses arquivos para o Hea void Leitura_para_Heap2(HEAP *Heap,FILE **Aqr_fitas, int* ValorLido, int* FitasDesativadas, int* FitasInativas, int* Dentro_ou_Fora, int FitaAtual) { int Posicao; Posicao = Heap->tamanhoAtual; //Porque eu estou usando a posição 0 do Heap fscanf(Aqr_fitas[FitaAtual], "%d", ValorLido); if(feof(Aqr_fitas[FitaAtual]) && (Dentro_ou_Fora[FitaAtual] != 2)) { (*FitasDesativadas)++; (*FitasInativas)--; Dentro_ou_Fora[FitaAtual] = 2; }else if (((*ValorLido) == 0) && (Dentro_ou_Fora[FitaAtual] != 2)) { if ((Dentro_ou_Fora[FitaAtual]) == 1) { Dentro_ou_Fora[FitaAtual] = 2; (*FitasDesativadas)++; (*FitasInativas)--; }else { Dentro_ou_Fora[FitaAtual] = 1; (*FitasInativas)++; } }else { if (Dentro_ou_Fora[FitaAtual] != 2) { (Heap->tamanhoAtual)++; Heap->VETOR[Posicao].valor = *ValorLido; Heap->VETOR[Posicao].flag = FitaAtual; SWAP2(Heap, Posicao); } } }
/*@ PetscSortMPIIntWithArray - Sorts an array of integers in place in increasing order; changes a second array to match the sorted first array. Not Collective Input Parameters: + n - number of values . i - array of integers - I - second array of integers Level: intermediate Concepts: sorting^ints with array .seealso: PetscSortReal(), PetscSortIntPermutation(), PetscSortInt() @*/ PetscErrorCode PetscSortMPIIntWithArray(PetscMPIInt n,PetscMPIInt i[],PetscMPIInt Ii[]) { PetscErrorCode ierr; PetscMPIInt j,k,tmp,ik; PetscFunctionBegin; if (n<8) { for (k=0; k<n; k++) { ik = i[k]; for (j=k+1; j<n; j++) { if (ik > i[j]) { SWAP2(i[k],i[j],Ii[k],Ii[j],tmp); ik = i[k]; } } } } else { ierr = PetscSortMPIIntWithArray_Private(i,Ii,n-1);CHKERRQ(ierr); } PetscFunctionReturn(0); }
//Funcao que realiza a troca de posicoes entre pais e filhos para manter a prioridade do Heap de mínimo constante void SWAP2(HEAP *Heap, int Posicao) { int Temporario_flag, Temporario_valor, menor, Dad; Dad = Pai(Posicao); menor = Posicao; if (Heap->VETOR[Posicao].valor < Heap->VETOR[Dad].valor) menor = Dad; if ((menor != Posicao) && (menor >= 0)) { //printf("SWAP2: %d sobe, %d desce\n",Heap->VETOR[Posicao].valor,Heap->VETOR[Dad].valor); Temporario_valor = Heap->VETOR[Posicao].valor; Temporario_flag = Heap->VETOR[Posicao].flag; Heap->VETOR[Posicao].valor = Heap->VETOR[menor].valor; Heap->VETOR[Posicao].flag = Heap->VETOR[menor].flag; Heap->VETOR[menor].valor = Temporario_valor; Heap->VETOR[menor].flag = Temporario_flag; SWAP2(Heap, menor); } }
Void Init() { #if defined(NULL_IAT) if (!InitFunction(&g_func)) DebugException(); #endif CMem::CreateGlobalHeap(); // AddVectoredExceptionHandler(True, VectoredHandler); INTEL_STATIC SPatch p[] = { { 0xB8, 1, 0x3B98C }, // 验证 // { 0x24EB, 2, 0x4B14F }, // 边界 // { 0xD5EB, 2, 0x4B17E }, // 边界 { 0xEB, 1, 0x4B14F }, // 边界 { 0xEB, 1, 0x4B17E }, // 边界 { 0xEB, 1, 0x483D1 }, // 边界 { 0xEB, 1, 0x4AADA }, // 边界 { 0xEB, 1, 0x4AB12 }, // 边界 { SWAP2('【'), 4, 0x49C7B }, { SWAP2('【'), 4, 0x49CA0 }, { SWAP2('】'), 4, 0x49EF6 }, { SWAP2('】'), 4, 0x49F1B }, #if defined (MY_DEBUG) { (UInt32)MyCreateFontIndirectA, 4, 0x5B044 }, { (UInt32)MyGetGlyphOutlineA, 4, 0x5B04C }, #endif }; INTEL_STATIC SFuncPatch f[] = { { CALL, 0x2F2E1, DecodeImage, 0x00 }, { CALL, 0x23EAA, MylstrcmpiA, 0x01 }, // { CALL, 0x2F065, MylstrcmpA, 0x01 }, { CALL, 0x43904, GetSeSize, 0x00 }, { CALL, 0x4392D, ReadSe, 0x00 }, }; WChar szPath[MAX_PATH]; DWORD i; #if defined (MY_DEBUG) PatchMemory(p, countof(p), f, countof(f), MYAPI(GetModuleHandleW)(0)); #else PatchMemoryNoVP(p, countof(p), f, countof(f), MYAPI(GetModuleHandleW)(0)); #endif #if defined(NULL_IAT) HMODULE hVorbisfile = g_func.LoadStayedLibraryA("vorbisfile.dll", &g_func); #else HMODULE hVorbisfile = LoadLibraryExW(L"vorbisfile.dll", NULL, 0); #endif GetFuncAddress(vorbis_func.ov_clear, hVorbisfile, "ov_clear"); GetFuncAddress(vorbis_func.ov_open_callbacks, hVorbisfile, "ov_open_callbacks"); GetFuncAddress(vorbis_func.ov_test_callbacks, hVorbisfile, "ov_test_callbacks"); GetFuncAddress(vorbis_func.ov_pcm_seek, hVorbisfile, "ov_pcm_seek"); GetFuncAddress(vorbis_func.ov_pcm_total, hVorbisfile, "ov_pcm_total"); GetFuncAddress(vorbis_func.ov_read, hVorbisfile, "ov_read"); GetFuncAddress(vorbis_func.ov_time_total, hVorbisfile, "ov_time_total"); i = MYAPI(GetModuleFileNameW)(NULL, szPath, countof(szPath)); while (szPath[--i] != '\\'); ++i; *(PULONG64)&szPath[i] = TAG4W('save'); szPath[i + 4] = 0; MYAPI(CreateDirectoryW)(szPath, NULL); #if defined(USE_CACHE) g_ImageCache.Init(); #endif }
int main(int argc, char *argv[]) { /* Global variable & function declarations */ struct GModule *module; struct { struct Option *orig, *real, *imag; } opt; const char *Cellmap_real, *Cellmap_imag; const char *Cellmap_orig; int realfd, imagfd, outputfd, maskfd; /* the input and output file descriptors */ struct Cell_head realhead, imaghead; DCELL *cell_real, *cell_imag; CELL *maskbuf; int i, j; /* Loop control variables */ int rows, cols; /* number of rows & columns */ long totsize; /* Total number of data points */ double (*data)[2]; /* Data structure containing real & complex values of FFT */ G_gisinit(argv[0]); /* Set description */ module = G_define_module(); G_add_keyword(_("imagery")); G_add_keyword(_("transformation")); G_add_keyword(_("Fast Fourier Transform")); module->description = _("Inverse Fast Fourier Transform (IFFT) for image processing."); /* define options */ opt.real = G_define_standard_option(G_OPT_R_INPUT); opt.real->key = "real"; opt.real->description = _("Name of input raster map (image fft, real part)"); opt.imag = G_define_standard_option(G_OPT_R_INPUT); opt.imag->key = "imaginary"; opt.imag->description = _("Name of input raster map (image fft, imaginary part"); opt.orig = G_define_standard_option(G_OPT_R_OUTPUT); opt.orig->description = _("Name for output raster map"); /*call parser */ if (G_parser(argc, argv)) exit(EXIT_FAILURE); Cellmap_real = opt.real->answer; Cellmap_imag = opt.imag->answer; Cellmap_orig = opt.orig->answer; /* get and compare the original window data */ Rast_get_cellhd(Cellmap_real, "", &realhead); Rast_get_cellhd(Cellmap_imag, "", &imaghead); if (realhead.proj != imaghead.proj || realhead.zone != imaghead.zone || realhead.north != imaghead.north || realhead.south != imaghead.south || realhead.east != imaghead.east || realhead.west != imaghead.west || realhead.ew_res != imaghead.ew_res || realhead.ns_res != imaghead.ns_res) G_fatal_error(_("The real and imaginary original windows did not match")); Rast_set_window(&realhead); /* set the window to the whole cell map */ /* open input raster map */ realfd = Rast_open_old(Cellmap_real, ""); imagfd = Rast_open_old(Cellmap_imag, ""); /* get the rows and columns in the current window */ rows = Rast_window_rows(); cols = Rast_window_cols(); totsize = rows * cols; /* Allocate appropriate memory for the structure containing the real and complex components of the FFT. DATA[0] will contain the real, and DATA[1] the complex component. */ data = G_malloc(rows * cols * 2 * sizeof(double)); /* allocate the space for one row of cell map data */ cell_real = Rast_allocate_d_buf(); cell_imag = Rast_allocate_d_buf(); #define C(i, j) ((i) * cols + (j)) /* Read in cell map values */ G_message(_("Reading raster maps...")); for (i = 0; i < rows; i++) { Rast_get_d_row(realfd, cell_real, i); Rast_get_d_row(imagfd, cell_imag, i); for (j = 0; j < cols; j++) { data[C(i, j)][0] = cell_real[j]; data[C(i, j)][1] = cell_imag[j]; } G_percent(i+1, rows, 2); } /* close input cell maps */ Rast_close(realfd); Rast_close(imagfd); /* Read in cell map values */ G_message(_("Masking raster maps...")); maskfd = Rast_maskfd(); if (maskfd >= 0) { maskbuf = Rast_allocate_c_buf(); for (i = 0; i < rows; i++) { Rast_get_c_row(maskfd, maskbuf, i); for (j = 0; j < cols; j++) { if (maskbuf[j] == 0) { data[C(i, j)][0] = 0.0; data[C(i, j)][1] = 0.0; } } G_percent(i+1, rows, 2); } Rast_close(maskfd); G_free(maskbuf); } #define SWAP1(a, b) \ do { \ double temp = (a); \ (a) = (b); \ (b) = temp; \ } while (0) #define SWAP2(a, b) \ do { \ SWAP1(data[(a)][0], data[(b)][0]); \ SWAP1(data[(a)][1], data[(b)][1]); \ } while (0) /* rotate the data array for standard display */ G_message(_("Rotating data...")); for (i = 0; i < rows; i++) for (j = 0; j < cols / 2; j++) SWAP2(C(i, j), C(i, j + cols / 2)); for (i = 0; i < rows / 2; i++) for (j = 0; j < cols; j++) SWAP2(C(i, j), C(i + rows / 2, j)); /* perform inverse FFT */ G_message(_("Starting Inverse FFT...")); fft2(1, data, totsize, cols, rows); /* open the output cell map */ outputfd = Rast_open_fp_new(Cellmap_orig); /* Write out result to a new cell map */ G_message(_("Writing raster map <%s>..."), Cellmap_orig); for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) cell_real[j] = data[C(i, j)][0]; Rast_put_d_row(outputfd, cell_real); G_percent(i+1, rows, 2); } Rast_close(outputfd); G_free(cell_real); G_free(cell_imag); fft_colors(Cellmap_orig); /* Release memory resources */ G_free(data); G_done_msg(" "); exit(EXIT_SUCCESS); }
Int_Arraylist *Stack_Route(const Stack *stack, int start[], int end[], Stack_Graph_Workspace *sgw) { if (sgw->gw == NULL) { sgw->gw = New_Graph_Workspace(); } if (sgw->range == NULL) { double dist = Geo3d_Dist(start[0], start[1], start[2], end[0], end[1], end[2]); int margin[3]; int i = 0; for (i = 0; i < 3; ++i) { margin[i] = iround(dist - abs(end[i] - start[i] + 1)); if (margin[i] < 0) { margin[i] = 0; } } Stack_Graph_Workspace_Set_Range(sgw, start[0], end[0], start[1], end[1], start[2], end[2]); Stack_Graph_Workspace_Expand_Range(sgw, margin[0], margin[0], margin[1], margin[1], margin[2], margin[2]); Stack_Graph_Workspace_Validate_Range(sgw, stack->width, stack->height, stack->depth); } int swidth = sgw->range[1] - sgw->range[0] + 1; int sheight = sgw->range[3] - sgw->range[2] + 1; int sdepth = sgw->range[5] - sgw->range[4] + 1; int start_index = Stack_Util_Offset(start[0] - sgw->range[0], start[1] - sgw->range[2], start[2] - sgw->range[4], swidth, sheight, sdepth); int end_index = Stack_Util_Offset(end[0] - sgw->range[0], end[1] - sgw->range[2], end[2] - sgw->range[4], swidth, sheight, sdepth); if (start_index > end_index) { int tmp; SWAP2(start_index, end_index, tmp); } ASSERT(start_index >= 0, "Invalid starting index."); ASSERT(end_index >= 0, "Invalid ending index."); tic(); Graph *graph = Stack_Graph_W(stack, sgw); ptoc(); tic(); int *path = NULL; switch (sgw->sp_option) { case 0: path = Graph_Shortest_Path_E(graph, start_index, end_index, sgw->gw); break; case 1: { //printf("%g\n", sgw->intensity[start_index]); sgw->intensity[end_index] = 4012; sgw->intensity[start_index] = 4012; path = Graph_Shortest_Path_Maxmin(graph, start_index, end_index, sgw->intensity, sgw->gw); } break; } sgw->value = sgw->gw->dlist[end_index]; Kill_Graph(graph); if (isinf(sgw->value)) { return NULL; } #ifdef _DEBUG_2 { Graph_Update_Edge_Table(graph, sgw->gw); Stack *stack = Make_Stack(GREY, swidth, sheight, sdepth); Zero_Stack(stack); int nvoxel = (int) Stack_Voxel_Number(stack); int index = end_index; printf("%d -> %d\n", start_index, end_index); while (index >= 0) { if (index < nvoxel) { stack->array[index] = 255; } int x, y, z; Stack_Util_Coord(index, swidth, sheight, &x, &y, &z); printf("%d (%d, %d, %d), %g\n", index, x, y, z, sgw->gw->dlist[index]); index = path[index]; } Write_Stack("../data/test2.tif", stack); Kill_Stack(stack); } #endif Int_Arraylist *offset_path = Parse_Stack_Shortest_Path(path, start_index, end_index, stack->width, stack->height, sgw); int org_start = Stack_Util_Offset(start[0], start[1], start[2], stack->width, stack->height, stack->depth); if (org_start != offset_path->array[0]) { iarray_reverse(offset_path->array, offset_path->length); } int org_end = Stack_Util_Offset(end[0], end[1], end[2], stack->width, stack->height, stack->depth); //printf("%d, %d\n", org_end, offset_path->array[offset_path->length -]); ASSERT(org_start == offset_path->array[0], "Wrong path head."); if (org_end != offset_path->array[offset_path->length - 1]) { printf("debug here\n"); } ASSERT(org_end == offset_path->array[offset_path->length - 1], "Wrong path tail."); ptoc(); return offset_path; }
//把指定的一个record中的数据解压到内存缓冲中 int CSeedFile::DecompOneRecord( char* lpBase, int* lpData, int iBufLen ) { FIX_DATA_HEADER* lpFDH; FRAME_STEIM* lpFrame; int iFormat, iOrder, iLen; int iSample, iNeedCnt; int x0, xn; unsigned short ustmp, usCnt; lpFDH = (FIX_DATA_HEADER*) lpBase; ustmp = lpFDH->usDataOffset; usCnt = lpFDH->usSampleNum; if(m_swap) { ustmp = SWAP2(ustmp); usCnt = SWAP2(usCnt); } iNeedCnt = usCnt;//仅仅是类型转换 //提取信息,准备解压 lpFrame = (FRAME_STEIM*)(lpBase + ustmp); if( GetDataAttrib((FIX_DATA_HEADER*)lpBase, m_step, m_swap, &iFormat, &iOrder, &iLen) == 0) { //非标准mini-seed文件,没有相应的1000块,使用默认设置 iFormat = m_defFormat; iOrder = m_swap; iLen = m_step; } else iLen = POWER2(iLen); //日志,添加解压信息 if(log != NULL && log->IsDetailLog()) { char szSequ[8]; strcpy_ULN(szSequ, lpFDH->szSeqNum, 6); sprintf_s(log->logString, MAX_LOG_STR_SIZE, "record sequence:%s. " "encoding code:%d. word order:%d. record length:%d", szSequ, iFormat, iOrder, iLen); log->FormatAndAppendLog(info_seed, info_unpack_record, log->logString); } //解压 switch(iFormat) { case DE_STEIM1: iSample = unpack_steim1(lpFrame, iLen - ustmp, iOrder, lpData, \ iBufLen, &x0, &xn); break; case DE_STEIM2: iSample = unpack_steim2(lpFrame, iLen - ustmp, iOrder, lpData, \ iBufLen, &x0, &xn); break; default: //日志,不能解压的数据 if(log != NULL) { sprintf_s(log->logString, MAX_LOG_STR_SIZE, "format code:%d", iFormat); log->FormatAndAppendLog(error_seed, erro_unknow_code, log->logString); } return -1; } //检查错误 if(lpData[iNeedCnt -1] != xn)//数据不一致 { //日志,数据不一致 if(log != NULL) { sprintf_s(log->logString, MAX_LOG_STR_SIZE, "the last data should be:%d", xn); log->FormatAndAppendLog(warning_seed, warn_data_differ, log->logString); } } if(iSample > iNeedCnt)//解压出来的数据量过多 { //日志,解压的数据过多 if(log != NULL) { sprintf_s(log->logString, MAX_LOG_STR_SIZE, "total decompressed samples:%d. " "the needed samples:%d. " "the exceeded samples:%d", iSample, iNeedCnt, iSample - iNeedCnt); log->FormatAndAppendLog(warning_seed, warn_data_exceed,log->logString); } } else if(iSample < iNeedCnt)//解压出来的数据量不足 { //填补最后面的数据 int n, last; last = iSample - 1; for(n = iSample; n < iNeedCnt; n++) lpData[n] = lpData[last]; //日志,解压的数据缺少 if(log != NULL) { sprintf_s(log->logString, MAX_LOG_STR_SIZE, "total decompressed samples:%d. " "the needed samples:%d. " "the missing samples:%d. " "use the last data to fill:%d", iSample, iNeedCnt, iNeedCnt - iSample, lpData[last]); log->FormatAndAppendLog(warning_seed, warn_data_missing,log->logString); } } else//解压出来的数据刚好足数 //日志,解压的数据正确 if(log != NULL && log->IsDetailLog()) { sprintf_s(log->logString, MAX_LOG_STR_SIZE, "total decompressed samples:%d", iSample); log->FormatAndAppendLog(info_seed, info_data_ok,log->logString); } return iNeedCnt; }