Skip to content

Commit

Permalink
Merge pull request #92 from kubilus1/stopwait
Browse files Browse the repository at this point in the history
Add max wait limit for chunks.  Use lower quality chunks as a fallback.
  • Loading branch information
kubilus1 authored Apr 1, 2023
2 parents 01ca2d4 + f7e4a8b commit afa7d08
Show file tree
Hide file tree
Showing 19 changed files with 413 additions and 84 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ autoortho.log
out.dds
/cache
.release_info
*/libjpeg-turbo-gcc64
**/libjpeg-turbo-gcc64
tags
4 changes: 4 additions & 0 deletions autoortho/aoconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,10 @@ class AOConfig(object):
maptype_override =
# Minimum zoom level to allow
min_zoom = 12
# Max time to wait for images. Higher numbers mean better quality, but more
# stutters. Lower numbers will be more responsive at the expense of
# ocassional low quality tiles.
maxwait = 0.5
[pydds]
# ISPC or STB for dds file compression
Expand Down
2 changes: 2 additions & 0 deletions autoortho/aoimage/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.o
*.jpg
61 changes: 48 additions & 13 deletions autoortho/aoimage/AoImage.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
from ctypes import *
import platform


import logging
log = logging.getLogger(__name__)

class AOImageException(Exception):
pass

