Logo Search packages:      
Sourcecode: gaia version File versions  Download package

void Texture::LoadJPEG ( int  source,
  ... 
)

Loads texture from JPEG file

Usage:

LoadJPEG(TEXTURE_SOURCE_FILE, char *filename); LoadJPEG(TEXTURE_SOURCE_MEM, void *buffer, size_t size);

Definition at line 122 of file Texture.cc.

References m_glFormat, m_glInternalFormat, m_Height, m_ID, m_Pixels, and m_Width.

Referenced by TextureTile::Load(), and PreloadedTextureManager::LoadJPEG().

                                      {
      if (m_Pixels || m_ID)
            throw Exception("texture is already loaded");

      va_list va;

      struct jpeg_decompress_struct cinfo;
      struct my_jpeg_error_ptr      jerr;
      JSAMPARRAY        scanlinebuffer;

      FILE        *infile = 0;

      /* variable arguments stuff */
      va_start(va, source);

      /* establish error handling */
      cinfo.err = jpeg_std_error(&jerr.mgr);

      jerr.mgr.error_exit = my_jpeg_error_exit;

      try {
            if (setjmp(jerr.jmpbuf)) {
                  throw jerr.err;
            }

            /* initialize decompression */
            jpeg_create_decompress(&cinfo);

            if (source == TEXTURE_SOURCE_FILE) {
                  if ((infile = fopen(va_arg(va, char*), "rb")) == 0)
                        throw ErrnoException("fopen() failed", errno);

                  jpeg_stdio_src(&cinfo, infile);
            } else if (source == TEXTURE_SOURCE_MEM) {
                  void *data = va_arg(va, void*);
                  size_t len = va_arg(va, size_t);
                  my_jpeg_mem_src(&cinfo, data, len);
            } else {
                  throw Exception("unknown texture source in JPEG loading");
            }
      
            /* read header */
            jpeg_read_header(&cinfo, 1);

            if (cinfo.jpeg_color_space == JCS_GRAYSCALE) {
                  cinfo.out_color_space = JCS_GRAYSCALE;
                  m_glInternalFormat = GL_LUMINANCE8;
                  m_glFormat = GL_LUMINANCE;
            } else {
                  cinfo.out_color_space = JCS_RGB;
                  m_glInternalFormat = GL_RGB8;
                  m_glFormat = GL_RGB;
            }

            jpeg_calc_output_dimensions(&cinfo);

            m_Width = cinfo.output_width;
            m_Height = cinfo.output_height;

            /* initialize buffers */
            if ((m_Pixels = (unsigned char *)malloc(cinfo.output_width*cinfo.output_components*cinfo.output_height)) == 0)
                  throw std::bad_alloc();

            scanlinebuffer = (*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo, JPOOL_IMAGE, cinfo.output_width*cinfo.output_components, 1);

            /* start decompression */
            jpeg_start_decompress(&cinfo);

            /* read scanlines */
            unsigned int sl = 0;
            while (sl < cinfo.output_height) {
                  jpeg_read_scanlines(&cinfo, scanlinebuffer, 1);
                  memcpy(&m_Pixels[cinfo.output_width*cinfo.output_components*sl], scanlinebuffer[0], cinfo.output_width*cinfo.output_components);
                  sl++;
            }

            /* finish decompress */
            jpeg_finish_decompress(&cinfo);

      } catch (...) {
            /* error encountered - cleanup and rethrow */
            jpeg_destroy_decompress(&cinfo);

            if (infile)
                  fclose(infile);

            if (m_Pixels)
                  free(m_Pixels);

            m_Pixels = 0;

            va_end(va);

            throw;
      }

      /* successfully loaded - cleanup */
      jpeg_destroy_decompress(&cinfo);

      if (infile)
            fclose(infile);

      va_end(va);
}


Generated by  Doxygen 1.6.0   Back to index