Initial GXFP5130 userspace prototype
This commit is contained in:
222
tools/capture_to_pgm.c
Normal file
222
tools/capture_to_pgm.c
Normal file
@@ -0,0 +1,222 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
/*
|
||||
* Experimental offline decoder only.
|
||||
* The 12-bit unpack and transpose candidates are adapted from Metrohan's
|
||||
* gxfp5130-linux decoder. They are not part of the capture/library path and
|
||||
* are not yet verified for this 0x2504 ChicagoHU device.
|
||||
*/
|
||||
#include <errno.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define IMAGE_OFFSET 8u
|
||||
#define PACKED_LENGTH 7680u
|
||||
#define CRC_LENGTH 4u
|
||||
#define SAMPLE_COUNT 5120u
|
||||
#define RAW_WIDTH 64u
|
||||
#define RAW_HEIGHT 80u
|
||||
#define TRANSPOSED_WIDTH 80u
|
||||
#define TRANSPOSED_HEIGHT 64u
|
||||
#define SAMPLE_MAX 4095u
|
||||
|
||||
static uint16_t get_le16(const uint8_t *p)
|
||||
{
|
||||
return (uint16_t)p[0] | ((uint16_t)p[1] << 8);
|
||||
}
|
||||
|
||||
static uint32_t crc32_mpeg2(const uint8_t *data, size_t length)
|
||||
{
|
||||
uint32_t crc = UINT32_MAX;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < length; i++) {
|
||||
unsigned int bit;
|
||||
|
||||
crc ^= (uint32_t)data[i] << 24;
|
||||
for (bit = 0; bit < 8; bit++)
|
||||
crc = (crc & UINT32_C(0x80000000))
|
||||
? (crc << 1) ^ UINT32_C(0x04c11db7)
|
||||
: crc << 1;
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
static uint32_t get_goodix_crc(const uint8_t *p)
|
||||
{
|
||||
return ((uint32_t)p[2] << 24) | ((uint32_t)p[3] << 16) |
|
||||
((uint32_t)p[0] << 8) | (uint32_t)p[1];
|
||||
}
|
||||
|
||||
static int read_file(const char *path, uint8_t **out, size_t *out_length)
|
||||
{
|
||||
FILE *file = fopen(path, "rb");
|
||||
long end;
|
||||
uint8_t *data;
|
||||
|
||||
if (!file)
|
||||
return -errno;
|
||||
if (fseek(file, 0, SEEK_END) != 0 || (end = ftell(file)) < 0 ||
|
||||
fseek(file, 0, SEEK_SET) != 0) {
|
||||
fclose(file);
|
||||
return -EIO;
|
||||
}
|
||||
data = malloc(end > 0 ? (size_t)end : 1u);
|
||||
if (!data) {
|
||||
fclose(file);
|
||||
return -ENOMEM;
|
||||
}
|
||||
if (end > 0 && fread(data, 1, (size_t)end, file) != (size_t)end) {
|
||||
free(data);
|
||||
fclose(file);
|
||||
return -EIO;
|
||||
}
|
||||
if (fclose(file) != 0) {
|
||||
free(data);
|
||||
return -EIO;
|
||||
}
|
||||
*out = data;
|
||||
*out_length = (size_t)end;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void unpack_four(const uint8_t *p, uint16_t out[4])
|
||||
{
|
||||
out[0] = (uint16_t)(((p[0] & 0x0fu) << 8) | p[1]);
|
||||
out[1] = (uint16_t)((p[3] << 4) | (p[0] >> 4));
|
||||
out[2] = (uint16_t)(((p[5] & 0x0fu) << 8) | p[2]);
|
||||
out[3] = (uint16_t)((p[4] << 4) | (p[5] >> 4));
|
||||
}
|
||||
|
||||
static int write_pgm(const char *path, const uint16_t *pixels,
|
||||
size_t width, size_t height, int inverted)
|
||||
{
|
||||
FILE *file = fopen(path, "wb");
|
||||
size_t i;
|
||||
|
||||
if (!file)
|
||||
return -errno;
|
||||
if (fprintf(file, "P5\n%zu %zu\n%u\n",
|
||||
width, height, SAMPLE_MAX) < 0) {
|
||||
fclose(file);
|
||||
return -EIO;
|
||||
}
|
||||
for (i = 0; i < width * height; i++) {
|
||||
uint16_t value = inverted
|
||||
? (uint16_t)(SAMPLE_MAX - pixels[i])
|
||||
: pixels[i];
|
||||
uint8_t encoded[2] = {
|
||||
(uint8_t)(value >> 8), (uint8_t)value
|
||||
};
|
||||
|
||||
if (fwrite(encoded, 1, sizeof(encoded), file) !=
|
||||
sizeof(encoded)) {
|
||||
fclose(file);
|
||||
return -EIO;
|
||||
}
|
||||
}
|
||||
return fclose(file) == 0 ? 0 : -EIO;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
const char *path = argc > 1 ? argv[1] : "capture_plaintext.bin";
|
||||
uint8_t *frame = NULL;
|
||||
size_t frame_length = 0;
|
||||
uint16_t *raw = NULL;
|
||||
uint16_t *transposed = NULL;
|
||||
uint16_t declared;
|
||||
const uint8_t *packed;
|
||||
const uint8_t *crc_bytes;
|
||||
uint32_t calculated_crc;
|
||||
uint32_t stored_crc;
|
||||
size_t offset;
|
||||
size_t sample = 0;
|
||||
int ret;
|
||||
|
||||
ret = read_file(path, &frame, &frame_length);
|
||||
if (ret) {
|
||||
fprintf(stderr, "%s: %s\n", path, strerror(-ret));
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (frame_length < 3u) {
|
||||
fprintf(stderr, "input is shorter than the frame header\n");
|
||||
ret = -EBADMSG;
|
||||
goto out;
|
||||
}
|
||||
declared = get_le16(frame + 1);
|
||||
if (frame[0] != 0x20u || frame_length != 3u + declared ||
|
||||
frame_length < IMAGE_OFFSET + PACKED_LENGTH + CRC_LENGTH + 1u) {
|
||||
fprintf(stderr,
|
||||
"unexpected frame: cmd=0x%02x declared=%u size=%zu\n",
|
||||
frame[0], declared, frame_length);
|
||||
ret = -EBADMSG;
|
||||
goto out;
|
||||
}
|
||||
|
||||
packed = frame + IMAGE_OFFSET;
|
||||
crc_bytes = packed + PACKED_LENGTH;
|
||||
calculated_crc = crc32_mpeg2(packed, PACKED_LENGTH);
|
||||
stored_crc = get_goodix_crc(crc_bytes);
|
||||
if (calculated_crc != stored_crc) {
|
||||
fprintf(stderr,
|
||||
"image CRC mismatch: calculated=%08x stored=%08x\n",
|
||||
calculated_crc, stored_crc);
|
||||
ret = -EBADMSG;
|
||||
goto out;
|
||||
}
|
||||
|
||||
raw = calloc(SAMPLE_COUNT, sizeof(*raw));
|
||||
transposed = calloc(SAMPLE_COUNT, sizeof(*transposed));
|
||||
if (!raw || !transposed) {
|
||||
ret = -ENOMEM;
|
||||
goto out;
|
||||
}
|
||||
|
||||
for (offset = 0; offset < PACKED_LENGTH; offset += 6u) {
|
||||
uint16_t values[4];
|
||||
size_t i;
|
||||
|
||||
unpack_four(packed + offset, values);
|
||||
for (i = 0; i < 4u; i++, sample++) {
|
||||
size_t destination;
|
||||
|
||||
raw[sample] = values[i];
|
||||
destination = (sample % RAW_WIDTH) *
|
||||
TRANSPOSED_WIDTH +
|
||||
sample / RAW_WIDTH;
|
||||
transposed[destination] = values[i];
|
||||
}
|
||||
}
|
||||
|
||||
ret = write_pgm("candidate_raw.pgm", raw,
|
||||
RAW_WIDTH, RAW_HEIGHT, 0);
|
||||
if (!ret)
|
||||
ret = write_pgm("candidate_transposed.pgm", transposed,
|
||||
TRANSPOSED_WIDTH, TRANSPOSED_HEIGHT, 0);
|
||||
if (!ret)
|
||||
ret = write_pgm("candidate_inverted.pgm", raw,
|
||||
RAW_WIDTH, RAW_HEIGHT, 1);
|
||||
if (!ret)
|
||||
ret = write_pgm("candidate_transposed_inverted.pgm",
|
||||
transposed, TRANSPOSED_WIDTH,
|
||||
TRANSPOSED_HEIGHT, 1);
|
||||
if (ret) {
|
||||
fprintf(stderr, "PGM write failed: %s\n", strerror(-ret));
|
||||
goto out;
|
||||
}
|
||||
|
||||
printf("Input CRC valid: 0x%08x\n", calculated_crc);
|
||||
printf("Experimental outputs written:\n");
|
||||
printf(" candidate_raw.pgm (64x80)\n");
|
||||
printf(" candidate_transposed.pgm (80x64)\n");
|
||||
printf(" candidate_inverted.pgm (64x80)\n");
|
||||
printf(" candidate_transposed_inverted.pgm (80x64)\n");
|
||||
|
||||
out:
|
||||
free(transposed);
|
||||
free(raw);
|
||||
free(frame);
|
||||
return ret ? EXIT_FAILURE : EXIT_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user