pw-guild-icon-parser
Version:
Parser for Perfect World guild icon lists - converts PNG icons to DDS atlas format with DXT5 compression
56 lines • 2.4 kB
JavaScript
import { calculateIconPosition } from '../parser.js';
/**
* Build an empty atlas with the specified dimensions
*/
export function createEmptyAtlas(config) {
const width = config.iconWidth * config.gridWidth;
const height = config.iconHeight * config.gridHeight;
const rgbaData = Buffer.alloc(width * height * 4); // Fill with transparent black
return { width, height, rgbaData };
}
/**
* Copy an icon from source RGBA data to the atlas at the specified position
*/
export function placeIconInAtlas(atlas, iconRgbaData, iconWidth, iconHeight, position) {
const atlasX = position.col * iconWidth;
const atlasY = position.row * iconHeight;
for (let y = 0; y < iconHeight; y++) {
for (let x = 0; x < iconWidth; x++) {
const srcIndex = (y * iconWidth + x) * 4;
const dstIndex = ((atlasY + y) * atlas.width + (atlasX + x)) * 4;
if (dstIndex + 3 < atlas.rgbaData.length && srcIndex + 3 < iconRgbaData.length) {
atlas.rgbaData[dstIndex] = iconRgbaData[srcIndex]; // R
atlas.rgbaData[dstIndex + 1] = iconRgbaData[srcIndex + 1]; // G
atlas.rgbaData[dstIndex + 2] = iconRgbaData[srcIndex + 2]; // B
atlas.rgbaData[dstIndex + 3] = iconRgbaData[srcIndex + 3]; // A
}
}
}
}
/**
* Copy existing atlas data (from DDS) to new atlas
*/
export function copyExistingAtlas(existingAtlas, newAtlas) {
const minWidth = Math.min(existingAtlas.width, newAtlas.width);
const minHeight = Math.min(existingAtlas.height, newAtlas.height);
const minLength = minWidth * minHeight * 4;
const copyLength = Math.min(existingAtlas.rgbaData.length, minLength);
existingAtlas.rgbaData.copy(newAtlas.rgbaData, 0, 0, copyLength);
}
/**
* Build atlas from existing DDS and add new icon
*/
export function buildAtlasWithNewIcon(existingAtlas, newIconRgbaData, config, newIconIndex) {
// Create new atlas
const atlas = createEmptyAtlas(config);
// Copy existing data if available
if (existingAtlas) {
copyExistingAtlas(existingAtlas, atlas);
}
// Calculate position for new icon
const position = calculateIconPosition(newIconIndex, config.gridWidth);
// Place new icon
placeIconInAtlas(atlas, newIconRgbaData, config.iconWidth, config.iconHeight, position);
return atlas;
}
//# sourceMappingURL=builder.js.map