[Swift-commit] r6708 - in SwiftApps/modis: . src
wozniak at ci.uchicago.edu
wozniak at ci.uchicago.edu
Fri Aug 2 09:44:41 CDT 2013
Author: wozniak
Date: 2013-08-02 09:44:41 -0500 (Fri, 02 Aug 2013)
New Revision: 6708
Added:
SwiftApps/modis/src/rgb_adjust_color-main.c
SwiftApps/modis/src/rgb_adjust_color.h
Modified:
SwiftApps/modis/Makefile
SwiftApps/modis/src/rgb_adjust_color.c
Log:
Apparently working version
Modified: SwiftApps/modis/Makefile
===================================================================
--- SwiftApps/modis/Makefile 2013-08-02 14:43:41 UTC (rev 6707)
+++ SwiftApps/modis/Makefile 2013-08-02 14:44:41 UTC (rev 6708)
@@ -1,24 +1,33 @@
MODIS = lib/libmodis.so
-BINS = bin/rgb_histogram
+BINS = bin/rgb_histogram bin/rgb_adjust_color
all: $(MODIS) $(BINS)
-CFLAGS = -std=gnu99 -I . -fPIC
+CFLAGS = -g -std=gnu99 -fPIC -Wall -I .
# Include library objects that are not a main program file
SRCS = $(shell ls src/*.c | grep -v -- -main )
OBJS = $(patsubst %.c,%.o,$(SRCS))
+map = $(foreach a,$(2),$(call $(1),$(a)))
+get_main = src/$(patsubst bin/%,%,$(1))-main.c
+MAINS = $(call map,get_main,$(BINS))
+
+LIBS = -L lib -lmodis
+RPATHS = -Wl,-rpath -Wl,$(PWD)/lib
+LDFLAGS = $(LIBS) $(RPATHS)
+
lib:
mkdir lib
bin/rgb_histogram: src/rgb_histogram-main.o $(MODIS)
- gcc -o $(@) $(<) $(OBJS) \
- -L lib -lmodis \
- -Wl,-rpath -Wl,$(PWD)/lib
+ gcc -o $(@) $(<) $(OBJS) $(LDFLAGS)
+bin/rgb_adjust_color: src/rgb_adjust_color-main.o $(MODIS)
+ gcc -o $(@) $(<) $(OBJS) $(LDFLAGS)
+
$(MODIS): lib $(OBJS)
gcc -shared -o $(@) $(OBJS)
@@ -29,5 +38,6 @@
debug_build:
@echo "BINS: $(BINS)"
@echo "BIN_NAMES: $(BIN_NAMES)"
+ @echo "MAINS: $(MAINS)"
@echo "OBJS: $(OBJS)"
@echo "SRCS: $(SRCS)"
Added: SwiftApps/modis/src/rgb_adjust_color-main.c
===================================================================
--- SwiftApps/modis/src/rgb_adjust_color-main.c (rev 0)
+++ SwiftApps/modis/src/rgb_adjust_color-main.c 2013-08-02 14:44:41 UTC (rev 6708)
@@ -0,0 +1,98 @@
+
+/*
+ * rgb_adjust_color-main.c
+ *
+ * Created on: Aug 1, 2013
+ * Author: wozniak
+ */
+
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "src/rgb_adjust_color.h"
+
+static void usage(void)
+{
+ printf("usage: rbg_adjust_color input.rgb table.txt output.rgb\n");
+}
+
+static bool scan_translation(const char* translation_filename,
+ int* translation);
+
+/**
+ Usage: rgb_adjust_color input.rgb table.txt output.rgb
+*/
+int
+main(int argc, char* argv[])
+{
+ if (argc != 4)
+ {
+ usage();
+ return 1;
+ }
+
+ int translation[17];
+
+ bool result = scan_translation(argv[2], translation);
+ if (!result) return 1;
+
+ FILE* input_file = fopen(argv[1], "r");
+ if (input_file == NULL)
+ {
+ printf("could not open input file: %s\n", argv[1]);
+ return false;
+ }
+
+ FILE* output_file = fopen(argv[3], "w");
+ if (output_file == NULL)
+ {
+ printf("could not open output file: %s\n", argv[3]);
+ return false;
+ }
+
+ translate(input_file, translation, output_file);
+
+ return 0;
+}
+
+/**
+ * @param translation OUT The translation table
+ */
+static bool
+scan_translation(const char* translation_filename, int* translation)
+{
+ FILE* translation_file = fopen(translation_filename, "r");
+ if (translation_file == NULL)
+ {
+ printf("could not open translation table: %s\n",
+ translation_filename);
+ return false;
+ }
+
+ char buffer[64];
+ int key;
+ int value;
+ while (true)
+ {
+ char* result = fgets(buffer, 64, translation_file);
+ if (!result) break;
+ int n = sscanf(buffer, "#%x #%x", &key, &value);
+ if (n != 2)
+ {
+ printf("bad line in translation table: %s\n", buffer);
+ return false;
+ }
+ // printf("%x __ %x\n", key, value);
+ int k = key % 17;
+ // printf("k: %i\n", k);
+ unsigned char* p = (unsigned char*) &value;
+ unsigned char t = p[0];
+ // printf("p: %x %x %x %x\n", p[0], p[1], p[2], p[3]);
+ memcpy(&p[0], &p[2], sizeof(char));
+ memcpy(&p[2], &t, sizeof(char));
+ translation[k] = value;
+ }
+ printf("scan done\n");
+ return true;
+}
Modified: SwiftApps/modis/src/rgb_adjust_color.c
===================================================================
--- SwiftApps/modis/src/rgb_adjust_color.c 2013-08-02 14:43:41 UTC (rev 6707)
+++ SwiftApps/modis/src/rgb_adjust_color.c 2013-08-02 14:44:41 UTC (rev 6708)
@@ -1,35 +1,40 @@
-#!/usr/bin/perl
-# Usage: rgb_adjust_color.pl input.rgb table.txt output.rgb
+/*
+ * rgb_adjust_color.c
+ *
+ * Created on: Aug 1, 2013
+ * Author: wozniak
+ */
-my ($input_filename, $translation_table, $output_filename) = @ARGV;
-open(FILE_INPUT, "$input_filename") || die "Unable to open $input_filename!";
-open(FILE_OUTPUT, ">$output_filename") || die "Unable to create $output_filename";
+#include <string.h>
-# Read translation table into a hash
-my %tr_table = ();
-open(TRANSLATION_TABLE, "$translation_table") || die "Unable to open $translation_table";
-while(<TRANSLATION_TABLE>) {
- my ($from, $to) = split;
- $tr_table{$from} = $to;
-}
+#include "src/rgb_adjust_color.h"
-# Read data
-$/ = \3;
-while(<FILE_INPUT>) {
- my $hex = sprintf ("#%2.2X%2.2X%2.2X", unpack('C3', $_));
+bool
+translate(FILE* file, int* translation, FILE* output)
+{
+ unsigned char b[3];
- if(defined($tr_table{$hex})) {
- my $new_value = $tr_table{$hex};
- print FILE_OUTPUT pack('C3',
- hex(substr($new_value,1,2)),
- hex(substr($new_value,3,2)),
- hex(substr($new_value,5,2))
- );
- }
+ while (true)
+ {
+ int rc = fread(b, sizeof(b), 1, file);
+ if (rc == 0) break;
+ int k = 0;
+ char* t = (char*) &k;
+ memcpy(&t[0], &b[0], sizeof(char)*3);
+ int v;
+ if (k == 0xffffff)
+ v = 0xffffff;
+ else
+ v = translation[k%17];
+ char* p = (char*) &v;
+ // printf("%u %u %u\n", b[0], b[1], b[2]);
+ // printf("%x -> %x\n", k, v);
+ fwrite(&p[0], sizeof(char), 3, output);
+ }
- else { print FILE_OUTPUT $_; }
+ if (!feof(file))
+ return false;
+
+ return true;
}
-
-close(FILE_INPUT);
-close(FILE_OUTPUT);
Added: SwiftApps/modis/src/rgb_adjust_color.h
===================================================================
--- SwiftApps/modis/src/rgb_adjust_color.h (rev 0)
+++ SwiftApps/modis/src/rgb_adjust_color.h 2013-08-02 14:44:41 UTC (rev 6708)
@@ -0,0 +1,17 @@
+/*
+ * rgb_adjust_color.h
+ *
+ * Created on: Aug 1, 2013
+ * Author: wozniak
+ */
+
+#ifndef RGB_ADJUST_COLOR_H_
+#define RGB_ADJUST_COLOR_H_
+
+#include <stdbool.h>
+#include <stdio.h>
+
+bool
+translate(FILE* file, int* translation, FILE* output);
+
+#endif
More information about the Swift-commit
mailing list