www.pudn.com > ShowImage.rar > Unit1.cpp
//--------------------------------------------------------------------------- #include#include #pragma hdrstop #include "Unit1.h" #include "Unit2.h" #include "Resample.h" #include "ImageAPI.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { m_nCols = 0; m_nRows =0; strDataType = ""; strTitle = ""; pByteData = NULL; pShortData = NULL; pFloatData = NULL; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormDestroy(TObject *Sender) { if(pByteData) delete pByteData; if(pShortData) delete pShortData; if(pFloatData) delete pFloatData; } //--------------------------------------------------------------------------- void __fastcall TForm1::ShowImageFileClick(TObject *Sender) { OpenDialog1->Filter="删格文件(*.img)|*.img"; if(OpenDialog1->Execute()) { ImageFileName=OpenDialog1->FileName; AnsiString DocFileName=ChangeFileExt(ImageFileName,".doc"); //使用TStringList类将文本文件一次性读入 TStringList *StringList1=new TStringList(); StringList1->LoadFromFile(DocFileName); //图象标题 strTitle=StringList1->Strings[0].SubString(15,StringList1->Strings[0].Length()-13); //得到数据类型 strDataType=StringList1->Strings[1].SubString(15,StringList1->Strings[1].Length()-13); //得到文件类型 AnsiString FileType=StringList1->Strings[2].SubString(15,StringList1->Strings[2].Length()-13); //得到图象的列数 m_nCols=StrToInt(StringList1->Strings[3].SubString(15,StringList1->Strings[3].Length()-13)); //得到图象的行数 m_nRows=StrToInt(StringList1->Strings[4].SubString(15,StringList1->Strings[4].Length()-13)); if(strDataType=="byte" && FileType=="binary") //如果文件格式是二进制字节型,调用该函数 ByteBinaryImg(); if(strDataType=="integer" && FileType=="binary") { int min=StrToInt(StringList1->Strings[14].SubString(15,StringList1->Strings[14].Length()-13)); int max=StrToInt(StringList1->Strings[15].SubString(15,StringList1->Strings[14].Length()-13)); //如果文件格式是二进制整型,则调用如下函数 IntegerBinaryImg(min,max); } if(strDataType=="real" && FileType=="binary") { float min=StringList1->Strings[14].SubString(15,StringList1->Strings[14].Length()-13).ToDouble(); float max=StringList1->Strings[15].SubString(15,StringList1->Strings[14].Length()-13).ToDouble(); //如果文件格式是二进制实型,则调用如下函数 RealBinaryImg(min,max); } if(strDataType=="byte" && FileType=="packed binary") //如果文件格式是二进制压缩字节型,则调用如下函数 BytePackedBinaryImg(); delete StringList1; } } //--------------------------------------------------------------------------- void __stdcall TForm1::ByteBinaryImg() { byte *ByteData = new Byte[m_nRows* m_nCols]; FILE *fp1=fopen(ImageFileName.c_str(),"rb"); //从图象文件中读数据 fread(ByteData,1,m_nRows* m_nCols, fp1); fclose(fp1); pByteData = new Byte[m_nRows* m_nCols]; if(pByteData == NULL) { MessageBox(NULL,"内存不足!","错误", MB_OK); return; } memcpy(pByteData, ByteData,m_nRows* m_nCols); ShowImage(ByteData); } //------------------------------------------------------------------------- void __stdcall TForm1::BytePackedBinaryImg() { byte *ByteData=new Byte[m_nRows* m_nCols]; FILE *fp1=fopen(ImageFileName.c_str(),"rb"); Byte Two[2]={0,0}; int j=0,old=0; while(!feof(fp1)) { fread(Two,1,2,fp1); for(int i=0;i m_nCols) { old%=m_nCols; j++; } ByteData[j*m_nCols + old] = Two[1]; } } fclose(fp1); pByteData = new Byte[m_nRows* m_nCols]; if(pByteData == NULL) { MessageBox(NULL,"内存不足!","错误", MB_OK); return; } memcpy(pByteData, ByteData,m_nRows* m_nCols); ShowImage(ByteData); } //------------------------------------------------------------------------- void __stdcall TForm1::IntegerBinaryImg(int min,int max) { short *IntegerData=new short[m_nRows* m_nCols]; FILE *fp1=fopen(ImageFileName.c_str(),"rb"); fread(IntegerData,2,m_nRows* m_nCols,fp1); fclose(fp1); byte *ByteData=new Byte[m_nRows* m_nCols]; int k=0; int range = max - min; for(int i=0;i Filter="调色板文件(*.smp)|*.smp"; if(!OpenDialog1->Execute()) { if(ByteData) delete ByteData; return; } AnsiString PaletteFile=OpenDialog1->FileName; FILE *fp2=fopen(PaletteFile.c_str(),"rb"); byte c[256][3]; //跳过前18个字节 fseek(fp2,18,0); //读调色板数据 fread(c[0],3,256,fp2); fclose(fp2); TForm2 *Form2=new TForm2(this); Form2->Caption= strTitle; Form2->Image1->Width = m_nCols; Form2->Image1->Height = m_nRows; //构造一TBitmap类 Graphics::TBitmap *pBitmap = new Graphics::TBitmap(); pBitmap->Width=m_nCols; pBitmap->Height=m_nRows; pBitmap->PixelFormat=pf8bit; LOGPALETTE *myPalette=(LOGPALETTE *)new char[sizeof(LOGPALETTE)+sizeof(PALETTEENTRY)*256]; myPalette->palVersion=0x300; myPalette->palNumEntries=256; for(int i=0;i<256;i++) { myPalette->palPalEntry[i].peRed=c[i][0]; myPalette->palPalEntry[i].peGreen=c[i][1]; myPalette->palPalEntry[i].peBlue=c[i][2]; myPalette->palPalEntry[i].peFlags=PC_RESERVED; } pBitmap->Palette=::CreatePalette(myPalette); byte *ptr=new byte[m_nCols]; for (int y= 0; y (pBitmap->ScanLine[y]); for (int x = 0; x Image1->Canvas->Draw(0,0,pBitmap); delete pBitmap; if(ByteData) delete ByteData; } //--------------------------------------------------------------------------- void __fastcall TForm1::ResampleClick(TObject *Sender) { TResampleDialog *ResampleDialog = new TResampleDialog(this); ResampleDialog->OldRows->Text = IntToStr(m_nRows); ResampleDialog->OldCols->Text = IntToStr(m_nCols); ResampleDialog->ShowModal(); if(ResampleDialog->ModalResult != mrOk) return; int newRows = StrToInt(ResampleDialog->NewRows->Text); int newCols = StrToInt(ResampleDialog->NewCols->Text); if(strDataType == "byte") { Byte* pOutputData = new Byte[newRows* newCols]; if(pOutputData == NULL) { MessageBox(NULL,"内存不足!","错误", MB_OK); return; } switch(ResampleDialog->RadioGroup1->ItemIndex) { case 0: ProResample(pByteData, pOutputData, m_nCols, m_nRows, newCols, newRows); break; case 1: break; case 2: break; } } } //---------------------------------------------------------------------------