summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorLizzy Fleckenstein <lizzy@vlhl.dev>2023-12-15 16:10:22 +0100
committerLizzy Fleckenstein <lizzy@vlhl.dev>2023-12-15 16:11:38 +0100
commit2298d17186cb0e58a96d285384de431902da9b1e (patch)
tree635d642931d9f55e701639ee3b3707e0e28a805e /util
parent8a25a2935a60e65fcb3e2b715bada858f5fcd6a2 (diff)
downloadcuddles-2298d17186cb0e58a96d285384de431902da9b1e.tar.xz
big chungus
* fix a heap corruption bug * add qemu support * add an ATA driver * add an USTAR read-only file system * boot from disk instead of floppy * font rendering * image rendering * PCI enumeration * init script
Diffstat (limited to 'util')
-rw-r--r--util/png2cuddleimg/.gitignore1
-rw-r--r--util/png2cuddleimg/Makefile2
-rw-r--r--util/png2cuddleimg/main.c68
-rw-r--r--util/ttf2cuddlefont/.gitignore1
-rw-r--r--util/ttf2cuddlefont/Makefile2
-rw-r--r--util/ttf2cuddlefont/main.c52
6 files changed, 126 insertions, 0 deletions
diff --git a/util/png2cuddleimg/.gitignore b/util/png2cuddleimg/.gitignore
new file mode 100644
index 0000000..daad221
--- /dev/null
+++ b/util/png2cuddleimg/.gitignore
@@ -0,0 +1 @@
+png2cuddleimg
diff --git a/util/png2cuddleimg/Makefile b/util/png2cuddleimg/Makefile
new file mode 100644
index 0000000..3a18ee6
--- /dev/null
+++ b/util/png2cuddleimg/Makefile
@@ -0,0 +1,2 @@
+png2cuddleimg: main.c
+ gcc $$(pkg-config --cflags --libs libpng) -Wall -Wextra main.c -o png2cuddleimg
diff --git a/util/png2cuddleimg/main.c b/util/png2cuddleimg/main.c
new file mode 100644
index 0000000..8dbfb14
--- /dev/null
+++ b/util/png2cuddleimg/main.c
@@ -0,0 +1,68 @@
+#include <png.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <endian.h>
+
+int main(int argc, char *argv[])
+{
+#define TRY(expr, ...) if (!(expr)) { \
+ fprintf(stderr, "%s: ", argv[0]); \
+ fprintf(stderr, __VA_ARGS__); \
+ if (file != NULL) fclose(file); \
+ png_destroy_read_struct(&png, &info, NULL); \
+ return EXIT_FAILURE; }
+
+ FILE *file = NULL;
+ png_structp png = NULL;
+ png_infop info = NULL;
+
+ TRY(argc == 2, "usage: %s infile.png > outfile.cuddleimg\n", argv[0]);
+ TRY((file = fopen(argv[1], "r")) != NULL, "failed to open %s\n", argv[1]);
+
+ TRY(png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL),
+ "png_create_read_struct failed\n");
+
+ TRY(info = png_create_info_struct(png), "png_create_info_struct failed\n");
+
+ png_init_io(png, file);
+ png_read_info(png, info);
+
+ png_uint_32 width = png_get_image_width(png, info);
+ png_uint_32 height = png_get_image_height(png, info);
+ png_byte color_type = png_get_color_type(png, info);
+ png_byte bit_depth = png_get_bit_depth(png, info);
+
+ if (bit_depth == 16)
+ png_set_strip_16(png);
+ if (color_type == PNG_COLOR_TYPE_PALETTE)
+ png_set_palette_to_rgb(png);
+ if(png_get_valid(png, info, PNG_INFO_tRNS))
+ png_set_tRNS_to_alpha(png);
+ if (color_type == PNG_COLOR_TYPE_RGB || color_type == PNG_COLOR_TYPE_GRAY_ALPHA || color_type == PNG_COLOR_TYPE_PALETTE)
+ png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
+ if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
+ png_set_gray_to_rgb(png);
+
+ png_read_update_info(png, info);
+
+ fwrite(&width, 1, sizeof width, stdout);
+ fwrite(&height, 1, sizeof height, stdout);
+
+ png_uint_32 pitch = png_get_rowbytes(png, info);
+ png_byte row[pitch];
+ for (png_uint_32 y = 0; y < height; y++) {
+ png_read_row(png, row, NULL);
+
+ for (png_uint_32 x = 0; x < width; x++) {
+ png_byte tmp = row[4*x];
+ row[4*x] = row[4*x+2];
+ row[4*x+2] = tmp;
+ }
+
+ fwrite(row, 1, pitch, stdout);
+ }
+
+ fclose(file);
+ png_destroy_read_struct(&png, &info, NULL);
+ return EXIT_SUCCESS;
+}
diff --git a/util/ttf2cuddlefont/.gitignore b/util/ttf2cuddlefont/.gitignore
new file mode 100644
index 0000000..1423b0e
--- /dev/null
+++ b/util/ttf2cuddlefont/.gitignore
@@ -0,0 +1 @@
+ttf2cuddlefont
diff --git a/util/ttf2cuddlefont/Makefile b/util/ttf2cuddlefont/Makefile
new file mode 100644
index 0000000..82b54cf
--- /dev/null
+++ b/util/ttf2cuddlefont/Makefile
@@ -0,0 +1,2 @@
+ttf2cuddlefont: main.c
+ gcc $$(pkg-config --cflags --libs freetype2) -Wall -Wextra main.c -o ttf2cuddlefont
diff --git a/util/ttf2cuddlefont/main.c b/util/ttf2cuddlefont/main.c
new file mode 100644
index 0000000..764dec9
--- /dev/null
+++ b/util/ttf2cuddlefont/main.c
@@ -0,0 +1,52 @@
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <ft2build.h>
+#include FT_FREETYPE_H
+
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
+
+static inline unsigned char bitreverse(unsigned char b) {
+ b = (b & 0b11110000) >> 4 | (b & 0b00001111) << 4;
+ b = (b & 0b11001100) >> 2 | (b & 0b00110011) << 2;
+ b = (b & 0b10101010) >> 1 | (b & 0b01010101) << 1;
+ return b;
+}
+
+int main(int argc, char *argv[])
+{
+#define TRY(expr, ...) if (!(expr)) \
+ { fprintf(stderr, "%s: ", argv[0]); fprintf(stderr, __VA_ARGS__); return EXIT_FAILURE; }
+
+ TRY(argc == 2, "usage: %s infile.ttf > outfile.cuddlefont\n", argv[0]);
+
+ FT_Library lib;
+ TRY(FT_Init_FreeType(&lib) == 0, "failed to initialize freetype\n");
+
+ FT_Face face;
+ TRY(FT_New_Face(lib, argv[1], 0, &face) == 0, "failed to load %s\n", argv[1]);
+ // TRY(FT_Set_Pixel_Sizes(face, 0, 16) == 0, "failed to select pixel size of 8x16\n");
+
+ for (int i = 0;; i++) {
+ TRY(i != face->num_fixed_sizes, "no 8x16 size available\n");
+
+ if (face->available_sizes[i].height == 16 &&
+ face->available_sizes[i].width == 8) {
+ TRY(FT_Select_Size(face, i) == 0, "failed to select font size");
+ break;
+ }
+ }
+
+ assert(bitreverse(0b10000000) == 0b00000001);
+
+ for (int i = 0; i < 256; i++) {
+ TRY(FT_Load_Glyph(face, FT_Get_Char_Index(face, i), FT_LOAD_DEFAULT) == 0,
+ "failed to load glyph %d\n", i);
+
+ TRY(FT_Render_Glyph(face->glyph, FT_RENDER_MODE_MONO) == 0,
+ "failed to render glyph %d\n", i);
+
+ for (unsigned int y = 0; y < 16; y++)
+ putchar(bitreverse(face->glyph->bitmap.buffer[y * face->glyph->bitmap.pitch]));
+ }
+}