Skip to content

Commit

Permalink
Some more image handling code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Melchizedek6809 committed Apr 25, 2024
1 parent 29e0646 commit 765c711
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 30 deletions.
8 changes: 0 additions & 8 deletions lib/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,6 @@ typedef enum {

} lImageType;

typedef struct {
i32 buffer;
i32 length;
i32 offset;
u8 flags;
u8 type;
} lImageBufferView;

typedef struct {
i32 parent;
i32 data;
Expand Down
44 changes: 29 additions & 15 deletions lib/imageReader.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ static void readMapSet(readImageMap *map, i32 key, void *val){
lMapSet(map->map, lValInt(key), lValInt((u64)val));
}

static inline i32 readI8(const lImage *img, i32 off){
return img->data[off ];
}

static inline i32 readI16(const lImage *img, i32 off){
return img->data[off ] | (img->data[off+1]<<8);
}

static i32 readI24(const lImage *img, i32 off){
return img->data[off ] | (img->data[off+1]<<8) | (img->data[off+2]<<16);
}

static i32 readI32(const lImage *img, i32 off){
return img->data[off ] | (img->data[off+1]<<8) | (img->data[off+2]<<16) | (img->data[off+3]<<24);
}

static const lSymbol *readSymbol(readImageMap *map, const lImage *img, i32 off, bool staticImage){
(void)staticImage;
(void)map;
Expand Down Expand Up @@ -85,8 +101,8 @@ static lPair *readPair(readImageMap *map, const lImage *img, i32 off, bool stati
if(off >= (1<<24)-1){return NULL;}
lPair *ret = lPairAllocRaw();
readMapSet(map, off, ret);
const i32 car = img->data[off ] | (img->data[off+1]<<8) | (img->data[off+2]<<16);
const i32 cdr = img->data[off+3] | (img->data[off+4]<<8) | (img->data[off+5]<<16);
const i32 car = readI24(img, off);
const i32 cdr = readI24(img, off+3);
ret->car = car < ((1<<24)-1) ? readVal(map, img, car, staticImage) : NIL;
ret->cdr = cdr < ((1<<24)-1) ? readVal(map, img, cdr, staticImage) : NIL;
return ret;
Expand Down Expand Up @@ -118,15 +134,15 @@ static lBufferView *readBufferView(readImageMap *map, const lImage *img, i32 off
if(mapP != NULL){ return (lBufferView *)mapP; }
if(off < 0){return NULL;}
if(off >= (1<<24)-1){return NULL;}
lImageBufferView *imgBuf = (lImageBufferView *)((void *)&img->data[off]);
lBuffer *buf = readBuffer(map, img, imgBuf->buffer, staticImage);
const u8 flags = readI8(img, off);
lBuffer *buf = readBuffer(map, img, readI24(img, off+1), staticImage);
lBufferView *view = lBufferViewAllocRaw();
readMapSet(map, off, view);
view->flags = flags;
view->buf = buf;
view->offset = imgBuf->offset;
view->length = imgBuf->length;
view->flags = imgBuf->flags;
view->type = imgBuf->type;
view->length = readI32(img, off+4);
view->offset = readI32(img, off+8);
view->type = readI8(img, off+12);
return view;
}

Expand All @@ -140,8 +156,8 @@ static lMap *readMap(readImageMap *map, const lImage *img, i32 off, bool staticI
readMapSet(map, off, ret);
off += 2;
for(int i=0;i<len;i++){
const i32 key = img->data[off+0] | (img->data[off+1] << 8) | (img->data[off+2] << 16);
const i32 val = img->data[off+3] | (img->data[off+4] << 8) | (img->data[off+5] << 16);
const i32 key = readI24(img, off+0);
const i32 val = readI24(img, off+3);
off += 6;
lMapSet(ret, readVal(map, img, key, staticImage), readVal(map, img, val, staticImage));
}
Expand All @@ -158,8 +174,8 @@ static lTree *readTree(readImageMap *map, const lImage *img, i32 off, bool stati
readMapSet(map, off, root);
off += 2;
for(int i=0;i<len;i++){
const i32 sym = img->data[off+0] | (img->data[off+1] << 8) | (img->data[off+2] << 16);
const i32 val = img->data[off+3] | (img->data[off+4] << 8) | (img->data[off+5] << 16);
const i32 sym = readI24(img, off+0);
const i32 val = readI24(img, off+3);
off += 6;

const lSymbol *s = readSymbol(map, img, sym, staticImage);
Expand All @@ -172,7 +188,6 @@ static lTree *readTree(readImageMap *map, const lImage *img, i32 off, bool stati
}

static lNFunc *readNFunc(readImageMap *map, const lImage *img, i32 off, bool staticImage){
(void)map;
if(off < 0){return NULL;}
const lSymbol *sym = readSymbol(map, img, off, staticImage);
for(uint i=0;i<lNFuncMax;i++){
Expand All @@ -186,7 +201,6 @@ static lNFunc *readNFunc(readImageMap *map, const lImage *img, i32 off, bool sta
}

static lClass *readType(readImageMap *map, const lImage *img, i32 off, bool staticImage){
(void)map;
const lSymbol *sym = readSymbol(map, img, off, staticImage);
if(off < 0){return NULL;}
if(off >= (1<<24)-1){return NULL;}
Expand Down Expand Up @@ -270,7 +284,7 @@ static lVal readVal(readImageMap *map, const lImage *img, i32 off, bool staticIm
break;
}

const i32 tbyte = img->data[off] | (img->data[off+1] << 8) | (img->data[off+2] << 16);
const i32 tbyte = readI24(img, off);
map->curOff += 3;
switch(T){
default:
Expand Down
24 changes: 17 additions & 7 deletions lib/imageWriter.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ typedef struct {
lMap *map;
} writeImageContext;

static void writeI8(writeImageContext *ctx, i32 curOff, i32 v){
ctx->start[curOff ] = (v )&0xFF;
}

static void writeI16(writeImageContext *ctx, i32 curOff, i32 v){
ctx->start[curOff ] = (v )&0xFF;
ctx->start[curOff+1] = (v>> 8)&0xFF;
Expand All @@ -25,6 +29,13 @@ static void writeI24(writeImageContext *ctx, i32 curOff, i32 v){
ctx->start[curOff+2] = (v>>16)&0xFF;
}

static void writeI32(writeImageContext *ctx, i32 curOff, i32 v){
ctx->start[curOff ] = (v )&0xFF;
ctx->start[curOff+1] = (v>> 8)&0xFF;
ctx->start[curOff+2] = (v>>16)&0xFF;
ctx->start[curOff+3] = (v>>24)&0xFF;
}

static i32 ctxAddVal(writeImageContext *ctx, lVal v);

static i32 writeMapGet(lMap *map, void *key){
Expand Down Expand Up @@ -217,20 +228,19 @@ static i32 ctxAddBufferView(writeImageContext *ctx, lBufferView *v){
const i32 mapOff = writeMapGet(ctx->map, (void *)v);
if(mapOff > 0){ return mapOff; }

const i32 eleSize = sizeof(lImageBufferView);
const i32 eleSize = 13;
ctxRealloc(ctx, eleSize);

const i32 curOff = ctx->curOff;
writeMapSet(ctx->map, (void *)v, curOff);
ctx->curOff += eleSize;
const i32 buf = ctxAddBuffer(ctx, v->buf);

lImageBufferView *out = (lImageBufferView *)((void *)&ctx->start[curOff]);
out->buffer = buf;
out->length = v->length;
out->offset = v->offset;
out->flags = v->flags;
out->type = v->type;
writeI8 (ctx, curOff+0, v->flags);
writeI24(ctx, curOff+1, buf);
writeI32(ctx, curOff+4, v->length);
writeI32(ctx, curOff+8, v->offset);
writeI8 (ctx, curOff+12, v->type);

return curOff;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/testsuite/image.nuj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
("asdq" (image/deserialize (image/serialize "asdq")))
("asdq" (image/deserialize (image/serialize "asdq")))
("asd" (image/deserialize (image/serialize "asd")))
(#x20 (ref (image/deserialize (image/serialize (:u8 #m2030))) 0))
(#x30 (ref (image/deserialize (image/serialize (:u8 #m2030))) 1))
("" (image/deserialize (image/serialize "")))
('(1 2 3) (image/deserialize (image/serialize '(1 2 3))))
('(1 "qwe" :asd) (image/deserialize (image/serialize '(1 "qwe" :asd))))
Expand Down

0 comments on commit 765c711

Please sign in to comment.