/** Dato il path, carica la texture e restituisce l'id */ unsigned GlCanvas::loadTexture(char *path) { /* Carica l'immagine png in memoria, usando la libreria pnglite */ png_t png; unsigned int id; png_init(malloc, free); if(png_open_file_read(&png, path) != PNG_NO_ERROR) return 0; unsigned char *data = (unsigned char *) malloc(png.width * png.height * png.bpp); png_get_data(&png, (unsigned char *) data); /* Crea la texture in opengl */ glGenTextures(1, &id); glBindTexture(GL_TEXTURE_2D, id); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, png.bpp, png.width, png.height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); /* Libera la risorsa png */ png_close_file(&png); free(data); /* Restituisce l'id della texture */ return id; }
int LoadPNG(CImageInfo *pImg, const char *pFilename) { unsigned char *pBuffer; png_t Png; int Error = png_open_file(&Png, pFilename); if(Error != PNG_NO_ERROR) { dbg_msg("map_replace_image", "failed to open image file. filename='%s'", pFilename); if(Error != PNG_FILE_ERROR) png_close_file(&Png); return 0; } if(Png.depth != 8 || (Png.color_type != PNG_TRUECOLOR && Png.color_type != PNG_TRUECOLOR_ALPHA) || Png.width > (2<<12) || Png.height > (2<<12)) { dbg_msg("map_replace_image", "invalid image format. filename='%s'", pFilename); png_close_file(&Png); return 0; } pBuffer = (unsigned char *)malloc(Png.width * Png.height * Png.bpp); png_get_data(&Png, pBuffer); png_close_file(&Png); pImg->m_Width = Png.width; pImg->m_Height = Png.height; if(Png.color_type == PNG_TRUECOLOR) pImg->m_Format = CImageInfo::FORMAT_RGB; else if(Png.color_type == PNG_TRUECOLOR_ALPHA) pImg->m_Format = CImageInfo::FORMAT_RGBA; pImg->m_pData = pBuffer; return 1; }
int FixFile(const char *pFileName) { png_t Png; CPixel *pBuffer[2] = {0,0}; png_init(0, 0); png_open_file(&Png, pFileName); if(Png.color_type != PNG_TRUECOLOR_ALPHA) { dbg_msg("tileset_borderadd", "%s: not an RGBA image", pFileName); return 1; } int w = Png.width; int h = Png.height; pBuffer[0] = (CPixel*)mem_alloc(w*h*sizeof(CPixel), 1); pBuffer[1] = (CPixel*)mem_alloc((w+16*4)*(h+16*4)*sizeof(CPixel), 1); png_get_data(&Png, (unsigned char *)pBuffer[0]); png_close_file(&Png); TilesetBorderadd(w, h, pBuffer[0], pBuffer[1]); // save here png_open_file_write(&Png, pFileName); png_set_data(&Png, w + 16 * 4, h + 16 * 4, 8, PNG_TRUECOLOR_ALPHA, (unsigned char *)pBuffer[1]); png_close_file(&Png); return 0; }
int CModAPI_ModCreator::AddImage(IStorage* pStorage, const char* pFilename) { CModAPI_ModItem_Image Image; char aCompleteFilename[512]; unsigned char *pBuffer; png_t Png; // ignore_convention // open file for reading png_init(0,0); // ignore_convention IOHANDLE File = pStorage->OpenFile(pFilename, IOFLAG_READ, IStorage::TYPE_ALL, aCompleteFilename, sizeof(aCompleteFilename)); if(File) { io_close(File); } else { dbg_msg("mod", "failed to open file. filename='%s'", pFilename); return -1; } int Error = png_open_file(&Png, aCompleteFilename); // ignore_convention if(Error != PNG_NO_ERROR) { dbg_msg("mod", "failed to open file. filename='%s'", aCompleteFilename); if(Error != PNG_FILE_ERROR) { png_close_file(&Png); // ignore_convention } return -1; } if(Png.depth != 8 || (Png.color_type != PNG_TRUECOLOR && Png.color_type != PNG_TRUECOLOR_ALPHA) || Png.width > (2<<12) || Png.height > (2<<12)) // ignore_convention { dbg_msg("mod", "invalid format. filename='%s'", aCompleteFilename); png_close_file(&Png); // ignore_convention return -1; } pBuffer = (unsigned char *)mem_alloc(Png.width * Png.height * Png.bpp, 1); // ignore_convention png_get_data(&Png, pBuffer); // ignore_convention png_close_file(&Png); // ignore_convention Image.m_Id = m_Images.size(); Image.m_Width = Png.width; // ignore_convention Image.m_Height = Png.height; // ignore_convention if(Png.color_type == PNG_TRUECOLOR) // ignore_convention Image.m_Format = CImageInfo::FORMAT_RGB; else if(Png.color_type == PNG_TRUECOLOR_ALPHA) // ignore_convention Image.m_Format = CImageInfo::FORMAT_RGBA; m_Images.add(Image); m_ImagesData.add(pBuffer); return Image.m_Id; }
void gui_display_image(png_t *png) { Display *display = XOpenDisplay(NULL); int screen = DefaultScreen(display); int black = BlackPixel(display, screen); Window window = XCreateSimpleWindow( display, DefaultRootWindow(display), 0, 0, png->width, png->height, 0, black, black); char title[500]; snprintf(title, 500, "%s - %dx%d - pngview", png->name, png->width, png->height); XStoreName(display, window, title); XSizeHints *hints = XAllocSizeHints(); hints->flags = PMinSize | PMaxSize; hints->min_width = hints->max_width = png->width; hints->min_height = hints->max_height = png->height; XSetWMNormalHints(display, window, hints); XFree(hints); XMapWindow(display, window); GC gc = DefaultGC(display, screen); Visual *visual = DefaultVisual(display, screen); char *image_data = png_get_data(png); XImage *image = XCreateImage(display, visual, 24, ZPixmap, 0, image_data, png->width, png->height, 32, 0); XSelectInput(display, window, ExposureMask); while (1) { XEvent event; XNextEvent(display, &event); switch(event.type) { case Expose: if (event.xexpose.count > 0) { break; } XPutImage(display, window, gc, image, 0, 0, 0, 0, png->width, png->height); break; } } XCloseDisplay(display); }
int open_png(png_file* file, const char* path) { int r; r = png_open_file(&file->png_obj, path); png_write_error(r); if( r >= PNG_NO_ERROR ) { file->data = (unsigned char*)malloc(file->png_obj.width*file->png_obj.height*file->png_obj.bpp); r = png_get_data(&file->png_obj, file->data); png_write_error(r); file->is_open = 1; } return r; }
int CGraphics_OpenGL::LoadPNG(CImageInfo *pImg, const char *pFilename, int StorageType) { char aCompleteFilename[512]; unsigned char *pBuffer; png_t Png; // ignore_convention // open file for reading png_init(0,0); // ignore_convention IOHANDLE File = m_pStorage->OpenFile(pFilename, IOFLAG_READ, StorageType, aCompleteFilename, sizeof(aCompleteFilename)); if(File) io_close(File); else { dbg_msg("game/png", "failed to open file. filename='%s'", pFilename); return 0; } int Error = png_open_file(&Png, aCompleteFilename); // ignore_convention if(Error != PNG_NO_ERROR) { dbg_msg("game/png", "failed to open file. filename='%s'", aCompleteFilename); if(Error != PNG_FILE_ERROR) png_close_file(&Png); // ignore_convention return 0; } if(Png.depth != 8 || (Png.color_type != PNG_TRUECOLOR && Png.color_type != PNG_TRUECOLOR_ALPHA)) // ignore_convention { dbg_msg("game/png", "invalid format. filename='%s'", aCompleteFilename); png_close_file(&Png); // ignore_convention return 0; } pBuffer = (unsigned char *)mem_alloc(Png.width * Png.height * Png.bpp, 1); // ignore_convention png_get_data(&Png, pBuffer); // ignore_convention png_close_file(&Png); // ignore_convention pImg->m_Width = Png.width; // ignore_convention pImg->m_Height = Png.height; // ignore_convention if(Png.color_type == PNG_TRUECOLOR) // ignore_convention pImg->m_Format = CImageInfo::FORMAT_RGB; else if(Png.color_type == PNG_TRUECOLOR_ALPHA) // ignore_convention pImg->m_Format = CImageInfo::FORMAT_RGBA; pImg->m_pData = pBuffer; return 1; }
static void LoadTexture() { png_t gandhi; unsigned char* data; GLuint textureHandle; png_init(0, 0); png_open_file_read(&gandhi, "../demo/Gandhi.png"); data = (unsigned char*) malloc(gandhi.width * gandhi.height * gandhi.bpp); png_get_data(&gandhi, data); glGenTextures(1, &textureHandle); glBindTexture(GL_TEXTURE_2D, textureHandle); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, gandhi.width, gandhi.height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); png_close_file(&gandhi); free(data); }
int DilateFile(const char *pFileName) { png_t Png; CPixel *pBuffer[3] = {0,0,0}; png_init(0, 0); png_open_file(&Png, pFileName); if(Png.color_type != PNG_TRUECOLOR_ALPHA) { dbg_msg("dilate", "%s: not an RGBA image", pFileName); return 1; } pBuffer[0] = (CPixel*)mem_alloc(Png.width*Png.height*sizeof(CPixel), 1); pBuffer[1] = (CPixel*)mem_alloc(Png.width*Png.height*sizeof(CPixel), 1); pBuffer[2] = (CPixel*)mem_alloc(Png.width*Png.height*sizeof(CPixel), 1); png_get_data(&Png, (unsigned char *)pBuffer[0]); png_close_file(&Png); int w = Png.width; int h = Png.height; Dilate(w, h, pBuffer[0], pBuffer[1]); for(int i = 0; i < 5; i++) { Dilate(w, h, pBuffer[1], pBuffer[2]); Dilate(w, h, pBuffer[2], pBuffer[1]); } CopyAlpha(w, h, pBuffer[0], pBuffer[1]); // save here png_open_file_write(&Png, pFileName); png_set_data(&Png, w, h, 8, PNG_TRUECOLOR_ALPHA, (unsigned char *)pBuffer[1]); png_close_file(&Png); return 0; }
static Colour *buffer_from_png(const char *filename, int *width, int *height) { unsigned char *data; Colour *buffer; png_t png; png_init(NULL, NULL); if (png_open_file_read(&png, filename) != PNG_NO_ERROR) { printf("Couldn't open file %s\n", filename); return NULL; } *width = png.width; *height = png.height; assert(png.bpp == 4 || png.bpp == 3); data = calloc(png.width * png.height * png.bpp, 1); int ret = png_get_data(&png, data); if (ret != PNG_NO_ERROR) { printf("%d\n", ret); free(data); printf("Error getting data from png %s\n", filename); return NULL; } if (png.bpp == 4) buffer = colour_buffer_from_rgba(data, png.width, png.height); else buffer = colour_buffer_from_rgb(data, png.width, png.height); free(data); return buffer; }