home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Graphics / Graphics.zip / kdc2jpg.zip / kdc2jpg.c next >
Text File  |  1998-02-01  |  3KB  |  135 lines

  1. /*
  2.  * kdc2jpg.c - Converts a compressed DC-120 .kdc file to a .jpg file
  3.  *
  4.  * The data in the .kdc file is actually just a JPEG file starting
  5.  * at offset 15680.  The JPEG data is stored as an 848x488 image.
  6.  * with 100 dots per inch horizontally and 75 dots per inch
  7.  * vertically.  This utility just strips off the .KDC header and
  8.  * adds a JPEG JFIF header.
  9.  *
  10.  * The CCD data that was used to create the JPEG data is an array
  11.  * of 848x976 pixels, organized as:
  12.  *
  13.  *   GRGRGR (R is red, G is green, B is blue)
  14.  *   BGBGBG
  15.  *   GRGRGR
  16.  *   BGBGBG
  17.  *
  18.  * The camera combines each pair of CCD rows and then JPEG
  19.  * compresses it.  To accurately reconstruct the image from the
  20.  * JPEG data, the 488 lines need to be split apart into 976
  21.  * rows in the original CCD Bayer pattern.  Then a linear
  22.  * interpolation needs to be applied to construct a full-resolution
  23.  * image (along with a factor of 1.5 stretching horizontally).
  24.  *
  25.  * Author  : Ed Hamrick
  26.  * Date    : October 24, 1997
  27.  * See also: http://www.hamrick.com/
  28.  */
  29.  
  30. /*
  31.  * Further modifications by Stephane Charette, charette@writeme.com
  32.  * 1998Feb01
  33.  * compiled for OS/2 and uploaded to the internet as freeware, SC, 1998Feb01
  34.  */
  35.  
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39.  
  40. unsigned char jpghead[] = {
  41.   0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46,
  42.   0x49, 0x46, 0x00, 0x01, 0x02, 0x01, 0x00, 0x64,
  43.   0x00, 0x4B, 0x00, 0x00
  44. };
  45.  
  46. #define CCDOFF 15680
  47.  
  48. unsigned char hdr[CCDOFF];
  49.  
  50. void main(int argc, char *argv[])
  51. {
  52.   FILE *kdc;
  53.   FILE *jpg;
  54.  
  55.   char name[512];
  56.  
  57.   fprintf(stdout, "\n"
  58.                   "KDC2JPG - convert Kodak's DC120 .KDC file format to .JPG\n"
  59.                   "Based on code by Ed Hamrick, http://www.hamrick.com\n"
  60.                   "OS/2 version compiled by Stéphane Charette, charette@writeme.com\n\n" );
  61.  
  62.   /* Make sure called with only one argument */
  63.   if (argc != 2)
  64.   {
  65.     fprintf(stdout,  "Usage: kdc2jpg filename\n\n"
  66.                      "Note that you must not specify the file extension,\n"
  67.                      "and that only compressed .KDC files are supported.\n");
  68.     return;
  69.   }
  70.  
  71.   /* Open the .kdc file */
  72.   strcpy(name, argv[1]);
  73.   strcat(name, ".kdc");
  74.   fprintf(stdout, "...reading %s...\n", name );
  75.   kdc = fopen(name,"rb");
  76.   if (!kdc)
  77.   {
  78.     fprintf(stderr,"Error: can't open %s\n",name);
  79.     return;
  80.   }
  81.  
  82.   /* Verify that it's a compressed .kdc file */
  83.   fread(hdr, 1, sizeof(hdr), kdc);
  84.  
  85.   if (strcmp(hdr+470, "Kodak DC120 ZOOM Digital Camera"))
  86.   {
  87.     fprintf(stderr, "Error: not a DC120 .kdc file\n");
  88.     return;
  89.   }
  90.  
  91.   if (hdr[707] != 7)
  92.   {
  93.     fprintf(stderr, "Error: not a compressed .kdc file\n");
  94.     return;
  95.   }
  96.  
  97.   /* Open the .jpg file */
  98.   strcpy(name, argv[1]);
  99.   strcat(name, ".jpg");
  100.   fprintf(stdout, "...writing %s...\n", name );
  101.   jpg = fopen(name, "wb");
  102.   if (!jpg)
  103.   {
  104.     fprintf(stderr,"Error: can't create %s\n",name);
  105.     return;
  106.   }
  107.  
  108.   /* Write out the .jpg header */
  109.   fwrite(jpghead,1,sizeof(jpghead),jpg);
  110.  
  111.   /* Discard the extra marker  (included already in jpghead) */
  112.   fgetc(kdc);
  113.   fgetc(kdc);
  114.  
  115.   /* Read data in pairs and exit if eof */
  116.   while(1)
  117.   {
  118.     int c1, c2;
  119.  
  120.     c1 = fgetc(kdc);
  121.     c2 = fgetc(kdc);
  122.  
  123.     if ((c1 < 0) || (c2 < 0))
  124.       break;
  125.  
  126.     fputc(c2, jpg);
  127.     fputc(c1, jpg);
  128.   }
  129.  
  130.   /* Close the .jpg and .kdc files */
  131.   fclose(jpg);
  132.   fclose(kdc);
  133. }
  134.  
  135.