Ejemplo n.º 1
0
void Image::read_png(const char* filename)
{
    std::vector<unsigned char> buffer, image;
    //load the image file with given filename
    LodePNG::loadFile(buffer, filename);

    //decode the png
    LodePNG::Decoder decoder;
    decoder.decode(image, buffer.empty() ? 0 : &buffer[0], (unsigned)buffer.size());

    if (decoder.getChannels()<3 || decoder.getBpp()<24) {
        cerr << "Error: only color (RGBA), 8 bit per channel png images are supported." << endl;
        cerr << "Either convert your image or change the sourcecode." << endl;
        exit(1);
    }
    int w = decoder.getWidth();
    int h = decoder.getHeight();
    set_extent(w,h);

    // now convert the image data
    std::vector<unsigned char>::iterator imageIterator = image.begin();
    Color *currentPixel = _pixel;
    while (imageIterator != image.end()) {
        currentPixel->r = (*imageIterator)/255.0;
        imageIterator++;
        currentPixel->g = (*imageIterator)/255.0;
        imageIterator++;
        currentPixel->b = (*imageIterator)/255.0;
        imageIterator++;
        // Let's just ignore the alpha channel
        imageIterator++; 
        currentPixel++;
    }	
}
Ejemplo n.º 2
0
bool Res::Init() {
	Shader* shader = nullptr;
	Texture* texture = nullptr;

	// block shader
	shader = new Shader();
	if (!shader->Init("res/block.vert", "res/block.frag")) return false;
	AddShader("block", shader);

	// simpler color shader
	shader = new Shader();
	if (!shader->Init("res/color.vert", "res/color.frag")) return false;
	AddShader("color", shader);


	std::vector<byte> buffer, image;
	LodePNG::loadFile(buffer, "res/blocks.png");
	LodePNG::Decoder decoder;
	decoder.decode(image, buffer);

	int dim = decoder.getWidth();

	texture = Texture::Create3DTexture(dim, dim, decoder.getHeight() / dim, image.data());
	AddTexture("blocks", texture);

	return true;
}
Ejemplo n.º 3
0
bool ProceduralTexture::loadPNG(char *png_map)
{
	LodePNG::Decoder pngDecoder;
	std::vector<unsigned char> buffer;
	
	Logger::log("Loading png file %s..\n",png_map);
	
	LodePNG::loadFile(buffer,png_map);
	
	Logger::log("Calling PNG decoder..\n");
	pngDecoder.decode(image, buffer);
	
	Logger::log("Checking decoder state..\n");
	if(pngDecoder.hasError())
	{
		Logger::log("[loadTextureFromPng] Error %d\n", pngDecoder.getError());
		Logger::log(" while trying to load \'%s\'\n",png_map);
		return false;
	}
	
	width = pngDecoder.getWidth();
	height = pngDecoder.getHeight();
	
	Logger::log("PNG Decoder Info [ width: %d, height: %d ]..\n",width,height);
	
	Logger::log("image size: %d\n",image.size());
	
	Logger::log("isGreyScale: %s\n",pngDecoder.isGreyscaleType()?"true":"false");
	Logger::log("isAlphaType: %s\n",pngDecoder.isAlphaType()?"true":"false");
	Logger::log("Channels: %d\n",pngDecoder.getChannels());
	Logger::log("BPP: %d\n",pngDecoder.getBpp());
		
	return true;
}
Ejemplo n.º 4
0
Image* FW::importLodePngImage(InputStream& stream)
{
    // Read the entire input stream.

    Array<U8> dataBuffer;
    int blockSize = 4096;
    for (;;)
    {
        int pos = dataBuffer.getSize();
        dataBuffer.resize(pos + blockSize);
        int num = stream.read(dataBuffer.getPtr(pos), blockSize);
        if (num < blockSize)
        {
            dataBuffer.resize(pos + num);
            break;
        }
    }

    if (hasError())
        return NULL;

    // Decode image info.

    LodePNG::Decoder decoder;
    decoder.inspect(dataBuffer.getPtr(), dataBuffer.getSize());
    Vec2i size(decoder.getWidth(), decoder.getHeight());
    bool hasAlpha = (LodePNG_InfoColor_canHaveAlpha(&decoder.getInfoPng().color) != 0);

    if (decoder.hasError())
        setError("importLodePngImage(): LodePNG error %d!", decoder.getError());
    if (min(size) <= 0)
        setError("importLodePngImage(): Invalid image size!");
    if (hasError())
        return NULL;

    // Decode image data.

    int numBytes = size.x * size.y * ((hasAlpha) ? 4 : 3);
    std::vector<U8> pixelBuffer;
    pixelBuffer.reserve(numBytes);
    decoder.getInfoRaw().color.colorType = (hasAlpha) ? 6 : 2;
    decoder.decode(pixelBuffer, dataBuffer.getPtr(), dataBuffer.getSize());

    if (decoder.hasError())
        setError("importLodePngImage(): LodePNG error %d!", decoder.getError());
    if ((int)pixelBuffer.size() != numBytes)
        setError("importLodePngImage(): Incorrect amount of pixel data!");
    if (hasError())
        return NULL;

    // Create image.

    Image* image = new Image(size, (hasAlpha) ? ImageFormat::R8_G8_B8_A8 : ImageFormat::R8_G8_B8);
    image->getBuffer().set(&pixelBuffer[0], numBytes);
    return image;
}
Ejemplo n.º 5
0
bool C_TexturePNG::Load(S_Texture * texture, const char * filename)
{
      std::vector<unsigned char> buffer, image;
      LodePNG::loadFile(buffer, filename);
      LodePNG::Decoder decoder;
      decoder.decode(image, buffer);

      if(decoder.hasError())
        return false;

      texture->width = decoder.getWidth();
      texture->height = decoder.getHeight();
      texture->bpp = decoder.getBpp();
      texture->imageData = new GLubyte[image.size()];
      std::copy(image.begin(), image.end(), texture->imageData);
      texture->type = GL_RGBA;

      return true;
}
Ejemplo n.º 6
0
// Load image from file [true = success, false otherwise]
bool PNG::load(string pathname){
	std::vector<unsigned char> buffer;
	LodePNG::loadFile(buffer, pathname); //load the image file with given filename
	LodePNG::Decoder decoder;
	decoder.inspect(buffer);
	if (decoder.isGreyscaleType()){
		decoder.getInfoRaw().color.colorType = 0;
		decoder.getInfoRaw().color.bitDepth = 16;
		// is grayscale

	}
	decoder.decode(image, buffer);
	width  = decoder.getWidth(); 
	height = decoder.getHeight();
	if(decoder.hasError())
	  {
		return false;
	  }
	return true;
}
Ejemplo n.º 7
0
int LuaCube::testScreenshot(lua_State *L)
{
    const char *filename = luaL_checkstring(L, 1);
    const lua_Integer tolerance = lua_tointeger(L, 2);

    Cube::LCD &lcd = LuaSystem::sys->cubes[id].lcd;
    std::vector<uint8_t> pngData;
    std::vector<uint8_t> pixels;
    LodePNG::Decoder decoder;
    
    LodePNG::loadFile(pngData, filename);
    if (!pngData.empty())
        decoder.decode(pixels, pngData);
    
    if (pixels.empty()) {
        lua_pushfstring(L, "error loading PNG file \"%s\"", filename);
        lua_error(L);
    }
    
    for (unsigned i = 0; i < lcd.FB_SIZE; i++) {
        RGB565 fbColor = lcd.fb_mem[i];
        RGB565 refColor = &pixels[i*4];

        int dR = int(fbColor.red()) - int(refColor.red());
        int dG = int(fbColor.green()) - int(refColor.green());
        int dB = int(fbColor.blue()) - int(refColor.blue());
        int error = dR*dR + dG*dG + dB*dB;

        if (error > tolerance) {
            // Image mismatch. Return (x, y, lcdPixel, refPixel, error)
            lua_pushinteger(L, i % lcd.WIDTH);
            lua_pushinteger(L, i / lcd.WIDTH);
            lua_pushinteger(L, fbColor.value);
            lua_pushinteger(L, refColor.value);
            lua_pushinteger(L, error);
            return 5;
        }
    }
    
    return 0;
}
Ejemplo n.º 8
0
  static bool Load(Image *img, ImageDesc *desc, uchar *data, int data_size) {
    //lodepng를 기반으로 적절히 로딩하기  
    SR_ASSERT(data != NULL);
    SR_ASSERT(data_size > 0);
    img->image_data_.clear();

    LodePNG::Decoder decoder;
    decoder.decode(img->image_data(), data, data_size);
    if(decoder.hasError()) { 
      return false;

    } else {
      desc->width = decoder.getWidth();
      desc->height = decoder.getHeight();
      desc->bit_depth = decoder.getInfoPng().color.bitDepth;
      desc->bpp = decoder.getBpp();
      desc->color_channels = decoder.getChannels();
      desc->is_grayscale = decoder.isGreyscaleType() > 0 ? true : false;
      desc->is_alpha = decoder.isAlphaType() > 0 ? true : false;

      return true;
    }
  }
