PPM format isn't consitent with tile's memory layout as a result ppm display isn't right.
wellcomez opened this issue · 2 comments
wellcomez commented
wellcomez commented
export2file()
{
jniTiledSurface* surface = this;
uint16_t * data = surface->tile_buffer;
struct rgba {
uint16_t arr[4];
};
struct tiledata {
struct rgba aaa[MYPAINT_TILE_SIZE][MYPAINT_TILE_SIZE];
};
struct rgba* begin = (struct rgba*)data;
struct rgba* dest= (struct rgba*)malloc(sizeof(struct tiledata)*surface->tiles_width*surface->height);
struct tiledata* beginTile = (struct tiledata*)data;
for (int i=0; i<surface->tiles_width*surface->tiles_height; i++) {
struct rgba* r =(struct rgba*)(beginTile+i);
int txOffset = i%surface->tiles_width*MYPAINT_TILE_SIZE;
int tyOffset = i/surface->tiles_width*MYPAINT_TILE_SIZE;
for (int y=0; y<MYPAINT_TILE_SIZE; y++) {
for (int x=0; x<MYPAINT_TILE_SIZE; x++) {
int xx = txOffset+x;
int yy = tyOffset+y;
struct rgba* e = r+y*MYPAINT_TILE_SIZE+x;
dest[MYPAINT_TILE_SIZE*surface->tiles_width*yy+xx] = *e;
}
}
}
int h = surface->height;
int w = surface->width;
FILE * fp = fopen("output_raw.ppm", "w");
if (!fp) {
perror("fopen 'output_raw.ppm'");
return;
}
fprintf(fp, "P3\n#Handwritten\n%d %d\n255\n", w, h);
data = (uint16_t*)dest;
for (int y=0; y<h; y++) {
struct rgba* r = (struct rgba*)data+y*surface->tiles_width*MYPAINT_TILE_SIZE;
for (int x=0; x<w; x++) {
fprintf(fp, "%d ", r->arr[0]/256);
fprintf(fp, "%d ", r->arr[1]/256);
fprintf(fp, "%d ", r->arr[2]/256);
r++;
}
fprintf(fp, "\n");
}
fclose(fp);
}
Above is my implementation.
jplloyd commented
The write_ppm
function is now fixed. I only fixed the issues in the existing implementation, but thank you nonetheless for providing a reference implementation.