Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3094 in orxonox.OLD


Ignore:
Timestamp:
Dec 5, 2004, 1:55:40 AM (19 years ago)
Author:
bensch
Message:

orxonox/branches/images: importing uncompressed TGA-files

Location:
orxonox/branches/images/importer
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • orxonox/branches/images/importer/material.cc

    r3093 r3094  
    1212   main-programmer: Benjamin Grauer
    1313   co-programmer: ...
     14
     15   TGA-code: borrowed from nehe-Tutorials
    1416*/
    1517
     
    357359}
    358360
     361bool Material::loadTexToGL (Image* pImage, GLuint* texture)
     362{
     363  glGenTextures(1, texture);
     364  glBindTexture(GL_TEXTURE_2D, *texture);
     365  /* not Working, and not needed.
     366  glTexImage2D( GL_TEXTURE_2D, 0, 3, width,
     367                height, 0, GL_BGR,
     368                GL_UNSIGNED_BYTE, map->pixels );
     369  */
     370  gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pImage->width, pImage->height, GL_RGB, GL_UNSIGNED_BYTE, pImage->data);
     371 
     372  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
     373  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
     374}
     375
     376
     377
    359378/**
    360379   \brief Makes the Programm ready to Read-in a texture-File
     
    376395        printf ("Requested jpeg-image. Trying to Import\n");
    377396      return loadJPG(imageName, texture);
     397    }
     398  else if (!strncmp(imageName+strlen(imageName)-4, ".tga", 4))
     399    {
     400      if (verbose >=2)
     401        printf ("Requested tga-image. Trying to Import\n");
     402      return loadTGA(imageName, texture);
    378403    }
    379404  else
     
    412437 
    413438  // read the width
    414   if ((i = fread(&pImage->sizeX, 4, 1, file)) != 1)
     439  if ((i = fread(&pImage->width, 4, 1, file)) != 1)
    415440    {
    416441      if (verbose >=1)
     
    419444    }
    420445  // read the height
    421   if ((i = fread(&pImage->sizeY, 4, 1, file)) != 1)
     446  if ((i = fread(&pImage->height, 4, 1, file)) != 1)
    422447    {
    423448      if (verbose>=1)
     
    427452 
    428453  // calculate the size (assuming 24 bits or 3 bytes per pixel).
    429   size = pImage->sizeX * pImage->sizeY * 3;
     454  size = pImage->width * pImage->height * 3;
    430455 
    431456  // read the planes
     
    565590  // Get the image dimensions and row span to read in the pixel data
    566591  pImageData->rowSpan = cinfo->image_width * cinfo->num_components;
    567   pImageData->sizeX   = cinfo->image_width;
    568   pImageData->sizeY   = cinfo->image_height;
     592  pImageData->width   = cinfo->image_width;
     593  pImageData->height   = cinfo->image_height;
    569594 
    570595  // Allocate memory for the pixel buffer
    571   pImageData->data = new unsigned char[pImageData->rowSpan * pImageData->sizeY];
     596  pImageData->data = new unsigned char[pImageData->rowSpan * pImageData->height];
    572597 
    573598  // Here we use the library's state variable cinfo.output_scanline as the
     
    575600 
    576601  // Create an array of row pointers
    577   unsigned char** rowPtr = new unsigned char*[pImageData->sizeY];
    578   for (int i = 0; i < pImageData->sizeY; i++)
     602  unsigned char** rowPtr = new unsigned char*[pImageData->height];
     603  for (int i = 0; i < pImageData->height; i++)
    579604    rowPtr[i] = &(pImageData->data[i*pImageData->rowSpan]);
    580605 
     
    594619}
    595620
    596 bool Material::loadTexToGL (Image* pImage, GLuint* texture)
    597 {
    598   glGenTextures(1, texture);
    599   glBindTexture(GL_TEXTURE_2D, *texture);
    600   /* not Working, and not needed.
    601   glTexImage2D( GL_TEXTURE_2D, 0, 3, width,
    602                 height, 0, GL_BGR,
    603                 GL_UNSIGNED_BYTE, map->pixels );
    604   */
    605   gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pImage->sizeX, pImage->sizeY, GL_RGB, GL_UNSIGNED_BYTE, pImage->data);
    606  
    607   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
    608   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
    609  
    610 
    611 }
     621
     622
     623
     624bool Material::loadTGA(const char * tgaName, GLuint* texture)
     625{
     626  typedef struct
     627  {
     628    GLubyte Header[12];                                                                 /* TGA File Header */
     629  } TGAHeader;
     630 TGAHeader tgaHeader;                   
     631
     632  GLubyte uTGAcompare[12] = {0,0,2, 0,0,0,0,0,0,0,0,0}; /* Uncompressed TGA Header */
     633  GLubyte cTGAcompare[12] = {0,0,10,0,0,0,0,0,0,0,0,0}; /* Compressed TGA Header */
     634  FILE * fTGA;
     635  fTGA = fopen(tgaName, "rb");
     636
     637  if(fTGA == NULL)
     638    {
     639      printf("Error could not open texture file: %s\n", tgaName);
     640      return false;
     641    }
     642 
     643  if(fread(&tgaHeader, sizeof(TGAHeader), 1, fTGA) == 0) /// hmm... don't know if this is io
     644    {
     645      printf("Error could not read file header of %s\n", tgaName);
     646      if(fTGA != NULL)
     647        {
     648          fclose(fTGA);
     649        }
     650      return false;
     651    }
     652 
     653  if(memcmp(uTGAcompare, &tgaHeader, sizeof(TGAHeader)) == 0)
     654    {
     655      loadUncompressedTGA(tgaName, fTGA, texture);
     656      if (fTGA)
     657        fclose (fTGA);
     658    }
     659  else if(memcmp(cTGAcompare, &tgaHeader, sizeof(TGAHeader)) == 0)
     660    {
     661      //loadCompressedTGA(tgaName, fTGA, texture);
     662        if (fTGA)
     663          fclose (fTGA);
     664    }
     665  else
     666    {
     667      printf("Error TGA file be type 2 or type 10\n");
     668      if (fTGA)
     669        fclose(fTGA);
     670      return false;
     671    }
     672  return true;
     673}
     674
     675bool Material::loadUncompressedTGA(const char * filename, FILE * fTGA, GLuint* texture)
     676{
     677  GLubyte header[6];      // First 6 Useful Bytes From The Header
     678  GLuint  bytesPerPixel;  // Holds Number Of Bytes Per Pixel Used In The TGA File
     679  GLuint  imageSize;      // Used To Store The Image Size When Setting Aside Ram
     680  GLuint  temp;           // Temporary Variable
     681  GLuint  type;
     682  GLuint  Height;         // Height of Image
     683  GLuint  Width;          // Width of Image
     684  GLuint  Bpp;            // Bits Per Pixel
     685
     686  Image* pImage = new Image;
     687  GLuint cswap;
     688  if(fread(header, sizeof(header), 1, fTGA) == 0)
     689    {
     690      printf("Error could not read info header\n");
     691      return false;
     692    }
     693 
     694  Width = pImage->width  = header[1] * 256 + header[0];
     695  Height =  pImage->height = header[3] * 256 + header[2];
     696  Bpp = pImage->bpp = header[4];
     697  // Make sure all information is valid
     698  if((pImage->width <= 0) || (pImage->height <= 0) || ((pImage->bpp != 24) && (pImage->bpp !=32)))
     699    {
     700      printf("Error invalid texture information\n");
     701      return false;
     702    }
     703 
     704  if(pImage->bpp == 24)
     705    {
     706      pImage->type = GL_RGB;
     707    }
     708  else
     709    {
     710      pImage->type = GL_RGBA;
     711    }
     712 
     713  bytesPerPixel = (Bpp / 8);
     714  imageSize = (bytesPerPixel * Width * Height);
     715  pImage->data = (GLubyte*) malloc(imageSize);
     716 
     717  if(pImage->data == NULL)
     718    {
     719      printf("Error could not allocate memory for image\n");
     720      return false;
     721    }
     722 
     723  if(fread(pImage->data, 1, imageSize, fTGA) != imageSize)
     724    {
     725      printf("Error could not read image data\n");
     726      if(pImage->data != NULL)
     727        {
     728          free(pImage->data);
     729        }
     730      return false;
     731    }
     732 
     733  /* Byte Swapping Optimized By Steve Thomas */
     734  for(cswap = 0; cswap < (int)imageSize; cswap += bytesPerPixel)
     735    {
     736      pImage->data[cswap] ^= pImage->data[cswap+2] ^=
     737        pImage->data[cswap] ^= pImage->data[cswap+2];
     738    }
     739 
     740  loadTexToGL (pImage, texture);
     741
     742  return true;
     743}
     744
     745#ifdef __auskomentiert__
     746bool Material::LoadCompressedTGA(Texture * texture,const char * filename, FILE * fTGA)
     747{
     748  GLuint pixelcount     = Height * Width;                                                       /* Nuber of pixels in the image */
     749  GLuint currentpixel   = 0;                                                                                            /* Current pixel being read */
     750  GLuint currentbyte    = 0;                                                                                            /* Current byte */
     751  GLubyte * colorbuffer = (GLubyte *)malloc(bytesPerPixel);                     /* Storage for 1 pixel */
     752 
     753  if(fread(header, sizeof(header), 1, fTGA) == 0)                               /* Attempt to read header */
     754    {
     755      printf("Error could not read info header");                                                       /* Display Error */
     756      if(fTGA != NULL)                                                                                                  /* If file is open */
     757        {
     758          fclose(fTGA);                                                                                                 /* Close it */
     759        }
     760      return false;                                                                                                             /* Return failed */
     761    }
     762 
     763  pImage->width  = header[1] * 256 + header[0];                                 /* Determine The TGA Width      (highbyte*256+lowbyte) */
     764  pImage->height = header[3] * 256 + header[2];                                 /* Determine The TGA Height     (highbyte*256+lowbyte) */
     765  pImage->bpp   = header[4];                                                                            /* Determine Bits Per Pixel */
     766  Width         = pImage->width;                                                                                /* Copy width to local structure */
     767  Height                = pImage->height;                                                                               /* Copy width to local structure */
     768  Bpp                   = pImage->bpp;                                                                                  /* Copy width to local structure */
     769 
     770  if((pImage->width <= 0) || (pImage->height <= 0) || ((pImage->bpp != 24) && (pImage->bpp !=32)))      /*Make sure all pImage info is ok */
     771    {
     772      printf("Error Invalid pImage information");                                               /* If it isnt...Display error */
     773      if(fTGA != NULL)                                                                                                  /* Check if file is open */
     774        {
     775          fclose(fTGA);                                                                                                 /* Ifit is, close it */
     776        }
     777      return false;                                                                                                             /* Return failed */
     778    }
     779 
     780  bytesPerPixel = (Bpp / 8);                                                                    /* Compute BYTES per pixel */
     781  imageSize             = (bytesPerPixel * Width * Height);             /* Compute amout of memory needed to store image */
     782  pImage->data  = (GLubyte *)malloc(imageSize);                                 /* Allocate that much memory */
     783 
     784  if(pImage->data == NULL)                                                                                      /* If it wasnt allocated correctly.. */
     785    {
     786      printf("Error could not allocate memory for image");                              /* Display Error */
     787      fclose(fTGA);                                                                                                             /* Close file */
     788      return false;                                                                                                             /* Return failed */
     789    }
     790 
     791  do
     792    {
     793      GLubyte chunkheader = 0;                                                                                  /* Storage for "chunk" header */
     794     
     795      if(fread(&chunkheader, sizeof(GLubyte), 1, fTGA) == 0)                            /* Read in the 1 byte header */
     796        {
     797          printf("Error could not read RLE header");                                            /*Display Error */
     798          if(fTGA != NULL)                                                                                              /* If file is open */
     799            {
     800              fclose(fTGA);                                                                                             /* Close file */
     801            }
     802          if(pImage->data != NULL)                                                                      /* If there is stored image data */
     803            {
     804              free(pImage->data);                                                                       /* Delete image data */
     805            }
     806          return false;                                                                                                 /* Return failed */
     807        }
     808     
     809      if(chunkheader < 128)                                                                                             /* If the ehader is < 128, it means the that is the number of RAW color packets minus 1 */
     810        {
     811          short counter;                                                                                                        /* that follow the header */
     812          chunkheader++;                                                                                                        /* add 1 to get number of following color values */
     813          for(counter = 0; counter < chunkheader; counter++)                            /* Read RAW color values */
     814            {
     815              if(fread(colorbuffer, 1, bytesPerPixel, fTGA) != bytesPerPixel) /* Try to read 1 pixel */
     816                {
     817                  printf("Error could not read image data");                            /* IF we cant, display an error */
     818                 
     819                  if(fTGA != NULL)                                                                              /* See if file is open */
     820                    {
     821                      fclose(fTGA);                                                                             /* If so, close file */
     822                    }
     823                 
     824                  if(colorbuffer != NULL)                                                                       /* See if colorbuffer has data in it */
     825                    {
     826                      free(colorbuffer);                                                                        /* If so, delete it */
     827                    }
     828                 
     829                  if(pImage->data != NULL)                                                                              /* See if there is stored Image data */
     830                    {
     831                      free(pImage->data);                                                                               /* If so, delete it too */
     832                    }
     833                 
     834                  return false;                                                                                                         /* Return failed */
     835                }
     836              /* write to memory */
     837              pImage->data[currentbyte          ] = colorbuffer[2];                                 /* Flip R and B vcolor values around in the process */
     838              pImage->data[currentbyte + 1      ] = colorbuffer[1];
     839              pImage->data[currentbyte + 2      ] = colorbuffer[0];
     840             
     841              if(bytesPerPixel == 4)                                                                                            /* if its a 32 bpp image */
     842                {
     843                  pImage->data[currentbyte + 3] = colorbuffer[3];                               /* copy the 4th byte */
     844                }
     845             
     846              currentbyte += bytesPerPixel;                                                                             /* Increase thecurrent byte by the number of bytes per pixel */
     847              currentpixel++;                                                                                                                   /* Increase current pixel by 1 */
     848             
     849              if(currentpixel > pixelcount)                                                                                     /* Make sure we havent read too many pixels */
     850                {
     851                  printf("Error too many pixels read");                                                         /* if there is too many... Display an error! */
     852                 
     853                  if(fTGA != NULL)                                                                                                      /* If there is a file open */
     854                    {
     855                      fclose(fTGA);                                                                                                     /* Close file */
     856                    }
     857                 
     858                  if(colorbuffer != NULL)                                                                                               /* If there is data in colorbuffer */
     859                    {
     860                      free(colorbuffer);                                                                                                /* Delete it */
     861                    }
     862                 
     863                  if(pImage->data != NULL)                                                                              /* If there is Image data */
     864                    {
     865                      free(pImage->data);                                                                               /* delete it */
     866                    }
     867                 
     868                  return false;                                                                                                         /* Return failed */
     869                }
     870            }
     871        }
     872      else                                                                                                                                                      /* chunkheader > 128 RLE data, next color  reapeated chunkheader - 127 times */
     873        {
     874          short counter;
     875          chunkheader -= 127;                                                                                                                   /* Subteact 127 to get rid of the ID bit */
     876          if(fread(colorbuffer, 1, bytesPerPixel, fTGA) != bytesPerPixel)               /* Attempt to read following color values */
     877            {
     878              printf("Error could not read from file");                 /* If attempt fails.. Display error (again) */
     879             
     880              if(fTGA != NULL)                                                                                                          /* If thereis a file open */
     881                {
     882                  fclose(fTGA);                                                                                                         /* Close it */
     883                }
     884             
     885              if(colorbuffer != NULL)                                                                                                   /* If there is data in the colorbuffer */
     886                {
     887                  free(colorbuffer);                                                                                                    /* delete it */
     888                }
     889             
     890              if(pImage->data != NULL)                                                                                  /* If thereis image data */
     891                {
     892                  free(pImage->data);                                                                                   /* delete it */
     893                }
     894             
     895              return false;                                                                                                                     /* return failed */
     896            }
     897         
     898          for(counter = 0; counter < chunkheader; counter++)                                    /* copy the color into the image data as many times as dictated */
     899            {                                                                                                                                                   /* by the header */
     900              pImage->data[currentbyte          ] = colorbuffer[2];                                     /* switch R and B bytes areound while copying */
     901              pImage->data[currentbyte + 1      ] = colorbuffer[1];
     902              pImage->data[currentbyte + 2      ] = colorbuffer[0];
     903             
     904              if(bytesPerPixel == 4)                                                                                            /* If TGA images is 32 bpp */
     905                {
     906                  pImage->data[currentbyte + 3] = colorbuffer[3];                               /* Copy 4th byte */
     907                }
     908             
     909              currentbyte += bytesPerPixel;                                                                             /* Increase current byte by the number of bytes per pixel */
     910              currentpixel++;                                                                                                                   /* Increase pixel count by 1 */
     911             
     912              if(currentpixel > pixelcount)                                                                                     /* Make sure we havent written too many pixels */
     913                {
     914                  printf("Error too many pixels read");                                                         /* if there is too many... Display an error! */
     915                 
     916                  if(fTGA != NULL)                                                                                                      /* If there is a file open */
     917                    {
     918                      fclose(fTGA);                                                                                                     /* Close file */
     919                    }
     920                 
     921                  if(colorbuffer != NULL)                                                                                               /* If there is data in colorbuffer */
     922                    {
     923                      free(colorbuffer);                                                                                                /* Delete it */
     924                    }
     925                 
     926                  if(pImage->data != NULL)                                                                              /* If there is Image data */
     927                    {
     928                      free(pImage->data);                                                                               /* delete it */
     929                    }
     930                 
     931                  return false;                                                                                                         /* Return failed */
     932                }
     933            }
     934        }
     935    }
     936 
     937  while(currentpixel < pixelcount);                                                                                                     /* Loop while there are still pixels left */
     938  fclose(fTGA);                                                                                                                                         /* Close the file */
     939  return true;                                                                                                                                          /* return success */
     940}
     941
     942#endif
  • orxonox/branches/images/importer/material.h

    r3093 r3094  
    6161  {
    6262    int rowSpan;
    63     unsigned long sizeX;
    64     unsigned long sizeY;
     63    GLuint width;
     64    GLuint height;
     65    GLuint bpp;
     66    GLuint type;
    6567    GLubyte *data;
    6668  };
     
    8688
    8789  // TEXTURING
     90  bool loadTexToGL (Image* pImage, GLuint* texture);
     91
    8892  bool loadImage(char* imageName, GLuint* texture);
    8993
     
    9397  void decodeJPG(jpeg_decompress_struct* cinfo, Image *pImageData);
    9498
    95   bool loadTexToGL (Image* pImage, GLuint* texture);
     99  /// TGA ///
     100
     101  bool loadTGA(const char * tgaName, GLuint* texture);
     102  bool loadUncompressedTGA(const char * filename, FILE * fTGA, GLuint* texture);
     103  //  bool LoadCompressedTGA(const char * filename, FILE * fTGA, GLuint* texture);
     104
    96105};
    97106#endif
Note: See TracChangeset for help on using the changeset viewer.