zoukankan
html css js c++ java
通用图象文件读取类
//
---------------------------------------------------------------------------------------------
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;
}
}
}
}
//
---------------------------------------------------------------------------------------------
查看全文
相关阅读:
asp.net 验证输入有效性
优化SQL SERVER访问性能
with nocheck ; nocheck
VB SStab设置当前页
相互关联的子查询
mybatis 批量查询参数语句
转载 mysql函数大全
Tchar.h 中的一般文本映射
字节序的概念、判断、及转换
Windows风格与C/C++风格:UNICODE VS _UNICODE 与 TEXT() VS _T()
原文地址:https://www.cnblogs.com/len3d/p/355883.html
最新文章
Server.MapPath()
web设计 网页流动布局和弹性布局 转载
HTML <map> 设置图热点
文字链接颜色设置
sql server 2000数据库备份文件还原成sql server 2005 /2008
软件测试的原则
排除软件缺陷的两种重要手段
转:SVN中冲突的解决
天下武功出少林之IBM
Rational download and installation
热门文章
有趣的国家实力写照
打趣时下工程质量说
IBM Redbook之SCM解释
测试目的
IBM Redbook之Seven keys to improving business value(seven attributes)
sql server :两个null不相等;ISNULL函数
arcGIS如何删除已定义坐标转换
【转】ARCGIS SERVER 9.3 错误:unknown error code 6118
sql server 中 min() 的用法备注
混合数据类型:CAST 和 CONVERT
Copyright © 2011-2022 走看看