EFI_STATUS ew_create_bitmap(WINDOW **w, RECT *loc, WINDOW *parent, EW_BITMAP *info) { if((w == NULL) || (loc == NULL) || (info == NULL) || (parent == NULL)) { fprintf(stderr, "efiwindow: ew_create_bitmap: invalid param: w: %#016llx, loc: %#016llx, parent: %#016llx, fname: %#016llx\n", w, loc, parent, info->fname); return EFI_INVALID_PARAMETER; } FILE *fp = fopen(info->fname, "r"); if(fp == NULL) return EFI_NOT_FOUND; switch(info->bitmap_type) { case EW_BITMAP_TYPE_GUESS: { /* Try and guess the bitmap type */ #ifdef HAVE_LIBPNG uint8_t png_header[8]; size_t png_fread_ret = fread(png_header, 1, 8, fp); fseek(fp, 0, SEEK_SET); if((png_fread_ret == 8) && (!png_sig_cmp(png_header, 0, 8))) { /* Its a png file */ return png_init(w, loc, parent, fp, info); } #endif /* Unknown file type */ fprintf(stderr, "efiwindow: ew_create_bitmap: unknown file type for %s\n", info->fname); return EFI_INVALID_PARAMETER; } #ifdef HAVE_LIBPNG case EW_BITMAP_TYPE_PNG: { /* Still confirm its a png */ uint8_t png_header[8]; size_t png_fread_ret = fread(png_header, 1, 8, fp); fseek(fp, 0, SEEK_SET); if((png_fread_ret == 8) && (!png_sig_cmp(png_header, 0, 8))) return png_init(w, loc, parent, fp, info); fprintf(stderr, "efiwindow: ew_create_bitmap: %s is not a png file\n", info->fname); return EFI_INVALID_PARAMETER; } #endif default: fprintf(stderr, "efiwindow: ew_create_bitmap: unknown bitmap_type: %i\n", info->bitmap_type); return EFI_INVALID_PARAMETER; } }
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; }
/** 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 main(int argc, const char **argv) { if(argc != 2) { printf("wrong arguments, there has to be one and only one and this is the mapfile to extract the images from.\n"); return -1; } IStorage *pStorage = CreateStorage("Teeworlds", argc, argv); CDataFileReader DataFile; if(!pStorage) { printf("Cannot get storage\n"); return -1; } if(!DataFile.Open(pStorage, argv[1], IStorage::TYPE_ALL)) { printf("Cannot read %s\n", argv[1]); return -1; } printf("Loading %s\n", argv[1]); png_init(0, 0); // load images int start, num; DataFile.GetType(MAPITEMTYPE_IMAGE, &start, &num); for(int i = 0; i < num; i++) { CMapItemImage *pImg = (CMapItemImage *)DataFile.GetItem(start+i, 0, 0); char *pName = (char *)DataFile.GetData(pImg->m_ImageName); if(pImg->m_External) { printf("skipping external %s\n", pName); } else { printf("writing %s.png\n", pName); void *pData = DataFile.GetData(pImg->m_ImageData); char buf[255]; #if defined(CONF_FAMILY_WINDOWS) _snprintf(buf, sizeof(buf), "%s.png", pName); #else snprintf(buf, sizeof(buf), "%s.png", pName); #endif png_t png; png_open_file_write(&png, buf); png_set_data(&png, pImg->m_Width, pImg->m_Height, 8, PNG_TRUECOLOR_ALPHA, (unsigned char*) pData); png_close_file(&png); DataFile.UnloadData(pImg->m_ImageData); } } DataFile.Close(); 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; }
// Initializes the graphics system void _Graphics::Init(const _WindowSettings &WindowSettings) { this->WindowSize = WindowSettings.Size; this->Anisotropy = 0; FramesPerSecond = 0; FrameCount = 0; FrameRateTimer = 0; Context = 0; Window = 0; Enabled = true; DirtyState(); // Set root element Element = new _Element(); Element->Visible = true; Element->Size = WindowSize; Element->CalculateBounds(); // Set video flags Uint32 VideoFlags = SDL_WINDOW_OPENGL; if(WindowSettings.Fullscreen) VideoFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP; // Set opengl attributes SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 1); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); // Set video mode Window = SDL_CreateWindow(WindowSettings.WindowTitle.c_str(), WindowSettings.Position.x, WindowSettings.Position.y, WindowSettings.Size.x, WindowSettings.Size.y, VideoFlags); if(Window == nullptr) throw std::runtime_error("SDL_CreateWindow failed"); // Set up opengl context Context = SDL_GL_CreateContext(Window); if(Context == nullptr) throw std::runtime_error("SDL_GL_CreateContext failed"); InitGLFunctions(); int MajorVersion, MinorVersion; SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &MajorVersion); SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &MinorVersion); // Set vsync SDL_GL_SetSwapInterval(WindowSettings.Vsync); // Set up OpenGL SetupOpenGL(); // Setup viewport ChangeViewport(WindowSize); png_init(0, 0); }
void saveFrameBuffer() { unsigned char* pixBuffer = (unsigned char*)malloc(window_width*window_height*4); glReadPixels(0,0, window_width, window_height, GL_RGB, GL_UNSIGNED_BYTE, pixBuffer); char filename[50]; sprintf(filename, "%d-%d.png",baseNum, ++saveNumber); // initialize png_t pngStruct; png_init(0, 0); if(png_open_file_write(&pngStruct, filename)==0) if(png_set_data(&pngStruct, window_width, window_height, 8, PNG_TRUECOLOR, pixBuffer)==0) if(png_close_file(&pngStruct)==0) printf("Saved frame buffer.\n"); }
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; }
int gfxio_init( void ) { ios=list_new(); #ifdef HAVE_LIBJPEG list_append( ios, (Node *)jpeg_init() ); #endif #ifdef HAVE_LIBPNG list_append( ios, (Node *)png_init() ); #endif #ifdef USE_DCRAW #ifdef HAVE_LIBJPEG list_append( ios, (Node *)dcraw_init() ); #endif #endif return( ERR_OK ); }
static int write_png_file(char* filename, int width, int height, unsigned char *buffer) { png_t png; FILE * fp = fopen(filename, "wb"); if (fp == NULL) { fprintf(stderr, "Could not open file %s for writing\n", filename); return 1; } fclose(fp); png_init(0,0); png_open_file_write(&png, filename); png_set_data(&png, width, height, 8, PNG_TRUECOLOR, buffer); png_close_file(&png); return 0; }
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; }
int main(void) { dbg_logger_stdout(); char aUserdir[1024] = {0}; char aPixelFile[1024] = {0}; int PngCounter = 0; fs_storage_path("Teeworlds", aUserdir, sizeof(aUserdir)); str_format(aPixelFile, sizeof(aPixelFile), "%s/tmp/pixelstream/video.stream", aUserdir); IOHANDLE PixelStream = io_open(aPixelFile, IOFLAG_READ); long long t = 0; long long tOld = 0; long long tOldAdj = 0; unsigned char *pData = 0; unsigned char *pDataOld = 0; unsigned char *pDataMixed = 0; unsigned char *pTempRow = 0; CThreadData *pThreadData = new CThreadData(); mem_zero(pThreadData, sizeof(CThreadData)); int64 StartTime = time_get(); int64 SpeedTime = time_get(); while(PixelStream) { int Size = 0; int W = 0; int H = 0; int len = io_read(PixelStream, &t, sizeof(t)); io_read(PixelStream, &Size, sizeof(Size)); io_read(PixelStream, &W, sizeof(W)); io_read(PixelStream, &H, sizeof(H)); if (len == 0) break; if (!pData) pData = new unsigned char[Size]; if (!pDataOld) pDataOld = new unsigned char[Size]; if (!pDataMixed) pDataMixed = new unsigned char[Size]; if (!pTempRow) pTempRow = new unsigned char[W * 3]; io_read(PixelStream, pData, Size); if (pThreadData->m_Size == 0) { pThreadData->m_Size = Size; pThreadData->m_H = H; pThreadData->m_W = W; thread_detach(thread_create(RotThread, pThreadData)); } int i = 0; while(1) { if (pThreadData->m_pDataIn[i] == 0) { pThreadData->m_CounterIn++; pThreadData->m_aIndexIn[i] = pThreadData->m_CounterIn; unsigned char *p = new unsigned char[Size]; mem_copy(p, pData, Size); pThreadData->m_pDataIn[i] = p; } i++; if (i == 10) break; if (THREADWAITDELAY) thread_sleep(THREADWAITDELAY); } while(1) { int MinIndex = 0; int MinI= 0; for (int i = 0; i < 10; i++) { if (pThreadData->m_pDataOut[i] != 0 && (MinIndex == 0 || MinIndex > pThreadData->m_aIndexOut[i])) { MinIndex = pThreadData->m_aIndexOut[i]; MinI = i; } } if (MinIndex != 0) { mem_copy(pData, pThreadData->m_pDataOut[MinI], Size); delete []pThreadData->m_pDataOut[MinI]; pThreadData->m_pDataOut[MinI] = 0; break; } if (THREADWAITDELAY) thread_sleep(THREADWAITDELAY); } float tAdj = 0.0f; if (tOld && tOld + tAdj + FRAMETIME < t) { while(tOld && tOld + tAdj + FRAMETIME < t) { PngCounter++; char aBuf[1024]; if (PngCounter < 10) str_format(aBuf, sizeof(aBuf), "%s/tmp/pixelstream/png0000%i.png", aUserdir, PngCounter); if (PngCounter < 100) str_format(aBuf, sizeof(aBuf), "%s/tmp/pixelstream/png000%i.png", aUserdir, PngCounter); if (PngCounter < 1000) str_format(aBuf, sizeof(aBuf), "%s/tmp/pixelstream/png00%i.png", aUserdir, PngCounter); if (PngCounter < 10000) str_format(aBuf, sizeof(aBuf), "%s/tmp/pixelstream/png0%i.png", aUserdir, PngCounter); if (PngCounter < 100000) str_format(aBuf, sizeof(aBuf), "%s/tmp/pixelstream/png%i.png", aUserdir, PngCounter); #if THREADCOUNT > 0 CBlendThreadData *apBlendData[THREADCOUNT]; for (int i = 0; i < THREADCOUNT; i++) { apBlendData[i] = new CBlendThreadData(); mem_zero(apBlendData[i], sizeof(CBlendThreadData)); apBlendData[i]->m_pIn1 = pData; apBlendData[i]->m_pIn2 = pDataOld; apBlendData[i]->m_pOut = pDataMixed; apBlendData[i]->m_Size = Size; apBlendData[i]->m_Steps = THREADCOUNT; apBlendData[i]->m_Start = i; apBlendData[i]->m_Mix = 1.0f - (tAdj + FRAMETIMEHALF) / ((float)(t - tOld)); thread_detach(thread_create(BlendThread, apBlendData[i])); } while (1) { bool Done = true; for (int i = 0; i < THREADCOUNT; i++) { if (apBlendData[i]->m_Finished == false) { Done = false; break; } } if (THREADWAITDELAY) thread_sleep(THREADWAITDELAY); if (Done) break; } #else for (int i = 0; i < Size; i++) { pDataMixed[i] = mix(pData[i], pDataOld[i], 1.0f - (tAdj + FRAMETIMEHALF) / ((float)(t - tOld))); } #endif png_t Png; png_init(0,0); png_open_file_write(&Png, aBuf); // ignore_convention png_set_data(&Png, W, H, 8, PNG_TRUECOLOR, (unsigned char *)pDataMixed); // ignore_convention png_close_file(&Png); // ignore_convention if (time_get() - SpeedTime > time_freq()) { dbg_msg("Frame", "%i (Avg: %.2f s)", PngCounter, ((float)(time_get() - StartTime) / (float)time_freq()) / (float)PngCounter); dbg_msg("FPS", "%.f", (float)PngCounter / ((float)(time_get() - StartTime) / (float)time_freq())); SpeedTime = time_get(); } tAdj = tAdj + FRAMETIME; } } else { PngCounter++; char aBuf[1024]; if (PngCounter < 10) str_format(aBuf, sizeof(aBuf), "%s/tmp/pixelstream/png0000%i.png", aUserdir, PngCounter); if (PngCounter < 100) str_format(aBuf, sizeof(aBuf), "%s/tmp/pixelstream/png000%i.png", aUserdir, PngCounter); if (PngCounter < 1000) str_format(aBuf, sizeof(aBuf), "%s/tmp/pixelstream/png00%i.png", aUserdir, PngCounter); if (PngCounter < 10000) str_format(aBuf, sizeof(aBuf), "%s/tmp/pixelstream/png0%i.png", aUserdir, PngCounter); if (PngCounter < 100000) str_format(aBuf, sizeof(aBuf), "%s/tmp/pixelstream/png%i.png", aUserdir, PngCounter); png_t Png; png_init(0,0); png_open_file_write(&Png, aBuf); // ignore_convention png_set_data(&Png, W, H, 8, PNG_TRUECOLOR, (unsigned char *)pData); // ignore_convention png_close_file(&Png); // ignore_convention if (time_get() - SpeedTime > time_freq()) { dbg_msg("Frame", "%i (Avg: %.2f s)", PngCounter, ((float)(time_get() - StartTime) / (float)time_freq()) / (float)PngCounter); dbg_msg("FPS", "%.f", (float)PngCounter / ((float)(time_get() - StartTime) / (float)time_freq())); SpeedTime = time_get(); } } mem_copy(pDataOld, pData, Size); tOld = t; } delete[] pData; delete[] pDataOld; delete[] pDataMixed; return 0; }
int main(int argc, char **argv) { // Handle command line arguments prog_options p; parse_options(&p,argc,argv); validate_options(&p); // Return value int r; // Init library r = png_init(0, 0); // Read current image png_file pf_current; png_file pf_previous; // Find motion if ( p.file_out != NULL ) { // Run until signal trap is reached signal(SIGINT, &trap); execute = 1; while(execute){ if(p.verbose || p.debug) fprintf(stdout,"\nNew round\n"); if(p.verbose || p.debug) gettimeofday(&tv1_total, NULL); // Read current image if(p.debug) gettimeofday(&tv1, NULL); r = open_png(&pf_current, p.file_current); png_write_error(r); if( r >= PNG_NO_ERROR ) { // Read "previous" image if( pf_previous.is_open != 1 ) { r = open_png(&pf_previous, p.file_current); png_write_error(r); } // Image instance from png motion_image image_current; image_from_png_data (&image_current, pf_current.data, pf_current.png_obj.width, pf_current.png_obj.height); motion_image image_previous; image_from_png_data (&image_previous, pf_previous.data, pf_current.png_obj.width, pf_current.png_obj.height); if(p.debug) gettimeofday(&tv2, NULL); if(p.debug) printf ("Open file:\t\t%f seconds\n", get_timediff(&tv1,&tv2)); // Noise reduction if(p.debug) gettimeofday(&tv1, NULL); filter_noise_reduction (&(image_current.average),1); filter_noise_reduction (&(image_previous.average),1); if(p.debug) gettimeofday(&tv2, NULL); if(p.debug) printf ("Noise reduction:\t%f seconds\n", get_timediff(&tv1,&tv2)); // Intensity correction if(p.debug) gettimeofday(&tv1, NULL); int intensity_current = get_average_intensity(&(image_current.average)); int intensity_previous = get_average_intensity(&(image_previous.average)); int intensity_difference_before = (intensity_current - intensity_previous) / 2; filter_adjust_intensity (&(image_current.average),-intensity_difference_before); filter_adjust_intensity (&(image_previous.average),intensity_difference_before); if(p.debug) gettimeofday(&tv2, NULL); if(p.debug) printf ("Intensity correction:\t%f seconds\n", get_timediff(&tv1,&tv2)); // Background subtraction if(p.debug) gettimeofday(&tv1, NULL); filter_background_subtraction(&(image_current.average),&(image_previous.average)); if(p.debug) gettimeofday(&tv2, NULL); if(p.debug) printf ("Background subtraction:\t%f seconds\n", get_timediff(&tv1,&tv2)); // Binary split if(p.debug) gettimeofday(&tv1, NULL); filter_split_binary(&(image_current.average),p.sensitivity); if(p.debug) gettimeofday(&tv2, NULL); if(p.debug) printf ("Binary split:\t\t%f seconds\n", get_timediff(&tv1,&tv2)); // Blob reduction if(p.debug) gettimeofday(&tv1, NULL); int pixels_left = filter_reduce_blobs(&(image_current.average),p.passes); if(p.debug) gettimeofday(&tv2, NULL); if(p.debug) printf ("Blob reduction:\t\t%f seconds\n", get_timediff(&tv1,&tv2)); if(p.verbose || p.debug) printf ("Pixels left:\t\t%i/10\n", pixels_left); if( pixels_left >= 10 ) { // Save file if(p.debug) gettimeofday(&tv1, NULL); // ToDo: Delegate file saving to thread char * filename = format_time(p.file_out); png_save_file(&pf_current,format_time(p.file_out)); free(filename); if(p.debug) gettimeofday(&tv2, NULL); if(p.debug) printf ("File save:\t\t%f seconds\n", get_timediff(&tv1,&tv2)); } // png_close_file(&pf_current.png_obj); // pf_current is closed as pf_previous on signal png_close_file(&pf_previous.png_obj); free(pf_previous.data); pf_previous = pf_current; free_image(image_current); free_image(image_previous); if(p.verbose || p.debug) gettimeofday(&tv2_total, NULL); if(p.verbose || p.debug) printf ("Total round time:\t%f seconds\n", get_timediff(&tv1_total,&tv2_total)); } else { pf_previous.is_open = 0; png_close_file(&pf_previous.png_obj); free(pf_previous.data); pf_previous.data = NULL; } } if(p.verbose || p.debug) printf ("\nExecution interrupted, cleaning up...\n"); r = png_close_file(&pf_previous.png_obj); free(pf_previous.data); signal(SIGINT, SIG_DFL); } // Find difference if ( p.file_out_difference != NULL ) { // Fixme: Move calculation to motion.c int change_treshold = 10; int x, y; for( x = 0 ; x < pf_current.png_obj.width ; x++ ) { for( y = 0 ; y < pf_current.png_obj.height ; y++ ) { int pixel_idx = (pf_current.png_obj.width*y*4+x*4); // Check for changes on pixel level int avg_new = (pf_current.data[pixel_idx]+pf_current.data[pixel_idx+1]+pf_current.data[pixel_idx+2])/3; int avg_old = (pf_previous.data[pixel_idx]+pf_previous.data[pixel_idx+1]+pf_previous.data[pixel_idx+2])/3; int difference = (avg_new - avg_old); if(!( abs(pf_current.data[pixel_idx]-pf_previous.data[pixel_idx]) > change_treshold || abs(pf_current.data[pixel_idx+1]-pf_previous.data[pixel_idx+1]) > change_treshold || abs(pf_current.data[pixel_idx+2]-pf_previous.data[pixel_idx+2]) > change_treshold || abs(difference) > change_treshold )) { pf_current.data[pixel_idx] = 0; pf_current.data[pixel_idx+1] = 0; pf_current.data[pixel_idx+2] = 0; pf_current.data[pixel_idx+3] = 0; } } } // Save difference png_save_file(&pf_current,p.file_out_difference); // Output file old r = png_close_file(&pf_current.png_obj); r = png_close_file(&pf_previous.png_obj); free(pf_current.data); free(pf_previous.data); } exit(EXIT_SUCCESS); }
PngWriter() : IBitmapCodec() { png_init(nullptr, nullptr); }
int main(int argc, const char **argv) { dbg_logger_stdout(); IStorage *pStorage = CreateStorage("Teeworlds", IStorage::STORAGETYPE_BASIC, argc, argv); if(argc != 5) { dbg_msg("map_replace_image", "Invalid arguments"); dbg_msg("map_replace_image", "Usage: map_replace_image <source map filepath> <dest map filepath> <current image name> <new image filepath>"); dbg_msg("map_replace_image", "Notes: map filepath must be relative to user default teeworlds folder"); dbg_msg("map_replace_image", " new image filepath must be absolute or relative to the current position"); return -1; } if (!pStorage) { dbg_msg("map_replace_image", "error loading storage"); return -1; } const char *pSourceFileName = argv[1]; const char *pDestFileName = argv[2]; const char *pImageName = argv[3]; const char *pImageFile = argv[4]; int ID = 0; int Type = 0; int Size = 0; void *pItem = 0; void *pData = 0; if(!g_DataReader.Open(pStorage, pSourceFileName, IStorage::TYPE_ALL)) { dbg_msg("map_replace_image", "failed to open source map. filename='%s'", pSourceFileName); return -1; } if(!g_DataWriter.Open(pStorage, pDestFileName)) { dbg_msg("map_replace_image", "failed to open destination map. filename='%s'", pDestFileName); return -1; } png_init(0,0); // add all items for(int Index = 0; Index < g_DataReader.NumItems(); Index++) { CMapItemImage NewImageItem; pItem = g_DataReader.GetItem(Index, &Type, &ID); Size = g_DataReader.GetItemSize(Index); pItem = ReplaceImageItem(pItem, Type, pImageName, pImageFile, &NewImageItem); if(!pItem) return -1; g_DataWriter.AddItem(Type, ID, Size, pItem); } if(g_NewDataID == -1) { dbg_msg("map_replace_image", "image '%s' not found on source map '%s'.", pImageName, pSourceFileName); return -1; } // add all data for(int Index = 0; Index < g_DataReader.NumItems(); Index++) { if(Index == g_NewDataID) { pData = g_pNewData; Size = g_NewDataSize; } else if (Index == g_NewNameID) { pData = (void *)g_aNewName; Size = str_length(g_aNewName) + 1; } else { pData = g_DataReader.GetData(Index); Size = g_DataReader.GetDataSize(Index); } g_DataWriter.AddData(Size, pData); } g_DataReader.Close(); g_DataWriter.Finish(); dbg_msg("map_replace_image", "image '%s' replaced", pImageName); return 0; }