2014-09-30 22:54:36 +04:00
|
|
|
/**
|
|
|
|
* RdTk: Remote Desktop Toolkit
|
|
|
|
*
|
|
|
|
* Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 13:10:11 +03:00
|
|
|
#include <rdtk/config.h>
|
2014-09-30 22:54:36 +04:00
|
|
|
|
|
|
|
#include "rdtk_resources.h"
|
|
|
|
|
|
|
|
#include "rdtk_nine_patch.h"
|
|
|
|
|
2021-06-18 11:01:52 +03:00
|
|
|
static int rdtk_image_copy_alpha_blend(uint8_t* pDstData, int nDstStep, int nXDst, int nYDst,
|
|
|
|
int nWidth, int nHeight, uint8_t* pSrcData, int nSrcStep,
|
2019-11-20 13:30:14 +03:00
|
|
|
int nXSrc, int nYSrc)
|
2014-09-30 22:54:36 +04:00
|
|
|
{
|
|
|
|
int x, y;
|
2021-06-18 11:01:52 +03:00
|
|
|
uint8_t A, R, G, B;
|
2014-09-30 22:54:36 +04:00
|
|
|
|
|
|
|
for (y = 0; y < nHeight; y++)
|
|
|
|
{
|
2021-06-18 11:01:52 +03:00
|
|
|
const uint8_t* pSrcPixel = &pSrcData[((nYSrc + y) * nSrcStep) + (nXSrc * 4)];
|
|
|
|
uint8_t* pDstPixel = &pDstData[((nYDst + y) * nDstStep) + (nXDst * 4)];
|
2014-09-30 22:54:36 +04:00
|
|
|
|
|
|
|
for (x = 0; x < nWidth; x++)
|
|
|
|
{
|
|
|
|
B = pSrcPixel[0];
|
|
|
|
G = pSrcPixel[1];
|
|
|
|
R = pSrcPixel[2];
|
|
|
|
A = pSrcPixel[3];
|
|
|
|
pSrcPixel += 4;
|
|
|
|
|
|
|
|
if (A == 255)
|
|
|
|
{
|
|
|
|
pDstPixel[0] = B;
|
|
|
|
pDstPixel[1] = G;
|
|
|
|
pDstPixel[2] = R;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
R = (R * A) / 255;
|
|
|
|
G = (G * A) / 255;
|
|
|
|
B = (B * A) / 255;
|
|
|
|
pDstPixel[0] = B + (pDstPixel[0] * (255 - A) + (255 / 2)) / 255;
|
|
|
|
pDstPixel[1] = G + (pDstPixel[1] * (255 - A) + (255 / 2)) / 255;
|
|
|
|
pDstPixel[2] = R + (pDstPixel[2] * (255 - A) + (255 / 2)) / 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
pDstPixel[3] = 0xFF;
|
|
|
|
pDstPixel += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-02-20 20:31:58 +03:00
|
|
|
int rdtk_nine_patch_draw(rdtkSurface* surface, int nXDst, int nYDst, int nWidth, int nHeight,
|
|
|
|
rdtkNinePatch* ninePatch)
|
2014-10-01 03:40:16 +04:00
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
int nXSrc;
|
|
|
|
int nYSrc;
|
|
|
|
int nSrcStep;
|
|
|
|
int nDstStep;
|
2021-06-18 11:01:52 +03:00
|
|
|
uint8_t* pSrcData;
|
|
|
|
uint8_t* pDstData;
|
2014-10-01 03:40:16 +04:00
|
|
|
int scaleWidth;
|
|
|
|
|
|
|
|
if (nWidth < ninePatch->width)
|
|
|
|
nWidth = ninePatch->width;
|
|
|
|
|
|
|
|
if (nHeight < ninePatch->height)
|
|
|
|
nHeight = ninePatch->height;
|
|
|
|
|
2022-04-28 11:49:42 +03:00
|
|
|
WINPR_UNUSED(nHeight);
|
|
|
|
|
2014-10-01 03:40:16 +04:00
|
|
|
scaleWidth = nWidth - (ninePatch->width - ninePatch->scaleWidth);
|
|
|
|
nSrcStep = ninePatch->scanline;
|
|
|
|
pSrcData = ninePatch->data;
|
|
|
|
pDstData = surface->data;
|
|
|
|
nDstStep = surface->scanline;
|
|
|
|
/* top */
|
|
|
|
x = 0;
|
|
|
|
y = 0;
|
|
|
|
/* top left */
|
|
|
|
nXSrc = 0;
|
|
|
|
nYSrc = 0;
|
|
|
|
width = ninePatch->scaleLeft;
|
|
|
|
height = ninePatch->scaleTop;
|
2019-11-06 17:24:51 +03:00
|
|
|
rdtk_image_copy_alpha_blend(pDstData, nDstStep, nXDst + x, nYDst + y, width, height, pSrcData,
|
|
|
|
nSrcStep, nXSrc, nYSrc);
|
2014-10-01 03:40:16 +04:00
|
|
|
x += width;
|
|
|
|
/* top middle (scalable) */
|
|
|
|
nXSrc = ninePatch->scaleLeft;
|
|
|
|
nYSrc = 0;
|
|
|
|
height = ninePatch->scaleTop;
|
|
|
|
|
|
|
|
while (x < (nXSrc + scaleWidth))
|
|
|
|
{
|
|
|
|
width = (nXSrc + scaleWidth) - x;
|
|
|
|
|
|
|
|
if (width > ninePatch->scaleWidth)
|
|
|
|
width = ninePatch->scaleWidth;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
rdtk_image_copy_alpha_blend(pDstData, nDstStep, nXDst + x, nYDst + y, width, height,
|
|
|
|
pSrcData, nSrcStep, nXSrc, nYSrc);
|
2014-10-01 03:40:16 +04:00
|
|
|
x += width;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* top right */
|
|
|
|
nXSrc = ninePatch->scaleRight;
|
|
|
|
nYSrc = 0;
|
|
|
|
width = ninePatch->width - ninePatch->scaleRight;
|
|
|
|
height = ninePatch->scaleTop;
|
2019-11-06 17:24:51 +03:00
|
|
|
rdtk_image_copy_alpha_blend(pDstData, nDstStep, nXDst + x, nYDst + y, width, height, pSrcData,
|
|
|
|
nSrcStep, nXSrc, nYSrc);
|
2014-10-01 03:40:16 +04:00
|
|
|
/* middle */
|
|
|
|
x = 0;
|
|
|
|
y = ninePatch->scaleTop;
|
|
|
|
/* middle left */
|
|
|
|
nXSrc = 0;
|
|
|
|
nYSrc = ninePatch->scaleTop;
|
|
|
|
width = ninePatch->scaleLeft;
|
|
|
|
height = ninePatch->scaleHeight;
|
2019-11-06 17:24:51 +03:00
|
|
|
rdtk_image_copy_alpha_blend(pDstData, nDstStep, nXDst + x, nYDst + y, width, height, pSrcData,
|
|
|
|
nSrcStep, nXSrc, nYSrc);
|
2014-10-01 03:40:16 +04:00
|
|
|
x += width;
|
|
|
|
/* middle (scalable) */
|
|
|
|
nXSrc = ninePatch->scaleLeft;
|
|
|
|
nYSrc = ninePatch->scaleTop;
|
|
|
|
height = ninePatch->scaleHeight;
|
|
|
|
|
|
|
|
while (x < (nXSrc + scaleWidth))
|
|
|
|
{
|
|
|
|
width = (nXSrc + scaleWidth) - x;
|
|
|
|
|
|
|
|
if (width > ninePatch->scaleWidth)
|
|
|
|
width = ninePatch->scaleWidth;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
rdtk_image_copy_alpha_blend(pDstData, nDstStep, nXDst + x, nYDst + y, width, height,
|
|
|
|
pSrcData, nSrcStep, nXSrc, nYSrc);
|
2014-10-01 03:40:16 +04:00
|
|
|
x += width;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* middle right */
|
|
|
|
nXSrc = ninePatch->scaleRight;
|
|
|
|
nYSrc = ninePatch->scaleTop;
|
|
|
|
width = ninePatch->width - ninePatch->scaleRight;
|
|
|
|
height = ninePatch->scaleHeight;
|
2019-11-06 17:24:51 +03:00
|
|
|
rdtk_image_copy_alpha_blend(pDstData, nDstStep, nXDst + x, nYDst + y, width, height, pSrcData,
|
|
|
|
nSrcStep, nXSrc, nYSrc);
|
2014-10-01 03:40:16 +04:00
|
|
|
/* bottom */
|
|
|
|
x = 0;
|
|
|
|
y = ninePatch->scaleBottom;
|
|
|
|
/* bottom left */
|
|
|
|
nXSrc = 0;
|
|
|
|
nYSrc = ninePatch->scaleBottom;
|
|
|
|
width = ninePatch->scaleLeft;
|
|
|
|
height = ninePatch->height - ninePatch->scaleBottom;
|
2019-11-06 17:24:51 +03:00
|
|
|
rdtk_image_copy_alpha_blend(pDstData, nDstStep, nXDst + x, nYDst + y, width, height, pSrcData,
|
|
|
|
nSrcStep, nXSrc, nYSrc);
|
2014-10-01 03:40:16 +04:00
|
|
|
x += width;
|
|
|
|
/* bottom middle (scalable) */
|
|
|
|
nXSrc = ninePatch->scaleLeft;
|
|
|
|
nYSrc = ninePatch->scaleBottom;
|
|
|
|
height = ninePatch->height - ninePatch->scaleBottom;
|
|
|
|
|
|
|
|
while (x < (nXSrc + scaleWidth))
|
|
|
|
{
|
|
|
|
width = (nXSrc + scaleWidth) - x;
|
|
|
|
|
|
|
|
if (width > ninePatch->scaleWidth)
|
|
|
|
width = ninePatch->scaleWidth;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
rdtk_image_copy_alpha_blend(pDstData, nDstStep, nXDst + x, nYDst + y, width, height,
|
|
|
|
pSrcData, nSrcStep, nXSrc, nYSrc);
|
2014-10-01 03:40:16 +04:00
|
|
|
x += width;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* bottom right */
|
|
|
|
nXSrc = ninePatch->scaleRight;
|
|
|
|
nYSrc = ninePatch->scaleBottom;
|
|
|
|
width = ninePatch->width - ninePatch->scaleRight;
|
|
|
|
height = ninePatch->height - ninePatch->scaleBottom;
|
2019-11-06 17:24:51 +03:00
|
|
|
rdtk_image_copy_alpha_blend(pDstData, nDstStep, nXDst + x, nYDst + y, width, height, pSrcData,
|
|
|
|
nSrcStep, nXSrc, nYSrc);
|
2014-10-01 03:40:16 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rdtk_nine_patch_set_image(rdtkNinePatch* ninePatch, wImage* image)
|
|
|
|
{
|
|
|
|
int x, y;
|
2021-06-18 11:01:52 +03:00
|
|
|
uint8_t* data;
|
2014-10-01 03:40:16 +04:00
|
|
|
int beg, end;
|
|
|
|
int scanline;
|
2021-06-18 11:01:52 +03:00
|
|
|
uint32_t* pixel;
|
2014-10-01 03:40:16 +04:00
|
|
|
int width, height;
|
|
|
|
ninePatch->image = image;
|
|
|
|
width = image->width;
|
|
|
|
height = image->height;
|
|
|
|
scanline = image->scanline;
|
|
|
|
data = image->data;
|
|
|
|
/* parse scalable area */
|
|
|
|
beg = end = -1;
|
2021-06-18 11:01:52 +03:00
|
|
|
pixel = (uint32_t*)&data[4]; /* (1, 0) */
|
2014-10-01 03:40:16 +04:00
|
|
|
|
|
|
|
for (x = 1; x < width - 1; x++)
|
|
|
|
{
|
|
|
|
if (beg < 0)
|
|
|
|
{
|
|
|
|
if (*pixel)
|
|
|
|
{
|
|
|
|
beg = x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (end < 0)
|
|
|
|
{
|
|
|
|
if (!(*pixel))
|
|
|
|
{
|
|
|
|
end = x;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pixel++;
|
|
|
|
}
|
|
|
|
|
|
|
|
ninePatch->scaleLeft = beg - 1;
|
|
|
|
ninePatch->scaleRight = end - 1;
|
|
|
|
ninePatch->scaleWidth = ninePatch->scaleRight - ninePatch->scaleLeft;
|
|
|
|
beg = end = -1;
|
2021-06-18 11:01:52 +03:00
|
|
|
pixel = (uint32_t*)&data[scanline]; /* (0, 1) */
|
2014-10-01 03:40:16 +04:00
|
|
|
|
|
|
|
for (y = 1; y < height - 1; y++)
|
|
|
|
{
|
|
|
|
if (beg < 0)
|
|
|
|
{
|
|
|
|
if (*pixel)
|
|
|
|
{
|
|
|
|
beg = y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (end < 0)
|
|
|
|
{
|
|
|
|
if (!(*pixel))
|
|
|
|
{
|
|
|
|
end = y;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-18 11:01:52 +03:00
|
|
|
pixel = (uint32_t*)&((uint8_t*)pixel)[scanline];
|
2014-10-01 03:40:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ninePatch->scaleTop = beg - 1;
|
|
|
|
ninePatch->scaleBottom = end - 1;
|
|
|
|
ninePatch->scaleHeight = ninePatch->scaleBottom - ninePatch->scaleTop;
|
|
|
|
/* parse fillable area */
|
|
|
|
beg = end = -1;
|
2021-06-18 11:01:52 +03:00
|
|
|
pixel = (uint32_t*)&data[((height - 1) * scanline) + 4]; /* (1, height - 1) */
|
2014-10-01 03:40:16 +04:00
|
|
|
|
|
|
|
for (x = 1; x < width - 1; x++)
|
|
|
|
{
|
|
|
|
if (beg < 0)
|
|
|
|
{
|
|
|
|
if (*pixel)
|
|
|
|
{
|
|
|
|
beg = x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (end < 0)
|
|
|
|
{
|
|
|
|
if (!(*pixel))
|
|
|
|
{
|
|
|
|
end = x;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pixel++;
|
|
|
|
}
|
|
|
|
|
|
|
|
ninePatch->fillLeft = beg - 1;
|
|
|
|
ninePatch->fillRight = end - 1;
|
|
|
|
ninePatch->fillWidth = ninePatch->fillRight - ninePatch->fillLeft;
|
|
|
|
beg = end = -1;
|
2021-06-18 11:01:52 +03:00
|
|
|
pixel = (uint32_t*)&data[((width - 1) * 4) + scanline]; /* (width - 1, 1) */
|
2014-10-01 03:40:16 +04:00
|
|
|
|
|
|
|
for (y = 1; y < height - 1; y++)
|
|
|
|
{
|
|
|
|
if (beg < 0)
|
|
|
|
{
|
|
|
|
if (*pixel)
|
|
|
|
{
|
|
|
|
beg = y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (end < 0)
|
|
|
|
{
|
|
|
|
if (!(*pixel))
|
|
|
|
{
|
|
|
|
end = y;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-18 11:01:52 +03:00
|
|
|
pixel = (uint32_t*)&((uint8_t*)pixel)[scanline];
|
2014-10-01 03:40:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ninePatch->fillTop = beg - 1;
|
|
|
|
ninePatch->fillBottom = end - 1;
|
|
|
|
ninePatch->fillHeight = ninePatch->fillBottom - ninePatch->fillTop;
|
|
|
|
/* cut out borders from image */
|
|
|
|
ninePatch->width = width - 2;
|
|
|
|
ninePatch->height = height - 2;
|
|
|
|
ninePatch->data = &data[scanline + 4]; /* (1, 1) */
|
|
|
|
ninePatch->scanline = scanline;
|
|
|
|
#if 0
|
|
|
|
printf("width: %d height: %d\n", ninePatch->width, ninePatch->height);
|
|
|
|
printf("scale: left: %d right: %d top: %d bottom: %d\n",
|
2017-02-20 20:31:58 +03:00
|
|
|
ninePatch->scaleLeft, ninePatch->scaleRight,
|
|
|
|
ninePatch->scaleTop, ninePatch->scaleBottom);
|
2014-10-01 03:40:16 +04:00
|
|
|
printf("fill: left: %d right: %d top: %d bottom: %d\n",
|
2017-02-20 20:31:58 +03:00
|
|
|
ninePatch->fillLeft, ninePatch->fillRight,
|
|
|
|
ninePatch->fillTop, ninePatch->fillBottom);
|
2014-10-01 03:40:16 +04:00
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-09-30 22:54:36 +04:00
|
|
|
rdtkNinePatch* rdtk_nine_patch_new(rdtkEngine* engine)
|
|
|
|
{
|
|
|
|
rdtkNinePatch* ninePatch;
|
2019-11-06 17:24:51 +03:00
|
|
|
ninePatch = (rdtkNinePatch*)calloc(1, sizeof(rdtkNinePatch));
|
2014-09-30 22:54:36 +04:00
|
|
|
|
|
|
|
if (!ninePatch)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ninePatch->engine = engine;
|
|
|
|
return ninePatch;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rdtk_nine_patch_free(rdtkNinePatch* ninePatch)
|
|
|
|
{
|
|
|
|
if (!ninePatch)
|
|
|
|
return;
|
|
|
|
|
|
|
|
winpr_image_free(ninePatch->image, TRUE);
|
|
|
|
free(ninePatch);
|
|
|
|
}
|
|
|
|
|
|
|
|
int rdtk_nine_patch_engine_init(rdtkEngine* engine)
|
|
|
|
{
|
|
|
|
int status;
|
2016-03-30 03:34:52 +03:00
|
|
|
wImage* image = NULL;
|
2014-09-30 22:54:36 +04:00
|
|
|
rdtkNinePatch* ninePatch;
|
|
|
|
|
|
|
|
if (!engine->button9patch)
|
|
|
|
{
|
2021-06-18 11:01:52 +03:00
|
|
|
SSIZE_T size;
|
|
|
|
const uint8_t* data;
|
2014-09-30 22:54:36 +04:00
|
|
|
status = -1;
|
|
|
|
size = rdtk_get_embedded_resource_file("btn_default_normal.9.png", &data);
|
|
|
|
|
|
|
|
if (size > 0)
|
|
|
|
{
|
|
|
|
image = winpr_image_new();
|
|
|
|
|
|
|
|
if (image)
|
2021-06-18 11:01:52 +03:00
|
|
|
status = winpr_image_read_buffer(image, data, (size_t)size);
|
2014-09-30 22:54:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (status > 0)
|
|
|
|
{
|
|
|
|
ninePatch = engine->button9patch = rdtk_nine_patch_new(engine);
|
|
|
|
|
|
|
|
if (ninePatch)
|
2014-10-01 03:40:16 +04:00
|
|
|
rdtk_nine_patch_set_image(ninePatch, image);
|
2018-08-20 10:10:49 +03:00
|
|
|
else
|
|
|
|
winpr_image_free(image, TRUE);
|
2014-09-30 22:54:36 +04:00
|
|
|
}
|
2021-04-27 11:04:47 +03:00
|
|
|
else
|
|
|
|
winpr_image_free(image, TRUE);
|
2014-09-30 22:54:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!engine->textField9patch)
|
|
|
|
{
|
2021-06-18 11:01:52 +03:00
|
|
|
SSIZE_T size;
|
|
|
|
const uint8_t* data;
|
2014-09-30 22:54:36 +04:00
|
|
|
status = -1;
|
|
|
|
size = rdtk_get_embedded_resource_file("textfield_default.9.png", &data);
|
2021-04-27 11:04:47 +03:00
|
|
|
image = NULL;
|
2014-09-30 22:54:36 +04:00
|
|
|
|
|
|
|
if (size > 0)
|
|
|
|
{
|
|
|
|
image = winpr_image_new();
|
|
|
|
|
|
|
|
if (image)
|
2021-06-18 11:01:52 +03:00
|
|
|
status = winpr_image_read_buffer(image, data, (size_t)size);
|
2014-09-30 22:54:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (status > 0)
|
|
|
|
{
|
|
|
|
ninePatch = engine->textField9patch = rdtk_nine_patch_new(engine);
|
|
|
|
|
|
|
|
if (ninePatch)
|
2014-10-01 03:40:16 +04:00
|
|
|
rdtk_nine_patch_set_image(ninePatch, image);
|
2018-08-20 10:10:49 +03:00
|
|
|
else
|
|
|
|
winpr_image_free(image, TRUE);
|
2014-09-30 22:54:36 +04:00
|
|
|
}
|
2021-04-27 11:04:47 +03:00
|
|
|
else
|
|
|
|
winpr_image_free(image, TRUE);
|
2014-09-30 22:54:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2014-10-01 20:18:17 +04:00
|
|
|
|
|
|
|
int rdtk_nine_patch_engine_uninit(rdtkEngine* engine)
|
|
|
|
{
|
|
|
|
if (engine->button9patch)
|
|
|
|
{
|
|
|
|
rdtk_nine_patch_free(engine->button9patch);
|
|
|
|
engine->button9patch = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (engine->textField9patch)
|
|
|
|
{
|
|
|
|
rdtk_nine_patch_free(engine->textField9patch);
|
|
|
|
engine->textField9patch = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|