<commit_before>/*
 *  MATLABFind
 *  A Diamond application for interoperating with MATLAB
 *  Version 1
 *
 *  Copyright (c) 2006-2011 Carnegie Mellon University
 *  All Rights Reserved.
 *
 *  This software is distributed under the terms of the Eclipse Public
 *  License, Version 1.0 which can be found in the file named LICENSE.
 *  ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS SOFTWARE CONSTITUTES
 *  RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT
 */

#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <limits.h>
#include <glib.h>
#include <stdbool.h>
#include <assert.h>
#include <dlfcn.h>

#include "lib_filter.h"
#include "quick_tar.h"
#include "matlab-compat.h"

#ifndef MATLAB_EXE_PATH
#error Must specify MATLAB_EXE_PATH as compilation flag
#endif


static const int diamond_bytes_per_pixel = 4;
static const int matlab_bytes_per_pixel = 3;

#define matlab_img_name "image"

struct filter_instance {
  gchar *eval_matlab_cmd;
  Engine *eng;
  bool is_codec;
  struct mm mm;
};

static Engine *open_engine_in_src_dir(struct mm *mm, const char *src_dir_name)
{
   Engine *ret;
   char current_path[PATH_MAX];

   if (getcwd(current_path, sizeof(current_path)) == NULL) {
      return NULL;
   }

   if (chdir(src_dir_name) < 0) {
      return NULL;
   }

   // try to not have MATLAB screw up with LS_COLORS or other things
   clearenv();

   // set PATH so Intel's Math Kernel Library used by MATLAB doesn't crash
   // when loading mkl.cfg (used by POLYFIT and others)
   setenv("PATH", "/bin:/usr/bin", 0);

   printf("Trying to do an engOpen\n");
   ret = mm->engOpen("i386 " MATLAB_EXE_PATH);
   printf("engOpen done\n");

   if (chdir(current_path) < 0) {
      return NULL;
   }

   return ret;
}

static void populate_image_data(int img_height, int img_width, 
		const unsigned char * diamond_buf,
		unsigned char * matlab_buf)
{
   /* MATLAB arranges image data in a strange way */
   int img_row, img_col, i;
   for (img_row = 0; img_row < img_height; img_row++) {
      for (img_col = 0; img_col < img_width; img_col++) {
	 for (i = 0; i < matlab_bytes_per_pixel; i++) {
	    matlab_buf[img_row + img_col * img_height + 
		       img_height * img_width * i] =
	       diamond_buf[img_col * diamond_bytes_per_pixel +
			   img_row * diamond_bytes_per_pixel * img_width + i];
	 }
      }
   }
}

static void populate_rgbimage(struct mm *mm, Engine *eng,
			      mxArray *matrix, lf_obj_handle_t ohandle) {
  // convert the image into a normalized RGB
  mm->engPutVariable(eng, matlab_img_name, matrix);
  mm->engEvalString(eng, matlab_img_name " = im2uint8(" matlab_img_name ")");
<commit_msg>Have the filter complain if engOpen() fails due to missing /bin/csh<commit_after>/*
 *  MATLABFind
 *  A Diamond application for interoperating with MATLAB
 *  Version 1
 *
 *  Copyright (c) 2006-2011 Carnegie Mellon University
 *  All Rights Reserved.
 *
 *  This software is distributed under the terms of the Eclipse Public
 *  License, Version 1.0 which can be found in the file named LICENSE.
 *  ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS SOFTWARE CONSTITUTES
 *  RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT
 */

#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <limits.h>
#include <glib.h>
#include <stdbool.h>
#include <assert.h>
#include <dlfcn.h>

#include "lib_filter.h"
#include "quick_tar.h"
#include "matlab-compat.h"

#ifndef MATLAB_EXE_PATH
#error Must specify MATLAB_EXE_PATH as compilation flag
#endif


static const int diamond_bytes_per_pixel = 4;
static const int matlab_bytes_per_pixel = 3;

#define matlab_img_name "image"

struct filter_instance {
  gchar *eval_matlab_cmd;
  Engine *eng;
  bool is_codec;
  struct mm mm;
};

static Engine *open_engine_in_src_dir(struct mm *mm, const char *src_dir_name)
{
   Engine *ret;
   char current_path[PATH_MAX];

   if (getcwd(current_path, sizeof(current_path)) == NULL) {
      return NULL;
   }

   if (chdir(src_dir_name) < 0) {
      return NULL;
   }

   // try to not have MATLAB screw up with LS_COLORS or other things
   clearenv();

   // set PATH so Intel's Math Kernel Library used by MATLAB doesn't crash
   // when loading mkl.cfg (used by POLYFIT and others)
   setenv("PATH", "/bin:/usr/bin", 0);

   printf("Trying to do an engOpen\n");
   ret = mm->engOpen("i386 " MATLAB_EXE_PATH);
   printf("engOpen done\n");

   if (chdir(current_path) < 0) {
      return NULL;
   }

   if (ret == NULL && !g_file_test("/bin/csh", G_FILE_TEST_EXISTS)) {
      // Helpful debugging hint: engOpen() runs matlab via csh
      printf("engOpen failed: missing /bin/csh\n");
   }

   return ret;
}

static void populate_image_data(int img_height, int img_width, 
		const unsigned char * diamond_buf,
		unsigned char * matlab_buf)
{
   /* MATLAB arranges image data in a strange way */
   int img_row, img_col, i;
   for (img_row = 0; img_row < img_height; img_row++) {
      for (img_col = 0; img_col < img_width; img_col++) {
	 for (i = 0; i < matlab_bytes_per_pixel; i++) {
	    matlab_buf[img_row + img_col * img_height + 
		       img_height * img_width * i] =
	       diamond_buf[img_col * diamond_bytes_per_pixel +
			   img_row * diamond_bytes_per_pixel * img_width + i];
	 }
      }
   }
}

static void populate_rgbimage(struct mm *mm, Engine *eng,
			      mxArray *matrix, lf_obj_handle_t ohandle) {
  // convert the image into a normalized RGB
  mm->engPutVariable(eng, matlab_img_name, matrix);
  mm->engEvalString(eng, matlab_img_name " = im2uint8(" matlab_img_name ")");

{'fraction_non_alphanumeric': 0.10937773882559158, 'fraction_numerical': 0.008413672217353198, 'mean_word_length': 3.6655764513491413, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '750022', 'n_tokens_mistral': 2151, 'n_tokens_neox': 2013, 'n_words': 585}