cctw  0.2.1
cctwtcltiff.cpp
Go to the documentation of this file.
1 
2 /*
3  * cctwtcltiff.cpp
4  *
5  * Created on: Oct 29, 2013
6  * Author: wozniak
7  */
8 
9 #include <assert.h>
10 #include <inttypes.h>
11 #include <iostream>
12 #include <stdlib.h>
13 
14 #include <tiffio.h>
15 #include <tiffio.hxx>
16 #include <tcl.h>
17 
18 #include "cctwtcltiff.h"
19 
20 using namespace std;
21 
22 #define UNUSED __attribute__((unused))
23 
24 static bool tiff_read(char *filename,
25  void **output, uint32 *output_length)
26 {
27  TIFF *tif = TIFFOpen(filename, "r");
28  if (tif == NULL)
29  return false;
30 
31  int width, height;
32 
33  TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
34  TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
35  cout << "wh: " << width << " " << height << endl;
36 
37  int npixels = width * height;
38  int length = npixels * sizeof (uint32);
39  uint32* raster = (uint32*) malloc(length);
40  assert(raster != NULL);
41 
42  int rc = TIFFReadRGBAImage(tif, width, height, raster, 0);
43  assert(rc == 1);
44 
45  TIFFClose(tif);
46 
47  *output = raster;
48  *output_length = length;
49  return true;
50 }
51 
52 int Cctwtcltiff_Read_Cmd(UNUSED ClientData clientData,
53  UNUSED Tcl_Interp *interp,
54  int objc, Tcl_Obj *const objv[])
55 {
56  assert(objc == 4);
57 
58  char *filename = Tcl_GetString(objv[1]);
59  Tcl_Obj *pointer = objv[2];
60  Tcl_Obj *length = objv[3];
61 
62  void *output;
63  uint32 bytes;
64  bool rc = tiff_read(filename, &output, &bytes);
65  if (!rc)
66  return TCL_ERROR;
67 
68  Tcl_ObjSetVar2(interp, pointer, NULL, Tcl_NewLongObj((long)output), 0);
69  Tcl_ObjSetVar2(interp, length, NULL, Tcl_NewIntObj((int)bytes), 0);
70 
71  return TCL_OK;
72 }
#define UNUSED
Definition: cctwtcltiff.cpp:22
static bool tiff_read(char *filename, void **output, uint32 *output_length)
Definition: cctwtcltiff.cpp:24
int Cctwtcltiff_Read_Cmd(UNUSED ClientData clientData, UNUSED Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
Definition: cctwtcltiff.cpp:52