[Swift-commit] r6699 - in SwiftApps/modis: . src

wozniak at ci.uchicago.edu wozniak at ci.uchicago.edu
Thu Aug 1 14:55:42 CDT 2013


Author: wozniak
Date: 2013-08-01 14:55:42 -0500 (Thu, 01 Aug 2013)
New Revision: 6699

Added:
   SwiftApps/modis/Makefile
   SwiftApps/modis/src/rgb_histogram-main.c
   SwiftApps/modis/src/rgb_histogram.h
Modified:
   SwiftApps/modis/src/rgb_histogram.c
Log:
Working C version of rgb_histogram


Added: SwiftApps/modis/Makefile
===================================================================
--- SwiftApps/modis/Makefile	                        (rev 0)
+++ SwiftApps/modis/Makefile	2013-08-01 19:55:42 UTC (rev 6699)
@@ -0,0 +1,33 @@
+
+MODIS = lib/libmodis.so
+
+BINS = bin/rgb_histogram
+
+all: $(MODIS) $(BINS)
+
+CFLAGS = -std=gnu99 -I . -fPIC
+
+# Include library objects that are not a main program file
+SRCS = $(shell ls src/*.c | grep -v -- -main )
+OBJS = $(patsubst %.c,%.o,$(SRCS))
+
+lib:
+	mkdir lib
+
+bin/rgb_histogram: src/rgb_histogram-main.o $(MODIS)
+	gcc -o $(@) $(<) $(OBJS) \
+		-L lib -lmodis \
+		-Wl,-rpath -Wl,$(PWD)/lib
+
+$(MODIS): lib $(OBJS)
+	gcc -shared -o $(@) $(OBJS)
+
+clean:
+	rm -fv $(BINS) $(OBJS) $(MODIS)
+
+# Use this when patsubst get confusing
+debug_build:
+	@echo "BINS:      $(BINS)"
+	@echo "BIN_NAMES: $(BIN_NAMES)"
+	@echo "OBJS:      $(OBJS)"
+	@echo "SRCS:      $(SRCS)"

Added: SwiftApps/modis/src/rgb_histogram-main.c
===================================================================
--- SwiftApps/modis/src/rgb_histogram-main.c	                        (rev 0)
+++ SwiftApps/modis/src/rgb_histogram-main.c	2013-08-01 19:55:42 UTC (rev 6699)
@@ -0,0 +1,58 @@
+/*
+ * rgb_histogram-main.c
+ *
+ *  Created on: Aug 1, 2013
+ *      Author: wozniak
+ */
+
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "src/rgb_histogram.h"
+
+static void usage(void)
+{
+  printf("usage: rbg_histogram myfile.rgb\n");
+}
+
+/**
+  Input to this program should be a raw, greyscale RGB file
+  Usage: rgb_histogram.pl myfile.rgb
+*/
+int
+main(int argc, char* argv[])
+{
+  if (argc != 2)
+  {
+    usage();
+    return 1;
+  }
+
+  int histogram[256];
+
+  char* filename = argv[1];
+  FILE* file = fopen(filename, "r");
+  if (file == NULL)
+  {
+    printf("could not open: %s\n", filename);
+    return 1;
+  }
+
+  bool result = rgb_histogram(file, histogram);
+  if (!result)
+  {
+    printf("error reading file: %s\n", filename);
+    return 1;
+  }
+
+  for (int i = 0; i < 256; i++)
+  {
+     if (histogram[i] == 0) continue;
+     printf("%d %d %02x\n", histogram[i], i, i);
+  }
+
+  fclose(file);
+
+  return 0;
+}

Modified: SwiftApps/modis/src/rgb_histogram.c
===================================================================
--- SwiftApps/modis/src/rgb_histogram.c	2013-08-01 18:55:18 UTC (rev 6698)
+++ SwiftApps/modis/src/rgb_histogram.c	2013-08-01 19:55:42 UTC (rev 6699)
@@ -1,23 +1,26 @@
-#!/usr/bin/perl -w
 
-# Input to this program should be a raw, greyscale RGB file
-# Usage: rgb_histogram.pl myfile.rgb
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
 
-my $image_filename = shift;
-open(IMAGEFILE, "$image_filename") || die "Unable to open $image_filename!\n";
-binmode IMAGEFILE;
+#include "src/rgb_histogram.h"
 
-my @pixelcount;
-foreach my $count (0..255) { $pixelcount[$count] = 0; }
+bool
+rgb_histogram(FILE* file, int* histogram)
+{
+  memset(histogram, '\0', 256*sizeof(int));
 
-# Read values, three bytes at a time
-$/ = \3; 
-foreach(<IMAGEFILE>) {
-   $pixelcount[unpack('C', $_)]++;
-}
-close(IMAGEFILE);
+  unsigned char b[3];
 
-foreach my $count (0..255) {
-   if($pixelcount[$count] == 0) { next; }
-   printf("%d %d %02x\n", $pixelcount[$count], $count, $count);
+  while (true)
+  {
+    int rc = fread(b, sizeof(char), 3, file);
+    if (rc == 0) break;
+    histogram[b[0]]++;
+  }
+
+  if (!feof(file))
+    return false;
+
+  return true;
 }

Added: SwiftApps/modis/src/rgb_histogram.h
===================================================================
--- SwiftApps/modis/src/rgb_histogram.h	                        (rev 0)
+++ SwiftApps/modis/src/rgb_histogram.h	2013-08-01 19:55:42 UTC (rev 6699)
@@ -0,0 +1,20 @@
+/*
+ * rgb_histogram.h
+ *
+ *  Created on: Aug 1, 2013
+ *      Author: wozniak
+ */
+
+#ifndef RGB_HISTOGRAM_H
+#define RGB_HISTOGRAM_H
+
+#include <stdbool.h>
+
+/**
+   @param histogram OUT histogram[color]=<count of that color>
+                        Size of histogram is 256
+   @return True; false on error
+ */
+bool rgb_histogram(FILE* file, int* histogram);
+
+#endif




More information about the Swift-commit mailing list