float SquareMatrix::CalcFutureScore2( Bitmap const &bitmap, size_t startPos, size_t endPos ) const { const size_t notInGap= numeric_limits<size_t>::max(); float futureScore = 0.0f; size_t startGap = bitmap.GetFirstGapPos(); if (startGap == NOT_FOUND) return futureScore; // everything filled // start loop at first gap size_t startLoop = startGap+1; if (startPos == startGap) { // unless covered by phrase startGap = notInGap; startLoop = endPos+1; // -> postpone start } size_t lastCovered = bitmap.GetLastPos(); if (endPos > lastCovered || lastCovered == NOT_FOUND) lastCovered = endPos; for(size_t currPos = startLoop; currPos <= lastCovered ; currPos++) { // start of a new gap? if(startGap == notInGap && bitmap.GetValue(currPos) == false && (currPos < startPos || currPos > endPos)) { startGap = currPos; } // end of a gap? else if(startGap != notInGap && (bitmap.GetValue(currPos) == true || (startPos <= currPos && currPos <= endPos))) { futureScore += GetScore(startGap, currPos - 1); startGap = notInGap; } } // coverage ending with gap? if (lastCovered != bitmap.GetSize() - 1) { futureScore += GetScore(lastCovered+1, bitmap.GetSize() - 1); } return futureScore; }
Bitmap add(const Bitmap& lhs, const Bitmap& rhs){ assert(lhs.GetSize() == rhs.GetSize()); Bitmap dst(lhs.GetSize()); for (int y = 0; y != lhs.m_h; y++){ for (int x = 0; x != lhs.m_w; x++){ put_pixel_raw(dst, x, y, add(get_color_raw(lhs, x, y), get_color_raw(rhs, x, y))); } } return dst; }
void Canvas::invert_stretch_transparent(const Bitmap &src, Color key) { assert(IsDefined()); assert(src.IsDefined()); HDC virtual_dc = GetCompatibleDC(); HBITMAP old = (HBITMAP)::SelectObject(virtual_dc, src.GetNative()); const PixelSize size = src.GetSize(); BufferCanvas inverted(*this, size.cx, size.cy); ::BitBlt(inverted, 0, 0, size.cx, size.cy, virtual_dc, 0, 0, NOTSRCCOPY); ::SelectObject(virtual_dc, old); #ifdef _WIN32_WCE ::TransparentImage(dc, 0, 0, get_width(), get_height(), inverted, 0, 0, size.cx, size.cy, key); #else ::TransparentBlt(dc, 0, 0, get_width(), get_height(), inverted, 0, 0, size.cx, size.cy, key); #endif }
void Apply(Bitmap& bmp) const override{ // Fixme: Allocation, handle OOM Bitmap bg(bmp.GetSize(), color_transparent_white); for (int y = 1; y < bmp.m_h - 1; y++){ for (int x = 1; x < bmp.m_w - 1; x++){ Color current = get_color_raw(bmp, x, y); Color right(get_color_raw(bmp, x + 1, y)); if (current.a == 0 && right.a != 0){ fill_ellipse_color(bg, IntRect(IntPoint(x - 3, y - 3), IntSize(7,7)), Color(0,0,0)); } else if (current.a != 0 && right.a == 0){ fill_ellipse_color(bg, IntRect(IntPoint(x - 3 + 1, y - 3), IntSize(7,7)), Color(0,0,0)); } Color down(get_color_raw(bmp, x, y + 1)); if (current.a == 0 && down.a != 0){ fill_ellipse_color(bg, IntRect(IntPoint(x - 3, y - 3), IntSize(7,7)), Color(0,0,0)); } if (current.a != 0 && down.a == 0){ fill_ellipse_color(bg, IntRect(IntPoint(x - 3, y - 3 + 1), IntSize(7,7)), Color(0,0,0)); } } } blit_masked(at_top_left(bmp), onto(bg), color_transparent_white); bmp = bg; }
void test_scale_bilinear(){ using namespace faint; const Bitmap src = load_test_image(FileName("bicubic-source.png")); const Bitmap key = load_test_image(FileName("bilinear-key.png")); Bitmap dst = scale_bilinear(src, rounded(src.GetSize() * Scale(2.0, 3.0))); VERIFY(dst == key); }
void pixelize(Bitmap& bmp, const pixelize_range_t& width){ static_assert(pixelize_range_t::min_allowed > 0, "pixelize expects min bound > 0"); int w(width.GetValue()); IntSize sz(bmp.GetSize()); Bitmap bmp2(sz); for (int y = 0; y < sz.h; y +=w){ for (int x = 0; x < sz.w; x +=w){ int r = 0; int g = 0; int b = 0; int a = 0; int count = 0; for (int j = 0; y + j != sz.h && j != w; j++){ for (int i = 0; x + i != sz.w && i != w; i++){ Color c = get_color_raw(bmp, x + i, y + j); r += c.r; g += c.g; b += c.b; a += c.a; count++; } } Color c2(color_from_ints(r / count, g / count, b / count, a / count)); for (int j = 0; y + j < sz.h && j != w; j++){ for (int i = 0; x + i != sz.w && i != w; i++){ put_pixel_raw(bmp2, x + i, y + j, c2); } } } } bmp = bmp2; }
void Canvas::Stretch(const Bitmap &src) { assert(src.IsDefined()); const PixelSize size = src.GetSize(); Stretch(src, 0, 0, size.cx, size.cy); }
// Fixme: Pass a channel_t instead, and quantize outside static AlphaMap desaturate_AlphaMap(const Bitmap& bmp){ AlphaMap a(bmp.GetSize()); for (int y = 0; y != bmp.m_h; y++){ for (int x = 0; x != bmp.m_w; x++){ Color c(get_color_raw(bmp, x, y)); a.Set(x, y, static_cast<uchar>(sum_rgb(c) / 3)); } } return a; }
void Brush::Set(const Bitmap &bitmap) { /* GDI works best when the bitmap is 8x8 - to avoid bad performance, disallow using any other bitmap size */ assert(bitmap.GetSize().cx == 8); assert(bitmap.GetSize().cy == 8); Reset(); brush = ::CreatePatternBrush(bitmap.GetNative()); }
void Canvas::Stretch(int dest_x, int dest_y, unsigned dest_width, unsigned dest_height, const Bitmap &src) { assert(src.IsDefined()); const PixelSize size = src.GetSize(); Stretch(dest_x, dest_y, dest_width, dest_height, src, 0, 0, size.cx, size.cy); }
void Canvas::stretch(PixelScalar dest_x, PixelScalar dest_y, UPixelScalar dest_width, UPixelScalar dest_height, const Bitmap &src) { assert(src.IsDefined()); const PixelSize size = src.GetSize(); stretch(dest_x, dest_y, dest_width, dest_height, src, 0, 0, size.cx, size.cy); }
void Canvas::StretchNot(const Bitmap &src) { assert(IsDefined()); assert(src.IsDefined()); const PixelSize size = src.GetSize(); Stretch(0, 0, GetWidth(), GetHeight(), src.GetNative(), 0, 0, size.cx, size.cy, NOTSRCCOPY); }
float SquareMatrix::CalcFutureScore( Bitmap const &bitmap ) const { const size_t notInGap= numeric_limits<size_t>::max(); size_t startGap = notInGap; float futureScore = 0.0f; for(size_t currPos = 0 ; currPos < bitmap.GetSize() ; currPos++) { // start of a new gap? if(bitmap.GetValue(currPos) == false && startGap == notInGap) { startGap = currPos; } // end of a gap? else if(bitmap.GetValue(currPos) == true && startGap != notInGap) { futureScore += GetScore(startGap, currPos - 1); startGap = notInGap; } } // coverage ending with gap? if (startGap != notInGap) { futureScore += GetScore(startGap, bitmap.GetSize() - 1); } return futureScore; }
void Texture::Update(Rectangle area, const Bitmap& bitmap) { if (m_handle == 0) { throw std::runtime_error("Texture::Update(Rectangle, const Bitmap&): uninitialized texture"); } if (area.Size() != bitmap.GetSize() || !Rectangle(m_size).Contains(area)) { throw std::runtime_error("Texture::Update(Rectangle, const Bitmap&): invalid area"); } Bind(); glTexSubImage2D(GL_TEXTURE_2D, 0, area.left, area.top, area.width, area.height, color_format, GL_UNSIGNED_BYTE, (uint8_t*)bitmap.GetData()); }
void sepia(Bitmap& bmp, int intensity){ // Modified from: // https://groups.google.com/forum/#!topic/comp.lang.java.programmer/nSCnLECxGdA int depth = 20; const IntSize sz = bmp.GetSize(); for (int y = 0; y != sz.h; y++){ for (int x = 0; x != sz.w; x++){ Color c = get_color_raw(bmp, x, y); const int gray = (c.r + c.g + c.b) / 3; const int r = std::min(gray + depth * 2, 255); const int g = std::min(gray + depth, 255); const int b = std::max(gray - intensity, 0); put_pixel_raw(bmp, x, y, color_from_ints(r,g,b, c.a)); } } }
Bitmap brightness_and_contrast(const Bitmap& src, const brightness_contrast_t& v){ double scaledBrightness = v.brightness * 255.0; Bitmap dst(src.GetSize()); for (int y = 0; y != src.m_h; y++){ for (int x = 0; x != src.m_w; x++){ const auto c = get_color_raw(src, x, y); const auto c2 = color_from_double(c.r * v.contrast + scaledBrightness, c.g * v.contrast + scaledBrightness, c.b * v.contrast + scaledBrightness, c.a); put_pixel_raw(dst, x, y, c2); } } return dst; }
void Apply(Bitmap& bmp) const override{ // Fixme: Allocation, handle OOM Bitmap bg(bmp.GetSize(), color_transparent_black); const int c1 = 20; const int c2 = 15; const int c3 = 10; const int c4 = 5; for (int y = 0; y < bmp.m_h - m_dist - 5; y++){ for (int x = 0; x < bmp.m_w - m_dist - 5; x++){ if (get_color_raw(bmp, x, y).a != 0){ int x2 = x + m_dist; int y2 = y + m_dist; increase_alpha(bg, x2 + 1, y2, c4); increase_alpha(bg, x2 + 2, y2, c3); increase_alpha(bg, x2 + 3, y2, c4); increase_alpha(bg, x2, y2 + 1, c4); increase_alpha(bg, x2 + 1, y2 + 1, c3); increase_alpha(bg, x2 + 2, y2 + 1, c2); increase_alpha(bg, x2 + 3, y2 + 1, c3); increase_alpha(bg, x2 + 4, y2 + 1, c4); increase_alpha(bg, x2, y2 + 2, c3); increase_alpha(bg, x2 + 1, y2 + 2, c2); increase_alpha(bg, x2 + 2, y2 + 2, c1); increase_alpha(bg, x2 + 3, y2 + 2, c2); increase_alpha(bg, x2 + 4, y2 + 2, c3); increase_alpha(bg, x2, y2 + 3, c4); increase_alpha(bg, x2 + 1, y2 + 3, c3); increase_alpha(bg, x2 + 2, y2 + 3, c2); increase_alpha(bg, x2 + 3, y2 + 3, c3); increase_alpha(bg, x2 + 4, y2 + 3, c4); increase_alpha(bg, x2 + 1, y2 + 4, c4); increase_alpha(bg, x2 + 2, y2 + 4, c3); increase_alpha(bg, x2 + 3, y2 + 4, c4); } } } blit_masked(at_top_left(bmp), onto(bg), color_transparent_white); bmp = bg; }
void KJofol::HandleBitmap(string &oDir, char *name) { Bitmap *pBitmap; string sname = name; #ifdef WIN32 pBitmap = new Win32Bitmap(sname); #elif defined(HAVE_GTK) pBitmap = new GTKBitmap(sname); #elif defined(__BEOS__) pBitmap = new BeOSBitmap(sname); #endif string path = oDir + string(DIR_MARKER_STR) + string(name); if (IsError(pBitmap->LoadBitmapFromDisk(path))) { delete pBitmap; return; } Color color; Pos pos; pos.x = 0; pos.y = 0; pBitmap->GetSize(pos); bmp_sizes[name] = pos; pos.x -= 2; pos.y -= 2; pBitmap->GetColor(pos, color); char trans[32]; char outline[_MAX_PATH]; sprintf(trans, "#%02X%02X%02X", color.red, color.green, color.blue); sprintf(outline, "<Bitmap Name=\"%s\" File=\"%s\" TransColor=\"%s\"/>\n", name, name, trans); Write(outline); delete pBitmap; }
void Texture::Update(const Bitmap& bitmap) { // This class implements POTD texture only Size bitmap_size = bitmap.GetSize(); if ((!IsPowerOfTwo(bitmap_size.width) || !IsPowerOfTwo(bitmap_size.height)) && !g_has_texture_npot) { LOG(Error, L"[Texture::Update] supplied bitmap is NPOTD"); throw std::runtime_error("invalid bitmap"); } if (m_handle == 0) { // Allocate a fresh texture object m_size = bitmap_size; glGenTextures(1, &m_handle); Bind(); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_size.width, m_size.height, 0, color_format, GL_UNSIGNED_BYTE, (uint8_t*)bitmap.GetData()); } else { // Texture object already exists, update Bind(); if (bitmap_size == m_size) { // Texture may be updated in-place glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_size.width, m_size.height, color_format, GL_UNSIGNED_BYTE, (uint8_t*)bitmap.GetData()); } else { // Texture must be reallocated with new size (done by driver) m_size = bitmap_size; glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_size.width, m_size.height, 0, color_format, GL_UNSIGNED_BYTE, (uint8_t*)bitmap.GetData()); } } }
Texture::Texture(const Bitmap &bitmap, bool mip) : mTextureId(0), mSize(bitmap.GetSize()), mMip(mip) { if(!mSize.x || !mSize.y || !IsPow2(mSize.x) || !IsPow2(mSize.y)) { throw "Texture not created. Incorrect size."; } bool smoothing = false; GL_CALL(glGenTextures(1, &mTextureId)); if(!mTextureId) { throw "Texture not created. GL error."; } GL_CALL(glBindTexture(GL_TEXTURE_2D, mTextureId)); GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, smoothing ? GL_LINEAR : GL_NEAREST)); GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, smoothing ? GL_LINEAR : GL_NEAREST)); if (mip) { GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, smoothing ? GL_LINEAR_MIPMAP_LINEAR : GL_NEAREST_MIPMAP_NEAREST)); GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, smoothing ? GL_LINEAR_MIPMAP_LINEAR : GL_NEAREST_MIPMAP_NEAREST)); } else { GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0)); GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0)); GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, smoothing ? GL_LINEAR : GL_NEAREST)); GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, smoothing ? GL_LINEAR : GL_NEAREST)); } GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mSize.x, mSize.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, &bitmap.GetRaw()[0])); }
void osmHypothesis :: generateOperations(int & startIndex , int j1 , int contFlag , Bitmap & coverageVector , string english , string german , set <int> & targetNullWords , vector <string> & currF) { int gFlag = 0; int gp = 0; int ans; if ( j < j1) { // j1 is the index of the source word we are about to generate ... //if(coverageVector[j]==0) // if source word at j is not generated yet ... if(coverageVector.GetValue(j)==0) { // if source word at j is not generated yet ... operations.push_back("_INS_GAP_"); gFlag++; gap[j]="Unfilled"; } if (j == E) { j = j1; } else { operations.push_back("_JMP_FWD_"); j=E; } } if (j1 < j) { // if(j < E && coverageVector[j]==0) if(j < E && coverageVector.GetValue(j)==0) { operations.push_back("_INS_GAP_"); gFlag++; gap[j]="Unfilled"; } j=closestGap(gap,j1,gp); operations.push_back("_JMP_BCK_"+ intToString(gp)); //cout<<"I am j "<<j<<endl; //cout<<"I am j1 "<<j1<<endl; if(j==j1) gap[j]="Filled"; } if (j < j1) { operations.push_back("_INS_GAP_"); gap[j] = "Unfilled"; gFlag++; j=j1; } if(contFlag == 0) { // First words of the multi-word cept ... if(english == "_TRANS_SLF_") { // Unknown word ... operations.push_back("_TRANS_SLF_"); } else { operations.push_back("_TRANS_" + english + "_TO_" + german); } //ans = firstOpenGap(coverageVector); ans = coverageVector.GetFirstGapPos(); if (ans != -1) gapWidth += j - ans; } else if (contFlag == 2) { operations.push_back("_INS_" + german); ans = coverageVector.GetFirstGapPos(); if (ans != -1) gapWidth += j - ans; deletionCount++; } else { operations.push_back("_CONT_CEPT_"); } //coverageVector[j]=1; coverageVector.SetValue(j,1); j+=1; if(E<j) E=j; if (gFlag > 0) gapCount++; openGapCount += getOpenGaps(); //if (coverageVector[j] == 0 && targetNullWords.find(j) != targetNullWords.end()) if (j < coverageVector.GetSize()) { if (coverageVector.GetValue(j) == 0 && targetNullWords.find(j) != targetNullWords.end()) { j1 = j; german = currF[j1-startIndex]; english = "_INS_"; generateOperations(startIndex, j1, 2 , coverageVector , english , german , targetNullWords , currF); } } }
SaveResult write_bmp(const FilePath& filePath, const Bitmap& bmp, BitmapQuality quality) { BinaryWriter out(filePath); if (!out.good()){ return SaveResult::SaveFailed(error_open_file_write(filePath)); } const auto bitsPerPixel = bits_per_pixel(quality); const IntSize size(bmp.GetSize()); const int rowStride = bmp_row_stride(bitsPerPixel, size.w); switch(quality){ // Note: No default, to ensure warning if unhandled enum value case BitmapQuality::COLOR_8BIT: { const auto pixelData = quantized(bmp, Dithering::ON); PaletteColors paletteColors(pixelData.palette.size()); write_struct(out, create_bitmap_file_header(paletteColors, rowStride, size.h)); write_struct(out, create_bitmap_info_header_8bipp(bmp.GetSize(), default_DPI(), PaletteColors(pixelData.palette.size()), false)); write_8bipp_BI_RGB(out, pixelData); return SaveResult::SaveSuccessful(); } case BitmapQuality::GRAY_8BIT: { MappedColors pixelData(desaturate_AlphaMap(bmp), grayscale_color_table()); PaletteColors paletteColors(pixelData.palette.size()); write_struct(out, create_bitmap_file_header(paletteColors, rowStride, size.h)); write_struct(out, create_bitmap_info_header_8bipp(bmp.GetSize(), default_DPI(), PaletteColors(pixelData.palette.size()), false)); write_8bipp_BI_RGB(out, pixelData); return SaveResult::SaveSuccessful(); } case BitmapQuality::COLOR_24BIT: { write_struct(out, create_bitmap_file_header(PaletteColors(0), rowStride, size.h)); write_struct(out, create_bitmap_info_header_24bipp(bmp.GetSize(), default_DPI(), false)); write_24bipp_BI_RGB(out, bmp); return SaveResult::SaveSuccessful(); } } assert(false); return SaveResult::SaveFailed(utf8_string("Internal error in save_bitmap")); }
void Canvas::Copy(const Bitmap &src) { const PixelSize size = src.GetSize(); Copy(0, 0, size.cx, size.cy, src, 0, 0); }