DO NOT REPLY TO THIS MESSAGE.  INSTEAD, POST ANY RESPONSES TO THE LINK BELOW.

[STR New]

Link: http://www.fltk.org/str.php?L2520
Version: 1.3.0


Why not that we are talking about load image from memory, add this too:
(I've got this from the fltk forum so credits should be given to jseb,
http://fltk.org/newsgroups.php?s1+gfltk.general+v13+T0+Qjseb)

----Fl_PNG_Image.H
struct Png_infos {
  char name_png[256];
  int offset_png; //where we stopped last time we read the "file"
  unsigned char *datas_png;
};

class Fl_PNG_Memory_Image : public Fl_RGB_Image {
  public:
    Fl_PNG_Memory_Image(const char *name_png, unsigned char *buffer_png);
    int get_png_h() { return this->h(); }
    int get_png_w() { return this->w(); }
  private:
    Png_infos png_infos;
};
----

---Fl_PNG_Image.cxx
static void png_read_data_from_mem( png_structp png_ptr, //pointer on
struct which contains addy on our source datas.
                                   png_bytep data,     //pointer on the
dest buffer we have to fill
                                   png_size_t length); //nbr of bytes to
read this time

/* when we create the class, we have to pass a pointer on the beginning
of the PNG file in memory
   we pass also the original name of the png, for error logging purpose
*/
Fl_PNG_Memory_Image::Fl_PNG_Memory_Image (const char *name_png, unsigned
char *buffer_png):
    Fl_RGB_Image(0,0,0)
{
  int i; // Looping var
  int channels; // Number of color channels
  png_structp pp; // PNG read pointer
  png_infop info; // PNG info pointers
  png_bytep *rows; // PNG row pointers

  //prepare the struct
  png_infos.offset_png=0;
  png_infos.datas_png = buffer_png;


  // Setup the PNG data structures...
  pp   = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  info = png_create_info_struct(pp);

  if (setjmp(pp->jmpbuf))
  {
    Fl::warning("PNG file \"%s\" contains errors!\n", name_png);
    return;
  }

  // Initialize the function pointer to the PNG read "engine"...
//  png_set_read_fn (pp, (png_voidp) buffer_png, png_read_data_from_mem);
  png_set_read_fn (pp, (png_voidp) &png_infos, png_read_data_from_mem);

  //TODO : impl�menter png_check_sig


  // Get the image dimensions and convert to grayscale or RGB...
  png_read_info(pp, info);

  if (info->color_type == PNG_COLOR_TYPE_PALETTE)
    png_set_expand(pp);

  if (info->color_type & PNG_COLOR_MASK_COLOR)
    channels = 3;
  else
    channels = 1;

  if ((info->color_type & PNG_COLOR_MASK_ALPHA) || info->num_trans)
    channels ++;

  w((int)(info->width));
  h((int)(info->height));
  d(channels);

  if (info->bit_depth < 8)
  {
    png_set_packing(pp);
    png_set_expand(pp);
  }
  else if (info->bit_depth == 16)
    png_set_strip_16(pp);

#  if defined(HAVE_PNG_GET_VALID) && defined(HAVE_PNG_SET_TRNS_TO_ALPHA)
  // Handle transparency...
  if (png_get_valid(pp, info, PNG_INFO_tRNS))
    png_set_tRNS_to_alpha(pp);
#  endif // HAVE_PNG_GET_VALID && HAVE_PNG_SET_TRNS_TO_ALPHA

  array = new uchar[w() * h() * d()];
  alloc_array = 1;

  // Allocate pointers...
  rows = new png_bytep[h()];

  for (i = 0; i < h(); i ++)
    rows[i] = (png_bytep)(array + i * w() * d());

  // Read the image, handling interlacing as needed...
  for (i = png_set_interlace_handling(pp); i > 0; i --)
    png_read_rows(pp, rows, NULL, h());

#ifdef WIN32
  // Some Windows graphics drivers don't honor transparency when RGB ==
white
  if (channels == 4) {
    // Convert RGB to 0 when alpha == 0...
    uchar *ptr = (uchar *)array;
    for (i = w() * h(); i > 0; i --, ptr += 4)
      if (!ptr[3]) ptr[0] = ptr[1] = ptr[2] = 0;
  }
#endif // WIN32

  // Free memory and return...
  delete[] rows;

  png_read_end(pp, info);
  png_destroy_read_struct(&pp, &info, NULL);
}


static void png_read_data_from_mem( png_structp png_ptr, //pointer on
struct which contains pointer on our datas
                                    png_bytep data,  //where you have to
copy the sources datas for libpng computing
                                    png_size_t length) //length of datas
to copy
{
  Png_infos *pnginfo = (Png_infos *)png_get_io_ptr (png_ptr); //get the
pointer on our struct
  unsigned char *src = &pnginfo->datas_png[pnginfo->offset_png];

  /* copy data from image buffer */
  memcpy (data, src, length);

  /* advance in the file */
  pnginfo->offset_png += length;
}


Link: http://www.fltk.org/str.php?L2520
Version: 1.3.0

_______________________________________________
fltk-bugs mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk-bugs

Reply via email to