================================================================
 CCD Astrocamera format CGE for the CCDGALRY Software
================================================================

 Preamble
----------
The early CCD cameras were for astronomy buffs with a lot of
money to spare, monochrome and quite small. Image processing
software could hardly be found and it was even harder to bring
the money together to buy it. Some amateurs wrote dedicated
programs and saved the processed pictures in newly invented file
formats.

The CCDTOOLS [1] - a suite of astro CCD image processing 
software - were developed by myself in the early 90s. A DOS
application only, but quite comprehensiv, very fast, extremly
cheap (gratis) and thus rather popular.

Having collected a plethora of pictures, I needed an archiving
tool and with it came a new file format. The tool creates
thumbnails of the picture and accepts formatted text that
contains descriptions about the picture. CGE stands for
Ccd Gallery Encoded.


 The CGE format
----------------
The CGE file format consists of 3 parts: the text field, the
data header and the image data. The text field is 256 bytes,
the data header 5 and the image data is variable since data
compression is employed. The encoding type is a blend of Hewlett
Packards Run Length Encoding (RLE) as found in printer software
[2], and Run Length Limitation Encoding (RLLE) initially
developed by Mark Zachmann of ZSoft [3,4], later to be found in
Microsofts Paintbrush PCX image file format.

+----------------+--------------+-------------------------+
 256 bytes Text  5 bytes Head  n bytes RLCE image data 
+----------------+--------------+-------------------------+


 Text Field
------------
There are 4 different predefined formats. The file begins with a
format class byte, 254 characters and ends with an end of file
(EOF) marker code 26 (1A hex). The text part of a CGE file can
thus be displayed with the simple DOS command TYPE. It wont
mess up the screen with the unprintable image data.

The first byte in the file defines the format:

32 (20 hex) " ": Free format, 254 characters follow.

59 (3B hex) ";": 60 bytes describe the depository of the full
                 size picture, 2 bytes for a CR/LF sequence
                 (13, 10; 0Dh, 0Ah), three text fields with
                 62 bytes each and terminated by a CR/LF
                 sequence.

46 (2E hex) ".": 42 bytes describe the location where the full
                 sized picture can be found -- and a CR/LF
                 sequence. Five fields with 40 bytes plus a
                 CR/LF sequence each follow.

58 (3A hex) ":": This marks a database structure with 10 fields
                 of different sizes, each terminated by a CR/LF
                 sequence.
                 11 bytes for the disk name, 12 bytes for the
                 filename, 21 for the path, 46 for the place
                 were the picture was taken, 8 for the date, 5
                 for the time, 46 to describe the optical
                 system, 4 for the focal length in mm, 5 for the
                 exposure time in seconds and 76 for additional
                 comments.

A reader program should make use of the data structure and
present it accordingly. The last example could lead to the
following example mask to fill in the data.

     Disk name [...........]
      Filename [............]
      Filepath [.....................]
         Place [..............................................]
          Date [dd/mm/yy]
          Time [hh:mm]
Optical system [..............................................]
  Focal length [....]
 Exposure time [.....]
       Remarks [...............//.............................]


 Image Data
------------
The thumbnail picture has 55 lines with 64 columns each. The
pixel values are represented by 4 bits: 16 shades of gray.
Considering the first pixel as pixel 1, it is an odd pixel while
the second pixel is an even one. The odd pixel values are saved
in the high nibble of the byte and the even pixels in the low
one (left shifted). The pixels are saved from left to right, top
to bottom. Uncompressed image data is 64 x 55 / 2 = 1760 bytes.
The image data is compressed using RLCE.


 File Head
-----------
The file head starts right after the text field, at the 257th
byte in the file (counting from 0, byte 256 is the first header
byte).

Bytes 0 & 1: Integer value (Intel format, i.e. low-high)
             containing the file size.
Bytes 2 & 3: Integer value containing the number of pixel values
             in the file.
Byte 4:      First Counter.


 Run Length Count Encoding (RLCE)
----------------------------------
RLCE combines the advantages of RLE and RLLE while evading
their disadvantages.

Byte 4, the 5th byte in the file, is the first counter. Bits 0
thru 6 make up the counter. A value of 0 is prohibitted and
flags a corrupted file. Bit 7 is special.

If bit 7 is 0: If this is the very first counter (byte 4 in the
file), this means that all data following it are uncompressed
pixel values. If it is any other counter, bit 7 = 0 means that
the following number of bytes as is the value of bit 0 thru 6 of
this counter byte, must be copied. The next counter byte will
follow immediately after the number of bytes to be copied.

Bit 7 = 1 means that the following byte has to be replicated as
many times, as is the value of bit 0 thru 6 of this counter
byte. The byte after the byte following the counter is the next
counter.

File size is between 317 and 2021 bytes.


 REFERENCES
------------
[1] Wernli, Hans-Ruedi; Die CCD Astrokamera fr den Amateur,
    1995, Birkhuser. ISBN 3-7643-5218-3.

[2] Hewlett Packard, Deskjet 500/500C Technical Reference
    Manual, February 1992, Part Number C2114-90008.

[3] Pohl, Martin; Eriksdottar Holger; Die wunderbare Welt der
    Grafikformate; 1991, Wolfram's, ISBN 3-925328-05-x.

[4] Born, Gnter; Referenzhandbuch Dateiformate, 1992,
    Addison-Wesley, ISBN 3-89319-446-0.


 Author
--------
hans-rudolf.wernli@bluewin.ch
http://mypage.bluewin.ch/horo/
05. August 2002
