日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

通用图象文件读取类

發布時間:2025/4/5 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 通用图象文件读取类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
通用圖象文件讀取類 //---------------------------------------------------------------------------------------------
typedef?struct?my_error_mgr?{
????
struct?jpeg_error_mgr?pub;
????jmp_buf?setjmp_buffer;
}
?*my_error_ptr;

METHODDEF?
void?my_error_exit?(j_common_ptr?cinfo)?{
????my_error_ptr?myerr?
=?(my_error_ptr)?cinfo->err;
????longjmp(myerr
->setjmp_buffer,?1);
}


//Number?of?simultaneous?live?strings.
//Libtiff?uses?a?large?number?of?strings?simultaneously?in?a?dialog,?so?this?method?
//was?modified?so?that?it?iterates?through?a?different?number?of?buffers.

#define?TIF_MAX_STRINGS????????????4
#define?TIF_MAX_STRING_SIZE????????256

//Declare?this?function?as?extern?"C"?so?that?the?libtiff?stuff?can?hook?into?it.

extern?"C"?TCHAR?*GetString(int?id)
{
????
static?int?bufPos=0;
????
static?TCHAR?buf[TIF_MAX_STRING_SIZE*TIF_MAX_STRINGS];
????TCHAR
*?ret;

????
if(?LoadString(NULL,?id,?buf+bufPos,?TIF_MAX_STRING_SIZE)?)?{
????????ret?
=?buf+bufPos;
????????bufPos?
+=?TIF_MAX_STRING_SIZE;
????????
if(?bufPos?>=?TIF_MAX_STRINGS*TIF_MAX_STRING_SIZE?)?bufPos?=?0;
????????
return?ret;
????}

????
return?NULL;
}

//---------------------------------------------------------------------------------------------
void?Eclinse::getExt(std::string?&?name)
{
????
int????pos;
????
if(?(pos?=?(int)?name.find_last_of('.'))?!=?name.npos?)?{
????????
int????count?=?(int)?name.length()?-?pos;
????????
if(?count?>?5?)
????????????count?
=?5;
????????name?
=?name.substr(?pos,?count?);
????????
if(?name[name.length()?-?1]?==?'\\'?)
????????????name?
=?name.substr(?0,?name.length()?-?1?);
????}

}

//---------------------------------------------------------------------------------------------
ImageFile::ImageFile()
{
????fp????????????
=?NULL;
????width????????
=?0;
????height????????
=?0;
????width1????????
=?0;
????height1????????
=?0;
????component????
=?0;
????ypos????????
=?0;
}

//---------------------------------------------------------------------------------------------
ImageFile::~ImageFile()
{
}

//---------------------------------------------------------------------------------------------
int?ImageFile::GetWidth()?const
{
????
return?width;
}

//---------------------------------------------------------------------------------------------
int?ImageFile::GetHeight()?const
{
????
return?height;
}

//---------------------------------------------------------------------------------------------
int?ImageFile::GetComponent()?const
{
????
return?component;
}

//---------------------------------------------------------------------------------------------
RAWImageFile::RAWImageFile()
{
????wb????
=?0;
}

//---------------------------------------------------------------------------------------------
RAWImageFile::~RAWImageFile()
{
????Close();
}

//---------------------------------------------------------------------------------------------
bool?RAWImageFile::GetInfo(const?char?*name,?int?&?w,?int?&?h,?int?&?c)
{
????Close();

????
if(?(fp?=?fopen(?name,?"rb"?))?==?NULL?)
????????
return?false;

????fread(?
&w,?sizeof(int),?1,?fp?);
????fread(?
&h,?sizeof(int),?1,?fp?);
????fread(?
&c,?sizeof(int),?1,?fp?);

????Close();
????
return?true;
}

//---------------------------------------------------------------------------------------------
bool?RAWImageFile::Load(const?char?*name)
{
????Close();

????
if(?(fp?=?fopen(?name,?"rb"?))?==?NULL?)
????????
return?false;

????fread(?
&width,?sizeof(int),?1,?fp?);
????fread(?
&height,?sizeof(int),?1,?fp?);
????fread(?
&component,?sizeof(int),?1,?fp?);

????
if(?component?!=?3?&&?component?!=?4?)
????????
return?false;

????width1????????
=?width?-?1;
????height1????????
=?height?-?1;
????ypos????????
=?0;
????wb????????????
=?width?*?component;
????
????
return?true;
}

