16a23e08bSopenharmony_ci/*
26a23e08bSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
36a23e08bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
46a23e08bSopenharmony_ci * you may not use this file except in compliance with the License.
56a23e08bSopenharmony_ci * You may obtain a copy of the License at
66a23e08bSopenharmony_ci *
76a23e08bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
86a23e08bSopenharmony_ci *
96a23e08bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
106a23e08bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
116a23e08bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
126a23e08bSopenharmony_ci * See the License for the specific language governing permissions and
136a23e08bSopenharmony_ci * limitations under the License.
146a23e08bSopenharmony_ci */
156a23e08bSopenharmony_ci
166a23e08bSopenharmony_ciconst Jimp = require('jimp');
176a23e08bSopenharmony_ciconst fs = require('fs');
186a23e08bSopenharmony_ciconst _path = require('path');
196a23e08bSopenharmony_ci/**
206a23e08bSopenharmony_ci * Find all image paths in png、jpg、bmp、jpeg format in the directory.
216a23e08bSopenharmony_ci * @param {String} imgPath The path of build folder.
226a23e08bSopenharmony_ci * @return {Array} Image path array.
236a23e08bSopenharmony_ci */
246a23e08bSopenharmony_ciasync function img2bin(imgPath) {
256a23e08bSopenharmony_ci  try {
266a23e08bSopenharmony_ci    const image = await Jimp.read(imgPath);
276a23e08bSopenharmony_ci    const HEAD_SIZE = 8;
286a23e08bSopenharmony_ci    const PIXEL_SIZE = 4;// BRGA
296a23e08bSopenharmony_ci    const DATA_SIZE = image.bitmap.width * image.bitmap.height * PIXEL_SIZE;
306a23e08bSopenharmony_ci    const binSize = HEAD_SIZE + DATA_SIZE;
316a23e08bSopenharmony_ci    const binBuffer = new ArrayBuffer(binSize);
326a23e08bSopenharmony_ci    const binView = new DataView(binBuffer);
336a23e08bSopenharmony_ci
346a23e08bSopenharmony_ci    const COLOR_MODE = 1 << 8 + 0;
356a23e08bSopenharmony_ci    const WIDTH_BIT_OFFSET = 0;
366a23e08bSopenharmony_ci    const HEIGHT_BIT_OFFSET = 16;
376a23e08bSopenharmony_ci    const header = (image.bitmap.width << WIDTH_BIT_OFFSET) +
386a23e08bSopenharmony_ci    (image.bitmap.height << HEIGHT_BIT_OFFSET);
396a23e08bSopenharmony_ci
406a23e08bSopenharmony_ci    let binFileOffset = 0;
416a23e08bSopenharmony_ci    binView.setUint32(binFileOffset, COLOR_MODE, true);
426a23e08bSopenharmony_ci    binFileOffset += 4;
436a23e08bSopenharmony_ci    binView.setUint32(binFileOffset, header, true);
446a23e08bSopenharmony_ci    binFileOffset += 4;
456a23e08bSopenharmony_ci
466a23e08bSopenharmony_ci    image.scan(0, 0, image.bitmap.width, image.bitmap.height, function(x, y, idx) {
476a23e08bSopenharmony_ci      // eslint-disable-next-line no-invalid-this
486a23e08bSopenharmony_ci      const blue = this.bitmap.data[idx + 2];
496a23e08bSopenharmony_ci      binView.setUint8(binFileOffset, blue, true);
506a23e08bSopenharmony_ci      binFileOffset += 1;
516a23e08bSopenharmony_ci
526a23e08bSopenharmony_ci      // eslint-disable-next-line no-invalid-this
536a23e08bSopenharmony_ci      const green = this.bitmap.data[idx + 1];
546a23e08bSopenharmony_ci      binView.setUint8(binFileOffset, green, true);
556a23e08bSopenharmony_ci      binFileOffset += 1;
566a23e08bSopenharmony_ci
576a23e08bSopenharmony_ci      // eslint-disable-next-line no-invalid-this
586a23e08bSopenharmony_ci      const red = this.bitmap.data[idx + 0];
596a23e08bSopenharmony_ci      binView.setUint8(binFileOffset, red, true);
606a23e08bSopenharmony_ci      binFileOffset += 1;
616a23e08bSopenharmony_ci
626a23e08bSopenharmony_ci      // eslint-disable-next-line no-invalid-this
636a23e08bSopenharmony_ci      const alpha = this.bitmap.data[idx + 3];
646a23e08bSopenharmony_ci      binView.setUint8(binFileOffset, alpha, true);
656a23e08bSopenharmony_ci      binFileOffset += 1;
666a23e08bSopenharmony_ci    });
676a23e08bSopenharmony_ci    if (process.env.PLATFORM_VERSION_VERSION <=6) {
686a23e08bSopenharmony_ci      const binPath1 = imgPath.replace(/(\.png|\.jpg|\.bmp|\.jpeg|\.BMP|\.JPG|\.PNG|\.JPEG)$/, '.bin');
696a23e08bSopenharmony_ci      fs.writeFileSync(binPath1, Buffer.from(binBuffer));
706a23e08bSopenharmony_ci    }
716a23e08bSopenharmony_ci    const binPath2 = imgPath+".bin";
726a23e08bSopenharmony_ci    fs.writeFileSync(binPath2, Buffer.from(binBuffer));
736a23e08bSopenharmony_ci  } catch (err) {
746a23e08bSopenharmony_ci    const imageName = _path.basename(imgPath);
756a23e08bSopenharmony_ci    console.error('\u001b[31m', `Failed to convert image ${imageName}.`, '\u001b[39m');
766a23e08bSopenharmony_ci    throw err;
776a23e08bSopenharmony_ci  }
786a23e08bSopenharmony_ci}
796a23e08bSopenharmony_ci
806a23e08bSopenharmony_cimodule.exports = img2bin;
81