Exemple #1
0
void SpriteSheet::LoadTexture(RESID spritesheet_res_id, bool invert_img_y)
{
    // Load the sprite sheet texture
    FileResource spritesheet_res = ResourceLoader::GetBinResource(spritesheet_res_id);
    int width = 0, height = 0, channels = 0;
    unsigned char* texture_img = SOIL_load_image_from_memory((unsigned char*)spritesheet_res.bin_data,
                                                             spritesheet_res.res_size, &width, &height, &channels,
                                                             SOIL_LOAD_RGBA);
    tex_width_ = width;
    tex_height_ = height;

    if (invert_img_y)
    {
        unsigned char* inverted_image = InvertImage(texture_img, width, height, channels);
        SOIL_free_image_data(texture_img);
        texture_img = inverted_image;
    }

    glGenTextures(1, &spritesheet_texture_);
    glBindTexture(GL_TEXTURE_2D, spritesheet_texture_);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_width_, tex_height_, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture_img);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glBindTexture(GL_TEXTURE_2D, 0);

    if (invert_img_y)
        delete[] texture_img;
    else
        SOIL_free_image_data(texture_img);
}
    GLuint createTexture2D(const char* name, GLint minFilter, GLint magFilter, GLint genMipmap)
    {
        File	src = VFS::openRead(name);

        //explicit conversion to avoid warning on 32-bit system
        assert(src.size()<SIZE_MAX);
        size_t fileSize = (size_t)src.size();

        unsigned char* data = new unsigned char[fileSize+1];

        src.read(data, fileSize, 1);

        GLuint texture;

        glGenTextures(1, &texture);

        int	imgWidth, imgHeight, imgChannels;
        unsigned char*	pixelsPtr = SOIL_load_image_from_memory(data, fileSize,
            &imgWidth, &imgHeight, &imgChannels, SOIL_LOAD_RGBA);

        glBindTexture(GL_TEXTURE_2D, texture);
        glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, genMipmap);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);

        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, imgWidth, imgHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixelsPtr);

        SOIL_free_image_data(pixelsPtr);

        delete [] data;

        return texture;
    }