//---------------------------------------------------------------------------------------------
void?RAWImageFile::Close()
{
????
if(?fp?!=?NULL?)?{
????????fclose(?fp?);
????????fp?
=?NULL;
????}

}

//---------------------------------------------------------------------------------------------
bool?RAWImageFile::MoveToLine(int?y)
{
????
if(?fp?==?NULL?||?y?<?0?||?y?>=?height?)
????????
return?false;
????fseek(?fp,?y?
*?wb,?SEEK_SET?);
????ypos?
=?y;
????
return?true;
}

//---------------------------------------------------------------------------------------------
bool?RAWImageFile::MoveToNextLine()
{
????
if(?fp?==?NULL?||?ypos?==?height1?)
????????
return?false;
????fseek(?fp,?wb,?SEEK_CUR?);
????
++?ypos;
????
return?true;
}

//---------------------------------------------------------------------------------------------
void?RAWImageFile::FormatSubLine(BYTE?*BGRbuf,?BYTE?*Abuf,?int?x1,?int?x2)
{
????
if(?fp?==?NULL?)
????????
return;
????fseek(?fp,?x1?
*?component,?SEEK_CUR?);
????
if(?component?==?3?)?{
????????fread(?BGRbuf,?(x2?
-?x1)?*?component,?1,?fp?);
????}
?else?if(?component?==?4?)?{
????????
for(int?i?=?x1;?i?<?x2;?++i)?{
????????????fread(?BGRbuf,?
3,?1,?fp?);
????????????fread(?Abuf,?
1,?1,?fp?);
????????????BGRbuf?
+=?3;
????????????
++?Abuf;
????????}

????}

????fseek(?fp,?
-?x2?*?component,?SEEK_CUR?);
}

//---------------------------------------------------------------------------------------------
BMPImageFile::BMPImageFile()
{
????wb????
=?0;
}

//---------------------------------------------------------------------------------------------
BMPImageFile::~BMPImageFile()
{
????Close();
}

//---------------------------------------------------------------------------------------------
bool?BMPImageFile::GetInfo(const?char?*name,?int?&?w,?int?&?h,?int?&?c)
{
????Close();

????
if(?(fp?=?fopen(?name,?"rb"?))?==?NULL?)
????????
return?false;

????BITMAPFILEHEADER????bfhHeader;

????fread(?
&bfhHeader,?sizeof(BITMAPFILEHEADER),?1,?fp?);

????
if(?bfhHeader.bfType?!=?0x4d42?)?{
????????fclose(?fp?);
????????
return?false;
????}


????UINT????uBmpInfoLen?
=?(UINT)?bfhHeader.bfOffBits?-?sizeof(BITMAPFILEHEADER);

????LPBITMAPINFOHEADER????m_lpBMPHdr?
=?(LPBITMAPINFOHEADER)?new?BYTE?[?uBmpInfoLen?];

????fread(?m_lpBMPHdr,?uBmpInfoLen,?
1,?fp?);

????
if(?m_lpBMPHdr->biSize?!=?sizeof(BITMAPINFOHEADER)?||
????????m_lpBMPHdr
->biBitCount?!=?24?||
????????m_lpBMPHdr
->biCompression?!=?BI_RGB?)?{
????????delete?[]?m_lpBMPHdr;
????????fclose(?fp?);
????????
return?false;
????}


????w?
=?m_lpBMPHdr->biWidth;
????h?
=?m_lpBMPHdr->biHeight;
????c?
=?4;

????delete?[]?m_lpBMPHdr;

????Close();
????
return?true;
}

//---------------------------------------------------------------------------------------------
bool?BMPImageFile::Load(const?char?*name)
{
????Close();

????
if(?(fp?=?fopen(?name,?"rb"?))?==?NULL?)
????????
return?false;

????BITMAPFILEHEADER????bfhHeader;

????fread(?
&bfhHeader,?sizeof(BITMAPFILEHEADER),?1,?fp?);

????
if(?bfhHeader.bfType?!=?0x4d42?)?{
????????fclose(?fp?);
????????
return?false;
????}


????UINT????uBmpInfoLen?
=?(UINT)?bfhHeader.bfOffBits?-?sizeof(BITMAPFILEHEADER);

????LPBITMAPINFOHEADER????m_lpBMPHdr?
=?(LPBITMAPINFOHEADER)?new?BYTE?[?uBmpInfoLen?];

????fread(?m_lpBMPHdr,?uBmpInfoLen,?
1,?fp?);

????
if(?m_lpBMPHdr->biSize?!=?sizeof(BITMAPINFOHEADER)?||
????????m_lpBMPHdr
->biBitCount?!=?24?||
????????m_lpBMPHdr
->biCompression?!=?BI_RGB?)?{
????????delete?[]?m_lpBMPHdr;
????????fclose(?fp?);
????????
return?false;
????}


????width????????
=?m_lpBMPHdr->biWidth;
????height????????
=?m_lpBMPHdr->biHeight;
????component????
=?3;
????width1????????
=?width?-?1;
????height1????????
=?height?-?1;
????ypos????????
=?height1;
????wb????????????
=?(width?*?3?+?3)?&?~3;

????delete?[]?m_lpBMPHdr;

????
return?true;
}

