archive/sane_hello.c

138 lines
3.4 KiB
C
Raw Normal View History

2021-11-22 16:13:24 -06:00
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sane/sane.h>
#include <png.h>
typedef struct {
uint8_t red;
uint8_t green;
uint8_t blue;
} pixel_t;
typedef struct {
pixel_t *pixels;
size_t width;
size_t height;
} bitmap_t;
pixel_t *pixel_at(bitmap_t * bitmap, int x, int y) {
return bitmap->pixels + bitmap->width * y + x;
}
int pngtofile(bitmap_t *bitmap, FILE *fp) {
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
size_t x, y;
png_byte **row_pointers = NULL;
int status = -1;
int pixel_size = 3;
int depth = 8;
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
info_ptr = png_create_info_struct(png_ptr);
png_set_IHDR(png_ptr, info_ptr,
bitmap->width, bitmap->height,
depth,
PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
row_pointers = png_malloc(png_ptr, bitmap->height * sizeof (png_byte *));
for(y = 0; y < bitmap->height; y++) {
png_byte *row = png_malloc(png_ptr, sizeof (uint8_t) * bitmap->width * pixel_size);
row_pointers[y] = row;
for(x = 0; x < bitmap->width; x++) {
pixel_t * pixel = pixel_at(bitmap, x, y);
*row++ = pixel->red;
*row++ = pixel->green;
*row++ = pixel->blue;
}
}
png_init_io(png_ptr, fp);
png_set_rows(png_ptr, info_ptr, row_pointers);
png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
status = 0;
return status;
}
int main(void) {
SANE_Status status;
SANE_Option_Descriptor *desc;
SANE_Int vc = SANE_VERSION_CODE(SANE_CURRENT_MAJOR, 1, 10);
SANE_Device **device_list;
SANE_Handle h;
SANE_Parameters p;
SANE_Int cap;
FILE *file = fopen("lsakjfsalkfd.png", "w");
status = sane_init(&vc, NULL);
sane_get_devices(&device_list, SANE_FALSE);
for(int i = 0; device_list[i]; i++) {
printf("%s %s %s %s\n", device_list[i]->name,
device_list[i]->vendor,
device_list[i]->model,
device_list[i]->type);
}
if(device_list[0]) sane_open(device_list[0]->name, &h);
sane_get_parameters(h, &p);
/*
printf("%i %i %i\n", p.pixels_per_line, p.lines, p.format);
*/
const SANE_Byte *storage = malloc(p.bytes_per_line * p.lines);
if(!storage) abort();
desc = sane_get_option_descriptor(h, (SANE_Int)0);
printf("%i\n", desc->cap);
int f;
sane_control_option(h, 0, SANE_ACTION_GET_VALUE, &f, NULL);
cap = f;
for(int i = 0; i < cap; i++) {
desc = sane_get_option_descriptor(h, (SANE_Int)i);
printf("|%i|%s|%s|%s|\n", i, desc->name, desc->title, desc->desc);
}
for(int i = 8; i < 12; i++) {
int hello;
sane_control_option(h, i, SANE_ACTION_GET_VALUE, &hello, NULL);
printf("%i\n", hello);
}
desc = sane_get_option_descriptor(h, 10);
printf("%i\n", desc->unit);
SANE_Int info = 0;
char valone[100];
char valtwo[100];
sane_control_option(h, 10, SANE_ACTION_GET_VALUE, &valone, NULL);
sane_control_option(h, 11, SANE_ACTION_GET_VALUE, &valtwo, NULL);
printf("%s %s\n", valone, valtwo);
// sane_control_option(h,
sane_start(h);
SANE_Int len;
SANE_Byte *buf = storage;
for(;;) {
status = sane_read(h, buf, (SANE_Int)80, (SANE_Int *)&len);
buf += (int)len;
if(status == SANE_STATUS_EOF) break;
}
bitmap_t *bitmap = malloc(sizeof *bitmap);
bitmap->width = p.pixels_per_line;
bitmap->height = p.lines;
bitmap->pixels = (pixel_t *)storage;
pngtofile(bitmap, file);
sane_close(h);
sane_exit();
return 0;
}