util(BE2DTextureData, BE2DTextureData*, newWithPathnameType, const char* path, unsigned type)
{
    BE2DTextureData* data = new(BE2DTextureData);
    
    char decodepath[PATH_MAX] = {0};
    MCString_percentDecode(path, decodepath);
    
    size_t psize = strlen(decodepath) * sizeof(char);
    data->path = strcpy(malloc(psize), decodepath);

    off_t buffsize;
    const char* buff = MCFileCopyContentWithPathGetBufferSize(path, &buffsize);
    data->raw = SOIL_load_image_from_memory((const unsigned char*)buff, (int)buffsize,
                                            &data->width, &data->height, &data->channels,
                                            SOIL_LOAD_AUTO);
    MCFileReleaseContent((void*)buff);

    if (!data->raw) {
        error_log("BE2DTextureData - load texture failed: %s (%s)\n", SOIL_last_result(), path);
        release(data);
        return null;
    }
    return data;

}
Exemple #4
0
void *load_texture_maps(void *arg)
{
	
	param *p=(param *)arg;
		
	if(p->scene_num > 4018)
		return NULL;
		
	E->mem_image[p->scene_num] = SOIL_load_image_from_memory(
													 E->im_cache[p->scene_num],
													 E->im_size[p->scene_num],
													 &E->im_width[p->scene_num], &E->im_height[p->scene_num], &E->im_channels[p->scene_num], 
														SOIL_LOAD_RGB
													 );
	
	// ADD ERROR CHECK
	
	if(E->mem_image[p->scene_num] == 0){ 
		//printf("thread %d failed to load image %d from memory\n", p->thread_id, p->scene_num);
		//exit(1);
		//printf("width = %d, height = %d, channels = %d\n", E->im_width[p->id], E->im_height[p->id], E->im_channels[p->id]);
	}
		
	// return (NULL);
}
Exemple #5
0
// query texture size
static void getTextureSize(const char *buffer, int length, int *width, 
        int *height)
{
    unsigned char *data = SOIL_load_image_from_memory(
            (const unsigned char*)buffer, length,
            width, height, NULL, SOIL_LOAD_AUTO);
    SOIL_free_image_data(data);
}
unsigned char *ImageLoader::loadRawDataFromMemory(const void *data, int len, int *w, int *h, int *comp, int req_comp)
{
    const unsigned char* buffer = static_cast<const unsigned char*>(data);
    unsigned char* texdata = 0;
#ifdef DQBIX_USE_STB_IMAGE
    texdata = stbi_load_from_memory(buffer, len, w, h, comp, req_comp);
#else
    texdata = SOIL_load_image_from_memory(buffer, len, w, h, comp, req_comp);
#endif
    return texdata;
}
Exemple #7
0
inline u8* LoadImageFromFile(const char* path, int& width, int& height)
{
  File::IOFile file(path, "rb");
  std::vector<u8> buffer(file.GetSize());
  if (!file.IsOpen() || !file.ReadBytes(buffer.data(), file.GetSize()))
  {
    return nullptr;
  }
  int image_channels;
  return SOIL_load_image_from_memory(buffer.data(), (int)buffer.size(), &width, &height,
                                     &image_channels, SOIL_LOAD_RGBA);
}
Exemple #8
0
  static bool Load(Image *img, ImageDesc *desc, uchar *data, int data_size) {
    //귀찮은 관계로 soil에 떠넘기자
    //압축된거같은 데이터에서 쌩 데이터로 떠내기
    int width, height, channels;
    unsigned char *raw_img = SOIL_load_image_from_memory(
      data, data_size, &width, &height, &channels, SOIL_LOAD_AUTO
      );
    if(raw_img == nullptr) {
      return false;
    }

    img->image_data_.resize(width * height * channels * sizeof(uchar));
    memcpy(img->image_data_.data(), raw_img, img->image_data_.size() * sizeof(uchar));
    SOIL_free_image_data(raw_img);

    //텍스쳐에서 얻어낸 속성을 적절히 재가공
    desc->width = width;
    desc->height = height;
    desc->bit_depth = 8; //?
    desc->bpp = 8 * channels;
    desc->color_channels = channels;

    switch(channels) {
    case SOIL_LOAD_L:
      desc->is_grayscale = true;
      desc->is_alpha = false;
      break;
    case SOIL_LOAD_LA:
      desc->is_grayscale = true;
      desc->is_alpha = true;
      break;
    case SOIL_LOAD_RGB:
      desc->is_grayscale = false;
      desc->is_alpha = false;
      break;
    case SOIL_LOAD_RGBA:
      desc->is_grayscale = false;
      desc->is_alpha = true;
      break;
    default:
      SR_ASSERT(!"do not reach");
      break;
    }
    return true;
  }
Exemple #9
0
	int TextureRes::loadAndCreateOgl(const unsigned char* buf, int buflen) {
		unsigned char* pImgData = SOIL_load_image_from_memory(buf, buflen, &w, &h, &numChannels, SOIL_LOAD_AUTO);
        assert(pImgData);
		glId = SOIL_internal_create_OGL_texture(pImgData, w, h, numChannels,
			SOIL_CREATE_NEW_ID, 0,
			GL_TEXTURE_2D, GL_TEXTURE_2D,
			GL_MAX_TEXTURE_SIZE);
		
		SOIL_free_image_data(pImgData);
		
		if ( glId == 0 ){
            glId = -1;
            return -1;
		}
        return 0;
        //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
        //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
	}
