pngs

Member
Posts: 567
Joined: 2004.07
Post: #1
good and simple loading function: HOW DO YOU UPLOAD AND TEXTURE WITH PNGS? (with c, no cocoa.)

It's not magic, it's Ruby.
Quote this message in a reply
Member
Posts: 131
Joined: 2004.10
Post: #2
I haven't got around to trying to compile on the mac for this but it's not too out there code wise. Most was just following the doc.

What you get back is an RGB buffer of the image. From there it shouldn't be too hard to put it into a texture.

This is using libpng.

Code:
int msPngLoad(char *file, int *ix, int *iy, char **ibuffer)
{
   char         *buffer,
                 header[8],
               **rows;
   int           result,
                 a,
                 bitDepth,
                 colorType;
   unsigned long x,
                 y,
                 rowBytes;
   FILE         *fp;
   png_structp   pptr;
   png_infop     iptr,
                 eptr;

   buffer = NULL;

   /* Open the file. */
   fp = fopen(file, "rb");
   if (!fp) {
      return FALSE;
   }

   /* Check to see if the file is a png. */
   fread(header, 1, 8, fp);
   if (png_sig_cmp((unsigned char *) header, 0, 8)) {
      fclose(fp);
      return FALSE;
   }
  
   /* Get the png information. */
   pptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
   if (!pptr) {
      fclose(fp);
      return FALSE;
   }
  
   iptr = png_create_info_struct(pptr);
   if (!iptr) {
      png_destroy_read_struct(&pptr, (png_infopp) NULL, (png_infopp) NULL);
      fclose(fp);
      return FALSE;
   }
  
   eptr = png_create_info_struct(pptr);
   if (!eptr) {
      png_destroy_read_struct(&pptr, &iptr, (png_infopp) NULL);
      fclose(fp);
      return FALSE;
   }

   /* For PNG error handling. */
   if (setjmp(png_jmpbuf(pptr))) {
      png_destroy_read_struct(&pptr, &iptr, &eptr);
      fclose(fp);
      return FALSE;
   }

   /* Init file IO */
   /* png_init_io(pptr, fp); NOT FOUND!?!?!*/
   pptr->io_ptr = (png_voidp)fp;

   png_set_sig_bytes(pptr, 8);
  
   /* Read the image info.*/
   png_read_info(pptr, iptr);
   png_get_IHDR(pptr, iptr, &x, &y, &bitDepth, &colorType, NULL, NULL,
      NULL);
  
   /* Set up some transforms. */
   if (colorType & PNG_COLOR_MASK_ALPHA) {
      png_set_strip_alpha(pptr);
   }
   if (bitDepth > 8) {
      png_set_strip_16(pptr);
   }
   if (colorType == PNG_COLOR_TYPE_GRAY ||
       colorType == PNG_COLOR_TYPE_GRAY_ALPHA) {
      png_set_gray_to_rgb(pptr);
   }
   if (colorType == PNG_COLOR_TYPE_PALETTE) {
      png_set_palette_to_rgb(pptr);
   }
  
   /* Update the png info struct.*/
   png_read_update_info(pptr, iptr);

   /* Rowsize in bytes. */
   rowBytes = png_get_rowbytes(pptr, iptr);

   /* Allocate the image buffer. */
   buffer = (char *) malloc(sizeof(char) * x * y * 3);

   /* Allocate the 'row pointers', image buffer. */
   rows = (char **) malloc(sizeof(char *) * y);
  
   /* Set up the row pointers. */
   for (a = 0; a < ((int) y); a++) {
      rows[a] = &(buffer[a * rowBytes]);
   }
  
   /* Read in the image. */
   png_read_image(pptr, (unsigned char **) rows);
  
   /* Some more info at the end. (needed or not.) */
   png_read_end(pptr, iptr);

   /* Clean up. */
   free(rows);
  
   result = TRUE;

   /* Clean up. */
   png_destroy_read_struct(&pptr, &iptr, &eptr);
   fclose(fp);

   *ix      = x;
   *iy      = y;
   *ibuffer = buffer;

   return result;
}
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #3
Use one of the existing libraries, like QuickTime, libpng, or Cocoa.

This question has been answered a dozen times on this forum, just search.
Quote this message in a reply
Member
Posts: 567
Joined: 2004.07
Post: #4
arekkusu Wrote:Use one of the existing libraries, like QuickTime, libpng, or Cocoa.

This question has been answered a dozen times on this forum, just search.

yea, i have. I saw QTValue pack, but I couldn't find it. As I said before, I'm trying to use C, therefore, I can't use Cocoa. Mad

It's not magic, it's Ruby.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #5
Nayr Wrote:yea, i have. I saw QTValue pack, but I couldn't find it.

http://onesadcookie.is-a-geek.net/svn/re...LTexture.c

- Alex Diener
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Trouble writing PNGs with 16 bits per channel using libpng flash 2 3,865 Aug 28, 2006 11:01 AM
Last Post: flash
  Problems with transparency/blending, .pngs, and SDL_image Nick 22 9,523 Mar 1, 2005 02:04 PM
Last Post: Nick