int ppm_raw(const char *filename, const Pixmap &fb) { if (!filename) return 1; if (!fb.width() || !fb.height()) return 3; FILE *fp = fopen(filename, "wb"); if (fp == NULL) return 2; // Write the header. fprintf(fp, "P6\n%d %d\n255\n", fb.width(), fb.height()); // Write the pixel data. for (unsigned int j = 0; j < fb.height(); j++) { for (unsigned int i = 0; i < fb.width(); i++) { const ColorRGBAf &pixel = fb.pixel_ro(i, j); // Do some basic tone mapping. scalar_t fmax = 0.0f; fmax = pixel.r() > fmax ? pixel.r() : fmax; fmax = pixel.g() > fmax ? pixel.g() : fmax; fmax = pixel.b() > fmax ? pixel.b() : fmax; scalar_t scale = 1.f; if (fmax > 1.f) { scale = 1.f / fmax; } // Write the pixel. fputc((char)(pixel.r() * scale * 255.0f), fp); fputc((char)(pixel.g() * scale * 255.0f), fp); fputc((char)(pixel.b() * scale * 255.0f), fp); // Check for errors if (ferror(fp)) { fclose(fp); return 2; } } } fclose(fp); return 0; }
Pixmap::Pixmap(const Pixmap& other) { m_width= other.width(); m_height = other.height(); m_depth = other.depth(); m_bitmap = new U8[m_width * m_height * m_depth]; memcpy(m_bitmap, other.const_data(), m_width * m_height * m_depth); }
Icon::Icon(const Pixmap &pm, SpritesHolder &h) : normal(h.load(pm)) { Pixmap d = Pixmap(pm.width(),pm.height(),pm.hasAlpha()); const uint8_t* p = pm.const_data(); if(pm.hasAlpha()){ for(int r=0; r<pm.height(); ++r) for(int i=0; i<pm.width(); ++i){ //0.299, 0.587, 0.114 uint8_t cl = uint8_t(p[0]*0.299 + p[1]*0.587 + p[2]*0.114); d.set(i,r,Pixmap::Pixel{cl,cl,cl,p[3]}); p += 4; } } else { for(int r=0; r<pm.height(); ++r) for(int i=0; i<pm.width(); ++i){ //0.299, 0.587, 0.114 uint8_t cl = uint8_t(p[0]*0.299 + p[1]*0.587 + p[2]*0.114); d.set(i,r,Pixmap::Pixel{cl,cl,cl,255}); p += 3; } } disabled = h.load(d); }
int xortex(Pixmap &map, const unsigned int width, const unsigned int height) { if (!width || !height) return 1; if(map.init(width, height)) return 2; for(unsigned int j = 0; j < map.height(); j++) { for(unsigned int i = 0; i < map.width(); i++) { scalar_t val = (scalar_t)(i ^ j) / 255.0f; ColorRGBAf &pixel = map.pixel(i, j); pixel.r(val); pixel.g(val); pixel.b(val); pixel.a(1.0f); } } return 0; }
int ppm_raw(const char *filename, Pixmap &fb) { if (!filename) return 1; FILE* fp = fopen(filename, "rb"); if (fp == NULL) { fb.init(0, 0); return 5; } // Read the header. int c = 0; std::string header_token[4]; for (unsigned int tcount = 0; tcount < 4; tcount++) { for (;;) { while (isspace(c = getc(fp))); if (c != '#') break; do { c = getc(fp); } while (c != '\n' && c != EOF); if (c == EOF) break; } if (c != EOF) { do { header_token[tcount].append(1, c); c = getc(fp); } while (!isspace(c) && c != '#' && c != EOF); if (c == '#') ungetc(c, fp); } } if (header_token[0].compare("P6")) { fclose(fp); return 3; } int nx, ny, pm; if (sscanf(header_token[1].c_str(), "%d", &nx) != 1 || sscanf(header_token[2].c_str(), "%d", &ny) != 1 || sscanf(header_token[3].c_str(), "%d", &pm) != 1) { fclose(fp); return 3; } if (pm != 255) { fclose(fp); return 3; } if (fb.init(nx, ny)) { fclose(fp); return 5; } // Read the pixel data. for (unsigned int j = 0; j < fb.height(); j++) { for (unsigned int i = 0; i < fb.width(); i++) { unsigned char p[3]; if (fread(p, 1, 3, fp) != 3) { fclose(fp); return 4; } ColorRGBAf &pixel = fb.pixel(i, j); pixel.r((scalar_t)p[0] / 255.0f); pixel.g((scalar_t)p[1] / 255.0f); pixel.b((scalar_t)p[2] / 255.0f); pixel.a(1.0f); } } fclose(fp); return 0; }
static HCURSOR pixmapToCursor( const Pixmap& pinput, int hotSpotX, int hotSpotY ) { if( pinput.isEmpty() ) { return 0; } Pixmap pm = pinput; pm.setFormat( Pixmap::Format_RGBA ); ICONINFO iconInfo; ZeroMemory(&iconInfo, sizeof(iconInfo)); iconInfo.fIcon = false; iconInfo.xHotspot = hotSpotX; iconInfo.yHotspot = hotSpotY; HBITMAP hBitmap = 0; HBITMAP hMonoBitmap = CreateBitmap( pm.width(), pm.height(), 1,1, NULL); iconInfo.hbmMask = hMonoBitmap; { BITMAPV5HEADER bi; ZeroMemory(&bi,sizeof(BITMAPV5HEADER)); bi.bV5Size = sizeof(BITMAPV5HEADER); bi.bV5Width = pm.width(); bi.bV5Height = pm.height(); bi.bV5Planes = 1; bi.bV5BitCount = 32; bi.bV5Compression = BI_BITFIELDS; // The following mask specification specifies a supported 32 BPP // alpha format for Windows XP. bi.bV5RedMask = 0x00FF0000; bi.bV5GreenMask = 0x0000FF00; bi.bV5BlueMask = 0x000000FF; bi.bV5AlphaMask = 0xFF000000; HDC hdc = GetDC(NULL); uint8_t *lpBits; const uint8_t* input = pm.const_data(); hBitmap = CreateDIBSection( hdc, (BITMAPINFO *)&bi, DIB_RGB_COLORS, (void **)&lpBits, NULL, (DWORD)0 ); size_t bperRow = pm.width()*4; for( int i=0; i<pm.height(); ++i ){ memcpy( lpBits + bperRow*i, input + bperRow*(pm.height()-i-1), bperRow ); } size_t bsz = pm.width()*pm.height()*4; for( size_t i=0; i<bsz; i+=4 ){ uint8_t a = *(lpBits+i); *(lpBits+i) = *(lpBits+i+2); *(lpBits+i+2) = a; } iconInfo.hbmColor = hBitmap; } HICON hicon = CreateIconIndirect(&iconInfo); DeleteObject(hBitmap); DeleteObject(hMonoBitmap); return (HCURSOR)hicon; }