source: trunk/packages/vizservers/vtkvis/TGAWriter.cpp @ 2217

Last change on this file since 2217 was 2100, checked in by ldelgass, 13 years ago

VTKVis server for 2D contour plots. Required modifications to vizservers build
to follow.

  • Property svn:eol-style set to native
File size: 2.8 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * Copyright (C) 2011, Purdue Research Foundation
4 *
5 * Author: Leif Delgass <ldelgass@purdue.edu>
6 */
7
8#include <cstdio>
9#include <cstdlib>
10#include <cstring>
11#include <cerrno>
12#include <sys/uio.h>
13
14#include <iostream>
15#include <fstream>
16
17#include "TGAWriter.h"
18#include "Trace.h"
19
20/**
21 * \brief Writes image command + data to supplied file descriptor.
22 *
23 * The image data must be supplied in BGR order with bottom to
24 * top scanline ordering.
25 */
26void
27Rappture::VtkVis::writeTGA(int fd, const char *cmdName, const unsigned char *data,
28                           int width, int height)
29{
30    TRACE("(%dx%d)\n", width, height);
31
32    size_t headerLength = 18;
33
34    char header[headerLength];
35    memset(header, 0, headerLength);
36    header[2] = (char)2;  // image type (2 = uncompressed true-color)
37    header[12] = (char)width;
38    header[13] = (char)(width >> 8);
39    header[14] = (char)height;
40    header[15] = (char)(height >> 8);
41    header[16] = (char)24; // bits per pixel
42
43    size_t dataLength = width * height * 3;
44
45    char command[200];
46    snprintf(command, sizeof(command), "%simage -type image -bytes %lu\n", cmdName,
47             (unsigned long)headerLength + dataLength);
48
49    size_t nRecs = 3;
50
51    struct iovec *iov;
52    iov = (struct iovec *)malloc(sizeof(struct iovec) * nRecs);
53
54    // Write the command, then the image header and data.
55    // Command
56    iov[0].iov_base = command;
57    iov[0].iov_len = strlen(command);
58    // Header of image data
59    iov[1].iov_base = header;
60    iov[1].iov_len = headerLength;
61    // Image data **must be BGR!**
62    iov[2].iov_base = const_cast<unsigned char *>(data);
63    iov[2].iov_len = dataLength;
64
65    if (writev(fd, iov, nRecs) < 0) {
66        ERROR("write failed: %s\n", strerror(errno));
67    }
68    free(iov);
69
70    TRACE("Leaving (%dx%d)\n", width, height);
71}
72
73/**
74 * \brief Writes image data to supplied file name
75 *
76 * The image data must be supplied in BGR order with bottom to
77 * top scanline ordering.
78 */
79void
80Rappture::VtkVis::writeTGAFile(const char *filename, const unsigned char *imgData,
81                               int width, int height)
82{
83    TRACE("%s (%dx%d)\n", filename, width, height);
84
85    std::ofstream outfile(filename, std::ios::out | std::ios::binary | std::ios::trunc);
86    char header[18];
87    memset(header, 0, 18);
88    header[2] = (char)2;  // image type (2 = uncompressed true-color)
89    header[12] = (char)width;
90    header[13] = (char)(width >> 8);
91    header[14] = (char)height;
92    header[15] = (char)(height >> 8);
93    header[16] = (char)24; // bits per pixel
94
95    outfile.write(header, sizeof(header));
96
97    // RGB -> BGR
98    for (int i = 0; i < width * height; i++) {
99        outfile << imgData[i*3+2]
100                << imgData[i*3+1]
101                << imgData[i*3];
102    }
103
104    outfile.close();
105}
Note: See TracBrowser for help on using the repository browser.