如何设置和获取fipTag

时间:2019-04-02 17:50:36

标签: c++ freeimage

我的尝试

fipTag t;
t.setType(FIDT_LONG);
t.setKey("FrameTime");
long l = 128;
t.setValue(&l);

应该将标签值设置为128长

但是..

const long * lo = static_cast<const long*> ( t.getValue() );
cout << "t.getValue() " << *lo << " ?? doesn't look right" << endl; // 847909360
cout << "l == *lo " << (l == *lo) << endl;                          // false

怎么了?我们如何在FreeImagePlus中正确设置和获取fipTag的值?

无用的文档:http://freeimage.sourceforge.net/fip/classfipTag.html#a34b0b079bc222aaf4b4d1ad4ce423f82

整个代码:

#include <FreeImagePlus.h>
#include <iostream>

using namespace std;
int main(void)
{
    fipTag t;
    t.setType(FIDT_LONG);
    t.setKey("FrameTime");
    long l = 128;
    t.setValue(&l);
    const long * lo = static_cast<const long*> ( t.getValue() );
    cout << "t.getValue() " << *lo << " ?? doesn't look right" << endl;
    cout << "l == *lo " << (l == *lo) << endl;

    return 0;
}

1 个答案:

答案 0 :(得分:1)

在类型设置为FIDT_LONG的情况下,setTag(仅是FreeImage_SetTagValue的包装器)期望由4个字节(32位)无符号整数组成的数组。 long在64位系统上通常为8个字节。

此外,该值的总字节大小以及其中包含的元素数必须明确设置。

#include <iostream>
#include <iterator> // std::size (C++17 or newer)

#include <FreeImagePlus.h>

int main()
{
  const uint32_t writeValue[] {123, 456};

  fipTag t;
  t.setKey("FrameTime");
  t.setType(FIDT_LONG);
  t.setCount(std::size(writeValue)); // number of elements in the array
  t.setLength(sizeof(writeValue));   // size of the entire array
  // length must be count * expected size for FIDT_LONG (4)

  if(t.setValue(writeValue)) {
    auto readValue = static_cast<const uint32_t *>(t.getValue());
    std::cout << "t.getValue() " << readValue[0] << std::endl;
  }

  return 0;
}