· 3 min read

Crafting Pixels for Minecraft - My Adventure with Sharp

Join me as I recount my journey of transforming standard images into Minecraft's unique data format, leveraging the power of the Node.js Sharp library.

Building an Image-to-Minecraft API: A Learning Adventure

Embarking on the journey to create an API that could convert images into Minecraft dat files was an trialing endeavor. The concept was clear-cut: transform an jpeg or png image into a format that Minecraft could understand. Here’s a recount of the steps, decisions, and discoveries I made along the way.

The Color Challenge

Minecraft operates with a limited colour palette and different minecraft versions support different colour palettes. This was the primary puzzle to solve. Images can have a vast array of colors, and I needed to map each of those to the closest available color in Minecraft. This is when I learned about color quantization, what it meant, what it is and how it’s meant to work. The process involves analyzing every pixel in the image and matching it to its Minecraft equivalent. Effectively your creating an 2D array of colours and then looping across the array and matching each colour to it’s closest match and then converting the new 2D array into a byte array that then get’s transformed into a .dat file that minecraft can understand.

The Power of Sharp

For image processing, in the Node.js ecosystem there is no better tool than Sharp. A high-performance Node.js module, Sharp offered the capabilities required to navigate through each pixel, grab it’s colour and then pass it onto the colour quantization function to match to the nearest minecraft colour. It was efficient and provided the functionalities necessary for the task at hand.

Why Fastify?

For the API’s backbone, I opted for Fastify. During this time, I’ve written quite a number of Express Node backend api services and it was time to try something new. So while Fastify is known for its speed and low overhead, it satiated my need to search for a better way. Its setup was intuitive, and its plugin system was beneficial as the project evolved. The schema-based approach was different, but it brought clarity to the API’s structure. One issue I did encounter was in the middleware packages and sending zip files back to the client but was able to work around and implement a solution.

Embracing Domain-Driven Design (DDD)

During this same time I was researching different methodologies of how to structure and architect software projects. For this particular task I implemented a Domain-Driven Design (DDD). This approach allowed for a clear separation of the API’s components, ensuring each part had a specific role. DDD encouraged a deeper understanding of the architecture and the interactions between different API segments. While it did add boilerplate for the relatively basic service this task required, it allowed me to flex my knowledge base within DDD and allow the project to be easily expanded in the future if required.

Reflections on the Journey

This project was a series of discoveries. From understanding the intricacies of color quantization for Minecraft specific colours, to diving deep into Fastify’s capabilities, and flexing my DDD knowledge each step was a learning opportunity.

In essence, this endeavor was more than just building an API. It was about exploration, understanding different tools, and integrating them to achieve a goal. The journey reinforced the essence of software development for me: continuous learning and the thrill of piecing together different components to create something unique.

Back to Blog