Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit f5bd0ad

Browse files
committed
[[ RefactorGraphics ]] add MCContextCopyImage function
1 parent fb30624 commit f5bd0ad

File tree

4 files changed

+53
-14
lines changed

4 files changed

+53
-14
lines changed

libgraphics/include/graphics.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,8 @@ void MCGContextDrawRectOfImage(MCGContextRef self, MCGImageRef p_image, MCGRecta
439439
void MCGContextDrawText(MCGContextRef context, const char* text, uindex_t length, MCGPoint location, uint32_t font_size);
440440
MCGFloat MCGContextMeasureText(MCGContextRef context, const char *text, uindex_t length, uint32_t font_size);
441441

442+
bool MCGContextCopyImage(MCGContextRef context, MCGImageRef &r_image);
443+
442444
////////////////////////////////////////////////////////////////////////////////
443445

444446
// Transforms

libgraphics/src/context.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "graphics-internal.h"
33

44
#include <SkCanvas.h>
5+
#include <SkDevice.h>
56
#include <SkPaint.h>
67
#include <SkBitmap.h>
78
#include <SkShader.h>
@@ -215,7 +216,7 @@ bool MCGContextCreate(uint32_t p_width, uint32_t p_height, bool p_alpha, MCGCont
215216
t_bitmap . setConfig(SkBitmap::kARGB_8888_Config, p_width, p_height);
216217
t_bitmap . setIsOpaque(!p_alpha);
217218

218-
if (t_bitmap . allocPixels())
219+
if (t_bitmap . allocPixels())
219220
return MCGContextCreateWithBitmap(t_bitmap, r_context);
220221
else
221222
return false;
@@ -1407,4 +1408,14 @@ MCGFloat MCGContextMeasureText(MCGContextRef self, const char *p_text, uindex_t
14071408
return t_width;
14081409
}
14091410

1410-
////////////////////////////////////////////////////////////////////////////////
1411+
////////////////////////////////////////////////////////////////////////////////
1412+
1413+
bool MCGContextCopyImage(MCGContextRef self, MCGImageRef &r_image)
1414+
{
1415+
if (!MCGContextIsValid(self))
1416+
return false;
1417+
1418+
return MCGImageCreateWithSkBitmap(self->canvas->getDevice()->accessBitmap(false), r_image);
1419+
}
1420+
1421+
////////////////////////////////////////////////////////////////////////////////

libgraphics/src/graphics-internal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,4 +368,8 @@ inline MCGRasterFormat MCGRasterFormatFromSkBitmapConfig(SkBitmap::Config p_conf
368368

369369
////////////////////////////////////////////////////////////////////////////////
370370

371+
bool MCGImageCreateWithSkBitmap(const SkBitmap &p_bitmap, MCGImageRef &r_image);
372+
373+
////////////////////////////////////////////////////////////////////////////////
374+
371375
#endif

libgraphics/src/image.cpp

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,24 @@ static void MCGImageDestroy(MCGImageRef self)
1313
}
1414
}
1515

16-
bool __MCGImageCreateWithRaster(const MCGRaster &p_raster, MCGImageRef &r_image, MCGPixelOwnershipType p_ownership)
16+
bool MCGImageCreateWithSkBitmap(const SkBitmap &p_bitmap, MCGImageRef &r_image)
1717
{
1818
bool t_success;
1919
t_success = true;
2020

2121
__MCGImage *t_image;
22-
t_image = NULL;
22+
t_image = nil;
2323
if (t_success)
2424
t_success = MCMemoryNew(t_image);
2525

2626
SkBitmap *t_bitmap;
27+
t_bitmap = nil;
2728
if (t_success)
2829
{
29-
t_bitmap = new SkBitmap();
30-
t_success = t_bitmap != NULL;
30+
t_bitmap = new SkBitmap(p_bitmap);
31+
t_success = nil != t_bitmap;
3132
}
3233

33-
if (t_success)
34-
t_success = MCGRasterToSkBitmap(p_raster, p_ownership, *t_bitmap);
35-
3634
if (t_success)
3735
{
3836
t_image -> bitmap = t_bitmap;
@@ -42,27 +40,51 @@ bool __MCGImageCreateWithRaster(const MCGRaster &p_raster, MCGImageRef &r_image,
4240
}
4341
else
4442
{
45-
if (t_bitmap != NULL)
43+
if (t_bitmap != nil)
4644
delete t_bitmap;
4745
MCMemoryDelete(t_image);
48-
}
46+
}
47+
48+
return t_success;
49+
}
50+
51+
bool MCGImageCreateWithRaster(const MCGRaster &p_raster, MCGPixelOwnershipType p_ownership, MCGImageRef &r_image)
52+
{
53+
bool t_success;
54+
t_success = true;
55+
56+
__MCGImage *t_image;
57+
t_image = nil;
58+
59+
SkBitmap t_bitmap;
60+
61+
if (t_success)
62+
t_success = MCGRasterToSkBitmap(p_raster, p_ownership, t_bitmap);
63+
64+
if (t_success)
65+
t_success = MCGImageCreateWithSkBitmap(t_bitmap, t_image);
66+
67+
if (t_success)
68+
r_image = t_image;
69+
else
70+
MCMemoryDelete(t_image);
4971

5072
return t_success;
5173
}
5274

5375
bool MCGImageCreateWithRasterAndRelease(const MCGRaster &p_raster, MCGImageRef &r_image)
5476
{
55-
return __MCGImageCreateWithRaster(p_raster, r_image, kMCGPixelOwnershipTypeTake);
77+
return MCGImageCreateWithRaster(p_raster, kMCGPixelOwnershipTypeTake, r_image);
5678
}
5779

5880
bool MCGImageCreateWithRasterNoCopy(const MCGRaster& p_raster, MCGImageRef& r_image)
5981
{
60-
return __MCGImageCreateWithRaster(p_raster, r_image, kMCGPixelOwnershipTypeBorrow);
82+
return MCGImageCreateWithRaster(p_raster, kMCGPixelOwnershipTypeBorrow, r_image);
6183
}
6284

6385
bool MCGImageCreateWithRaster(const MCGRaster& p_raster, MCGImageRef& r_image)
6486
{
65-
return __MCGImageCreateWithRaster(p_raster, r_image, kMCGPixelOwnershipTypeCopy);
87+
return MCGImageCreateWithRaster(p_raster, kMCGPixelOwnershipTypeCopy, r_image);
6688
}
6789

6890
bool MCGImageGetRaster(MCGImageRef p_image, MCGRaster &r_raster)

0 commit comments

Comments
 (0)