cairo_surface_t * BitmapSource::GetSurface (cairo_t *cr) { if (image_surface == NULL) return NULL; if (native_surface) return native_surface; if (cr == NULL) return image_surface; native_surface = cairo_surface_create_similar (cairo_get_group_target (cr), cairo_surface_get_content (image_surface), GetPixelWidth (), GetPixelHeight ()); cairo_t *context = cairo_create (native_surface); cairo_set_source_surface (context, image_surface, 0, 0); cairo_pattern_set_filter (cairo_get_source (context), CAIRO_FILTER_FAST); cairo_paint (context); cairo_destroy (context); return native_surface; }
void WriteableBitmap::Render (UIElement *element, Transform *transform) { CairoSurface *target; if (!element) return; cairo_surface_t *surface = GetSurface (NULL); if (!surface) { Invalidate (); // it could still be NULL (e.g. directly inheriting UIElement) surface = GetSurface (NULL); if (!surface) return; } target = new CairoSurface (surface); Rect bounds (0, 0, GetPixelWidth (), GetPixelHeight ()); cairo_matrix_t xform; cairo_matrix_init_identity (&xform); if (transform) transform->GetTransform (&xform); element->Paint (target, bounds, &xform); target->unref (); cairo_surface_flush (surface); }
bool PngDecodeImage(PngT *png, PixBufT *pixbuf) { if (png->ihdr.interlace_method != 0) { LOG("Interlaced PNG not supported."); } else if (png->ihdr.bit_depth != 8) { LOG("Non 8-bit components not supported."); } else { uint32_t pixelWidth = GetPixelWidth(png); uint32_t length = png->ihdr.width * png->ihdr.height * pixelWidth; uint32_t dstLength = length + png->ihdr.height; uint8_t *encoded = MemNew(dstLength); MergeIDATs(png); LOG("Uncompressing the image."); Inflate(png->idat.data + 2, encoded); LOG("Decoding pixels."); ReconstructImage(pixbuf->data, encoded, png->ihdr.width, png->ihdr.height, pixelWidth); MemUnref(encoded); return true; } return false; }
void cMap::UpdateRadius(cPlayer & a_Player, unsigned int a_Radius) { int PixelWidth = static_cast<int>(GetPixelWidth()); int PixelX = static_cast<int>(a_Player.GetPosX() - m_CenterX) / PixelWidth + static_cast<int>(m_Width / 2); int PixelZ = static_cast<int>(a_Player.GetPosZ() - m_CenterZ) / PixelWidth + static_cast<int>(m_Height / 2); UpdateRadius(PixelX, PixelZ, a_Radius); }
void BitmapSource::Invalidate () { if (GetPixelWidth () == 0 || GetPixelHeight () == 0) return; if (native_surface) { cairo_surface_destroy (native_surface); native_surface = NULL; } if (image_surface) cairo_surface_destroy (image_surface); image_surface = cairo_image_surface_create_for_data ((unsigned char *) GetBitmapData (), CAIRO_FORMAT_ARGB32, GetPixelWidth (), GetPixelHeight (), GetPixelWidth () * 4); Emit (BitmapSource::PixelDataChangedEvent); }
gpointer WriteableBitmap::InitializeFromBitmapSource (BitmapSource *source) { cairo_t *cr; if (!source) return NULL; SetPixelHeight (source->GetPixelHeight ()); SetPixelWidth (source->GetPixelWidth ()); image_surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, GetPixelWidth (), GetPixelHeight ()); cr = cairo_create (image_surface); cairo_set_source_surface (cr, source->GetImageSurface (), 0, 0); cairo_paint (cr); cairo_destroy (cr); SetBitmapData (cairo_image_surface_get_data (image_surface), false); return GetBitmapData (); }
void WriteableBitmap::Render (UIElement *element, Transform *transform) { cairo_t *cr; Region *region; if (!element) return; cairo_surface_t *surface = GetSurface (NULL); if (!surface) { Invalidate (); // it could still be NULL (e.g. directly inheriting UIElement) surface = GetSurface (NULL); if (!surface) return; } cr = cairo_create (surface); Rect bounds (0, 0, GetPixelWidth (), GetPixelHeight ()); // FIXME is this supposed to clear the surface? cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR); cairo_paint (cr); cairo_set_operator (cr, CAIRO_OPERATOR_OVER); cairo_matrix_t xform; cairo_matrix_init_identity (&xform); if (transform) transform->GetTransform (&xform); element->Paint (cr, bounds, &xform); cairo_destroy (cr); cairo_surface_flush (surface); }
void cMap::UpdateRadius(int a_PixelX, int a_PixelZ, unsigned int a_Radius) { int PixelRadius = static_cast<int>(a_Radius / GetPixelWidth()); unsigned int StartX = static_cast<unsigned int>(Clamp(a_PixelX - PixelRadius, 0, static_cast<int>(m_Width))); unsigned int StartZ = static_cast<unsigned int>(Clamp(a_PixelZ - PixelRadius, 0, static_cast<int>(m_Height))); unsigned int EndX = static_cast<unsigned int>(Clamp(a_PixelX + PixelRadius, 0, static_cast<int>(m_Width))); unsigned int EndZ = static_cast<unsigned int>(Clamp(a_PixelZ + PixelRadius, 0, static_cast<int>(m_Height))); for (unsigned int X = StartX; X < EndX; ++X) { for (unsigned int Z = StartZ; Z < EndZ; ++Z) { int dX = static_cast<int>(X) - a_PixelX; int dZ = static_cast<int>(Z) - a_PixelZ; if ((dX * dX) + (dZ * dZ) < (PixelRadius * PixelRadius)) { UpdatePixel(X, Z); } } } }
void WriteableBitmap::Render (UIElement *element, Transform *transform) { MoonSurface *src; Context *ctx; if (!element) return; cairo_surface_t *surface = GetImageSurface (); if (!surface) return; Rect bounds (0, 0, GetPixelWidth (), GetPixelHeight ()); #ifdef USE_GALLIUM struct pipe_resource pt, *texture; GalliumSurface *target; struct pipe_screen *screen = swrast_screen_create (null_sw_create ()); memset (&pt, 0, sizeof (pt)); pt.target = PIPE_TEXTURE_2D; pt.format = PIPE_FORMAT_B8G8R8A8_UNORM; pt.width0 = 1; pt.height0 = 1; pt.depth0 = 1; pt.last_level = 0; pt.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_TRANSFER_WRITE | PIPE_BIND_TRANSFER_READ; texture = (*screen->resource_create) (screen, &pt); target = new GalliumSurface (texture); pipe_resource_reference (&texture, NULL); ctx = new GalliumContext (target); target->unref (); #else CairoSurface *target; target = new CairoSurface (1, 1); ctx = new CairoContext (target); target->unref (); #endif ctx->Push (Context::Group (bounds)); cairo_matrix_t xform; cairo_matrix_init_identity (&xform); if (transform) transform->GetTransform (&xform); FrameworkElement *fe = (FrameworkElement *)element; if (fe->GetFlowDirection () == FlowDirectionRightToLeft) { cairo_matrix_translate (&xform, fe->GetActualWidth (), 0.0); cairo_matrix_scale (&xform, -1, 1); } element->Paint (ctx, bounds, &xform); bounds = ctx->Pop (&src); if (!bounds.IsEmpty ()) { cairo_surface_t *image = src->Cairo (); cairo_t *cr = cairo_create (surface); cairo_set_source_surface (cr, image, 0, 0); cairo_paint (cr); cairo_destroy (cr); cairo_surface_destroy (image); src->unref (); } delete ctx; #ifdef USE_GALLIUM screen->destroy (screen); #endif }
template<class T> DISPLAY_INT HTMLElementDisplay<T>::GetMaxWidth(void) { return GetPixelWidth(); }
void RenderingEngine::Render(float interpolation) { DrawingContext dc(m_textures, m_render, GetPixelWidth(), GetPixelHeight()); m_scheme.Draw(dc, interpolation); }
const cMapDecorator cMap::CreateDecorator(const cEntity * a_TrackedEntity) { int InsideWidth = (GetWidth() / 2) - 1; int InsideHeight = (GetHeight() / 2) - 1; // Center of pixel int PixelX = static_cast<int>(a_TrackedEntity->GetPosX() - GetCenterX()) / static_cast<int>(GetPixelWidth()); int PixelZ = static_cast<int>(a_TrackedEntity->GetPosZ() - GetCenterZ()) / static_cast<int>(GetPixelWidth()); cMapDecorator::eType Type; int Rot; if ((PixelX > -InsideWidth) && (PixelX <= InsideWidth) && (PixelZ > -InsideHeight) && (PixelZ <= InsideHeight)) { double Yaw = a_TrackedEntity->GetYaw(); if (GetDimension() == dimNether) { // TODO 2014-02-19 xdot: Refine Rot = GetRandomProvider().RandInt(15); } else { Rot = CeilC(((Yaw - 11.25) * 16) / 360); } Type = cMapDecorator::eType::E_TYPE_PLAYER; } else { if ((PixelX > 320.0) || (PixelZ > 320.0)) { ; } Rot = 0; Type = cMapDecorator::eType::E_TYPE_PLAYER_OUTSIDE; // Move to border if (PixelX <= -InsideWidth) { PixelX = -InsideWidth; } if (PixelZ <= -InsideHeight) { PixelZ = -InsideHeight; } if (PixelX > InsideWidth) { PixelX = InsideWidth; } if (PixelZ > InsideHeight) { PixelZ = InsideHeight; } } return {Type, static_cast<unsigned>(2 * PixelX + 1), static_cast<unsigned>(2 * PixelZ + 1), Rot}; }
bool cMap::UpdatePixel(unsigned int a_X, unsigned int a_Z) { int BlockX = m_CenterX + static_cast<int>((a_X - m_Width / 2) * GetPixelWidth()); int BlockZ = m_CenterZ + static_cast<int>((a_Z - m_Height / 2) * GetPixelWidth()); int ChunkX, ChunkZ; cChunkDef::BlockToChunk(BlockX, BlockZ, ChunkX, ChunkZ); int RelX = BlockX - (ChunkX * cChunkDef::Width); int RelZ = BlockZ - (ChunkZ * cChunkDef::Width); ASSERT(m_World != nullptr); ColorID PixelData; m_World->DoWithChunk(ChunkX, ChunkZ, [&](cChunk & a_Chunk) { if (!a_Chunk.IsValid()) { return false; } if (GetDimension() == dimNether) { // TODO 2014-02-22 xdot: Nether maps return false; } static const std::array<unsigned char, 4> BrightnessID = { { 3, 0, 1, 2 } }; // Darkest to lightest BLOCKTYPE TargetBlock; NIBBLETYPE TargetMeta; auto Height = a_Chunk.GetHeight(RelX, RelZ); auto ChunkHeight = cChunkDef::Height; a_Chunk.GetBlockTypeMeta(RelX, Height, RelZ, TargetBlock, TargetMeta); auto ColourID = BlockHandler(TargetBlock)->GetMapBaseColourID(TargetMeta); if (IsBlockWater(TargetBlock)) { ChunkHeight /= 4; while (((--Height) != -1) && IsBlockWater(a_Chunk.GetBlock(RelX, Height, RelZ))) { continue; } } else if (ColourID == 0) { while (((--Height) != -1) && ((ColourID = BlockHandler(a_Chunk.GetBlock(RelX, Height, RelZ))->GetMapBaseColourID(a_Chunk.GetMeta(RelX, Height, RelZ))) == 0)) { continue; } } // Multiply base color ID by 4 and add brightness ID const int BrightnessIDSize = static_cast<int>(BrightnessID.size()); PixelData = ColourID * 4 + BrightnessID[static_cast<size_t>(Clamp<int>((BrightnessIDSize * Height) / ChunkHeight, 0, BrightnessIDSize - 1))]; return false; } ); SetPixel(a_X, a_Z, PixelData); return true; }
float GlfwWindow::GetAspectRatio() const { return (float)GetPixelWidth() / (float)GetPixelHeight(); }