batch and queue done yay

This commit is contained in:
sillysagiri 2023-12-24 13:17:38 +07:00
parent a95d53643d
commit 2069bb2969
2 changed files with 263 additions and 42 deletions

View File

@ -1,7 +1,9 @@
#include <cstdio>
#include <cstring>
#include <iostream>
#include <filesystem>
#include <fstream>
#include <sstream>
#include <string>
#define STB_IMAGE_IMPLEMENTATION
@ -25,10 +27,14 @@ int main()
std::string path_out = "../assets/out/";
std::string path_palette = "../assets/palette.png";
std::string path_resource = "../resource";
std::string path_images = "../assets/out/out_";
std::string path_test = "../assets/out/out_3667.bmp";
int frame_total = -1;
std::stringstream ss;
std::ofstream out;
// Find total frame
for (const auto& entry : std::filesystem::directory_iterator(path_out)) {
if (entry.is_regular_file()) {
@ -62,50 +68,165 @@ int main()
// printf("%i> %i,%i,%i (%i)\n", i, r, g, b, palette[i]);
}
std::ofstream out;
out.open(path_resource + "/palette.bin", std::ios::binary);
out.write(reinterpret_cast<const char*>(palette), sizeof(palette));
out.close();
out.open(path_resource + "/image.bin", std::ios::binary);
uint8_t test[256*192];
uint8_t test_compress[(256*192)*2];
uint32_t test_compress_len = 0;
int test_w = -1;
int test_h = -1;
int test_n = -1;
unsigned char *test_data = stbi_load(path_test.c_str(), &test_w, &test_h, &test_n, 3);
uint8_t batch_map[256*192*4];
int batch_counter = 0;
for (int i=0; i<256*192; i++)
for (int frame_counter=1; frame_counter<=frame_total; frame_counter++)
{
int index = i*3;
unsigned char r = test_data[index];
unsigned char g = test_data[index + 1];
unsigned char b = test_data[index + 2];
ss.str("");
ss << path_images << frame_counter << ".bmp";
uint16_t current_pal = RGB15(r, g, b);
int current_index = -1;
int frame_w = -1;
int frame_h = -1;
int frame_n = -1;
unsigned char *frame_raw = stbi_load(ss.str().c_str(), &frame_w, &frame_h, &frame_n, 3);
for (int i2=0; i2<256; i2++)
for (int i=0; i<256*192; i++)
{
if (palette[i2] == current_pal) {
current_index = i2;
break;
int index = i*3;
unsigned char r = frame_raw[index];
unsigned char g = frame_raw[index + 1];
unsigned char b = frame_raw[index + 2];
uint16_t pixel_pal = RGB15(r, g, b);
int pixel_index = -1;
for (int i2=0; i2<256; i2++)
{
if (palette[i2] == pixel_pal) {
pixel_index = i2;
break;
}
}
if (pixel_index == -1) printf("%i> %i palette not found!!!\n", i, pixel_index);
else batch_map[i+ batch_counter*256*192] = pixel_index;
}
if (current_index == -1) printf("%i> %i\n", i, current_index);
else test[i] = current_index;
printf("%i) frame %i/%i (%1.f%s)> done\n", batch_counter, frame_counter, frame_total, ((float)frame_counter/frame_total)*100, "%");
batch_counter++;
if (batch_counter > 3)
{
uint8_t batch_map_compress[256*192*4*2];
uint32_t size = fastlz_compress_level(1, batch_map, 256*192*4, batch_map_compress);
out.write(reinterpret_cast<const char*>(&size), sizeof(size));
out.write(reinterpret_cast<const char*>(&batch_map_compress), size);
batch_counter = 0;
printf("write batch %i\n", size);
}
}
test_compress_len = fastlz_compress_level(1, test, 256*192, test_compress);
printf("len: %i\n", test_compress_len);
out.open(path_resource + "/image.bin", std::ios::binary);
out.write(reinterpret_cast<const char*>(&test_compress_len), sizeof(test_compress_len));
out.write(reinterpret_cast<const char*>(test_compress), test_compress_len);
out.close();
// for (int i=1; i<=frame_total/batch_size; i++)
// {
// uint8_t batch[256*192*batch_size];
// uint8_t batch_compress[256*192*batch_size*2];
// uint32_t batch_compress_len = 0;
// for (batch_current=0; batch_current<batch_size; batch_current++)
// {
// if (frame_counter > frame_total) break;
// uint8_t current_map[256*192];
// ss.str("");
// ss << path_images << frame_counter << ".bmp";
// int current_w = -1;
// int current_h = -1;
// int current_n = -1;
// unsigned char *current_data = stbi_load(ss.str().c_str(), &current_w, &current_h, &current_n, 3);
// for (int i2=0; i2<256*192; i2++)
// {
// int index = i2*3;
// unsigned char r = current_data[index];
// unsigned char g = current_data[index + 1];
// unsigned char b = current_data[index + 2];
// uint16_t current_pal = RGB15(r, g, b);
// int current_index = -1;
// for (int i3=0; i3<256; i3++)
// {
// if (palette[i3] == current_pal) {
// current_index = i3;
// break;
// }
// }
// if (current_index == -1) printf("%i> %i palette not found!!!\n", i, current_index);
// else current_map[i2] = current_index;
// }
// memcpy(current_map, &batch[batch_current*256*192], 256*192);
// printf("frame %i/%i\n", frame_counter, frame_total);
// frame_counter++;
// }
// batch_compress_len = fastlz_compress_level(1, batch, 256*192*batch_size, batch_compress);
// out.write(reinterpret_cast<const char*>(&batch_compress_len), sizeof(batch_compress_len));
// out.write(reinterpret_cast<const char*>(&batch_compress), batch_compress_len);
// printf("batch done! (%i)\n", batch_compress_len);
// }
out.close();
// uint8_t test[256*192];
// uint8_t test_compress[(256*192)*2];
// uint32_t test_compress_len = 0;
// int test_w = -1;
// int test_h = -1;
// int test_n = -1;
// unsigned char *test_data = stbi_load(path_test.c_str(), &test_w, &test_h, &test_n, 3);
// for (int i=0; i<256*192; i++)
// {
// int index = i*3;
// unsigned char r = test_data[index];
// unsigned char g = test_data[index + 1];
// unsigned char b = test_data[index + 2];
// uint16_t current_pal = RGB15(r, g, b);
// int current_index = -1;
// for (int i2=0; i2<256; i2++)
// {
// if (palette[i2] == current_pal) {
// current_index = i2;
// break;
// }
// }
// if (current_index == -1) printf("%i> %i\n", i, current_index);
// else test[i] = current_index;
// }
// test_compress_len = fastlz_compress_level(1, test, 256*192, test_compress);
// printf("len: %i\n", test_compress_len);
// out.open(path_resource + "/image.bin", std::ios::binary);
// out.write(reinterpret_cast<const char*>(&test_compress_len), sizeof(test_compress_len));
// out.write(reinterpret_cast<const char*>(test_compress), test_compress_len);
// out.close();
// Print
printf("Total Frame: %i\n", frame_total);
}

View File

@ -1,4 +1,5 @@
#include "fastlz.h"
#include <cstddef>
#include <cstdlib>
#include <nds.h>
#include <filesystem.h>
@ -8,9 +9,66 @@
#include <nds/ndstypes.h>
#include <stdio.h>
#include <cstdarg>
#include <sys/_stdint.h>
void wait_forever(const char* msg, ...);
size_t LoadFile(const char* file, unsigned char **buffer);
void readNextBatch(FILE *input, void *output);
void vbl_handler();
class CircularQueue {
public:
int front, rear, size, current;
CircularQueue(int s) {
size = s;
front = rear = -1;
current = 0;
}
bool isFull() {
return (front == 0 && rear == size - 1) || (rear == (front - 1) % (size - 1));
}
bool isEmpty() {
return front == -1;
}
void push() {
if (isFull()) {
fprintf(stderr, "Queue is full. Cannot push\n");
return;
}
if (isEmpty()) {
front = rear = 0;
} else {
rear = (rear + 1) % size;
}
current++;
}
void pop() {
if (isEmpty()) {
fprintf(stderr, "Queue is empty. Cannot pop\n");
return;
}
if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % size;
}
current--;
}
};
static uint8_t chunk[256*192*4*16];
static int chunk_counter = 0;
static CircularQueue queue(16);
static FILE *in = NULL;
static int bgp;
int main(void) {
@ -26,36 +84,78 @@ int main(void) {
nitroFSInit(NULL);
unsigned char* bmp = NULL;
unsigned char* bmp_decompress = (unsigned char*)malloc(256*192);
size_t bmp_len = LoadFile("nitro:/image.bin", &bmp);
in = fopen("nitro:/image.bin", "rb");
if (in == NULL) wait_forever("cannot load %s", "nitro:/image.bin");
// size_t bmp_decompress_len = bmp[0] | bmp[1] << 8;
size_t bmp_decompress_len = 0;
// unsigned char* bmp = NULL;
// unsigned char* bmp_decompress = (unsigned char*)malloc(256*192);
// size_t bmp_len = LoadFile("nitro:/image.bin", &bmp);
memcpy(&bmp_decompress_len, bmp, sizeof(uint32));
fprintf(stderr, "%i %i\n", bmp_decompress_len, bmp_len);
fastlz_decompress(&bmp[4], bmp_decompress_len, bmp_decompress, 256*192);
// // size_t bmp_decompress_len = bmp[0] | bmp[1] << 8;
// size_t bmp_decompress_len = 0;
// memcpy(&bmp_decompress_len, bmp, sizeof(uint32));
// fprintf(stderr, "%i %i\n", bmp_decompress_len, bmp_len);
// fastlz_decompress(&bmp[4], bmp_decompress_len, bmp_decompress, 256*192);
unsigned char* pal = NULL;
size_t pal_len = LoadFile("nitro:/palette.bin", &pal);
int bgp = bgInit(3, BgType_Bmp8, BgSize_B8_256x256, 0, 0);
bgp = bgInit(3, BgType_Bmp8, BgSize_B8_256x256, 0, 0);
// DC_FlushRange(bg, bg_len);
dmaCopy(bmp_decompress, bgGetGfxPtr(bgp), 256*192);
dmaCopy(pal, BG_PALETTE, pal_len);
nocashMessage("done\n");
// // DC_FlushRange(bg, bg_len);
// dmaCopy(bmp_decompress, bgGetGfxPtr(bgp), 256*192);
// nocashMessage("done\n");
while(!queue.isFull()) {
queue.push();
readNextBatch(in, &chunk[256*192*4 * queue.rear]);
fprintf(stderr, "read %i\n", queue.rear);
}
fprintf(stderr, "done read\n");
while(1) {
swiWaitForVBlank();
scanKeys();
irqSet(IRQ_VBLANK, vbl_handler);
if (!queue.isFull()) {
queue.push();
readNextBatch(in, &chunk[256*192*4 * queue.rear]);
// fprintf(stderr, "read %i\n", queue.rear);
}
}
return 0;
}
void vbl_handler()
{
if (queue.isEmpty()) return;
dmaCopy(&chunk[256*192*4 * queue.front + 256*192*chunk_counter], bgGetGfxPtr(bgp), 256*192);
fprintf(stderr, "count: %i, %i\n ", chunk_counter, queue.current);
chunk_counter++;
if (chunk_counter >= 3) {
queue.pop();
chunk_counter = 0;
}
}
void readNextBatch(FILE *input, void *output)
{
uint32_t chunk_len;
fread(&chunk_len, sizeof(uint32_t), 1, input);
unsigned char *buffer = (unsigned char*)malloc(chunk_len);
fread(buffer, 1, chunk_len, input);
fastlz_decompress(buffer, chunk_len, output, 256*192*4);
free(buffer);
}
size_t LoadFile(const char* file, unsigned char **buffer)
{
FILE *in = fopen(file, "rb");