懒人笔记-tiff/jpg篇<ubuntu>

前言

结合项目需要,需要在ubuntu/中标麒麟 进行图片转,即TIFF<–>JPG。

代码

include

#include

#include “tiffio.h”
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc//imgproc.hpp>

tiff转jpg

void tiff_jpg(TIFF *tiff, int pageIndex, const std::string &imgPath)
{
if(!tiff)
return;

TIFFSetDirectory(tiff, pageIndex);

int width = 0;
int height = 0;
TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &height);

int pixelCount = width*height;
uint32* srcImg = (uint32*)malloc(pixelCount * sizeof (uint32));
TIFFReadRGBAImage(tiff, width, height, srcImg, 1);

//由于tiff格式的图像数据与bmp图存储方式一致,是从下到上,所以读的时候,需要从下往上读
BYTE* destImg = new BYTE[pixelCount*3];

uint32 *rowPointerToSrc = srcImg + (height - 1)*width;
BYTE *rowPointerToDest = destImg;
for (int y = height - 1; y >= 0; --y)
{
    uint32 *colPointerToSrc = rowPointerToSrc;
    BYTE *colPointerToDest = rowPointerToDest;
    for (int x = 0; x <= width - 1; ++x)
    {
        colPointerToDest[0] = (BYTE)TIFFGetB(colPointerToSrc[0]);
        colPointerToDest[1] = (BYTE)TIFFGetG(colPointerToSrc[0]);
        colPointerToDest[2] = (BYTE)TIFFGetR(colPointerToSrc[0]);

        colPointerToDest += 3;
        colPointerToSrc++;
    }
    rowPointerToSrc -= width;
    rowPointerToDest += width*3;
}

cv::Mat destMat(height, width, CV_8UC3, destImg, width * 3);
cv::imwrite(imgPath, destMat);

_TIFFfree(srcImg);
_TIFFfree(destImg);

}

bool conver_tiff_jpg(const std::string &tifPath, const std::string &dirPath)
{
//打开Tiff文件,得到指针,以后所有的操作都通过指针进行
TIFF* tiff =TIFFOpen(tifPath.c_str(), “r”);
if (tiff)
{
int pageCount = TIFFNumberOfDirectories(tiff);
for (int i = 0; i < pageCount; ++i)
{
std::string imgFullPath = dirPath + std::to_string((long long)i) + “.jpg”;
tiff_jpg(tiff, i, imgFullPath);
}
TIFFClose(tiff);
return true ;
}
return false;
}

jpg转tiff

void jpg_tiff(TIFF *tiff, int pageIndex,std::string imgPath)
{
if(!tiff)
return;

cv::Mat firstImg = cv::imread(imgPath);
cv::cvtColor(firstImg, firstImg, CV_BGR2RGB);
int firstWidth = firstImg.cols;
int firstHeight = firstImg.rows;

TIFFSetDirectory(tiff, pageIndex);

TIFFSetField(tiff, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
TIFFSetField(tiff, TIFFTAG_IMAGEWIDTH, firstWidth);
TIFFSetField(tiff, TIFFTAG_IMAGELENGTH, firstHeight);
TIFFSetField(tiff, TIFFTAG_SAMPLESPERPIXEL, 3);
TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, 8);
TIFFSetField(tiff, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
TIFFSetField(tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
TIFFSetField(tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);

uchar *firstImgData = firstImg.data;
for (int i = 0; i < firstHeight; ++i)
{
    TIFFWriteScanline(tiff, &firstImgData[i * firstWidth * 3], i);
}

TIFFWriteDirectory(tiff);

}

bool convert_jpg_tiff(std::vectorstd::string vecImgPath, std::string tifPath)
{
TIFF *tiff = TIFFOpen(tifPath.c_str(), “w”);
if (tiff)
{
int pageIndex = 0;
for (auto vecIter = vecImgPath.begin(); vecIter != vecImgPath.end(); ++vecIter)
{
jpg_tiff(tiff, pageIndex++, *vecIter);
}
TIFFClose(tiff);
return true ;
}
return false ;
}

懒人完结。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

羽毛乱发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值