//---------------------------------------------------------------------------------------------
void?BMPImageFile::Close()
{
????
if(?fp?!=?NULL?)?{
????????fclose(?fp?);
????????fp?
=?NULL;
????}

}

//---------------------------------------------------------------------------------------------
bool?BMPImageFile::MoveToLine(int?y)
{
????
if(?fp?==?NULL?||?y?<?0?||?y?>=?height?)
????????
return?false;
????fseek(?fp,?
0,?SEEK_END?);
????fseek(?fp,?
-?wb?*?(y?+?1),?SEEK_CUR?);
????ypos?
=?y;
????
return?true;
}

//---------------------------------------------------------------------------------------------
bool?BMPImageFile::MoveToNextLine()
{
????
if(?fp?==?NULL?||?ypos?==?height1?)
????????
return?false;
????fseek(?fp,?
-?wb,?SEEK_CUR?);
????
++?ypos;
????
return?true;
}

//---------------------------------------------------------------------------------------------
void?BMPImageFile::FormatSubLine(BYTE?*BGRbuf,?BYTE?*Abuf,?int?x1,?int?x2)
{
????
if(?fp?==?NULL?)
????????
return;
????fseek(?fp,?x1?
*?component,?SEEK_CUR?);
????fread(?BGRbuf,?(x2?
-?x1)?*?component,?1,?fp?);
????fseek(?fp,?
-?x2?*?component,?SEEK_CUR?);
}

//---------------------------------------------------------------------------------------------
JPGImageFile::JPGImageFile()
{
}

//---------------------------------------------------------------------------------------------
JPGImageFile::~JPGImageFile()
{
????Close();
}

//---------------------------------------------------------------------------------------------
bool?JPGImageFile::GetInfo(const?char?*name,?int?&?w,?int?&?h,?int?&?c)
{
????Close();

????
if(?(fp?=?fopen(?name,?"rb"?)?)?==?NULL?)
????????
return?false;

????
struct?jpeg_decompress_struct????cinfo;
????
struct?my_error_mgr????????????????jerr;

????cinfo.err?
=?jpeg_std_error(?&jerr.pub?);
????jerr.pub.error_exit?
=?my_error_exit;

????
if(?setjmp(jerr.setjmp_buffer)?)?{
????????jpeg_destroy_decompress(?
&cinfo?);
????????
return?false;
????}


????jpeg_create_decompress(?
&cinfo?);
????jpeg_stdio_src(?
&cinfo,?fp?);

????jpeg_read_header(?
&cinfo,?TRUE?);
????jpeg_start_decompress(?
&cinfo?);

????
if(?cinfo.num_components?!=?3?&&?cinfo.num_components?!=?4?)
????????
return?false;

????w?
=?cinfo.output_width;
????h?
=?cinfo.output_height;
????c?
=?cinfo.num_components;

????jpeg_destroy_decompress(?
&cinfo?);

????Close();
????
return?true;
}

