diff --git a/ESP32_AP-Flasher/src/makeimage.cpp b/ESP32_AP-Flasher/src/makeimage.cpp index caebf706..92332869 100644 --- a/ESP32_AP-Flasher/src/makeimage.cpp +++ b/ESP32_AP-Flasher/src/makeimage.cpp @@ -408,6 +408,11 @@ void spr2buffer(TFT_eSprite &spr, String &fileout, imgParam &imageParams) { rewriteHeader(f_out); } else if (imageParams.g5) { // handling for G5-compressed image data + + uint8_t headerbuf[6]; + prepareHeader(headerbuf, bufw, bufh, imageParams, buffer_size); + f_out.write(headerbuf, sizeof(headerbuf)); + uint16_t height = imageParams.height; // spr.height(); uint16_t width = imageParams.width; spr.width(); @@ -446,11 +451,12 @@ void spr2buffer(TFT_eSprite &spr, String &fileout, imgParam &imageParams) { printf("Compressed %d to %d bytes\n", buffer_size, outbufferSize); if (outbufferSize > buffer_size) { printf("That wasn't very useful, falling back to raw\n"); - free(outBuffer); compressionSuccessful = false; + f_out.seek(0); } else { f_out.write(outBuffer, outbufferSize); } + free(outBuffer); } if (!compressionSuccessful) { // if we failed to compress the image, or the resulting image was larger than a raw file, fallback diff --git a/ESP32_AP-Flasher/wwwroot/g5decoder.js b/ESP32_AP-Flasher/wwwroot/g5decoder.js index e1146f16..f54cdf4a 100644 --- a/ESP32_AP-Flasher/wwwroot/g5decoder.js +++ b/ESP32_AP-Flasher/wwwroot/g5decoder.js @@ -143,8 +143,6 @@ function G5DrawLine(pPage, pCurFlips, pOut) { let visibleX = Math.max(0, startX); let visibleRun = Math.min(xright, startX + run) - visibleX; - if (pPage.y > 580) console.log("line " + visibleX + "-" + visibleRun); - if (visibleRun > 0) { const startByte = visibleX >> 3; const endByte = (visibleX + visibleRun) >> 3; @@ -221,13 +219,10 @@ function DecodeLine(pPage) { if (ulBitOff > (REGISTER_WIDTH - 8)) { pBufIndex += (ulBitOff >> 3); ulBitOff &= 7; - if (pPage.y > 580) console.log("ix: " + pBufIndex + " bit " + ulBitOff); ulBits = TIFFMOTOLONG(pBuf, pBufIndex); } - if (pPage.y > 580) console.log(ulBits.toString(2).padStart(32, '0') + " offset " + ulBitOff); if (((ulBits << ulBitOff) & 0x80000000) !== 0) { - if (pPage.y > 580) console.log("1> a0: " + pRef[pRefIndex] + " prefindex " + pRefIndex); a0 = pRef[pRefIndex++]; pCur[pCurIndex++] = a0; ulBitOff++; @@ -235,22 +230,17 @@ function DecodeLine(pPage) { const lBits = (ulBits >> (REGISTER_WIDTH - 8 - ulBitOff)) & 0xfe; const sCode = code_table[lBits]; ulBitOff += code_table[lBits + 1]; - if (pPage.y > 580) console.log("s: " + sCode); switch (sCode) { case 1: case 2: case 3: // V(-1), V(-2), V(-3) a0 = pRef[pRefIndex] - sCode; // A0 = B1 - x - if (pPage.y > 580) console.log("01> a0: " + pRef[pRefIndex] + " scode " + sCode + " prefindex " + pRefIndex); pCur[pCurIndex++] = a0; if (pRefIndex == 0) { pRefIndex += 2; - if (pPage.y > 580) console.log("01> =0 new prefindex " + pRefIndex); } pRefIndex--; while (a0 >= pRef[pRefIndex]) { pRefIndex += 2; - if (pPage.y > 580) console.log("01> >= new prefindex " + pRefIndex); } - if (pPage.y > 580) console.log("01> new prefindex " + pRefIndex); break; case 0x11: case 0x12: case 0x13: // V(1), V(2), V(3) @@ -313,7 +303,6 @@ function DecodeLine(pPage) { ulBitOff += u32HLen; break; } - if (pPage.y > 580) console.log("H: " + tot_run + " - " + tot_run1); a0 = a0_p + tot_run; pCur[pCurIndex++] = a0; a0 += tot_run1; @@ -332,7 +321,6 @@ function DecodeLine(pPage) { break; default: // ERROR - console.log("scode " + sCode); pPage.iError = G5_DECODE_ERROR; return pPage.iError; break; @@ -365,7 +353,6 @@ function processG5(data, width, height) { let outputBuffer = new Uint8Array(height * ((width + 7) >> 3)); // Adjust for byte alignment for (let y = 0; y < height; y++) { - console.log(y + " " + decoder.pBufIndex + " " + data.length); let lineBuffer = outputBuffer.subarray(y * ((width + 7) >> 3), (y + 1) * ((width + 7) >> 3)); decoder.y = y; let decodeResult = DecodeLine(decoder); @@ -374,14 +361,12 @@ function processG5(data, width, height) { console.log("Decoding error on line " + y + ": " + decoder.iError); } - if (y > 580) console.log("draw line " + y); G5DrawLine(decoder, decoder.pCur, lineBuffer); const temp = decoder.pRef; decoder.pRef = decoder.pCur; decoder.pCur = temp; } - console.log("Decoding complete."); return outputBuffer; } catch (error) { console.error("Error during G5 decoding:", error.message); diff --git a/ESP32_AP-Flasher/wwwroot/main.js b/ESP32_AP-Flasher/wwwroot/main.js index 86a2510b..ce2b9aae 100644 --- a/ESP32_AP-Flasher/wwwroot/main.js +++ b/ESP32_AP-Flasher/wwwroot/main.js @@ -1208,9 +1208,14 @@ function drawCanvas(buffer, canvas, hwtype, tagmac, doRotate) { data = processZlib(data); } if (data.length > 0 && tagTypes[hwtype].g5 > 0 && $('#tag' + tagmac).dataset.ver >= tagTypes[hwtype].g5) { - console.log("calling G5 decoder"); - console.log(tagTypes[hwtype].width); - data = processG5(data, tagTypes[hwtype].height, tagTypes[hwtype].width * 2); + const headerSize = data[0]; + let bufw = (data[2] << 8) | data[1]; + let bufh = (data[4] << 8) | data[3]; + if ((bufw == tagTypes[hwtype].width || bufw == tagTypes[hwtype].height) && (bufh == tagTypes[hwtype].width || bufh == tagTypes[hwtype].height) && (data[5] <= 3)) { + // valid header for g5 compression + if (data[5] == 2) bufh *= 2; + data = processG5(data.subarray(headerSize), bufw, bufh); + } } [canvas.width, canvas.height] = [tagTypes[hwtype].width, tagTypes[hwtype].height] || [0, 0];