File indexing completed on 2025-01-05 03:56:49

0001 /* -*- C++ -*-
0002  * File: raw2text.cpp
0003  * Copyright 2008-2021 LibRaw LLC (info@libraw.org)
0004  * Created: Sun Sept 01, 2020
0005  *
0006  * LibRaw sample
0007  * Dumps (small) selection of RAW data to text file
0008  *
0009 
0010 LibRaw is free software; you can redistribute it and/or modify
0011 it under the terms of the one of two licenses as you choose:
0012 
0013 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
0014    (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
0015 
0016 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
0017    (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
0018 
0019 
0020  */
0021 #include <stdio.h>
0022 #include <string.h>
0023 #include <math.h>
0024 #ifndef WIN32
0025 #include <netinet/in.h>
0026 #else
0027 #include <winsock2.h>
0028 #endif
0029 
0030 #include "libraw/libraw.h"
0031 
0032 void usage(const char *av)
0033 {
0034     printf(
0035         "Dump (small) selection of RAW file as tab-separated text file\n"
0036         "Usage: %s inputfile COL ROW [CHANNEL] [width] [height]\n"
0037         "  COL - start column\n"
0038         "  ROW - start row\n"
0039         "  CHANNEL - raw channel to dump, default is 0 (red for rggb)\n"
0040         "  width - area width to dump, default is 16\n"
0041         "  height - area height to dump, default is 4\n"
0042         , av);
0043 }
0044 
0045 unsigned subtract_bl(unsigned int val, int bl)
0046 {
0047     return val > (unsigned)bl ? val - (unsigned)bl : 0;
0048 }
0049 
0050 class LibRaw_bl : public LibRaw
0051 {
0052     public:
0053         void adjust_blacklevel() { LibRaw::adjust_bl(); }
0054 };
0055 
0056 int main(int ac, char *av[])
0057 {
0058     if (ac < 4)
0059     {
0060         usage(av[0]);
0061         exit(1);
0062     }
0063     int colstart = atoi(av[2]);
0064     int rowstart = atoi(av[3]);
0065     int channel = 0;
0066     if (ac > 4) channel = atoi(av[4]);
0067     int width = 16;
0068     if (ac > 5) width = atoi(av[5]);
0069     int height = 4;
0070     if (ac > 6) height = atoi(av[6]);
0071     if (width <1 || height<1)
0072     {
0073         usage(av[0]);
0074         exit(1);
0075     }
0076 
0077     LibRaw_bl lr;
0078 
0079     if (lr.open_file(av[1]) != LIBRAW_SUCCESS)
0080     {
0081         fprintf(stderr, "Unable to open file %s\n", av[1]);
0082         exit(1);
0083     }
0084     if ((lr.imgdata.idata.colors == 1 && channel>0) || (channel >3))
0085     {
0086         fprintf(stderr, "Incorrect CHANNEL specified: %d\n", channel);
0087         exit(1);
0088     }
0089     if (lr.unpack() != LIBRAW_SUCCESS)
0090     {
0091         fprintf(stderr, "Unable to unpack raw data from %s\n", av[1]);
0092         exit(1);
0093     }
0094     lr.adjust_blacklevel();
0095     printf("%s\t%d-%d-%dx%d\tchannel: %d\n", av[1], colstart, rowstart, width, height, channel);
0096 
0097     printf("%6s", "R\\C");
0098     for (int col = colstart; col < colstart + width && col < lr.imgdata.sizes.raw_width; col++)
0099         printf("%6u", col);
0100     printf("\n");
0101 
0102     if (lr.imgdata.rawdata.raw_image)
0103     {
0104         for (int row = rowstart; row < rowstart + height && row < lr.imgdata.sizes.raw_height; row++)
0105         {
0106             unsigned rcolors[48];
0107             if (lr.imgdata.idata.colors > 1)
0108                 for (int c = 0; c < 48; c++)
0109                     rcolors[c] = lr.COLOR(row, c);
0110             else
0111                 memset(rcolors, 0, sizeof(rcolors));
0112             unsigned short *rowdata = &lr.imgdata.rawdata.raw_image[row * lr.imgdata.sizes.raw_pitch / 2];
0113             printf("%6u", row);
0114             for (int col = colstart; col < colstart + width && col < lr.imgdata.sizes.raw_width; col++)
0115                 if (rcolors[col % 48] == (unsigned)channel) printf("%6u", subtract_bl(rowdata[col],lr.imgdata.color.cblack[channel]));
0116                 else printf("     -");
0117             printf("\n");
0118         }
0119     }
0120     else if (lr.imgdata.rawdata.color4_image && channel < 4)
0121     {
0122         for (int row = rowstart; row < rowstart + height && row < lr.imgdata.sizes.raw_height; row++)
0123         {
0124             unsigned short(*rowdata)[4] = &lr.imgdata.rawdata.color4_image[row * lr.imgdata.sizes.raw_pitch / 8];
0125             printf("%6u", row);
0126             for (int col = colstart; col < colstart + width && col < lr.imgdata.sizes.raw_width; col++)
0127                 printf("%6u", subtract_bl(rowdata[col][channel],lr.imgdata.color.cblack[channel]));
0128             printf("\n");
0129         }
0130     }
0131     else if (lr.imgdata.rawdata.color3_image && channel < 3)
0132     {
0133         for (int row = rowstart; row < rowstart + height && row < lr.imgdata.sizes.raw_height; row++)
0134         {
0135             unsigned short(*rowdata)[3] = &lr.imgdata.rawdata.color3_image[row * lr.imgdata.sizes.raw_pitch / 6];
0136             printf("%6u", row);
0137             for (int col = colstart; col < colstart + width && col < lr.imgdata.sizes.raw_width; col++)
0138                 printf("%6u", subtract_bl(rowdata[col][channel],lr.imgdata.color.cblack[channel]));
0139             printf("\n");
0140         }
0141     }
0142     else
0143         printf("Unsupported file data (e.g. floating point format), or incorrect channel specified\n");
0144 }