nedoPC.org

Electronics hobbyists community established in 2002
Atom Feed | View unanswered posts | View active topics It is currently 18 Mar 2024 20:13



Reply to topic  [ 1 post ] 
Как сохранить PNG картинку в своей программе 
Author Message
Online
Admin
User avatar

Joined: 08 Jan 2003 23:22
Posts: 22379
Location: Silicon Valley
Reply with quote
Пример на голых сях:
Code:
/* gcc savepng.c -o savepng -lpng */

#include <stdio.h>
#include <stdlib.h>
#include <png.h>

int savepng(char* filename, int width, int height, unsigned char* data)
{
   /* Assume that it's RGB888 image */
   int bitdepth = 8;
   int colortype = PNG_COLOR_TYPE_RGB;
   int transform = PNG_TRANSFORM_IDENTITY;
   int i = 0;
   int r = 0;
   FILE* fp = NULL;
   png_structp png_ptr = NULL;
   png_infop info_ptr = NULL;
   static png_bytep row_pointers[1024];
   if(NULL == data) { r = -1; goto endlabel; }
   if(!filename || !filename[0]) { r = -2; goto endlabel; }
   if(NULL == (fp = fopen(filename, "wb"))) { r = -4; goto endlabel; }
   if(NULL == (png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL))) { r = -5; goto endlabel; }
   if(NULL == (info_ptr = png_create_info_struct(png_ptr))) { r = -6; goto endlabel; }
   png_set_IHDR(png_ptr, info_ptr, width, height, bitdepth,
                colortype,          /* PNG_COLOR_TYPE_{GRAY, PALETTE, RGB, RGB_ALPHA, GRAY_ALPHA, RGBA, GA} */
                PNG_INTERLACE_NONE, /* PNG_INTERLACE_{NONE, ADAM7 } */
                PNG_COMPRESSION_TYPE_BASE,
                PNG_FILTER_TYPE_BASE);
   for(i = 0; i < height; ++i) row_pointers[i] = data + i*width*3;
   png_init_io(png_ptr, fp);
   png_set_rows(png_ptr, info_ptr, row_pointers);
   png_write_png(png_ptr, info_ptr, transform, NULL);
 endlabel:
   if(NULL != fp)
   {
     fclose(fp);
     fp = NULL;
   }
   if(NULL != png_ptr)
   {
     if(NULL == info_ptr) r = -7;
     png_destroy_write_struct(&png_ptr, &info_ptr);
   }
   return r;
}

int main()
{
   int i,j,k;
   int w = 320;
   int h = 240;
   unsigned char* img;

   img = (unsigned char*)malloc(w*h*3);
   if(img==NULL) return -10;

   for(j=0;j<h;j++)
   {
      for(i=0;i<w;i++)
      {
         k = j*w*3+i*3;
         img[k] = i*255/w;
         img[k+1] = j*255/h;
         img[k+2] = (i+j)&255;
      }
   }

   k = savepng("saved.png",w,h,img);

   printf("\nsavepng %i\n\n",k);

   free(img);

   return 0;
}

Результат работы программы (которая в функции main формирует картинку 320х240 и вызывает функцию savepng для сохранения этой картинки в файл) приаттачен ниже:


Attachments:
saved.png
saved.png [ 818 Bytes | Viewed 14473 times ]

_________________
:dj: https://mastodon.social/@Shaos
26 Oct 2021 22:19
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 1 post ] 

Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.