class AoImage(Structure):
_fields_ = [
('_data', c_uint64), # ctypes pointers are tricky when changed under the hud so we treat it as number
#('_data', POINTER(c_uint64)), # ctypes pointers are tricky when changed under the hud so we treat it as number
('_width', c_uint32),
('_height', c_uint32),
('_stride', c_uint32),
Expand All @@ -21,6 +24,7 @@ class AoImage(Structure):

def __init__(self):
self._data = 0
#self._data = cast('\x00', POINTER(c_uint64))
self._width = 0
self._height = 0
self._stride = 0
Expand All @@ -43,7 +47,7 @@ def convert(self, mode):
assert mode == "RGBA", "Sorry, only conversion to RGBA supported"
new = AoImage()
if not _aoi.aoimage_2_rgba(self, new):
log.error(f"AoImage.reduce_2 error: {new._errmsg.decode()}")
log.debug(f"AoImage.reduce_2 error: {new._errmsg.decode()}")
return None

return new
Expand All @@ -59,19 +63,29 @@ def reduce_2(self, steps = 1):
orig = half
half = AoImage()
if not _aoi.aoimage_reduce_2(orig, half):
log.error(f"AoImage.reduce_2 error: {new._errmsg.decode()}")
return None

log.debug(f"AoImage.reduce_2 error: {half._errmsg.decode()}")
raise AOImageException(f"AoImage.reduce_2 error: {half._errmsg.decode()}")
#return None

steps -= 1

return half

def scale(self, factor=2):
scaled = AoImage()
orig = self
if not _aoi.aoimage_scale(orig, scaled, factor):
log.debug(f"AoImage.scale error: {new._errmsg.decode()}")
return None

return scaled

def write_jpg(self, filename, quality = 90):
"""
Convenience function to write jpeg.
"""
if not _aoi.aoimage_write_jpg(filename.encode(), self, quality):
log.error(f"AoImage.new error: {new._errmsg.decode()}")
log.debug(f"AoImage.new error: {new._errmsg.decode()}")

def tobytes(self):
"""
Expand All @@ -89,7 +103,12 @@ def data_ptr(self):

def paste(self, p_img, pos):
_aoi.aoimage_paste(self, p_img, pos[0], pos[1])
return None
return True

def crop(self, c_img, pos):
_aoi.aoimage_crop(self, c_img, pos[0], pos[1])
return True


@property
def size(self):
Expand All @@ -101,24 +120,26 @@ def new(mode, wh, color):
assert(mode == "RGBA")
new = AoImage()
if not _aoi.aoimage_create(new, wh[0], wh[1], color[0], color[1], color[2]):
log.error(f"AoImage.new error: {new._errmsg.decode()}")
log.debug(f"AoImage.new error: {new._errmsg.decode()}")
return None

return new


def load_from_memory(mem):
def load_from_memory(mem, datalen=None):
if not datalen:
datalen = len(mem)
new = AoImage()
if not _aoi.aoimage_from_memory(new, mem, len(mem)):
log.error(f"AoImage.load_from_memory error: {new._errmsg.decode()}")
if not _aoi.aoimage_from_memory(new, mem, datalen):
log.debug(f"AoImage.load_from_memory error: {new._errmsg.decode()}")
return None

return new

def open(filename):
new = AoImage()
if not _aoi.aoimage_read_jpg(filename.encode(), new):
log.error(f"AoImage.open error for {filename}: {new._errmsg.decode()}")
log.debug(f"AoImage.open error for {filename}: {new._errmsg.decode()}")
return None

return new
Expand All @@ -137,11 +158,13 @@ def open(filename):
_aoi.aoimage_write_jpg.argtypes = (c_char_p, POINTER(AoImage), c_int32)
_aoi.aoimage_2_rgba.argtypes = (POINTER(AoImage), POINTER(AoImage))
_aoi.aoimage_reduce_2.argtypes = (POINTER(AoImage), POINTER(AoImage))
_aoi.aoimage_scale.argtypes = (POINTER(AoImage), POINTER(AoImage), c_uint32)
_aoi.aoimage_delete.argtypes = (POINTER(AoImage),)
_aoi.aoimage_create.argtypes = (POINTER(AoImage), c_uint32, c_uint32, c_uint32, c_uint32, c_uint32)
_aoi.aoimage_tobytes.argtypes = (POINTER(AoImage), c_char_p)
_aoi.aoimage_from_memory.argtypes = (POINTER(AoImage), c_char_p, c_uint32)
_aoi.aoimage_paste.argtypes = (POINTER(AoImage), POINTER(AoImage), c_uint32, c_uint32)
_aoi.aoimage_crop.argtypes = (POINTER(AoImage), POINTER(AoImage), c_uint32, c_uint32)

def main():
logging.basicConfig(level = logging.DEBUG)
Expand All @@ -166,14 +189,22 @@ def main():
log.info("Trying non jpg")
img = open("main.c")

img = open("../testfiles/test_tile.jpg")
img = open("../testfiles/test_tile2.jpg")
log.info(f"AoImage.open {img}")

img2 = img.reduce_2()
log.info(f"img2: {img2}")

img2.write_jpg("test_tile_2.jpg")

img3 = open("../testfiles/test_tile_small.jpg")
big = img3.scale(16)
big.write_jpg('test_tile_big.jpg')

cropimg = new('RGBA', (256,256), (0,0,0))
img.crop(cropimg, (256,256))
cropimg.write_jpg("crop.jpg")

green.paste(img2, (1024, 1024))
green.write_jpg("test_tile_p.jpg")

Expand All @@ -184,5 +215,9 @@ def main():
img.paste(img4, (0, 2048))
img.write_jpg("test_tile_p2.jpg")





if __name__ == "__main__":
main()
111 changes: 107 additions & 4 deletions autoortho/aoimage/aoimage.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ AOIAPI int32_t aoimage_2_rgba(const aoimage_t *s_img, aoimage_t *d_img) {
*dptr++ = *sptr++;
*dptr++ = *sptr++;
*dptr++ = 0xff;
//*dptr++ = 0x00;
}

d_img->ptr = dest;
Expand Down Expand Up @@ -197,11 +198,19 @@ AOIAPI int32_t aoimage_write_jpg(const char *filename, aoimage_t *img, int32_t q
}

AOIAPI int32_t aoimage_reduce_2(const aoimage_t *s_img, aoimage_t *d_img) {
assert(s_img->channels == 4);
if (s_img->channels != 4) {
sprintf(d_img->errmsg, "channel error %d != 4", s_img->channels);
d_img->ptr = NULL;
return FALSE;
}

assert((s_img->width >= 4)
&& (s_img->width == s_img->height)
&& (0 == (s_img->width & 0x03)));
if ( (s_img->width < 4)
|| (s_img->width != s_img->height)
|| (0 != (s_img->width & 0x03)) ) {
sprintf(d_img->errmsg, "width error: %d", s_img->width);
d_img->ptr = NULL;
return FALSE;
}

//aoimage_dump("aoimage_reduce_2 s_img", s_img);

Expand Down Expand Up @@ -234,6 +243,7 @@ AOIAPI int32_t aoimage_reduce_2(const aoimage_t *s_img, aoimage_t *d_img) {
*dptr++ = g;
*dptr++ = b;
*dptr++ = 0xff;
//*dptr++ = 0x00;
assert(dptr <= dest + dlen);
}
srptr += 2* stride;
Expand All @@ -249,6 +259,81 @@ AOIAPI int32_t aoimage_reduce_2(const aoimage_t *s_img, aoimage_t *d_img) {
return TRUE;
}

AOIAPI int32_t aoimage_scale(const aoimage_t *s_img, aoimage_t *d_img, uint32_t factor) {
assert(s_img->channels == 4);

assert((s_img->width >= 4)
&& (s_img->width == s_img->height)
&& (0 == (s_img->width & 0x03)));

uint32_t slen = s_img->width * s_img->height;
uint32_t dlen = slen * 16 * factor;

//fprintf(stderr, "malloc(%d)\n", (dlen*4)); fflush(stderr);
uint32_t *dest = malloc(dlen*4);
if (NULL == dest) {
sprintf(d_img->errmsg, "can't malloc %d bytes", dlen);
d_img->ptr = NULL;
return FALSE;
}

//const uint8_t *srptr = s_img->ptr; // source row start
//const uint8_t *send = srptr + slen;

// Start destination image pointer at the start
uint32_t *dptr = dest;

// Length of one row of the source image
int d_stride = s_img->width * factor;
//fprintf(stderr, "%d %d %d\n", slen, dlen, d_stride); fflush(stderr);
int s_stride = s_img->width;
//fprintf(stderr, "%p %d %d %d\n", srptr, slen, dlen, d_stride); fflush(stderr);

const uint32_t *sptr = s_img->ptr;

int d_idx;
int s_col_count = 0;

// Iterate over source image
for (int i=0; i<slen; i++) {

s_col_count++;

// Grab 32 bits
uint32_t s_rgba = sptr[i];
for (int j=0; j<factor; j++) {
//col
for (int k=0; k<factor; k++) {
//row
d_idx = ((d_stride * k) + j);
//fprintf(stderr, "D_IDX: %d D_STRIDE: %d k: %d j: %d %d %d \n", d_idx, d_stride, k, j, (d_stride * k), (d_stride * k) + j); fflush(stderr);
dptr[d_idx] = s_rgba;
}
}

dptr += factor;

if (s_col_count == s_stride) {
dptr += d_stride * (factor - 1);
s_col_count = 0;
//fprintf(stderr, "STARTROW %d %p %p\n", i, sptr, dptr); fflush(stderr);
}

}

d_img->ptr = dest;
d_img->width = s_img->width * factor;
d_img->height = s_img->height * factor;
d_img->stride = 4 * d_img->width;
d_img->channels = 4;

//fprintf(stderr, "start: %p end: %p expected: %p len: %d diff: %d \n", dest, dptr, (dest + dlen), dlen, (dest+dlen) - dptr); fflush(stderr);
//assert(dptr == (dest + dlen));
//fprintf(stderr, "width: %d height: %d \n", d_img->width, d_img->height); fflush(stderr);
//assert((s_img->width * s_img->height * 4) << factor == d_img->width * d_img->height * 4);
return TRUE;
}

AOIAPI int32_t aoimage_from_memory(aoimage_t *img, const uint8_t *data, uint32_t len) {
memset(img, 0, sizeof(aoimage_t));

Expand Down Expand Up @@ -333,3 +418,21 @@ AOIAPI int32_t aoimage_paste(aoimage_t *img, const aoimage_t *p_img, uint32_t x,
return TRUE;
}

AOIAPI int32_t aoimage_crop(aoimage_t *img, const aoimage_t *c_img, uint32_t x, uint32_t y) {
assert(x + c_img->width <= img->width);
assert(y + c_img->height <= img->height);
assert((img->channels == 4) && (c_img->channels == 4));

uint8_t *ip = img->ptr + (y * img->width * 4) + x * 4; // lower left corner of image
uint8_t *cp = c_img->ptr;

for (int i = 0; i < c_img->height; i++) {
memcpy(cp, ip, c_img->width * 4);
ip += img->width * 4;
cp += c_img->width * 4;
}

return TRUE;
}


Binary file modified autoortho/aoimage/aoimage.dll
Binary file not shown.
4 changes: 3 additions & 1 deletion autoortho/aoimage/aoimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ AOIAPI int32_t aoimage_2_rgba(const aoimage_t *s_img, aoimage_t *d_img);
AOIAPI int32_t aoimage_read_jpg(const char *filename, aoimage_t *img);
AOIAPI int32_t aoimage_write_jpg(const char *filename, aoimage_t *img, int32_t quality);
AOIAPI int32_t aoimage_reduce_2(const aoimage_t *s_img, aoimage_t *d_img);
AOIAPI int32_t aoimage_scale(const aoimage_t *s_img, aoimage_t *d_img, uint32_t factor);
AOIAPI void aoimage_delete(aoimage_t *img);
AOIAPI int32_t aoimage_create(aoimage_t *img, uint32_t width, uint32_t height, uint32_t r, uint32_t g, uint32_t b);
AOIAPI int32_t aoimage_from_memory(aoimage_t *img, const uint8_t *data, uint32_t len);
AOIAPI void aoimage_tobytes(aoimage_t *img, uint8_t *data);

// in place: img + pasted(p_img)
AOIAPI int32_t aoimage_paste(aoimage_t *img, const aoimage_t *p_img, uint32_t x, uint32_t y);
#endif
AOIAPI int32_t aoimage_crop(aoimage_t *img, const aoimage_t *c_img, uint32_t x, uint32_t y);
#endif
Binary file removed autoortho/aoimage/aoimage.o
Binary file not shown.
Binary file modified autoortho/aoimage/aoimage.so
Binary file not shown.
Binary file removed autoortho/aoimage/black.jpg
Binary file not shown.
Binary file removed autoortho/aoimage/green.jpg
Binary file not shown.
Binary file removed autoortho/aoimage/main
Binary file not shown.
Loading

0 comments on commit afa7d08

Please sign in to comment.