Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added common stuff

  • Loading branch information...
commit 21a1f5d493561255146a35bb8def0e1c56e1dbf0 1 parent e723be6
@pkrumins pkrumins authored
Showing with 97 additions and 0 deletions.
  1. +67 −0 src/common.cc
  2. +30 −0 src/common.h
View
67 src/common.cc
@@ -0,0 +1,67 @@
+#include <cstdlib>
+#include <cassert>
+#include "common.h"
+
+using namespace v8;
+
+Handle<Value>
+VException(const char *msg) {
+ HandleScope scope;
+ return ThrowException(Exception::Error(String::New(msg)));
+}
+
+bool str_eq(const char *s1, const char *s2)
+{
+ return strcmp(s1, s2) == 0;
+}
+
+unsigned char *
+rgba_to_rgb(const unsigned char *rgba, int rgba_size)
+{
+ assert(rgba_size%4==0);
+
+ int rgb_size = rgba_size*3/4;
+ unsigned char *rgb = (unsigned char *)malloc(sizeof(*rgb)*rgb_size);
+ if (!rgb) return NULL;
+
+ for (int i=0,j=0; i<rgba_size; i+=4,j+=3) {
+ rgb[j] = rgba[i];
+ rgb[j+1] = rgba[i+1];
+ rgb[j+2] = rgba[i+2];
+ }
+ return rgb;
+}
+
+unsigned char *
+bgra_to_rgb(const unsigned char *bgra, int bgra_size)
+{
+ assert(bgra_size%4==0);
+
+ int rgb_size = bgra_size*3/4;
+ unsigned char *rgb = (unsigned char *)malloc(sizeof(*rgb)*rgb_size);
+ if (!rgb) return NULL;
+
+ for (int i=0,j=0; i<bgra_size; i+=4,j+=3) {
+ rgb[j] = bgra[i+2];
+ rgb[j+1] = bgra[i+1];
+ rgb[j+2] = bgra[i];
+ }
+ return rgb;
+}
+
+unsigned char *
+bgr_to_rgb(const unsigned char *bgr, int bgr_size)
+{
+ assert(bgr_size%3==0);
+
+ unsigned char *rgb = (unsigned char *)malloc(sizeof(*rgb)*bgr_size);
+ if (!rgb) return NULL;
+
+ for (int i=0; i<bgr_size; i+=3) {
+ rgb[i] = bgr[i+2];
+ rgb[i+1] = bgr[i+1];
+ rgb[i+2] = bgr[i];
+ }
+ return rgb;
+}
+
View
30 src/common.h
@@ -0,0 +1,30 @@
+#ifndef COMMON_H
+#define COMMON_H
+
+#include <node.h>
+#include <cstring>
+
+v8::Handle<v8::Value> VException(const char *msg);
+
+struct Point {
+ int x, y;
+ Point() {}
+ Point(int xx, int yy) : x(xx), y(yy) {}
+};
+
+struct Rect {
+ int x, y, w, h;
+ Rect() {}
+ Rect(int xx, int yy, int ww, int hh) : x(xx), y(yy), w(ww), h(hh) {}
+ bool isNull() { return x == 0 && y == 0 && w == 0 && h == 0; }
+};
+
+bool str_eq(const char *s1, const char *s2);
+unsigned char *rgba_to_rgb(const unsigned char *rgba, int rgba_size);
+unsigned char *bgra_to_rgb(const unsigned char *rgba, int bgra_size);
+unsigned char *bgr_to_rgb(const unsigned char *rgb, int rgb_size);
+
+typedef enum { BUF_RGB, BUF_BGR, BUF_RGBA, BUF_BGRA } buffer_type;
+
+#endif
+
Please sign in to comment.
Something went wrong with that request. Please try again.