### Input Validation and Directory Setup Source: https://context7.com/tibiajs/sprites-extractor/llms.txt Validates command-line arguments, checks for the existence of the Tibia sprite file, and sets up the output directory. It ensures the sprite file has a '.spr' extension and creates the output directory if it doesn't exist. ```javascript const fs = require('fs'); var sprFile = process.argv[2]; // Path to .spr file var outDir = process.argv[3]; // Output directory (optional) // Validate sprite file argument if(!sprFile || sprFile.length < 6) { throw new Error('Missing Tibia.spr.'); } // Set default output directory if(!outDir) { outDir = './out/'; } // Ensure trailing slash if(outDir.charAt(outDir.length-1) != '/') { outDir = outDir + '/'; } // Create output directory if it doesn't exist if(!fs.existsSync(outDir)) { fs.mkdirSync(outDir); } // Validate .spr extension if(sprFile.substring((sprFile.length - 4), sprFile.length) != '.spr') { throw new Error('Only .spr is allowed!'); } // Validate file exists if(!fs.existsSync(sprFile)) { throw new Error('File not found: ' + sprFile); } console.log("Ready to process:", sprFile); console.log("Output directory:", outDir); ``` -------------------------------- ### Execute Sprite Extractor from Command Line Source: https://context7.com/tibiajs/sprites-extractor/llms.txt This section shows how to run the sprite extractor tool from the command line. It requires the path to the Tibia.spr file and optionally accepts a custom output directory. The tool processes the sprites and saves them as PNG files. ```bash # Basic usage with default output directory (./out/) node index.js /path/to/Tibia.spr # Specify custom output directory node index.js /path/to/Tibia.spr ./sprites/ # Example with real file path node index.js ~/tibia/Tibia.spr ./extracted_sprites/ ``` -------------------------------- ### Multi-Progress Bar Tracking with Custom Bars Source: https://context7.com/tibiajs/sprites-extractor/llms.txt Implements a custom multibar system to display multiple progress bars simultaneously for different phases like parsing and exporting. Each bar is configured with a template, styling options, and a total count. ```javascript const MultiBar = require('./multibar.js'); var mbars = new MultiBar(); var bars = []; // Create parsing progress bar bars.push(mbars.newBar( 'Parsing: [:bar] :percent | ETA: :eta | Time Elapsed: :elapsed', { complete: '=', incomplete: ' ', clear: true, width: 40, total: 1000 // Total sprites to parse } )); // Create exporting progress bar bars.push(mbars.newBar( 'Exporting: [:bar] :percent | ETA: :eta | Time Elapsed: :elapsed', { complete: '=', incomplete: ' ', clear: true, width: 40, total: 950 // Actual sprites exported (excluding empty ones) } )); // Update progress bars[0].tick(); // Increment parsing bar bars[1].tick(); // Increment exporting bar // Output displays as: // Parsing: [=========> ] 45% | ETA: 5s | Time Elapsed: 4s // Exporting: [===== ] 23% | ETA: 8s | Time Elapsed: 2s ``` -------------------------------- ### Create Base 32x32 PNG Image with Pink Background in Node.js Source: https://context7.com/tibiajs/sprites-extractor/llms.txt This JavaScript function creates a blank 32x32 PNG image with a solid pink background (RGB: 255,0,255). This serves as the canvas for drawing extracted sprite pixels. It utilizes the `pngjs-image` library. The function returns an object containing the sprite ID and the image object. ```javascript const PNGImage = require('pngjs-image'); const baseColor = {red: 255, green: 0, blue: 255, alpha: 255}; function base(spriteId) { // Create 32x32 image var image = PNGImage.createImage(32, 32); // Fill with pink background for(var i = 0; i < 32; i++) { for(var j = 0; j < 32; j++) { image.setPixel(i, j, baseColor); } } return {filename: spriteId, img: image}; } // Usage var spriteObj = base(123); // Creates base for sprite ID 123 console.log(spriteObj.filename); // Output: 123 // spriteObj.img is a 32x32 PNGImage object ``` -------------------------------- ### Asynchronous Image Export using async Queue Source: https://context7.com/tibiajs/sprites-extractor/llms.txt Exports sprite images to PNG files using an asynchronous queue to manage concurrency and prevent memory overflow. The `async.queue` function limits simultaneous export operations, and a `drain` event signals completion. ```javascript const async = require('async'); // Export function function exportImg(obj, cb) { obj.img.writeImage('./out/' + obj.filename + '.png', function() { cb(); // Signal completion to async queue // Optional: update progress bar }); } // Create queue with concurrency limit of 10 var queue = async.queue(exportImg, 10); // Handle queue completion queue.drain = function() { console.log("All sprites exported successfully"); }; // Add sprites to export queue for(var i = 1; i <= 100; i++) { var spriteObj = base(i); // ... populate sprite with pixel data ... queue.push(spriteObj); } // Queue automatically processes up to 10 exports simultaneously ``` -------------------------------- ### Read and Parse Tibia .spr File Structure in Node.js Source: https://context7.com/tibiajs/sprites-extractor/llms.txt This JavaScript code demonstrates how to read a Tibia .spr file using Node.js's `fs` module and parse its binary structure. It reads the file signature, total sprite count, and iterates through sprite addresses to locate pixel data. Dependencies include `fs` and `buffer-reader`. ```javascript const fs = require('fs'); const BufferReader = require('buffer-reader'); // Read sprite file fs.readFile('Tibia.spr', function (err, buffer) { if (err) throw err; var reader = new BufferReader(buffer); // Parse header var info = { signature: reader.nextUInt32LE(), // File signature identifier size: reader.nextUInt16LE(), // Total number of sprites }; console.log("Signature: " + info.signature); console.log("Sprites: " + info.size); // Iterate through each sprite for(var spriteId = 1; spriteId < info.size; spriteId++) { // Calculate address offset: 6 bytes header + (spriteId - 1) * 4 bytes per entry var formula = 6 + (spriteId - 1) * 4; reader.seek(formula); var address = reader.nextUInt32LE(); if (address == 0) { // Empty sprite, skip to next continue; } // Seek to sprite data location reader.seek(address); // Skip 3-byte color key reader.move(3); // Read data length and calculate end offset var offset = reader.tell() + reader.nextUInt16LE(); // Process sprite pixels... } }); ``` -------------------------------- ### Decode Run-Length Encoded Sprite Pixel Data in Node.js Source: https://context7.com/tibiajs/sprites-extractor/llms.txt This JavaScript snippet decodes run-length encoded pixel data for Tibia sprites. It reads sequences of transparent pixels and colored pixels from the sprite file buffer. The decoded pixel data is then used to set pixels on a PNG image object. This process assumes the `reader` is positioned correctly within the sprite data and `offset` is defined. ```javascript // Assuming reader is positioned at sprite pixel data and obj is base image var currentPixel = 0; var size = 32; // Read until reaching the calculated offset while(reader.tell() < offset) { // Read number of transparent pixels to skip var transparentPixels = reader.nextUInt16LE(); // Read number of colored pixels to process var coloredPixels = reader.nextUInt16LE(); // Skip transparent pixels currentPixel += transparentPixels; // Process colored pixels for (var i = 0; i < coloredPixels; i++) { var x = parseInt(currentPixel % size); var y = parseInt(currentPixel / size); // Read RGB values (3 bytes) var color = { red: reader.nextUInt8(), green: reader.nextUInt8(), blue: reader.nextUInt8(), alpha: 255 }; obj.img.setPixel(x, y, color); currentPixel++; } } // obj.img now contains the complete sprite ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.