/* compile with: * gcc -o pgm2dng -m32 pgm2dng.c /path/to/magic-lantern/src/chdk-dng.c -I/path/to/magic-lantern/src -lm */ /* * Copyright (C) 2013 Magic Lantern Team * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "stdint.h" #include "stdio.h" #include "stdlib.h" #include "string.h" #include "math.h" #include "raw.h" #include "chdk-dng.h" struct raw_info raw_info; #define FAIL(fmt,...) { fprintf(stderr, "Error: "); fprintf(stderr, fmt, ## __VA_ARGS__); fprintf(stderr, "\n"); exit(1); } #define CHECK(ok, fmt,...) { if (!ok) FAIL(fmt, ## __VA_ARGS__); } void raw_set_geometry(int width, int height, int skip_left, int skip_right, int skip_top, int skip_bottom) { raw_info.width = width; raw_info.height = height; raw_info.pitch = raw_info.width * raw_info.bits_per_pixel / 8; raw_info.frame_size = raw_info.height * raw_info.pitch; raw_info.active_area.x1 = skip_left; raw_info.active_area.y1 = skip_top; raw_info.active_area.x2 = raw_info.width - skip_right; raw_info.active_area.y2 = raw_info.height - skip_bottom; raw_info.jpeg.x = 0; raw_info.jpeg.y = 0; raw_info.jpeg.width = raw_info.width - skip_left - skip_right; raw_info.jpeg.height = raw_info.height - skip_top - skip_bottom; } //~ { "Nikon D5100", 0, 0x3de6, //~ { 8198,-2239,-724,-4871,12389,2798,-1043,2050,7181 } }, //~ { "Canon EOS 5D Mark III", 0, 0x3c80, //~ { 6722,-635,-963,-4287,12460,2028,-908,2162,5668 } }, #define CAM_COLORMATRIX1 \ 6722, 10000, -635, 10000, -963, 10000, \ -4287, 10000, 12460, 10000, 2028, 10000, \ -908, 10000, 2162, 10000, 5668, 10000 struct raw_info raw_info = { .api_version = 1, .bits_per_pixel = 16, .black_level = 2048, .white_level = 16382, .cfa_pattern = 0x02010100, // Red Green Green Blue .calibration_illuminant1 = 1, // Daylight .color_matrix1 = {CAM_COLORMATRIX1},// camera-specific, from dcraw.c }; int main(int argc, char** argv) { FILE* fp = fopen(argv[1], "rb"); CHECK(fp, "could not open %s", argv[1]); /* PGM read code from dcraw */ int dim[3]={0,0,0}, comment=0, number=0, error=0, nd=0, c; if (fgetc(fp) != 'P' || fgetc(fp) != '5') error = 1; while (!error && nd < 3 && (c = fgetc(fp)) != EOF) { if (c == '#') comment = 1; if (c == '\n') comment = 0; if (comment) continue; if (isdigit(c)) number = 1; if (number) { if (isdigit(c)) dim[nd] = dim[nd]*10 + c -'0'; else if (isspace(c)) { number = 0; nd++; } else error = 1; } } CHECK(!(error || nd < 3), "not a valid PGM file\n"); int width = dim[0]; int height = dim[1]; raw_set_geometry(width, height, 0, 0, 0, 0); printf("Resolution : %d x %d\n", raw_info.width, raw_info.height); printf("Frame size : %d bytes\n", raw_info.frame_size); printf("Black level : %d\n", raw_info.black_level); printf("White level : %d\n", raw_info.white_level); char* raw = malloc(raw_info.frame_size); CHECK(raw, "malloc"); raw_info.buffer = raw; int size = fread(raw, 1, width * height * 2, fp); CHECK(size == width * height * 2, "fread"); fclose(fp); char fo[50]; int n = snprintf(fo, sizeof(fo), "%s", argv[1]); fo[n-3] = 'D'; fo[n-2] = 'N'; fo[n-1] = 'G'; printf("Output file : %s\n", fo); save_dng(fo, &raw_info); printf("Done.\n"); return 0; } int raw_get_pixel(int x, int y) { /* fixme: return valid values here to create a thumbnail */ return 0; }