c++ - stb_png_to_mem的参数是什么意思?

标签 c++ opengl png textures stb-image

我正在使用 stb 库为 OpenGL 动态加载纹理。我正在尝试使用 stb_image_write.h 中的函数。我找不到有关如何正确使用函数 stbi_write_png_to_mem 的任何文档,我的代码如下所示

#ifndef STB_IMAGE_WRITE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb/stb_image_write.h> 

int main(  )
{
    // data is filled in later in program
    int size , width , height ; 
    unsigned char* data = ( unsigned char* ) malloc( width * height ) ;

    ...
    ...
    ...


    // this is where the issue lies
    stbi_write_png_to_mem(  ) ; 

}

我查看了 github 并查看了源代码本身。我在谷歌上找不到任何东西。我不确定这个函数的输入是什么,因为不清楚什么是什么意思。函数的源代码是

STBIWDEF unsigned char *stbi_write_png_to_mem(const unsigned char *pixels, int stride_bytes, int x, int y, int n, int *out_len)
{
   int force_filter = stbi_write_force_png_filter;
   int ctype[5] = { -1, 0, 4, 2, 6 };
   unsigned char sig[8] = { 137,80,78,71,13,10,26,10 };
   unsigned char *out,*o, *filt, *zlib;
   signed char *line_buffer;
   int j,zlen;

   if (stride_bytes == 0)
      stride_bytes = x * n;

   if (force_filter >= 5) {
      force_filter = -1;
   }

   filt = (unsigned char *) STBIW_MALLOC((x*n+1) * y); if (!filt) return 0;
   line_buffer = (signed char *) STBIW_MALLOC(x * n); if (!line_buffer) { STBIW_FREE(filt); return 0; }
   for (j=0; j < y; ++j) {
      int filter_type;
      if (force_filter > -1) {
         filter_type = force_filter;
         stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, force_filter, line_buffer);
      } else { // Estimate the best filter by running through all of them:
         int best_filter = 0, best_filter_val = 0x7fffffff, est, i;
         for (filter_type = 0; filter_type < 5; filter_type++) {
            stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, filter_type, line_buffer);

            // Estimate the entropy of the line using this filter; the less, the better.
            est = 0;
            for (i = 0; i < x*n; ++i) {
               est += abs((signed char) line_buffer[i]);
            }
            if (est < best_filter_val) {
               best_filter_val = est;
               best_filter = filter_type;
            }
         }
         if (filter_type != best_filter) {  // If the last iteration already got us the best filter, don't redo it
            stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, best_filter, line_buffer);
            filter_type = best_filter;
         }
      }
      // when we get here, filter_type contains the filter type, and line_buffer contains the data
      filt[j*(x*n+1)] = (unsigned char) filter_type;
      STBIW_MEMMOVE(filt+j*(x*n+1)+1, line_buffer, x*n);
   }
   STBIW_FREE(line_buffer);
   zlib = stbi_zlib_compress(filt, y*( x*n+1), &zlen, stbi_write_png_compression_level);
   STBIW_FREE(filt);
   if (!zlib) return 0;

   // each tag requires 12 bytes of overhead
   out = (unsigned char *) STBIW_MALLOC(8 + 12+13 + 12+zlen + 12);
   if (!out) return 0;
   *out_len = 8 + 12+13 + 12+zlen + 12;

   o=out;
   STBIW_MEMMOVE(o,sig,8); o+= 8;
   stbiw__wp32(o, 13); // header length
   stbiw__wptag(o, "IHDR");
   stbiw__wp32(o, x);
   stbiw__wp32(o, y);
   *o++ = 8;
   *o++ = STBIW_UCHAR(ctype[n]);
   *o++ = 0;
   *o++ = 0;
   *o++ = 0;
   stbiw__wpcrc(&o,13);

   stbiw__wp32(o, zlen);
   stbiw__wptag(o, "IDAT");
   STBIW_MEMMOVE(o, zlib, zlen);
   o += zlen;
   STBIW_FREE(zlib);
   stbiw__wpcrc(&o, zlen);

   stbiw__wp32(o,0);
   stbiw__wptag(o, "IEND");
   stbiw__wpcrc(&o,0);

   STBIW_ASSERT(o == out + *out_len);

   return out;
}

我的目标是基本上将我的图片数据输出到内存中的 png,而不是文件,这样我就可以在我的程序的其他地方使用它,格式就像 png 一样。

最佳答案

大多数参数与传递给 stbi_write_png 的参数相同,它有一个 documentation in the header file :

  • 像素是未压缩的图像数据
  • stride_bytes 是两个连续行的两个开头之间的字节数:

    "stride_in_bytes" is the distance in bytes from the first byte of a row of pixels to the first byte of the next row of pixels.

  • x(在stbi_write_png中称为w)是图像的宽度
  • y(在stbi_write_png中称为h)是图像的高度
  • n(在stbi_write_png中称为comp)是 channel 数:

    Each pixel contains 'comp' channels of data stored interleaved with 8-bits per channel, in the following order: 1=Y, 2=YA, 3=RGB, 4=RGBA.

  • out_len 是一个输出参数,它返回压缩数据的字节数。

关于c++ - stb_png_to_mem的参数是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59671565/

相关文章:

c++ - 与排序模型一起使用时的 Qt TreeView 问题

c++ - 使用 SOIL 使用 OpenGL 映射纹理

C++语句可以简化

c++ - 如何确定一个数字的基数?

c++ - typedef 结构中的重载运算符 (c++)

java.io.IOException 将 "player"纹理添加到屏幕时出现困难

python - 使用 PyOpenGL 可视化 3D NumPy 数组

python - 旋转图像会增加其尺寸?

javascript - 将 PNG 从 GET 请求转换为 Node.js 中的可读流

asp.net - 为什么这些资源无法加载?