int ForceRGBCommand::run(const char** args, unsigned int numArgs) { if ( numArgs < 2 ) { fprintf(stderr, "Usage: ImageTool forcergb <input> <output> [-filequality 0-100] [-pad N,N,N]\n"); fprintf(stderr, "\te.g. ImageTool forcergb input.jpg output.jpg\n"); return IMAGECORE_INVALID_USAGE; } int ret = open(args[0], args[1]); if (ret != IMAGECORE_SUCCESS) { return ret; } // Defaults unsigned int compressionQuality = 75; // Optional args unsigned int numOptional = numArgs - 2; if ( numOptional > 0 ) { unsigned int numPairs = numOptional / 2; for ( unsigned int i = 0; i < numPairs; i++ ) { const char* argName = args[2 + i * 2 + 0]; const char* argValue = args[2 + i * 2 + 1]; if( strcmp(argName, "-filequality") == 0 ) { compressionQuality = clamp(0, 100, atoi(argValue)); } else if( strcmp(argName, "-pad") == 0 ) { int ret = populateBuckets(argValue); if (ret != IMAGECORE_SUCCESS) { return ret; } } } } ImageReader* reader = ImageReader::create(m_Source); if( reader == NULL ) { fprintf(stderr, "error: unknown or corrupt image format for '%s'\n", m_InputFilePath); return IMAGECORE_INVALID_FORMAT; } EImageFormat outputFormat = ImageWriter::formatFromExtension(args[1], reader->getFormat()); unsigned int colorProfileSize = 0; reader->getColorProfile(colorProfileSize); if( colorProfileSize != 0 && reader->getFormat() == kImageFormat_JPEG ) { reader->setReadOptions(ImageReader::kReadOption_ApplyColorProfile); ImageRGBA* image = ImageRGBA::create(reader->getWidth(), reader->getHeight()); if( reader->readImage(image) ) { ImageWriter* writer = ImageWriter::createWithFormat(kImageFormat_JPEG, m_Output); if (writer == NULL) { fprintf(stderr, "error: unable to create ImageWriter\n"); return IMAGECORE_OUT_OF_MEMORY; } writer->setWriteOptions(ImageWriter::kWriteOption_WriteDefaultColorProfile); writer->setSourceReader(reader); writer->setQuality(compressionQuality); if( !writer->writeImage(image) ) { ret = IMAGECORE_WRITE_ERROR; } delete writer; } else { fprintf(stderr, "error unable to read input image"); ret = IMAGECORE_READ_ERROR; } delete image; } else { ImageWriter* imageWriter = ImageWriter::createWithFormat(outputFormat, m_Output); unsigned int writeOptions = 0; writeOptions |= ImageWriter::kWriteOption_WriteExifOrientation; writeOptions |= ImageWriter::kWriteOption_WriteDefaultColorProfile; if( imageWriter != NULL ) { imageWriter->setWriteOptions(writeOptions); if( imageWriter->copyLossless(reader) ) { ret = IMAGECORE_SUCCESS; } else { fprintf(stderr, "error: unable to perform lossless copy.\n"); ret = IMAGECORE_INVALID_OPERATION; } delete imageWriter; } } delete reader; reader = NULL; close(); return ret; }
void LoadTextureAMT::processGPU_Texture2D() { ImageReader* reader = m_imageReaders[0]; if(!reader) { return; } glActiveTexture(GL_TEXTURE0); glGenTextures(1, &m_textureId); glBindTexture(m_target, m_textureId); int levels; if(m_loadMipmaps) { levels = reader->getLevelAmount(); if(levels == 1) { // If need mipmaps and image has only base level, set number of mipmap levels to generate levels = gmath::imageLevelCount(reader->getWidth(0), reader->getHeight(0), 1); } } else { levels = 1; } glTexStorage2D(m_target, levels, m_imageGLFormat, reader->getWidth(0), reader->getHeight(0)); for(int i = 0; i < reader->getLevelAmount(); i++) { if(reader->isCompressedFormat()) { glCompressedTexSubImage2D(m_target, i, 0, 0, reader->getWidth(i), reader->getHeight(i), reader->getFormat(), reader->getLevelSize(i), reader->getData(i)); } else { glTexSubImage2D(m_target, i, 0, 0, reader->getWidth(i), reader->getHeight(i), reader->getFormat(), GL_UNSIGNED_BYTE, reader->getData(i)); } } glTexParameteri(m_target, GL_TEXTURE_BASE_LEVEL, 0); if((reader->getLevelAmount() == 1) && m_loadMipmaps) { glGenerateMipmap(m_target); } if(m_loadMipmaps) { glTexParameteri(m_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(m_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); } else { glTexParameteri(m_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(m_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(m_target, GL_TEXTURE_MAX_LEVEL, 0); } // Anisotropy filtering float maximumAnistropy; glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maximumAnistropy); glTexParameterf(m_target, GL_TEXTURE_MAX_ANISOTROPY_EXT, maximumAnistropy); glBindTexture(m_target, 0); utils::deleteAndNull(reader); m_imageReaders[0] = nullptr; m_context->m_target = m_target; m_context->m_textureId = m_textureId; }
C2DTextureResource::C2DTextureResource(const std::string &filePath, const int flags) : CTextureResource(filePath) { GLenum iFormat; int levels; //uint32_t size; ImageReader *img; m_textureId = 0; m_target = GL_TEXTURE_2D; m_type = ETT_TEX2D; // Fail loading if given extension is not supported. if((img = getImageFileByExtension(filePath)) == 0) { return; } // Fail loading if loaded file was invalid. if(!img->isValid()) { return; } iFormat = img->getOpenGLInternalFormat(flags); glActiveTexture(GL_TEXTURE0); glGenTextures(1, &m_textureId); glBindTexture(m_target, m_textureId); if(flags & ETF_MIPMAPS) { levels = img->getLevelAmount(); if(levels == 1) { // If need mipmaps and image has only base level, set number of mipmap levels to generate levels = gmath::imageLevelCount(img->getWidth(0), img->getHeight(0), 1); } } else { levels = 1; } glTexStorage2D(m_target, levels, iFormat, img->getWidth(0), img->getHeight(0)); for(int i = 0; i < img->getLevelAmount(); i++) { if(img->isCompressedFormat()) { glCompressedTexSubImage2D(m_target, i, 0, 0, img->getWidth(i), img->getHeight(i), img->getFormat(), img->getLevelSize(i), img->getData(i)); } else { glTexSubImage2D(m_target, i, 0, 0, img->getWidth(i), img->getHeight(i), img->getFormat(), GL_UNSIGNED_BYTE, img->getData(i)); } } glTexParameteri(m_target, GL_TEXTURE_BASE_LEVEL, 0); if((img->getLevelAmount() == 1) && (flags & ETF_MIPMAPS)) { glGenerateMipmap(m_target); } if(flags & ETF_MIPMAPS) { glTexParameteri(m_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(m_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); } else { glTexParameteri(m_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(m_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(m_target, GL_TEXTURE_MAX_LEVEL, 0); } // Anisotropy filtering float maximumAnistropy; glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maximumAnistropy); glTexParameterf(m_target, GL_TEXTURE_MAX_ANISOTROPY_EXT, maximumAnistropy); //size = approxSize(iFormat, img->getXBlocks(0), img->getYBlocks(0), 1, flags & ETF_MIPMAPS); glBindTexture(m_target, 0); delete img; m_loaded = true; }