//---------------------------------------------------------------------------------------------
bool?JPGImageFile::Load(const?char?*name)
{
????Close();

????
if(?(fp?=?fopen(?name,?"rb"?)?)?==?NULL?)
????????
return?false;

????
struct?jpeg_decompress_struct????cinfo;
????
struct?my_error_mgr????????????????jerr;
????JSAMPARRAY????????????????????????buffer;
????
int????????????????????????????????row_stride;
????FILE????????????????????????????
*tmpf?=?NULL;

????cinfo.err?
=?jpeg_std_error(?&jerr.pub?);
????jerr.pub.error_exit?
=?my_error_exit;

????
if(?setjmp(jerr.setjmp_buffer)?)?{
????????jpeg_destroy_decompress(?
&cinfo?);
????????
return?false;
????}


????jpeg_create_decompress(?
&cinfo?);
????jpeg_stdio_src(?
&cinfo,?fp?);

????jpeg_read_header(?
&cinfo,?TRUE?);
????jpeg_start_decompress(?
&cinfo?);

????
if(?cinfo.num_components?!=?3?&&?cinfo.num_components?!=?4?)
????????
return?false;

????width????????
=?cinfo.output_width;
????height????????
=?cinfo.output_height;
????component????
=?cinfo.num_components;
????width1????????
=?width?-?1;
????height1????????
=?height?-?1;
????ypos????????
=?0;
????row_stride????
=?width?*?component;

????buffer?
=?(*cinfo.mem->alloc_sarray)
????????????????((j_common_ptr)?
&cinfo,?JPOOL_IMAGE,?row_stride,?1);

????tmpnam(?tmpf_name?);
????
????
if(?(tmpf?=?fopen(tmpf_name,?"wb"))?==?NULL?)?{
????????jpeg_destroy_decompress(?
&cinfo?);
????????
return?false;
????}


????fwrite(?
&width,?sizeof(int),?1,?tmpf?);
????fwrite(?
&height,?sizeof(int),?1,?tmpf?);
????fwrite(?
&component,?sizeof(int),?1,?tmpf?);

????
while(?cinfo.output_scanline?<?cinfo.output_height?)?{
????????jpeg_read_scanlines(?
&cinfo,?buffer,?1?);
????????BYTE????
*ptr?=?buffer[0];
????????
if(?component?==?3?)?{
????????????
for(int?i?=?0;?i?<?width;?++i)?{
????????????????fwrite(?ptr?
+?2,?1,?1,?tmpf?);
????????????????fwrite(?ptr?
+?1,?1,?1,?tmpf?);
????????????????fwrite(?ptr?
+?0,?1,?1,?tmpf?);
????????????????ptr?
+=?component;
????????????}

????????}
?else?if(?component?==?4?)?{
????????????
for(int?i?=?0;?i?<?width;?++i)?{
????????????????fwrite(?ptr?
+?2,?1,?1,?tmpf?);
????????????????fwrite(?ptr?
+?1,?1,?1,?tmpf?);
????????????????fwrite(?ptr?
+?0,?1,?1,?tmpf?);
????????????????fwrite(?ptr?
+?3,?1,?1,?tmpf?);
????????????????ptr?
+=?component;
????????????}

????????}

????}
;

????fclose(?tmpf?);

????jpeg_finish_decompress(?
&cinfo?);
????jpeg_destroy_decompress(?
&cinfo?);

????raw.Load(?tmpf_name?);

????
return?true;
}

//---------------------------------------------------------------------------------------------
void?JPGImageFile::Close()
{
????
if(?fp?!=?NULL?)?{
????????fclose(?fp?);
????????fp?
=?NULL;
????}

????raw.Close();
????remove(?tmpf_name?);
}

//---------------------------------------------------------------------------------------------
bool?JPGImageFile::MoveToLine(int?y)
{
????
return?raw.MoveToLine(?y?);
}

//---------------------------------------------------------------------------------------------
bool?JPGImageFile::MoveToNextLine()
{
????
return?raw.MoveToNextLine();
}

//---------------------------------------------------------------------------------------------
void?JPGImageFile::FormatSubLine(BYTE?*BGRbuf,?BYTE?*Abuf,?int?x1,?int?x2)
{
????raw.FormatSubLine(?BGRbuf,?Abuf,?x1,?x2?);
}

//---------------------------------------------------------------------------------------------
TIFImageFile::TIFImageFile()
{
????tif????????
=?NULL;
????td????????
=?NULL;
????loadbuf????
=?NULL;
}

//---------------------------------------------------------------------------------------------
TIFImageFile::~TIFImageFile()
{
????Close();
}

//---------------------------------------------------------------------------------------------
bool?TIFImageFile::GetInfo(const?char?*name,?int?&?w,?int?&?h,?int?&?c)
{
????Close();

????
if(?(tif?=?TIFFOpen(?name,?_T("r")?))?==?NULL?)
????????
return?false;

????td?
=?&tif->tif_dir;

????
if(?td->td_photometric?!=?PHOTOMETRIC_RGB?||
????????(td
->td_samplesperpixel?!=?3?&&?td->td_samplesperpixel?!=?4)?||
????????td
->td_bitspersample?!=?8?)
????????
return?false;

????w?
=?td->td_imagewidth;
????h?
=?td->td_imagelength;
????c?
=?td->td_samplesperpixel;

????Close();
????
return?true;
}

