分析 Tiff的TIFFTAG_PHOTOSHOP标签的部分代码

2 篇文章 0 订阅
void test_TiffPhotoShopTag(const std::string& tiffFilePath, const std::string& binFilePath, const std::string& textFilePath)
{
    TIFF* tif = TIFFOpen(tiffFilePath.c_str(), "rCm");
    assert(tif);
    //写文件
    uint32 ps_size = 0;
    void* ps_data = NULL;
    if (TIFFGetField(tif, TIFFTAG_PHOTOSHOP, &ps_size, &ps_data))
    {
        {
            std::fstream  fs;
            fs.open(binFilePath, std::ios_base::out | std::ios_base::binary);
            assert(fs.is_open());
            fs.write((char*)ps_data, ps_size);
            fs.close();
        }

        {
            std::fstream  fs;
            fs.open(textFilePath, std::ios_base::out | std::ios_base::binary);
            assert(fs.is_open());

            //转化为文本
            char szBuf[1024] = { 0 };
            std::string str;
            str.reserve(1024 * 1024 * 5);
            uint8* p = (uint8*)ps_data;

            int pos = 0;
            for (uint32 i = 0; i < ps_size; ++i)
            {
                //特殊处理
                bool b = false;

                if (i + 5 < ps_size)
                {
                    if (p[i] == '8' && p[i] && p[i + 1] == 'B' && p[i + 2] == 'P' && p[i + 3] == 'S')
                    {
                        b = true;

                        _snprintf_s(szBuf, 1024, "\n\n[8BPS  %02X%02X] ", (int)(p[i + 4]), (int)(p[i + 5]));
                        i += 5;
                        pos = i + 1;
                    }

                    if (p[i] == '8' && p[i] && p[i + 1] == 'B' && p[i + 2] == 'I' && p[i + 3] == 'M')
                    {
                        b = true;
                        _snprintf_s(szBuf, 1024, "\n\n[8BIM  %02X%02X] ", (int)(p[i + 4]), (int)(p[i + 5]));
                        i += 5;
                        pos = i + 1;
                    }
                }

                if (b)
                {
                    str += szBuf;
                }
                else
                {
                    if ((i - pos) % 16 == 0)
                        str += "\n";

                    _snprintf_s(szBuf, 1024, "%02X ", (int)(p[i]));
                    str += szBuf;
                }
            }

            fs.write(str.c_str(), str.size());
            fs.close();
        }
    }

    TIFFClose(tif);
    tif = nullptr;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值