Exemple #10
0
bool GLES2Texture::LoadTexture(
	VideoWeakPtr video,
	const void* pBuffer,
	Color mask,
	const unsigned int width,
	const unsigned int height,
	const unsigned int nMipMaps,
	const unsigned int bufferLength)
{
	int iWidth, iHeight, channels;
	unsigned char *ht_map = SOIL_load_image_from_memory((unsigned char*)pBuffer, bufferLength, &iWidth, &iHeight, &channels, SOIL_LOAD_AUTO);

	if (ht_map)
	{
		ApplyPixelMask(ht_map, mask, channels, iWidth, iHeight);
		m_textureInfo.m_texture = SOIL_create_OGL_texture(ht_map, iWidth, iHeight, channels, m_textureID++, SOIL_FLAG_POWER_OF_TWO);
	}

	std::stringstream ss;
	ss << m_fileName << " file ID " << m_textureInfo.m_texture;
	m_logger.Log(ss.str(), Platform::FileLogger::INFO);

	if (!m_textureInfo.m_texture)
	{
		m_logger.Log(m_fileName + " couldn't load texture", Platform::FileLogger::ERROR);
		video.lock()->Message(m_fileName + " couldn't load texture", GSMT_ERROR);
		SOIL_free_image_data(ht_map);
		return false;
	}
	else
	{
		m_type = TT_STATIC;
		m_profile.width = static_cast<unsigned int>(iWidth);
		m_profile.height = static_cast<unsigned int>(iHeight);
		m_profile.originalWidth = m_profile.width;
		m_profile.originalHeight = m_profile.height;
		m_logger.Log(m_fileName + " texture loaded", Platform::FileLogger::INFO);
		SOIL_free_image_data(ht_map);
	}
	GLES2UniformParameter::m_boundTexture2D = 0;
	glBindTexture(GL_TEXTURE_2D, 0);
	return true;
}
Exemple #11
0
struct Image *Image_Load(const char *Path)
{
	struct Image *Image = malloc(sizeof(struct Image));

	struct F_FileInternal *file = FileInternal_Open(Path);

	if(!file)
    {
        log_info("Couldn't load internal image : %s", Path);
        return NULL;
    }

	int Length = FileInternal_Length(file);
	unsigned char *buffer = malloc(Length);
	FileInternal_Read(buffer, sizeof(char), Length, file);
	FileInternal_Close(file);

	int Channels;
	unsigned char *Buffer = SOIL_load_image_from_memory(buffer, Length, &Image->pW, &Image->pH, &Channels, SOIL_LOAD_RGBA);
	if (Buffer == NULL)
	{
		log_err("Error : %s failed to load in function Image_Load", Path);
		Application_Error();
	}

	free(buffer);

	PackAtlas_Add(Image_Atlas, Image->pW + 1, Image->pH + 1, &Image->pX, &Image->pY);


	Image->x = Image->pX / (float)ImageAtlas_Width;
	Image->y = Image->pY / (float)ImageAtlas_Height;
	Image->x2 = Image->x + Image->pW / (float)ImageAtlas_Width;
	Image->y2 = Image->y + Image->pH / (float)ImageAtlas_Height;
	Image->Image = Image_OpenglID;