//---------------------------------------------------------------------------------------------
bool?TIFImageFile::Load(const?char?*name)
{
????Close();

????
if(?(tif?=?TIFFOpen(?name,?_T("r")?))?==?NULL?)
????????
return?false;

????td?
=?&tif->tif_dir;

????
if(?td->td_photometric?!=?PHOTOMETRIC_RGB?||
????????(td
->td_samplesperpixel?!=?3?&&?td->td_samplesperpixel?!=?4)?||
????????td
->td_bitspersample?!=?8?)
????????
return?false;

????width????????
=?td->td_imagewidth;
????height????????
=?td->td_imagelength;
????component????
=?td->td_samplesperpixel;
????width1????????
=?width?-?1;
????height1????????
=?height?-?1;
????ypos????????
=?0;
????
????loadbuf?
=?new?BYTE[?tif->tif_scanlinesize?];

????
return?true;
}

//---------------------------------------------------------------------------------------------
void?TIFImageFile::Close()
{
????
if(?tif?!=?NULL?)?{
????????TIFFClose(?tif?);
????????tif????
=?NULL;
????????td????
=?NULL;
????}

????
if(?loadbuf?!=?NULL?)?{
????????delete?[]?loadbuf;
????????loadbuf?
=?NULL;
????}

}

//---------------------------------------------------------------------------------------------
bool?TIFImageFile::MoveToLine(int?y)
{
????
if(?tif?==?NULL?||?y?<?0?||?y?>=?height?)
????????
return?false;
????ypos?
=?y;
????
return?true;
}

//---------------------------------------------------------------------------------------------
bool?TIFImageFile::MoveToNextLine()
{
????
if(?tif?==?NULL?||?ypos?==?height1?)
????????
return?false;
????
++?ypos;
????
return?true;
}

//---------------------------------------------------------------------------------------------
void?TIFImageFile::FormatSubLine(BYTE?*BGRbuf,?BYTE?*Abuf,?int?x1,?int?x2)
{
????
if(?tif?==?NULL?)
????????
return;
????BYTE????
*dst,?*ptr;
????
if(?td->td_planarconfig?==?PLANARCONFIG_SEPARATE?)?{
????????
for(int?i?=?0;?i?<?3;?++i)?{
????????????TIFFReadScanline(?tif,?loadbuf,?ypos,?
2?-?i?);
????????????dst?
=?BGRbuf?+?i,
????????????ptr?
=?loadbuf?+?x1;
????????????
for(int?x?=?x1;?x?<?x2;?++x)?{
????????????????dst[
0]?=?ptr[0];
????????????????dst?
+=?3;
????????????????
++?ptr;
????????????}

????????}

????????
if(?component?==?4?)?{
????????????TIFFReadScanline(?tif,?loadbuf,?ypos,?
3?);
????????????dst?
=?Abuf;
????????????ptr?
=?loadbuf?+?x1;
????????????
for(int?x?=?x1;?x?<?x2;?++x)?{
????????????????dst[
0]?=?ptr[0];
????????????????
++?dst;
????????????????
++?ptr;
????????????}

????????}

????}
?else?{
????????TIFFReadScanline(?tif,?loadbuf,?ypos,?
0?);
????????ptr?
=?loadbuf?+?x1?*?component;
????????
if(?component?==?3?)?{
????????????
for(int?i?=?x1;?i?<?x2;?++i)?{
????????????????BGRbuf[
0]????=?ptr[2];
????????????????BGRbuf[
1]????=?ptr[1];
????????????????BGRbuf[
2]????=?ptr[0];
????????????????BGRbuf?
+=?3;
????????????????ptr?
+=?component;
????????????}

????????}
?else?if(?component?==?4?)?{
????????????
for(int?i?=?x1;?i?<?x2;?++i)?{
????????????????BGRbuf[
0]????=?ptr[2];
????????????????BGRbuf[
1]????=?ptr[1];
????????????????BGRbuf[
2]????=?ptr[0];
????????????????Abuf[
0]????????=?ptr[3];
????????????????BGRbuf?
+=?3;
????????????????
++?Abuf;
????????????????ptr?
+=?component;
????????????}

????????}

????}

}

//--------------------------------------------------------------------------------------------- posted on 2006-03-22 14:58 Len3d 閱讀(...) 評論(...) 編輯 收藏

轉載于:https://www.cnblogs.com/len3d/archive/2006/03/22/355883.html

總結

以上是生活随笔為你收集整理的通用图象文件读取类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。