OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
imageExample.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 
4 #include <imageutils.h>
5 #include <png.h>
6 
7 void write_png_file(char* filename, int width, int height, uint8_t* image_data,
8  int num_colors, uint8_t* palette) {
9 
10  FILE* outfp = fopen(filename, "w");
11  if (!outfp) {
12  fprintf(stderr, "-E- Unable to open file %s.\n", filename);
13  exit(EXIT_FAILURE);
14  }
15 
16  png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
17  NULL, NULL, NULL);
18  if (!png_ptr) {
19  fprintf(stderr, "-E- Unable to create PNG write structure.\n");
20  exit(EXIT_FAILURE);
21  }
22 
23  png_infop info_ptr = png_create_info_struct(png_ptr);
24  if (!info_ptr) {
25  png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
26  fprintf(stderr, "-E- Unable to create PNG info structure.\n");
27  exit(EXIT_FAILURE);
28  }
29  if (setjmp(png_jmpbuf(png_ptr))) {
30  png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
31  fprintf(stderr, "-E- Unable to call PNG setjmp().\n");
32  exit(EXIT_FAILURE);
33  }
34  png_init_io(png_ptr, outfp);
35 
36  // write the header
37  png_set_IHDR(png_ptr, info_ptr, width, height,
38  8, PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
39  PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
40 
41  // set the color palette
42  png_set_PLTE(png_ptr, info_ptr, (png_const_colorp) palette, num_colors);
43  png_write_info(png_ptr, info_ptr);
44 
45  // write lines
46  int y;
47  uint8_t* ptr = image_data;
48  for (y = 0; y < height; y++) {
49  png_write_row(png_ptr, (png_bytep) ptr);
50  ptr += width;
51  }
52 
53  // end writing
54  png_write_end(png_ptr, info_ptr);
55  png_destroy_write_struct(&png_ptr, &info_ptr);
56 
57  fclose(outfp);
58 }
59 
60 int main(int c, char *v[]) {
61  if (c < 3) {
62  fprintf(stderr, "usage: %s ppm_file n_colors\n", v[0]);
63  return 0;
64  }
65 
66  int numColors = atoi(v[2]) ? : 16; /* GCC extension */
67 
68  img_rgb_t* im = img_read_ppm(v[1]);
69  if (!im) {
70  printf("could not read PPM file %s\n", v[1]);
71  exit(EXIT_FAILURE);
72  }
73 
74  // allocate output image and color palette
75  uint8_t* palette = (uint8_t*) malloc(numColors * 3);
76  uint8_t* out_image = (uint8_t*) malloc(im->w * im->h);
77 
78  img_color_palette_quantization(im, numColors, palette, out_image);
79 
80  write_png_file("out.png", im->w, im->h, out_image, numColors, palette);
81 
82  // color_quant(im, numColors, 0);
83  // write_ppm(im, "out.ppm");
84 
85 
86  free(im);
87 
88  return 0;
89 }
#define NULL
Definition: decode_rs.h:63
int main(int c, char *v[])
Definition: imageExample.c:60
int32_t im
Definition: atrem_corl1.h:161
void write_png_file(char *filename, int width, int height, uint8_t *image_data, int num_colors, uint8_t *palette)
Definition: imageExample.c:7
char filename[FILENAME_MAX]
Definition: atrem_corl1.h:122
img_rgb_t * img_read_ppm(char *filename)
Definition: color_quant.c:75
void img_color_palette_quantization(img_rgb_t *in_image, int num_colors, uint8_t *palette, uint8_t *out_image)
Definition: color_quant.c:300
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude resolving resolving GSFcd00179 Corrected handling of fill values for[Sensor|Solar][Zenith|Azimuth] resolving MODxl01751 Changed to validate LUT version against a value retrieved from the resolving MODxl02056 Changed to calculate Solar Diffuser angles without adjustment for estimated post launch changes in the MODIS orientation relative to incidentally resolving defects MODxl01766 Also resolves MODxl01947 Changed to ignore fill values in SCI_ABNORM and SCI_STATE rather than treating them as resolving MODxl01780 Changed to use spacecraft ancillary data to recognise when the mirror encoder data is being set by side A or side B and to change calculations accordingly This removes the need for seperate LUTs for Side A and Side B data it makes the new LUTs incompatible with older versions of the and vice versa Also resolves MODxl01685 A more robust GRing algorithm is being which will create a non default GRing anytime there s even a single geolocated pixel in a granule Removed obsolete messages from seed as required for compatibility with version of the SDP toolkit Corrected test output file names to end in per delivery and then split off a new MYD_PR03 pcf file for Aqua Added AssociatedPlatformInstrumentSensor to the inventory metadata in MOD01 mcf and MOD03 mcf Created new versions named MYD01 mcf and MYD03 where AssociatedPlatformShortName is rather than Terra The program itself has been changed to read the Satellite Instrument validate it against the input L1A and LUT and to use it determine the correct files to retrieve the ephemeris and attitude data from Changed to produce a LocalGranuleID starting with MYD03 if run on Aqua data Added the Scan Type file attribute to the Geolocation copied from the L1A and attitude_angels to radians rather than degrees The accumulation of Cumulated gflags was moved from GEO_validate_earth_location c to GEO_locate_one_scan c
Definition: HISTORY.txt:464