File: pngcp.c

package info (click to toggle)
pngtools 0.4-1.3
  • links: PTS
  • area: main
  • in suites: bullseye, buster, stretch
  • size: 1,044 kB
  • ctags: 48
  • sloc: ansic: 726; sh: 659; makefile: 8
file content (93 lines) | stat: -rw-r--r-- 2,576 bytes parent folder | download | duplicates (3)
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
#include <unistd.h>
#include <stdio.h>
#include <math.h>
#include "pngcp.h"

/******************************************************************************
DOCBOOK START

FUNCTION pngcp
PURPOSE create a new PNG file, having changed some attributes

SYNOPSIS START
pnginfo [-d <depth>] [-s <samples per pixel>] <input filename> <output filename>
SYNOPSIS END

DESCRIPTION START
The <command>pngcp</command> create a new PNG file using the image data from the input file. The output file will have the bitdepth and number of samples per pixel as specified on the command line. There are limits on what is a valid combination imposed by the PNG specification -- <command>pngcp</command> will inform you of invalid combinations.
</para>

<para>
Samples with more than eight bits are not correctly handled at the moment.
DESCRIPTION END

RETURNS Nothing

EXAMPLE START
%bash: pngcp toucan.png new.png
EXAMPLE END
SEEALSO libpng libtiff tiffcp pngchunkdesc pnginfo
DOCBOOK END
******************************************************************************/

void usage();

int main(int argc, char *argv[]){
  unsigned long width, height;
  int channels, targetchannels = -1, bitdepth, targetbitdepth = -1, optchar, i;
  char *input, *output;
  
  i = 1;
  while ((optchar = getopt (argc, argv, "d:s:")) != -1)
    {
      switch (optchar)
	{
	case 'd':
	  targetbitdepth = atoi(optarg);
	  i += 2;
	  break;
	  
	case 's':
	  targetchannels = atoi(optarg);
	  i += 2;
	  break;

	case '?':
	default:
	  usage ();
	  break;
	}
    }

  // Determine if we were given a filename on the command line
  if (argc < 2)
    usage ();

  // Colour depth is the number of bits per sample
  // Bit depth is the number of samples per pixel
  if((input = readimage(argv[i], &width, &height, &bitdepth, &channels)) == -1){
    fprintf(stderr, "Failed to read the input raster\n");
    exit(42);
  }
  if(targetbitdepth == -1) targetbitdepth = bitdepth;
  if(targetchannels == -1) targetchannels = channels;
  
  if((output = inflateraster(input, width, height, bitdepth, targetbitdepth,
			     channels, targetchannels)) == -1){
    fprintf(stderr, "Failed to inflate the raster to the requested size\n");
    exit(42);
  }

  // Now push the raster into the output file
  if(writeimage(argv[i + 1], width, height, targetbitdepth, targetchannels, output) < 0){
    fprintf(stderr, "Error writing the output file\n");
    exit(42);
  }
}

void
usage ()
{
  fprintf(stderr, "Usage: pngcp [-d <target bitdepth>] [-s <target samples per pixel>] <input filename> <output filename>\n");
  exit(1);
}