Cardinal Urm__SwapRGMCallbackDesc (RGMCallbackDescPtr callb_desc, RGMWidgetRecordPtr widget_rec) { Cardinal ndx; /* inner loop index */ RGMResourceDescPtr res_desc; /* resource description literal */ char err_msg[300]; swapbytes( callb_desc->validation ); swapbytes( callb_desc->count ); swapbytes( callb_desc->annex ); swapbytes( callb_desc->unres_ref_count ); for (ndx=0 ; ndx < callb_desc->count ; ndx++) { #ifdef WORD64 swap4bytes( callb_desc->item[ndx].cb_item.routine ); swap4bytes( callb_desc->item[ndx].cb_item.rep_type ); #else swap2bytes( callb_desc->item[ndx].cb_item.routine ); swap2bytes( callb_desc->item[ndx].cb_item.rep_type ); #endif switch (callb_desc->item[ndx].cb_item.rep_type) { case MrmRtypeInteger: case MrmRtypeBoolean: swapbytes( callb_desc->item[ndx].cb_item.datum.ival ); break; case MrmRtypeSingleFloat: swapbytes( callb_desc->item[ndx].cb_item.datum.ival ); _MrmOSIEEEFloatToHost((float *) &(callb_desc->item[ndx].cb_item.datum.ival)); case MrmRtypeNull: break; case MrmRtypeResource: swapbytes( callb_desc->item[ndx].cb_item.datum.offset ); res_desc = (RGMResourceDesc *) ((char *)widget_rec + callb_desc->item[ndx].cb_item.datum.offset); Urm__SwapRGMResourceDesc( res_desc ); /* flag this resource as needing further byte swapping */ res_desc->cvt_type |= MrmResourceUnswapped; break; default: swapbytes( callb_desc->item[ndx].cb_item.datum.offset ); sprintf(err_msg, _MrmMMsg_0021, callb_desc->item[ndx].cb_item.rep_type, ndx); return Urm__UT_Error ("Urm__SwapRGMCallbackDesc", err_msg, NULL, NULL, MrmFAILURE) ; break; } } return MrmSUCCESS; }
Cardinal Urm__SwapRGMResourceDesc (RGMResourceDescPtr res_desc) { IDBridDesc *idb_rid_ptr; swapbytes( res_desc->size ); swapbytes( res_desc->annex1 ); if ( res_desc->type == URMrRID ) { idb_rid_ptr = (IDBridDesc *)&(res_desc->key.id); #ifdef WORD64 swap4bytes( idb_rid_ptr->internal_id.map_rec ); swap4bytes( idb_rid_ptr->internal_id.res_index ); #else swap2bytes( idb_rid_ptr->internal_id.map_rec ); swap2bytes( idb_rid_ptr->internal_id.res_index ); #endif } return MrmSUCCESS; }
void DumpWORD(FILE *flp, short word) { char byte; short wordswp; wordswp = swap2bytes(word); for (unsigned int i = 0; i < sizeof(WORD); i++) { memcpy(&byte, (char*)&wordswp + i, 1); DumpByte(flp, &byte); fprintf(flp, " "); } }
char *readTiff3DFile2Buffer ( void *fhandler, unsigned char *img, unsigned int img_width, unsigned int img_height, unsigned int first, unsigned int last, int b_swap ) { uint32 rps; uint16 spp, bpp, photo, comp, planar_config; int check, StripsPerImage,LastStripSize; TIFF *input = (TIFF *) fhandler; check=TIFFGetField(input, TIFFTAG_ROWSPERSTRIP, &rps); if (!check) { return ((char *) "Image length of undefined."); } //rps=600; check=TIFFGetField(input, TIFFTAG_BITSPERSAMPLE, &bpp); if (!check) { return ((char *) "Undefined bits per sample."); } check=TIFFGetField(input, TIFFTAG_SAMPLESPERPIXEL, &spp); if (!check) { return ((char *) "Undefined samples per pixel."); } check=TIFFGetField(input, TIFFTAG_PHOTOMETRIC, &photo); if (!check) { return ((char *) "Cannot determine photometric interpretation."); } check=TIFFGetField(input, TIFFTAG_COMPRESSION, &comp); if (!check) { return ((char *) "Cannot determine compression technique."); } check=TIFFGetField(input, TIFFTAG_PLANARCONFIG, &planar_config); if (!check) { return ((char *) "Cannot determine planar configuration."); } StripsPerImage = (img_height + rps - 1) / rps; LastStripSize = img_height % rps; if (LastStripSize==0) LastStripSize=rps; check=TIFFSetDirectory(input, first); if (!check) { return ((char *) "Cannot open the requested first strip."); } unsigned char *buf = img; int page=0; do{ for (int i=0; i < StripsPerImage-1; i++){ if (comp==1) { TIFFReadRawStrip(input, i, buf, spp * rps * img_width * (bpp/8)); buf = buf + spp * rps * img_width * (bpp/8); } else{ TIFFReadEncodedStrip(input, i, buf, spp * rps * img_width * (bpp/8)); buf = buf + spp * rps * img_width * (bpp/8); } } if (comp==1) { TIFFReadRawStrip(input, StripsPerImage-1, buf, spp * LastStripSize * img_width * (bpp/8)); } else{ TIFFReadEncodedStrip(input, StripsPerImage-1, buf, spp * LastStripSize * img_width * (bpp/8)); } buf = buf + spp * LastStripSize * img_width * (bpp/8); page++; }while ( page < static_cast<int>(last-first+1) && TIFFReadDirectory(input));//while (TIFFReadDirectory(input)); // input file is assumedo ti be already open and it is provided as an handler; the file should be closed by caller //TIFFClose(input); if ( page < static_cast<int>(last-first+1) ){ return ((char *) "Cannot read all the pages."); } // swap the data bytes if necessary if (b_swap) { int i; size_t total = img_width * img_height * spp * (last-first+1); if (bpp/8 == 2) { for (i=0;i<total; i++) { swap2bytes((void *)(img+2*i)); } } else if (bpp/8 == 4) { for (i=0;i<total; i++) { swap4bytes((void *)(img+4*i)); } } } return (char *) 0; }
int loadRaw2Stack(char * filename, unsigned char ** img, long ** sz, int datatype) //this is the function of 4-byte raw format. { /* This function reads 2-4D image stack from raw data generated by the program "saveStack2Raw.m". */ /* The input parameters img, sz, and datatype should be empty, especially the pointers "img" and "sz". */ int berror = 0; FILE * fid = fopen(filename, "rb"); if (!fid) { printf("Fail to open file for reading.\n"); berror = 1; return berror; } fseek (fid, 0, SEEK_END); long fileSize = ftell(fid); rewind(fid); /* Read header */ char formatkey[] = "raw_image_stack_by_hpeng"; long lenkey = strlen(formatkey); if (fileSize<lenkey+2+4*4+1) // datatype has 2 bytes, and sz has 4*4 bytes and endian flag has 1 byte. { printf("The size of your input file is too small and is not correct, -- it is too small to contain the legal header.\n"); printf("The fseek-ftell produces a file size = %ld.", fileSize); berror = 1; return berror; } char * keyread = (char*)Guarded_Malloc((lenkey+1)*sizeof(char), Program_Name()); if (!keyread) { printf("Fail to allocate memory.\n"); berror = 1; return berror; } long nread = fread(keyread, 1, lenkey, fid); if (nread!=lenkey) { printf("File unrecognized or corrupted file.\n"); berror = 1; return berror; } keyread[lenkey] = '\0'; long i; if (strcmp(formatkey, keyread)) /* is non-zero then the two strings are different */ { printf("Unrecognized file format.\n"); if (keyread) {free(keyread); keyread=0;} berror = 1; return berror; } char endianCodeData; fread(&endianCodeData, 1, 1, fid); if (endianCodeData!='B' && endianCodeData!='L') { printf("This program only supports big- or little- endian but not other format. Check your data endian.\n"); berror = 1; if (keyread) {free(keyread); keyread=0;} return berror; } char endianCodeMachine; endianCodeMachine = checkMachineEndian(); if (endianCodeMachine!='B' && endianCodeMachine!='L') { printf("This program only supports big- or little- endian but not other format. Check your data endian.\n"); berror = 1; if (keyread) {free(keyread); keyread=0;} return berror; } int b_swap = (endianCodeMachine==endianCodeData)?0:1; printf("machine endian=[%c] data endian=[%c] b_swap=%d\n", endianCodeMachine, endianCodeData, b_swap); short int dcode = 0; fread(&dcode, 2, 1, fid); /* because I have already checked the file size to be bigger than the header, no need to check the number of actual bytes read. */ if (b_swap) swap2bytes((void *)&dcode); switch (dcode) { case 1: datatype = 1; /* temporarily I use the same number, which indicates the number of bytes for each data point (pixel). This can be extended in the future. */ break; case 2: datatype = 2; break; case 4: datatype = 4; break; default: printf("Unrecognized data type code [%d]. The file type is incorrect or this code is not supported in this version.\n", dcode); if (keyread) {free(keyread); keyread=0;} berror = 1; return berror; } long unitSize = datatype; // temporarily I use the same number, which indicates the number of bytes for each data point (pixel). This can be extended in the future. printf("loadRaw2Stack unitSize=%d\n", unitSize); unsigned int mysz[4]; mysz[0]=mysz[1]=mysz[2]=mysz[3]=0; int tmpn=fread(mysz, 4, 4, fid); // because I have already checked the file size to be bigger than the header, no need to check the number of actual bytes read. if (tmpn!=4) { printf("This program only reads [%d] units.\n", tmpn); berror=1; return berror; } if (b_swap) { for (i=0;i<4;i++) { //swap2bytes((void *)(mysz+i)); printf("mysz raw read unit[%ld]: [%d] ", i, mysz[i]); swap4bytes((void *)(mysz+i)); printf("swap unit: [%d][%0x] \n", mysz[i], mysz[i]); } } if (*sz) {free(*sz); *sz=0;} *sz=(long*)Guarded_Malloc(4*sizeof(long), Program_Name()); if (!(*sz)) { printf("Fail to allocate memory.\n"); if (keyread) {free(keyread); keyread=0;} berror = 1; return berror; } long totalUnit = 1; for (i=0;i<4;i++) { (*sz)[i] = (long)mysz[i]; totalUnit *= (*sz)[i]; } //mexPrintf("The input file has a size [%ld bytes], different from what specified in the header [%ld bytes]. Exit.\n", fileSize, totalUnit*unitSize+4*4+2+1+lenkey); //mexPrintf("The read sizes are: %ld %ld %ld %ld\n", sz[0], sz[1], sz[2], sz[3]); if ((totalUnit*unitSize+4*4+2+1+lenkey) != fileSize) { printf("The input file has a size [%ld bytes], different from what specified in the header [%ld bytes]. Exit.\n", fileSize, totalUnit*unitSize+4*4+2+1+lenkey); printf("The read sizes are: %ld %ld %ld %ld\n", (*sz)[0], (*sz)[1], (*sz)[2], (*sz)[3]); printf("The read sizes are: %d %d %d %d\n", mysz[0], mysz[1], mysz[2], mysz[3]); if (keyread) {free(keyread); keyread=0;} if (*sz) {free(*sz); *sz=0;} berror = 1; return berror; } if (*img) {free(*img); *img=0;} long totalBytes = unitSize*totalUnit; *img = (unsigned char *)Guarded_Malloc(totalBytes*sizeof(unsigned char), Program_Name()); if (*img==0 || *img==NULL) { fprintf(stderr, "Fail to allocate memory in loadRaw2Stack().\n"); if (keyread) {free(keyread); keyread=0;} if (*sz) {free(*sz); *sz=0;} berror = 1; return berror; } long remainingBytes = totalBytes; long nBytes2G = 1024L*1024L*1024L*2L; long cntBuf = 0; while (remainingBytes>0) { long curReadBytes = (remainingBytes<nBytes2G) ? remainingBytes : nBytes2G; long curReadUnits = curReadBytes/unitSize; nread = fread(*img+cntBuf*nBytes2G, unitSize, curReadUnits, fid); if (nread!=curReadUnits) { printf("Something wrong in file reading. The program reads [%ld data points] but the file says there should be [%ld data points].\n", nread, totalUnit); if (keyread) {free(keyread); keyread=0;} if (*sz) {free(*sz); *sz=0;} if (*img) {free(*img); *img=0;} berror = 1; return berror; } remainingBytes -= nBytes2G; cntBuf++; } // swap the data bytes if necessary if (b_swap==1) { if (unitSize==2) { for (i=0;i<totalUnit; i++) { swap2bytes((void *)(*img+i*unitSize)); } } else if (unitSize==4) { for (i=0;i<totalUnit; i++) { swap4bytes((void *)(*img+i*unitSize)); } } } // clean and return if (keyread) {free(keyread); keyread = 0;} fclose(fid); //bug fix on 060412 double min=0.0, max=0.0; if (datatype==1) { findMinMax8bit(&min, &max, *img, totalUnit); } else if (datatype==2) { findMinMax16bit(&min, &max, *img, totalUnit); } else if (datatype==4) { findMinMaxFloat(&min, &max, *img, totalUnit); } printf("loadRaw2Stack() Min=%f Max=%f\n", min, max); return berror; }
Cardinal Idb__BM_SwapRecordBytes (IDBRecordBufferPtr buffer) { /* * Local variables */ Cardinal ndx; /* loop index */ IDBDummyRecordPtr idb_record; /* pointer to the generic IDB record */ IDBRecordHeaderPtr idb_header; /* pointer to hdr w/type and record # */ IDBHeaderRecordPtr header_rec; /* pointer to record type IDBrtHeader */ IDBHeaderHdrPtr header_hdr; /* pointer to the header in the header */ IDBIndexLeafRecordPtr leaf_rec; /* pointer to record type IDBrtIndexLeaf */ IDBIndexNodeRecordPtr node_rec; /* pointer to record type IDBrtIndexNode */ IDBridMapRecordPtr ridmap_rec; /* pointer to record type IDBrtRIDMap */ IDBDataRecordPtr data_rec; /* pointer to record type IDBrtData */ char err_msg[300] ; if ( ! Idb__BM_Valid(buffer) ) return Urm__UT_Error("Idb__BM_MarkActivity", _MrmMMsg_0002, NULL, NULL, MrmNOT_VALID) ; /* load pointers to the record and record header */ idb_record = (IDBDummyRecordPtr) buffer->IDB_record ; idb_header = (IDBRecordHeaderPtr)&idb_record->header ; /* swap the remaining record entries in IDBRecordHeader */ swapbytes( idb_header->record_type ) ; swapbytes( idb_header->record_num ) ; /* * Swap IDB record items based on record type */ switch ( idb_header->record_type ) { case IDBrtHeader: header_rec = (IDBHeaderRecordPtr)buffer->IDB_record ; header_hdr = (IDBHeaderHdrPtr)&header_rec->header_hdr ; /* swap the HeaderHdr first */ swapbytes( header_hdr->index_root ); swapbytes( header_hdr->num_indexed ); swapbytes( header_hdr->num_RID ); /* VAR check */ #ifdef WORD64 swap4bytes( header_hdr->next_RID.internal_id.map_rec ); swap4bytes( header_hdr->next_RID.internal_id.res_index ); #else swap2bytes( header_hdr->next_RID.internal_id.map_rec ); swap2bytes( header_hdr->next_RID.internal_id.res_index ); #endif swapbytes( header_hdr->last_record ); swapbytes( header_hdr->last_data_record ); for( ndx=0 ; ndx < URMgVecSize ; ndx++) swapbytes(header_hdr->group_counts[ndx]); for( ndx=0 ; ndx < IDBrtVecSize ; ndx++) swapbytes(header_hdr->rt_counts[ndx]); /* now swap the rest of the header */ /* VAR check */ for( ndx=0 ; ndx < IDBHeaderRIDMax ; ndx++) { swap2bytes(header_rec->RID_pointers[ndx].internal_id.rec_no); swap2bytes(header_rec->RID_pointers[ndx].internal_id.item_offs); } swapbytes( header_rec->num_entry ); swapbytes( header_rec->last_entry ); swapbytes( header_rec->free_ptr ); swapbytes( header_rec->free_count ); break; case IDBrtIndexLeaf: leaf_rec = (IDBIndexLeafRecordPtr)buffer->IDB_record ; swapbytes( leaf_rec->leaf_header.parent ); swapbytes( leaf_rec->leaf_header.index_count ); swapbytes( leaf_rec->leaf_header.heap_start ); swapbytes( leaf_rec->leaf_header.free_bytes ); for( ndx=0 ; ndx < leaf_rec->leaf_header.index_count ; ndx++ ) { swapbytes( leaf_rec->index[ndx].index_stg ); swap2bytes( leaf_rec->index[ndx].data.internal_id.rec_no ); swap2bytes( leaf_rec->index[ndx].data.internal_id.item_offs ); } break; case IDBrtIndexNode: node_rec = (IDBIndexNodeRecordPtr)buffer->IDB_record ; swapbytes( node_rec->node_header.parent ); swapbytes( node_rec->node_header.index_count ); swapbytes( node_rec->node_header.heap_start ); swapbytes( node_rec->node_header.free_bytes ); for( ndx=0 ; ndx < node_rec->node_header.index_count ; ndx++ ) { swapbytes( node_rec->index[ndx].index_stg ); swap2bytes( node_rec->index[ndx].data.internal_id.rec_no ); swap2bytes( node_rec->index[ndx].data.internal_id.item_offs ); swapbytes( node_rec->index[ndx].LT_record ); swapbytes( node_rec->index[ndx].GT_record ); } break; case IDBrtRIDMap: ridmap_rec = (IDBridMapRecordPtr)buffer->IDB_record ; ndx = 0; while ( (ndx < IDBridPtrVecMax) && (ridmap_rec->pointers[ndx].internal_id.rec_no != 0) ) { swap2bytes( ridmap_rec->pointers[ndx].internal_id.rec_no ); swap2bytes( ridmap_rec->pointers[ndx].internal_id.item_offs ); ndx++; } break; case IDBrtData: data_rec = (IDBDataRecordPtr)buffer->IDB_record ; swapbytes( data_rec->data_header.num_entry ); swapbytes( data_rec->data_header.last_entry ); swapbytes( data_rec->data_header.free_ptr ); swapbytes( data_rec->data_header.free_count ); break; default: sprintf(err_msg, _MrmMMsg_0020, idb_header->record_num, idb_header->record_type); return Urm__UT_Error ("Idb__BM_SwapRecordBytes", err_msg, NULL, NULL, MrmFAILURE) ; } return MrmSUCCESS ; }
Cardinal Urm__SwapRGMWidgetRecord(RGMWidgetRecordPtr widget_rec) { /* * Local variables */ RGMArgListDescPtr arg_list; /* pointer to widget arglist */ RGMChildrenDescPtr child_list; /* pointer to the widgets children */ RGMResourceDescPtr res_desc; /* resource description literal */ RGMCallbackDescPtr callb_desc; /* pointer to a callback decriptor */ Cardinal ndx; /* loop index */ IDBridDesc *idb_rid_ptr; void *offset; /* generic offset pointer */ char err_msg[300]; /* Swap the main part of the widget record */ swapbytes( widget_rec->size ); swapbytes( widget_rec->access ); swapbytes( widget_rec->lock ); swapbytes( widget_rec->type ); swapbytes( widget_rec->name_offs ); swapbytes( widget_rec->class_offs ); swapbytes( widget_rec->arglist_offs ); swapbytes( widget_rec->children_offs ); swapbytes( widget_rec->comment_offs ); swapbytes( widget_rec->creation_offs ); swapbytes( widget_rec->variety ); swapbytes( widget_rec->annex ); /* handle the argument list */ if (widget_rec->arglist_offs > 0) { arg_list = (RGMArgListDesc *) ((char *)widget_rec + widget_rec->arglist_offs); swapbytes( arg_list->count ); swapbytes( arg_list->extra ); for ( ndx=0 ; ndx<arg_list->count ; ndx++ ) { swapbytes( arg_list->args[ndx].tag_code ); swapbytes( arg_list->args[ndx].stg_or_relcode.tag_offs ); swapbytes( arg_list->args[ndx].arg_val.rep_type ); switch( arg_list->args[ndx].arg_val.rep_type ) { case MrmRtypeInteger: case MrmRtypeBoolean: swapbytes( arg_list->args[ndx].arg_val.datum.ival ); break; case MrmRtypeSingleFloat: swapbytes( arg_list->args[ndx].arg_val.datum.ival ); _MrmOSIEEEFloatToHost((float *) &(arg_list->args[ndx].arg_val.datum.ival)); default: swapbytes( arg_list->args[ndx].arg_val.datum.offset ); break; } offset = ((char *)widget_rec+ arg_list->args[ndx].arg_val.datum.offset); switch( arg_list->args[ndx].arg_val.rep_type ) { /* these are immediate, do nothing special */ case MrmRtypeInteger: case MrmRtypeBoolean: case MrmRtypeSingleFloat: break; /* these are offsets into the file, handle them specially */ case MrmRtypeCallback: callb_desc = (RGMCallbackDesc * )offset; Urm__SwapRGMCallbackDesc( callb_desc, widget_rec ); break; case MrmRtypeResource: res_desc = (RGMResourceDesc *)offset; Urm__SwapRGMResourceDesc( res_desc ); /* flag this resource as needing further byte swapping */ res_desc->cvt_type |= MrmResourceUnswapped; break; case MrmRtypeHorizontalInteger: case MrmRtypeVerticalInteger: case MrmRtypeHorizontalFloat: case MrmRtypeVerticalFloat: case MrmRtypeChar8: case MrmRtypeChar8Vector: case MrmRtypeCString: case MrmRtypeCStringVector: case MrmRtypeFloat: case MrmRtypePixmapImage: case MrmRtypePixmapDDIF: case MrmRtypeNull: case MrmRtypeAddrName: case MrmRtypeIconImage: case MrmRtypeFont: case MrmRtypeFontList: case MrmRtypeColor: case MrmRtypeColorTable: case MrmRtypeAny: case MrmRtypeTransTable: case MrmRtypeClassRecName: case MrmRtypeIntegerVector: case MrmRtypeXBitmapFile: case MrmRtypeCountedVector: case MrmRtypeKeysym: case MrmRtypeWideCharacter: case MrmRtypeFontSet: sprintf(err_msg,_MrmMMsg_0022, arg_list->args[ndx].arg_val.rep_type); return Urm__UT_Error ("Urm__SwapRGMWidgetRecord", err_msg, NULL, NULL, MrmFAILURE) ; break; } } } /* handle the child list */ if (widget_rec->children_offs > 0) { child_list = (RGMChildrenDesc *) ((char *)widget_rec + widget_rec->children_offs); swapbytes( child_list->count ); swapbytes( child_list->unused1 ); swapbytes( child_list->annex1 ); for ( ndx=0 ; ndx<child_list->count ; ndx++ ) { swapbytes( child_list->child[ndx].annex1 ); if (child_list->child[ndx].type == URMrRID ) { idb_rid_ptr = (IDBridDesc *)&(child_list->child[ndx].key.id); #ifdef WORD64 swap4bytes( idb_rid_ptr->internal_id.map_rec ); swap4bytes( idb_rid_ptr->internal_id.res_index ); #else swap2bytes( idb_rid_ptr->internal_id.map_rec ); swap2bytes( idb_rid_ptr->internal_id.res_index ); #endif } else swapbytes( child_list->child[ndx].key.index_offs ); } } /* handle the creation callback, if any */ if (widget_rec->creation_offs > 0) { callb_desc = (RGMCallbackDesc * ) ((char *)widget_rec + widget_rec->creation_offs); Urm__SwapRGMCallbackDesc( callb_desc, widget_rec ); } return MrmSUCCESS ; }