新增tiff G4压缩

This commit is contained in:
13038267101 2023-02-23 18:17:19 +08:00
parent 75be24b8c8
commit 32e4657116
5 changed files with 130 additions and 4 deletions

View File

@ -180,6 +180,12 @@ void on_aquireimage(HG_IMG *img, void *obj)
imagedata.insert(imagedata.end(), img->pimgdata, img->pimgdata + img->bufferlength); imagedata.insert(imagedata.end(), img->pimgdata, img->pimgdata + img->bufferlength);
writeimage(savepath, imagedata); writeimage(savepath, imagedata);
//bool b = G4TiffSave((char *)savepath.c_str());
//if (!b)
//{
// printf("Tiff G4 Conversion Fail \r\n");
// return;
//}
printf("--------------------savepath:%s-------------------------------------------\r\n", savepath.c_str()); printf("--------------------savepath:%s-------------------------------------------\r\n", savepath.c_str());
/* ofstream o(savepath); /* ofstream o(savepath);

View File

@ -12,25 +12,38 @@
#include <vector> #include <vector>
#include <opencv2/opencv.hpp> #include <opencv2/opencv.hpp>
#include "assert.h" #include "assert.h"
#include <tiff.h>
#include <tiffio.h>
using namespace std; using namespace std;
HG_SCANPARAMS m_params; HG_SCANPARAMS m_params;
std::string img_type_ = ".jpg"; std::string img_type_ = ".jpg";
void* m_obj_usb = nullptr; void* m_obj_usb = nullptr;
void* m_obj_img = nullptr; void* m_obj_img = nullptr;
static const char* SDKVersion = "1.22.9.30"; static const char* SDKVersion = "4.23.02.23";
char deviceList[60]; char deviceList[60];
unsigned int compression_data = 80; unsigned int compression_data = 80;
HG_OnDevice_Event_Callback m_callback_OnDevcie_Event = nullptr; HG_OnDevice_Event_Callback m_callback_OnDevcie_Event = nullptr;
HG_OnImage_Callback m_callback_OnImage_Event = nullptr; HG_OnImage_Callback m_callback_OnImage_Event = nullptr;
unsigned int m_scannum = 0; unsigned int m_scannum = 0;
void G4TiffExcept(bool condition, const std::string& message);
void* allocate_memory(size_t bytes, const char* log_msg)
{
bytes += 7;
bytes /= 8;
bytes *= 8;
return new char[bytes];
}
void callback_USBEvent(int conditioncode, void* obj) void callback_USBEvent(int conditioncode, void* obj)
{ {
if (m_callback_OnDevcie_Event) if (m_callback_OnDevcie_Event)
m_callback_OnDevcie_Event((HG_STATUSCODE)conditioncode, m_obj_usb); m_callback_OnDevcie_Event((HG_STATUSCODE)conditioncode, m_obj_usb);
} }
//有图事件回调 //有图事件回调
static int indeximg = 10; static int indeximg = 0;
static void HGAPI DeviceScanImageFunc(HGLibDevice device, HGLibImage image, HGPointer param) static void HGAPI DeviceScanImageFunc(HGLibDevice device, HGLibImage image, HGPointer param)
{ {
if (image == NULL) if (image == NULL)
@ -948,3 +961,103 @@ DLL_HG_SCHANNER_API int HG_Create_MultiTiff(char** srcFiles, int srcnum, char* d
printf("\nHGImgFmt_CloseTiffWriter %d", hgret); printf("\nHGImgFmt_CloseTiffWriter %d", hgret);
return 1; return 1;
} }
void G4TiffExcept(bool condition, const std::string& message)
{
if (!condition)
throw std::runtime_error("Error " + message);
}
DLL_HG_SCHANNER_API bool G4TiffSave(char* m_tmppath)
{
if (!m_tmppath)
{
printf("filename is not find\r\n");
return false;
}
cv::Mat mat = cv::imread(m_tmppath);
if (mat.empty())
{
printf("opencv mat read image fial\r\n");
return false;
}
if (mat.channels() == 3)
{
cvtColor(mat, mat, cv::COLOR_RGB2GRAY);
}
int compression = COMPRESSION_CCITT_T6;
if (compression == COMPRESSION_CCITT_T6 && mat.channels() != 1)
throw std::runtime_error("mat channel error");
TIFF* pTiffHandle = TIFFOpen(m_tmppath, "w");
if (!pTiffHandle)
{
printf("can't open TIFF descriptor\n");
return false;
}
int width = mat.cols;
int height = mat.rows;
try
{
G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_IMAGEWIDTH, width), "width");
G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_IMAGELENGTH, height), "length");
if (compression == COMPRESSION_CCITT_T6)
{
G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_BITSPERSAMPLE, 1), "bits per sample");
G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_SAMPLESPERPIXEL, 1), "samples per pixel");
}
else
{
G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_BITSPERSAMPLE, /*mat.depth()*/8), "bits per sample");
G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_SAMPLESPERPIXEL, mat.channels()), "samples per pixel");
}
G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_ROWSPERSTRIP, height), "rows per strip");
G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_COMPRESSION, compression), "compression");
G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE), "photometric");
G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB), "photometric");
G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG), "planar config");
// not necessary
G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_XRESOLUTION, (float)m_params.Resolution), "res x");
G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_YRESOLUTION, (float)m_params.Resolution), "res y");
G4TiffExcept(TIFFSetField(pTiffHandle, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH), "res unit");
if (compression == COMPRESSION_CCITT_T6)
{
std::vector<uchar> _buffer(width / 8 + 8, 0);
//std::vector<uchar> _buffer(width / 8 + 1, 0);
uchar* buffer = &_buffer[0];
//int bytes = int(width / 8.0 + 0.5);
for (int y = 0; y < height; ++y)
{
uint8_t* src_row = mat.ptr(y);
for (int x = 0; x < width; ++x, ++src_row)
{
uint8_t eight_pixels = buffer[x / 8];
eight_pixels = eight_pixels << 1;
if (*src_row < 127)
eight_pixels = eight_pixels | 1; //
buffer[x / 8] = eight_pixels;
}
G4TiffExcept(TIFFWriteScanline(pTiffHandle, buffer, y, 0) != -1, "write scanline");
}
}
else
{
int l = mat.total() / mat.rows * mat.channels();
for (int y = 0; y < height; ++y)
{
uint8_t* src = mat.ptr(y);
G4TiffExcept(TIFFWriteScanline(pTiffHandle, src, y, 0) != -1, "write scanline");
}
}
}
catch (const std::runtime_error& e)
{
printf("TIFF writing: %s\n", e.what());
// TIFFClose(pTiffHandle);
}
TIFFClose(pTiffHandle);
return true;
}

