Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3096 in orxonox.OLD


Ignore:
Timestamp:
Dec 5, 2004, 2:52:56 AM (19 years ago)
Author:
bensch
Message:

orxonox/branches/images: compressed TGA work to

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

Legend:

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

    r3095 r3096  
    633633    }
    634634 
    635   if(fread(&tgaHeader, sizeof(TGAHeader), 1, fTGA) == 0) /// hmm... don't know if this is io
     635  if(fread(&tgaHeader, sizeof(TGAHeader), 1, fTGA) == 0)
    636636    {
    637637      printf("Error could not read file header of %s\n", tgaName);
     
    651651  else if(memcmp(cTGAcompare, &tgaHeader, sizeof(TGAHeader)) == 0)
    652652    {
    653       //loadCompressedTGA(tgaName, fTGA, texture);
     653      loadCompressedTGA(tgaName, fTGA, texture);
    654654        if (fTGA)
    655655          fclose (fTGA);
     
    735735}
    736736
    737 #ifdef __auskomentiert__
    738 bool Material::LoadCompressedTGA(Texture * texture,const char * filename, FILE * fTGA)
    739 {
    740   GLuint pixelcount     = Height * Width;                                                       /* Nuber of pixels in the image */
    741   GLuint currentpixel   = 0;                                                                                            /* Current pixel being read */
    742   GLuint currentbyte    = 0;                                                                                            /* Current byte */
    743   GLubyte * colorbuffer = (GLubyte *)malloc(bytesPerPixel);                     /* Storage for 1 pixel */
    744  
    745   if(fread(header, sizeof(header), 1, fTGA) == 0)                               /* Attempt to read header */
    746     {
    747       printf("Error could not read info header");                                                       /* Display Error */
    748       if(fTGA != NULL)                                                                                                  /* If file is open */
     737bool Material::loadCompressedTGA(const char * filename, FILE * fTGA, GLuint* texture)
     738{
     739  GLubyte header[6];      // First 6 Useful Bytes From The Header
     740  GLuint  bytesPerPixel;  // Holds Number Of Bytes Per Pixel Used In The TGA File
     741  GLuint  imageSize;      // Used To Store The Image Size When Setting Aside Ram
     742  GLuint  temp;           // Temporary Variable
     743  GLuint  type;
     744  GLuint  Height;         // Height of Image
     745  GLuint  Width;          // Width of Image
     746  GLuint  Bpp;            // Bits Per Pixel
     747
     748  Image* pImage = new Image;
     749
     750 
     751  if(fread(header, sizeof(header), 1, fTGA) == 0)
     752    {
     753      printf("Error could not read info header\n");
     754      return false;
     755    }
     756 
     757  Width = pImage->width  = header[1] * 256 + header[0];
     758  Height = pImage->height = header[3] * 256 + header[2];
     759  Bpp = pImage->bpp     = header[4];
     760
     761  GLuint pixelcount     = Height * Width;
     762  GLuint currentpixel   = 0;
     763  GLuint currentbyte    = 0;
     764  GLubyte * colorbuffer = (GLubyte *)malloc(bytesPerPixel);
     765
     766  //Make sure all pImage info is ok
     767  if((pImage->width <= 0) || (pImage->height <= 0) || ((pImage->bpp != 24) && (pImage->bpp !=32)))
     768    {
     769      printf("Error Invalid pImage information\n");
     770      return false;
     771    }
     772 
     773  bytesPerPixel = (Bpp / 8);
     774  imageSize             = (bytesPerPixel * Width * Height);
     775  pImage->data  = (GLubyte*) malloc(imageSize);
     776 
     777  if(pImage->data == NULL)
     778    {
     779      printf("Error could not allocate memory for image\n");
     780      return false;
     781    }
     782 
     783  do
     784    {
     785      GLubyte chunkheader = 0;
     786     
     787      if(fread(&chunkheader, sizeof(GLubyte), 1, fTGA) == 0)
    749788        {
    750           fclose(fTGA);                                                                                                 /* Close it */
     789          printf("Error could not read RLE header\n");
     790          if(pImage->data != NULL)
     791            {
     792              free(pImage->data);
     793            }
     794          return false;
    751795        }
    752       return false;                                                                                                             /* Return failed */
    753     }
    754  
    755   pImage->width  = header[1] * 256 + header[0];                                 /* Determine The TGA Width      (highbyte*256+lowbyte) */
    756   pImage->height = header[3] * 256 + header[2];                                 /* Determine The TGA Height     (highbyte*256+lowbyte) */
    757   pImage->bpp   = header[4];                                                                            /* Determine Bits Per Pixel */
    758   Width         = pImage->width;                                                                                /* Copy width to local structure */
    759   Height                = pImage->height;                                                                               /* Copy width to local structure */
    760   Bpp                   = pImage->bpp;                                                                                  /* Copy width to local structure */
    761  
    762   if((pImage->width <= 0) || (pImage->height <= 0) || ((pImage->bpp != 24) && (pImage->bpp !=32)))      /*Make sure all pImage info is ok */
    763     {
    764       printf("Error Invalid pImage information");                                               /* If it isnt...Display error */
    765       if(fTGA != NULL)                                                                                                  /* Check if file is open */
     796      // If the ehader is < 128, it means the that is the number of RAW color packets minus 1
     797      if(chunkheader < 128)
    766798        {
    767           fclose(fTGA);                                                                                                 /* Ifit is, close it */
    768         }
    769       return false;                                                                                                             /* Return failed */
    770     }
    771  
    772   bytesPerPixel = (Bpp / 8);                                                                    /* Compute BYTES per pixel */
    773   imageSize             = (bytesPerPixel * Width * Height);             /* Compute amout of memory needed to store image */
    774   pImage->data  = (GLubyte *)malloc(imageSize);                                 /* Allocate that much memory */
    775  
    776   if(pImage->data == NULL)                                                                                      /* If it wasnt allocated correctly.. */
    777     {
    778       printf("Error could not allocate memory for image");                              /* Display Error */
    779       fclose(fTGA);                                                                                                             /* Close file */
    780       return false;                                                                                                             /* Return failed */
    781     }
    782  
    783   do
    784     {
    785       GLubyte chunkheader = 0;                                                                                  /* Storage for "chunk" header */
    786      
    787       if(fread(&chunkheader, sizeof(GLubyte), 1, fTGA) == 0)                            /* Read in the 1 byte header */
    788         {
    789           printf("Error could not read RLE header");                                            /*Display Error */
    790           if(fTGA != NULL)                                                                                              /* If file is open */
    791             {
    792               fclose(fTGA);                                                                                             /* Close file */
    793             }
    794           if(pImage->data != NULL)                                                                      /* If there is stored image data */
    795             {
    796               free(pImage->data);                                                                       /* Delete image data */
    797             }
    798           return false;                                                                                                 /* Return failed */
    799         }
    800      
    801       if(chunkheader < 128)                                                                                             /* If the ehader is < 128, it means the that is the number of RAW color packets minus 1 */
    802         {
    803           short counter;                                                                                                        /* that follow the header */
    804           chunkheader++;                                                                                                        /* add 1 to get number of following color values */
    805           for(counter = 0; counter < chunkheader; counter++)                            /* Read RAW color values */
    806             {
    807               if(fread(colorbuffer, 1, bytesPerPixel, fTGA) != bytesPerPixel) /* Try to read 1 pixel */
     799          short counter;
     800          chunkheader++;
     801          // Read RAW color values
     802          for(counter = 0; counter < chunkheader; counter++)
     803            {
     804              // Try to read 1 pixel
     805              if(fread(colorbuffer, 1, bytesPerPixel, fTGA) != bytesPerPixel)
    808806                {
    809                   printf("Error could not read image data");                            /* IF we cant, display an error */
    810                  
    811                   if(fTGA != NULL)                                                                              /* See if file is open */
     807                  printf("Error could not read image data\n");
     808                  if(colorbuffer != NULL)
    812809                    {
    813                       fclose(fTGA);                                                                             /* If so, close file */
     810                      free(colorbuffer);
    814811                    }
    815812                 
    816                   if(colorbuffer != NULL)                                                                       /* See if colorbuffer has data in it */
     813                  if(pImage->data != NULL)
    817814                    {
    818                       free(colorbuffer);                                                                        /* If so, delete it */
     815                      free(pImage->data);
    819816                    }
    820817                 
    821                   if(pImage->data != NULL)                                                                              /* See if there is stored Image data */
     818                  return false;
     819                }
     820              // write to memory
     821              // Flip R and B vcolor values around in the process
     822              pImage->data[currentbyte    ] = colorbuffer[2];                               
     823              pImage->data[currentbyte + 1] = colorbuffer[1];
     824              pImage->data[currentbyte + 2] = colorbuffer[0];
     825             
     826              if(bytesPerPixel == 4) // if its a 32 bpp image
     827                {
     828                  pImage->data[currentbyte + 3] = colorbuffer[3];// copy the 4th byte
     829                }
     830             
     831              currentbyte += bytesPerPixel;
     832              currentpixel++;
     833
     834              // Make sure we haven't read too many pixels
     835              if(currentpixel > pixelcount)     
     836                {
     837                  printf("Error too many pixels read\n");
     838                  if(colorbuffer != NULL)
    822839                    {
    823                       free(pImage->data);                                                                               /* If so, delete it too */
     840                      free(colorbuffer);
    824841                    }
    825842                 
    826                   return false;                                                                                                         /* Return failed */
    827                 }
    828               /* write to memory */
    829               pImage->data[currentbyte          ] = colorbuffer[2];                                 /* Flip R and B vcolor values around in the process */
    830               pImage->data[currentbyte + 1      ] = colorbuffer[1];
    831               pImage->data[currentbyte + 2      ] = colorbuffer[0];
    832              
    833               if(bytesPerPixel == 4)                                                                                            /* if its a 32 bpp image */
    834                 {
    835                   pImage->data[currentbyte + 3] = colorbuffer[3];                               /* copy the 4th byte */
    836                 }
    837              
    838               currentbyte += bytesPerPixel;                                                                             /* Increase thecurrent byte by the number of bytes per pixel */
    839               currentpixel++;                                                                                                                   /* Increase current pixel by 1 */
    840              
    841               if(currentpixel > pixelcount)                                                                                     /* Make sure we havent read too many pixels */
    842                 {
    843                   printf("Error too many pixels read");                                                         /* if there is too many... Display an error! */
    844                  
    845                   if(fTGA != NULL)                                                                                                      /* If there is a file open */
     843                  if(pImage->data != NULL)
    846844                    {
    847                       fclose(fTGA);                                                                                                     /* Close file */
     845                      free(pImage->data);
    848846                    }
    849847                 
    850                   if(colorbuffer != NULL)                                                                                               /* If there is data in colorbuffer */
    851                     {
    852                       free(colorbuffer);                                                                                                /* Delete it */
    853                     }
    854                  
    855                   if(pImage->data != NULL)                                                                              /* If there is Image data */
    856                     {
    857                       free(pImage->data);                                                                               /* delete it */
    858                     }
    859                  
    860                   return false;                                                                                                         /* Return failed */
     848                  return false;
    861849                }
    862850            }
    863851        }
    864       else                                                                                                                                                      /* chunkheader > 128 RLE data, next color  reapeated chunkheader - 127 times */
     852      // chunkheader > 128 RLE data, next color  reapeated chunkheader - 127 times
     853      else
    865854        {
    866855          short counter;
    867           chunkheader -= 127;                                                                                                                   /* Subteact 127 to get rid of the ID bit */
    868           if(fread(colorbuffer, 1, bytesPerPixel, fTGA) != bytesPerPixel)               /* Attempt to read following color values */
     856          chunkheader -= 127;   // Subteact 127 to get rid of the ID bit
     857          if(fread(colorbuffer, 1, bytesPerPixel, fTGA) != bytesPerPixel) // Attempt to read following color values
    869858            {
    870               printf("Error could not read from file");                 /* If attempt fails.. Display error (again) */
    871              
    872               if(fTGA != NULL)                                                                                                          /* If thereis a file open */
     859              printf("Error could not read from file");
     860              if(colorbuffer != NULL)
    873861                {
    874                   fclose(fTGA);                                                                                                         /* Close it */
     862                  free(colorbuffer);
    875863                }
    876864             
    877               if(colorbuffer != NULL)                                                                                                   /* If there is data in the colorbuffer */
     865              if(pImage->data != NULL)
    878866                {
    879                   free(colorbuffer);                                                                                                    /* delete it */
     867                  free(pImage->data);
    880868                }
    881869             
    882               if(pImage->data != NULL)                                                                                  /* If thereis image data */
     870              return false;
     871            }
     872         
     873          for(counter = 0; counter < chunkheader; counter++) //copy the color into the image data as many times as dictated
     874            {                                                   
     875              // switch R and B bytes areound while copying                                                                                             /* by the header */
     876              pImage->data[currentbyte    ] = colorbuffer[2];
     877              pImage->data[currentbyte + 1] = colorbuffer[1];
     878              pImage->data[currentbyte + 2] = colorbuffer[0];
     879             
     880              if(bytesPerPixel == 4)
    883881                {
    884                   free(pImage->data);                                                                                   /* delete it */
     882                  pImage->data[currentbyte + 3] = colorbuffer[3];
    885883                }
    886884             
    887               return false;                                                                                                                     /* return failed */
    888             }
    889          
    890           for(counter = 0; counter < chunkheader; counter++)                                    /* copy the color into the image data as many times as dictated */
    891             {                                                                                                                                                   /* by the header */
    892               pImage->data[currentbyte          ] = colorbuffer[2];                                     /* switch R and B bytes areound while copying */
    893               pImage->data[currentbyte + 1      ] = colorbuffer[1];
    894               pImage->data[currentbyte + 2      ] = colorbuffer[0];
     885              currentbyte += bytesPerPixel;
     886              currentpixel++;
    895887             
    896               if(bytesPerPixel == 4)                                                                                            /* If TGA images is 32 bpp */
     888              if(currentpixel > pixelcount)
    897889                {
    898                   pImage->data[currentbyte + 3] = colorbuffer[3];                               /* Copy 4th byte */
    899                 }
    900              
    901               currentbyte += bytesPerPixel;                                                                             /* Increase current byte by the number of bytes per pixel */
    902               currentpixel++;                                                                                                                   /* Increase pixel count by 1 */
    903              
    904               if(currentpixel > pixelcount)                                                                                     /* Make sure we havent written too many pixels */
    905                 {
    906                   printf("Error too many pixels read");                                                         /* if there is too many... Display an error! */
    907                  
    908                   if(fTGA != NULL)                                                                                                      /* If there is a file open */
     890                  printf("Error too many pixels read\n");
     891                  if(colorbuffer != NULL)
    909892                    {
    910                       fclose(fTGA);                                                                                                     /* Close file */
     893                      free(colorbuffer);
    911894                    }
    912895                 
    913                   if(colorbuffer != NULL)                                                                                               /* If there is data in colorbuffer */
     896                  if(pImage->data != NULL)
    914897                    {
    915                       free(colorbuffer);                                                                                                /* Delete it */
     898                      free(pImage->data);
    916899                    }
    917900                 
    918                   if(pImage->data != NULL)                                                                              /* If there is Image data */
    919                     {
    920                       free(pImage->data);                                                                               /* delete it */
    921                     }
    922                  
    923                   return false;                                                                                                         /* Return failed */
     901                  return false;
    924902                }
    925903            }
     
    927905    }
    928906 
    929   while(currentpixel < pixelcount);                                                                                                     /* Loop while there are still pixels left */
    930   fclose(fTGA);                                                                                                                                         /* Close the file */
    931   return true;                                                                                                                                          /* return success */
    932 }
    933 
    934 #endif
     907  while(currentpixel < pixelcount);     /* Loop while there are still pixels left */
     908  loadTexToGL (pImage, texture);
     909
     910  return true;
     911}
     912
  • orxonox/branches/images/importer/material.h

    r3095 r3096  
    100100  bool loadTGA(const char * tgaName, GLuint* texture);
    101101  bool loadUncompressedTGA(const char * filename, FILE * fTGA, GLuint* texture);
    102   //  bool LoadCompressedTGA(const char * filename, FILE * fTGA, GLuint* texture);
     102  bool loadCompressedTGA(const char * filename, FILE * fTGA, GLuint* texture);
    103103
    104104};
Note: See TracChangeset for help on using the changeset viewer.