Ejemplo n.º 9
0
void Level::LoadFromPng(std::string pngFile) {

    File file;
    if (!file.Load(pngFile)) {
        return;
    }
    LodePNG::Decoder decoder;
	std::vector<unsigned char> image;
	decoder.decode(image, (const unsigned char*)file.Data(), file.Size());


    for(int i=0; i<image.size();i+=4) {
        int pixelIndex = i/4;
    
        unsigned char r = image[i];
        unsigned char g = image[i+1];
        unsigned char b = image[i+2];
        unsigned char a = image[i+3];
        
        /*
        "Green",
        "Red",
        "Blue",
        "Yellow"
        */

        
        if (r==255 && g == 0 && b == 255) continue; //magenta is nothing
        
        int x = pixelIndex % decoder.getWidth();
        int y = pixelIndex / decoder.getWidth();
        
        Point p = {x,y};
        allPoints.insert(p);
        
        
        if (r==255 && g == 255 && b == 255) {
            blockable.insert(p);
        } else if (r == 0 && g == 0 && b == 0) {
            spawnable.insert(p);
        } else if (r==0 && g == 255 && b == 0) {
            colorSpawnPoints[0].insert(p);
        }else if (r==255 && g == 0 && b == 0) {
            colorSpawnPoints[1].insert(p);
        }else if (r==0 && g == 0 && b == 255) {
            colorSpawnPoints[2].insert(p);
        }else if (r==255 && g == 255 && b == 0) {
            colorSpawnPoints[3].insert(p);
        }
    }
    
    if (allPoints.empty()) return;
    
    Point min = *allPoints.begin();
    Point max = min;
    
    for(auto p : allPoints) {
        if (p.x>max.x) max.x = p.x;
        if (p.x<min.x) min.x = p.x;
        
        if (p.y>max.y) max.y = p.y;
        if (p.y<min.y) min.y = p.y;
    }
    
    Point mid = (max - min)/2;
    Point size = (max - min)/2 ;
    
    ModifyList(blockable, -mid - size);
    ModifyList(spawnable, -mid-size);
}
Ejemplo n.º 10
0
GLuint createTexture(const char* filename)
{
	std::vector< unsigned char > rawImage;
	LodePNG::loadFile( rawImage, filename );

	LodePNG::Decoder decoder;
	std::vector< unsigned char > image;
	decoder.decode( image, rawImage.empty() ? 0 : &rawImage[0],
		            (unsigned)rawImage.size() );

	//
	// Flip and invert the PNG image since OpenGL likes to load everything
	// backwards from what is considered normal!
	//

	unsigned char *imagePtr = &image[0];
	int halfTheHeightInPixels = decoder.getHeight() / 2;
	int heightInPixels = decoder.getHeight();

	// Assuming RGBA for 4 components per pixel.
	int numColorComponents = 4;

	// Assuming each color component is an unsigned char.
	int widthInChars = decoder.getWidth() * numColorComponents;

	unsigned char *top = NULL;
	unsigned char *bottom = NULL;
	unsigned char temp = 0;

	for( int h = 0; h < halfTheHeightInPixels; ++h )
	{
		top = imagePtr + h * widthInChars;
		bottom = imagePtr + (heightInPixels - h - 1) * widthInChars;

		for( int w = 0; w < widthInChars; ++w )
		{
		    // Swap the chars around.
		    temp = *top;
		    *top = *bottom;
		    *bottom = temp;

		    ++top;
		    ++bottom;
		}
	}

	GLuint texid;
	//glEnable(GL_TEXTURE_2D);
	glGenTextures( 1, &texid );
	glActiveTexture(GL_TEXTURE0);
	glBindTexture( GL_TEXTURE_2D, texid );

	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

	glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, decoder.getWidth(),
		          decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE,
		          &image[0] );

	if (isGLError())
		return -1;

	return texid;
}