将原始mono8数据转换为PNG时,加快PNG++的速度

Speed up PNG++ when converting raw mono8 data to PNG

本文关键字:加快 PNG++ 速度 PNG mono8 原始 数据 转换      更新时间:2023-10-16

我需要使用PNG++来执行相机缓冲区图像(在原始mono8数据中(到无PNG压缩文件的优化转换的帮助。下面的行得通,但太慢了。我将在ARM中执行它。我需要一个非常快速的过程。

    sprintf(mBuffer, "%lu.png", pFrame->FrameCount);
    try
    {
        //Take the image from my camera buffer and put it into  vector i can 
        //work with 
        images = (const unsigned char*)pFrame->ImageBuffer;
        //create the png profile to be used
        png::image< png::gray_pixel > image(1024,1024); 
        //Take every byte from the vector and put in the determined position on 
        //the image matrix
        for(int i=0;i<1024;i++)
        {
            for(int j=0;j<1024;j++)
            {
                png::gray_pixel pix = images[i*1024+j];
                        image[i][j] = pix;
            }
        }
            image.write(mBuffer);
    }
    catch (std::exception const& error)
        {
            std::cerr << "Teste: " << error.what() << std::endl;
    }

这可能有帮助,也可能没有帮助——这只是一个建议。如果您的帧缓冲区是8位无符号像素的顺序加载,则其格式与此处记录的P5类型的NetPBM Portable Greymap(PGM(完全相同。这可以写得很快,可以直接在网页中使用,也可以很容易地使用ImageMagick(此处(转换为png,如下所示:

convert image.pgm image.png

然后你的图像写作可以很简单:

#include <stdio.h>
#include <stdlib.h>
#define HEIGHT  100
#define WIDTH   256
int main(){
   FILE *imageFile;
   uint8_t image[HEIGHT][WIDTH];
   int x,y,pixel;
   /* Create a greyscale ramp - you don't need this, you would use your framebuffer */
   for(x=0;x<HEIGHT;x++){
      for(y=0;y<WIDTH;y++){
         image[x][y]=y;
      }
   }
   /* Now write to a file */
   imageFile=fopen("image.pgm","wb");
   if(imageFile==NULL){
      perror("ERROR: Cannot open output file");
      exit(EXIT_FAILURE);
   }
   /* You could coalesce the next 3 lines into a single fprintf() if you wanted */
   fprintf(imageFile,"P5n");           // P5 filetype
   fprintf(imageFile,"%d %dn",WIDTH,HEIGHT);   // dimensions
   fprintf(imageFile,"255n");          // Max pixel
   fwrite(image,HEIGHT,WIDTH,imageFile);
   fclose(imageFile);
}

根据您实际想要对图像进行的处理,您可以在完成高速采集运行后对其进行批量转换,或者稍后在后台进行转换,或者完全在另一台机器上的其他地方进行转换。

在我的例子中,我创建了图像,但你已经在帧缓冲区中有了你的图像,所以你的图像写作将变成:

imageFile=fopen("image.pgm","wb");
fprintf(imageFile,"P5n%d %dn255n",WIDTH,HEIGHT);
fwrite(images,HEIGHT,WIDTH,imageFile);
close(imageFile);