	glBindTexture(GL_TEXTURE_2D, Image_OpenglID);
	glTexSubImage2D(GL_TEXTURE_2D, 0, Image->pX, Image->pY, Image->pW, Image->pH, GL_RGBA, GL_UNSIGNED_BYTE,
			Buffer);
	free(Buffer);
	return Image;
}
Exemple #12
0
OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options)
{
	CGDataProviderRef dataProvider = CGDataProviderCreateWithURL(url);
	if (!dataProvider) return -1;
	CFDataRef data = CGDataProviderCopyData(dataProvider);
	CGDataProviderRelease(dataProvider);
	if (!data) return -1;
	
	int width, height, channels;
	unsigned char* rgbadata = SOIL_load_image_from_memory(CFDataGetBytePtr(data), CFDataGetLength(data), &width, &height, &channels, SOIL_LOAD_RGBA);
	CFStringRef format=CFStringCreateWithBytes(NULL, CFDataGetBytePtr(data) + 0x54, 4, kCFStringEncodingASCII, false);
    CFRelease(data);
	if (!rgbadata) return -1;
	
	CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
	CGContextRef context = CGBitmapContextCreate(rgbadata, width, height, 8, width * 4, rgb, kCGImageAlphaPremultipliedLast);
	SOIL_free_image_data(rgbadata);
	CGColorSpaceRelease(rgb);
	if (!context) return -1;

	CGImageRef image = CGBitmapContextCreateImage(context);
	CGContextRelease(context);
	if (!image) return -1;

	/* Add basic metadata to title */
	CFStringRef name = CFURLCopyLastPathComponent(url);
	CFTypeRef keys[1] = {kQLPreviewPropertyDisplayNameKey};
	CFTypeRef values[1] = {CFStringCreateWithFormat(NULL, NULL, CFSTR("%@ (%dx%d %@)"), name, width, height, format)};
 	CFDictionaryRef properties = CFDictionaryCreate(NULL, (const void**)keys, (const void**)values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
	CFRelease(name);

	context = QLPreviewRequestCreateContext(preview, CGSizeMake(width, height), true, properties);
	CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
	QLPreviewRequestFlushContext(preview, context);
	
	CGContextRelease(context);
	CFRelease(format);
	CFRelease(properties);
	
	return noErr;
}
Exemple #13
0
TextureLoadResult TextureLoader::do_load(const std::vector<uint8_t> &buffer) {
    TextureLoadResult result;

    int width, height, channels;
    unsigned char* data = SOIL_load_image_from_memory(
        &buffer[0],
        buffer.size(),
        &width,
        &height,
        &channels,
        SOIL_LOAD_AUTO
    );

    if(data) {
        result.width = (uint32_t) width;
        result.height = (uint32_t) height;
        result.channels = (uint32_t) channels;
        result.data.assign(data, data + (width * height * channels));
        SOIL_free_image_data(data);
    }

    return result;
}
Exemple #14
0
OTexture* TextureUtils::loadTexture(const char* texname, bool invert_y /* = true */,
                                   GLint param_W_S /* = GL_CLAMP_TO_BORDER */,
                                   GLint param_W_T /* = GL_CLAMP_TO_BORDER */)
{

    int width;
    int height;
    int channels;

    int scaleFactor;
    std::string fileName;
    getFileName(texname, fileName, scaleFactor);

    //Load file into buffer
    unsigned char* buffer;
    size_t buffer_length;
    ResourcesUtils::openFile(fileName.c_str(), buffer, buffer_length);

    unsigned char *img = SOIL_load_image_from_memory(buffer,(int) buffer_length, &width, &height, &channels, SOIL_LOAD_RGBA);

    if(img == NULL)
    {
        OErrLog("Could not load texture : " << fileName);
        return nullptr;
    }


    if (invert_y) {
        int i, j;
        for( j = 0; j*2 < height; ++j )
        {
            int index1 = j * width * channels;
            int index2 = (height - 1 - j) * width * channels;
            for( i = width * channels; i > 0; --i )
            {
                unsigned char temp = img[index1];
                img[index1] = img[index2];
                img[index2] = temp;
                ++index1;
                ++index2;
            }
        }
    }

    OTexture *texture = new OTexture();
    texture->param_W_S = param_W_S;
    texture->param_W_T = param_W_T;

    glBindTexture(GL_TEXTURE_2D,0);
    // Créer la texture
    glGenTextures(1, &(texture->textureID));
    glBindTexture(GL_TEXTURE_2D, texture->textureID);

    if(glIsTexture(texture->textureID) == false)
    {
        OErrLog("Could not create ogl texture for : " << texname);
    }
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, param_W_S);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, param_W_T);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img);

    glGenerateMipmap(GL_TEXTURE_2D);


    SOIL_free_image_data(img);

    texture->setHeight(height);
    texture->setWidth(width);
    texture->setScale(scaleFactor);

    glBindTexture(GL_TEXTURE_2D, 0);

    return texture;
}