View File

@ -448,6 +448,13 @@ extern "C" {
/// </summary> /// </summary>
/// <returns>成功:true 失败:false</returns> /// <returns>成功:true 失败:false</returns>
DLL_HG_SCHANNER_API bool HG_Set_Img_Quality(HG_Scanner_Handle pScanner, bool compressiondata = true); DLL_HG_SCHANNER_API bool HG_Set_Img_Quality(HG_Scanner_Handle pScanner, bool compressiondata = true);
/// <summary>
/// 转换tiff G4无压缩图像 支持24位图和8位图 注意:此方法会随损失出图的效率
/// m_tmppath:图像路径
/// </summary>
/// <returns>成功:true 失败:false</returns>
DLL_HG_SCHANNER_API bool G4TiffSave(char* m_tmppath);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

Binary file not shown.

View File

@ -66,7 +66,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental> <LinkIncremental>true</LinkIncremental>
<OutDir>$(SolutionDir)x86\$(Configuration)\</OutDir> <OutDir>$(SolutionDir)x86\$(Configuration)\</OutDir>
<IncludePath>..\..\..\..\..\sdk\include;$(SolutionDir)..\..\..\..\..\sdk\include\opencv\;$(IncludePath)</IncludePath> <IncludePath>..\..\..\..\..\sdk\include;$(SolutionDir)..\..\..\..\..\sdk\include\opencv\;..\..\..\..\..\code_device\hgdriver\3rdparty\tiff\include</IncludePath>
<IntDir>$(SolutionDir)x86\$(Configuration)\</IntDir> <IntDir>$(SolutionDir)x86\$(Configuration)\</IntDir>
<LibraryPath>$(ProjectDir)..\..\..\..\..\sdk\lib\win\$(PlatformTarget)\$(Configuration);$(ProjectDir)..\..\..\..\build\windows\Debug;$(LibraryPath)</LibraryPath> <LibraryPath>$(ProjectDir)..\..\..\..\..\sdk\lib\win\$(PlatformTarget)\$(Configuration);$(ProjectDir)..\..\..\..\build\windows\Debug;$(LibraryPath)</LibraryPath>
</PropertyGroup> </PropertyGroup>
@ -74,7 +74,7 @@
<LinkIncremental>true</LinkIncremental> <LinkIncremental>true</LinkIncremental>
<OutDir>$(SolutionDir)x86\$(Configuration)\</OutDir> <OutDir>$(SolutionDir)x86\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)x86\$(Configuration)\</IntDir> <IntDir>$(SolutionDir)x86\$(Configuration)\</IntDir>
<IncludePath>..\..\..\..\..\sdk\include;$(SolutionDir)..\..\..\..\..\sdk\include\opencv\;$(IncludePath)</IncludePath> <IncludePath>..\..\..\..\..\sdk\include;$(SolutionDir)..\..\..\..\..\sdk\include\opencv\;..\..\..\..\..\code_device\hgdriver\3rdparty\tiff\include</IncludePath>
<LibraryPath>$(ProjectDir)..\..\..\..\..\sdk\lib\win\$(PlatformTarget)\$(Configuration);$(ProjectDir)..\..\..\..\build\windows\Release;$(LibraryPath)</LibraryPath> <LibraryPath>$(ProjectDir)..\..\..\..\..\sdk\lib\win\$(PlatformTarget)\$(Configuration);$(ProjectDir)..\..\..\..\build\windows\Release;$(LibraryPath)</LibraryPath>
</PropertyGroup> </PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">