File: image_genesis.c

package info (click to toggle)
libforms 1.2.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,192 kB
  • sloc: ansic: 98,419; sh: 11,156; makefile: 959
file content (156 lines) | stat: -rw-r--r-- 3,625 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 *  This file is part of the XForms library package.
 *
 *  XForms is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation; either version 2.1, or
 *  (at your option) any later version.
 *
 *  XForms 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with XForms.  If not, see <http://www.gnu.org/licenses/>.
 */


/*
 *  This file is part of the XForms library package.
 *  Copyright (c) 1998-2002  By T.C. Zhao
 *  All rights reserved.
 *
 *  but genesis format. Eventually dicom will
 *  be supported
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "include/forms.h"
#include "flimage.h"
#include "flimage_int.h"

#define IMG_MAGIC       0x494d4746

typedef struct
{
    int magic;
    int hdr_len;
    int width;
    int height;
    int depth;
    int compress;
    int windoww;
    int windowl;
} SPEC;


/***************************************
 ***************************************/

static int
GENESIS_identify( FILE * fp )
{
    char buf[ 4 ];
    size_t c;

    c = fread( buf, 1, 4, fp );
    rewind( fp );

    return c == 4 && strncmp( buf, "IMGF", 4 ) == 0;
}


/***************************************
 ***************************************/

static int
GENESIS_description( FL_IMAGE * im )
{
    SPEC *sp = fl_calloc( 1, sizeof *sp );
    FILE *fp = im->fpin;

    im->io_spec = sp;
    im->spec_size = sizeof *sp;

    rewind( fp );
    sp->magic    = fli_fget4MSBF( fp );
    sp->hdr_len  = fli_fget4MSBF( fp );
    sp->width    = fli_fget4MSBF( fp );
    sp->height   = fli_fget4MSBF( fp );
    sp->depth    = fli_fget4MSBF( fp );
    sp->compress = fli_fget4MSBF( fp );
    sp->windoww  = fli_fget4MSBF( fp );
    sp->windowl  = fli_fget4MSBF( fp );

    if ( sp->depth < 8 || sp->compress != 1 )
        return -1;

    im->type = sp->depth == 8 ? FL_IMAGE_GRAY : FL_IMAGE_GRAY16;
    im->gray_maxval = ( 1 << sp->depth ) - 1;
    im->w = sp->width;
    im->h = sp->height;

    return 0;
}


/***************************************
 * convert MSBF
 ***************************************/

static void
convert_msbf( unsigned short * pixels,
              int              npixels )
{
    unsigned short *p = pixels,
                   *ps = p + npixels;
    unsigned char *c = ( unsigned char * ) p;

    for ( ; p < ps; p++, c += 2 )
        *p = ( c[ 0 ] << 8 ) | c[ 1 ];
}


/***************************************
 ***************************************/

static int
GENESIS_load( FL_IMAGE * im )
{
    FILE *fp = im->fpin;
    SPEC *sp = im->io_spec;

    fseek( fp, sp->hdr_len, SEEK_SET );
    if ( fread( im->gray[ 0 ], sp->depth / 8, im->w * im->h, fp )
                                                 != ( size_t ) im->w * im->h )
        convert_msbf( im->gray[ 0 ], im->w * im->h );

    return 0;
}


/***************************************
 ***************************************/

void
flimage_enable_genesis( void )
{
    flimage_add_format( "GE Genesis", "genesis", "ge",
                        FL_IMAGE_GRAY16 | FL_IMAGE_GRAY,
                        GENESIS_identify,
                        GENESIS_description,
                        GENESIS_load,
                        0 );
}


/*
 * Local variables:
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */