// Creates an unboxed array of dimensions width * height (= size) // Note that height is num rows, width is num cols // returns a pointer to the unboxed array T UArray2_new(int height, int width, int size) { T uarray2 = malloc(sizeof(*uarray2)); //RAISE(Mem_Failed); uarray2 -> array = UArray_new(height, (sizeof(UArray_T*))); int i; for(i = 0; i < height; i++) { //UArray_T uarray = malloc(size); // necessary? // UArray_T uarray = UArray_new(width, size); UArray_T* row = UArray_at(uarray2 -> array, i); *row = UArray_new(width, size); int j; for(j = 0; j < width; j++) { int* elemp = UArray_at(*row, j); *elemp = 0; } } uarray2 -> height = height; uarray2 -> width = width; uarray2 -> size = size; return uarray2; }
T UArray2_new (int width, int height, int size) { UArray2_T array; NEW(array); if (width > 0 && height > 0) array->long_array = UArray_new(width*height, size); else array->long_array = UArray_new(0, size); return array; }
BStream *BStream_new(void) { int flipEndian; BStream *self = (BStream *)io_calloc(1, sizeof(BStream)); self->ba = UArray_new(); self->index = 0; self->ownsUArray = 1; self->tmp = UArray_new(); self->errorBa = UArray_new(); flipEndian = 0; self->typeBuf = (unsigned char *)io_calloc(1, 512); return self; }
void Image_makeRGBA(Image *self) { if (self->componentCount == 3) { //Image_addAlpha(self); //printf("converted component count from 3 to 4\n"); } else if (self->componentCount == 1) { UArray *outUArray = UArray_new(); UArray_setSize_(outUArray, 4 * self->width * self->height); uint8_t *outData = (uint8_t *)UArray_bytes(outUArray); uint8_t *inData = (uint8_t *)UArray_bytes(self->byteArray); size_t numPixels = self->width * self->height; size_t p1; size_t p2 = 0; for (p1 = 0; p1 < numPixels; p1 ++) { outData[p2] = inData[p1]; p2 ++; outData[p2] = inData[p1]; p2 ++; outData[p2] = inData[p1]; p2 ++; outData[p2] = 255; p2 ++; } UArray_copy_(self->byteArray, outUArray); UArray_free(outUArray); self->componentCount = 4; //printf("converted component count from 1 to 4\n"); } }
IoMP3Decoder *IoMP3Decoder_proto(void *state) { IoObject *self = IoObject_new(state); IoObject_tag_(self, IoMP3Decoder_newTag(state)); IoObject_setDataPointer_(self, calloc(1, sizeof(IoMP3DecoderData))); //DATA(self)->outSound = 0x0; DATA(self)->willProcessMessage = IoMessage_newWithName_label_(state, IOSYMBOL("willProcess"), IOSYMBOL("[MP3Decoder]")); DATA(self)->didProcessMessage = IoMessage_newWithName_label_(state, IOSYMBOL("didProcess"), IOSYMBOL("[MP3Decoder]")); DATA(self)->inputBuffer = IoSeq_new(state); DATA(self)->outputBuffer = IoSeq_new(state); DATA(self)->tmpInputBa = UArray_new(); IoState_registerProtoWithFunc_(state, self, IoMP3Decoder_proto); { IoMethodTable methodTable[] = { {"start", IoMP3Decoder_start}, {"stop", IoMP3Decoder_stop}, {"inputBuffer", IoMP3Decoder_inputBuffer}, {"outputBuffer", IoMP3Decoder_outputBuffer}, {NULL, NULL}, }; IoObject_addMethodTable_(self, methodTable); } return self; }
IOIMAGE_API IoObject *IoImage_filterGauss(IoImage *self, IoObject *locals, IoMessage *m) { /*doc Image filterGauss(sigma) Returns new image as a result of applying filter. Implements Gauss smoothing filtering with parameter sigma. */ double sigma = IoMessage_locals_doubleArgAt_(m, locals, 0); int filterSize = round(sigma * 2.5) * 2 + 1; UArray* filter = UArray_new(); UArray_setItemType_(filter, CTYPE_int8_t); UArray_setEncoding_(filter, CENCODING_NUMBER); UArray_setSize_(filter, filterSize * filterSize); int8_t *filterBytes = UArray_mutableBytes(filter); int x, y, x1, y1; for(y = 0; y < filterSize; y++) { y1 = y - filterSize / 2; for(x = 0; x < filterSize; x++) { x1 = x - filterSize / 2; filterBytes[x + y * filterSize] = exp(-(x1*x1 + y1*y1)/2/sigma) * filterSize * filterSize * 2; } } IoImage* toReturn = IoImage_newWithImage_(IOSTATE, Image_applyLinearFilter(DATA(self)->image, filterSize, filterSize, filter)); UArray_free(filter); return toReturn; }
AudioDevice *AudioDevice_new(void) { AudioDevice *self = calloc(1, sizeof(AudioDevice)); self->writeBuffer = UArray_new(); self->nextWriteBuffer = UArray_new(); self->readBuffer = UArray_new(); self->nextReadBuffer = UArray_new(); self->maxReadFrame = 4096 * 100; self->lockSleepMicroSeconds = 10; self->needsData = 1; AudioDevice_init(self); return self; }
IoAudioMixer *IoAudioMixer_rawClone(IoAudioMixer *proto) { IoObject *self = IoObject_rawClonePrimitive(proto); self->data = cpalloc(proto->data, sizeof(IoAudioMixerData)); DATA(self)->ioBuffer = IoSeq_new(IOSTATE); DATA(self)->buffer = IoSeq_rawUArray(DATA(self)->ioBuffer); DATA(proto)->mixBuffer = UArray_new(); DATA(self)->writeMessage = IoMessage_newWithName_label_(IOSTATE, IOSYMBOL("write"), IOSYMBOL("AudioMixer")); IoMessage_setCachedArg_to_(DATA(self)->writeMessage, 0, DATA(self)->ioBuffer); DATA(self)->nonBlockingWriteMessage = IoMessage_newWithName_label_(IOSTATE, IOSYMBOL("nonBlockingWrite"), IOSYMBOL("AudioMixer")); IoMessage_setCachedArg_to_(DATA(self)->nonBlockingWriteMessage, 0, DATA(self)->ioBuffer); DATA(self)->sounds = List_new(); DATA(self)->soundsToRemove = List_new(); DATA(self)->events = List_new(); DATA(self)->activeEvents = List_new(); DATA(self)->volume = DATA(self)->volume; DATA(self)->soundTouch = SoundTouch_init(); SoundTouch_setSampleRate(DATA(self)->soundTouch, 44100); SoundTouch_setChannels(DATA(self)->soundTouch, 2); DATA(self)->tempo = 1.0; IoState_addValue_(IOSTATE, self); return self; }
IO_METHOD(IoFile, contents) { /*doc File contents Returns contents of the file as a mutable Sequence of bytes. */ UArray *ba = UArray_new(); long result = -1; if (DATA(self)->stream == stdin) { result = UArray_readFromCStream_(ba, DATA(self)->stream); } else { result = UArray_readFromFilePath_(ba, IoSeq_rawUArray(DATA(self)->path)); } if (result != -1) { return IoSeq_newWithUArray_copy_(IOSTATE, ba, 0); } else { UArray_free(ba); IoState_error_(IOSTATE, m, "unable to read file '%s'", UTF8CSTRING(DATA(self)->path)); } return IONIL(self); }
IoObject *IoODEMass_parameters(IoODEMass *self, IoObject *locals, IoMessage *m) { // vector(theMass, cgx, cgy, cgz, I11, I22, I33, I12, I13, I23) UArray *u = UArray_new(); int i, j = 0; UArray_setItemType_(u, CTYPE_float32_t); UArray_setSize_(u, 10); UArray_at_putDouble_(u, j++, DATA(self)->mass); for(i=0; i < 3; i++) { UArray_at_putDouble_(u, j++, DATA(self)->c[i]); } // 0 1 2 3 4 5 6 7 8 9 10 11 // I == vector(I11, I12, I13, _, I12, I22, I23, _, I13, I23, I33, _) UArray_at_putDouble_(u, j++, DATA(self)->I[0 ]); // I11 UArray_at_putDouble_(u, j++, DATA(self)->I[5 ]); // I22 UArray_at_putDouble_(u, j++, DATA(self)->I[10]); // I33 UArray_at_putDouble_(u, j++, DATA(self)->I[1 ]); // I12 UArray_at_putDouble_(u, j++, DATA(self)->I[2 ]); // I13 UArray_at_putDouble_(u, j++, DATA(self)->I[6 ]); // I23 return IoSeq_newWithUArray_copy_(IOSTATE, u, 1); }
/* this function takes in the Y, Pb, Pr representation and outputs the 4 pixels in a UArray_T */ UArray_T CVC_to_rgb_pixels(CVC *YPbPr) { UArray_T r_array = UArray_new(PIX_PER_BLOCK, sizeof(struct Pnm_rgb)); assert(r_array != NULL); Pnm_rgb cur_pix; Pnm_rgb_float cur_pix_float = malloc(sizeof(struct Pnm_rgb_float)); for (int i = 0; i < PIX_PER_BLOCK; i++) { cur_pix = (Pnm_rgb)UArray_at(r_array, i); assert(cur_pix != NULL); cur_pix_float->red = get_R(YPbPr, i); cur_pix_float->green = get_G(YPbPr, i); cur_pix_float->blue = get_B(YPbPr, i); denormalize_pixel(cur_pix_float, DEFAULT_DENOMINATOR); /* we could have rounded this properly before assigning to integers, but we ran out of time. sorry! */ cur_pix->red = cur_pix_float->red; cur_pix->blue = cur_pix_float->blue; cur_pix->green = cur_pix_float->green; } free(cur_pix_float); return r_array; }
T UArray2_new (int width, int height, int size) { UArray_T unboxed = UArray_new(width*height, size); UArray2_T temp; NEW(temp); UArray2_rep_init(temp, width, height, size, unboxed); return temp; }
T UArray2_new (int width, int height, int size) { assert( (width >0) && (height > 0) && (size > 0)); UArray_T unboxed = UArray_new(width*height, size); UArray2_T temp; NEW(temp); UArray2_rep_init(temp, width, height, size, unboxed); return temp; }
PNGImage *PNGImage_new(void) { PNGImage *self = (PNGImage *)calloc(1, sizeof(PNGImage)); PNGImage_path_(self, ""); PNGImage_error_(self, ""); self->byteArray = UArray_new(); self->ownsBuffer = 1; return self; }
TIFFImage *TIFFImage_new(void) { TIFFImage *self = (TIFFImage *)calloc(1, sizeof(TIFFImage)); TIFFImage_path_(self, ""); TIFFImage_error_(self, ""); self->byteArray = UArray_new(); self->ownsBuffer = 1; TIFFSetErrorHandler(MyTIFFErrorHandler); return self; }
UArray2_T UArray2_new (int dim1, int dim2, size_t element_size) { assert((dim1 > 0) && (dim2 > 0)); assert(element_size > 0); int length = dim1 * dim2; UArray2_T new_array = malloc(sizeof(*new_array)); new_array->width = dim1; new_array->height = dim2; new_array->uarray = UArray_new(length, element_size); return new_array; }
IoSeq *IoSeq_proto(void *state) { IoObject *self = IoObject_new(state); IoObject_tag_(self, IoSeq_newTag(state)); IoObject_setDataPointer_(self, UArray_new()); IoState_registerProtoWithFunc_((IoState *)state, self, protoId); return self; }
/* * Creates a new segment of length given as a parameter * Parameters: sm - Segment manager struct * length - length of segment to create */ unsigned map_Segment(Segment_Manager sm, int length) { Segment segment = (Segment)UArray_new(length, sizeof(uint32_t)); for (int i=0; i<length; i++) { *((uint32_t *)UArray_at(segment, i)) = 0; } unsigned id = insert_Segment(sm, segment); return id; }
Image *Image_new(void) { Image *self = (Image *)io_calloc(1, sizeof(Image)); Image_path_(self, ""); Image_fileType_(self, ""); self->byteArray = UArray_new(); self->ownsUArray = 1; self->componentCount = 4; self->encodingQuality = 1.0; return self; }
static UArray_T get_new_registers() { UArray_T registers = UArray_new(8, sizeof(UM_word)); /* initialize each register to 0 */ for(int i=0; i<8; i++){ UM_word *rI = UArray_at(registers, i); *rI = 0; } return registers; }
UArray *Date_asString(const Date *self, const char *format) { UArray *u = UArray_new(); time_t t = self->tv.tv_sec; struct tm *tm = localtime(&t); // what about unicode formats? UArray_setSize_(u, 1024 + strlen(format)); strftime((char *)UArray_bytes(u), 1024, format, tm); UArray_setSize_(u, strlen((char *)UArray_bytes(u))); return u; }
IO_METHOD(IoSeq, asFixedSizeType) { /*doc Sequence asFixedSizeType Returns a new sequence with the receiver encoded in the minimal fixed width text encoding that it's characters can fit into (either, ascii, utf8, utf16 or utf32). */ UArray *out = UArray_new(); UArray_copy_(out, DATA(self)); UArray_convertToFixedSizeType(out); return IoSeq_newWithUArray_copy_(IOSTATE, out, 0); }
/* deletes the memory contained in a segment and replaces it with a new sequence */ bool clear_seg(Segment s, uint32_t ID) { TRY assert(s); UArray_T to_rmv = Seq_put(s, ID, UArray_new(0, sizeof(uint32_t))); UArray_free(&to_rmv); EXCEPT(Mem_Failed) return false; END_TRY; return true; }
IoMP3Decoder *IoMP3Decoder_rawClone(IoMP3Decoder *proto) { IoObject *self = IoObject_rawClonePrimitive(proto); IoObject_setDataPointer_(self, calloc(1, sizeof(IoMP3DecoderData))); DATA(self)->willProcessMessage = DATA(proto)->willProcessMessage; DATA(self)->didProcessMessage = DATA(proto)->didProcessMessage; DATA(self)->inputBuffer = IOCLONE(DATA(proto)->inputBuffer); DATA(self)->outputBuffer = IOCLONE(DATA(proto)->outputBuffer); DATA(self)->tmpInputBa = UArray_new(); IoState_addValue_(IOSTATE, self); return self; }
UArray *UArray_fromHexStringUArray(UArray *self) { size_t i, newSize = self->size / 2; UArray *ba = UArray_new(); UArray_setSize_(ba, newSize); for(i = 0; i < newSize; i ++) { int h = self->data[i*2]; int l = self->data[i*2+1]; ba->data[i] = (charFromHex(h)<<4) + charFromHex(l); } return ba; }
IoObject *IoFile_rawAsString(IoFile *self) { UArray *ba = UArray_new(); if (UArray_readFromFilePath_(ba, IoSeq_rawUArray(DATA(self)->path)) == 1) { return IoState_symbolWithUArray_copy_(IOSTATE, ba, 0); } else { UArray_free(ba); IoState_error_(IOSTATE, NULL, "unable to read file '%s'", UTF8CSTRING(DATA(self)->path)); } return IONIL(self); }
IOIMAGE_API IoObject *IoImage_filterMedian(IoImage *self, IoObject *locals, IoMessage *m) { /*doc Image filterMedian(filterSizeX, filterSizeY) Returns new image as a result of applying filter. */ int filterSizeX = IoMessage_locals_intArgAt_(m, locals, 0); int filterSizeY = IoMessage_locals_intArgAt_(m, locals, 1); UArray* filter = UArray_new(); UArray_setItemType_(filter, CTYPE_int8_t); UArray_setEncoding_(filter, CENCODING_NUMBER); UArray_setSize_(filter, filterSizeX * filterSizeY); memset(UArray_mutableBytes(filter), 1, filterSizeX * filterSizeY); IoImage* toReturn = IoImage_newWithImage_(IOSTATE, Image_applyWeightedMedianFilter(DATA(self)->image, filterSizeX, filterSizeY, filter)); UArray_free(filter); return toReturn; }
void create_UM() { NEW(um); um->registers = UArray_new(NUM_REGS, sizeof(uint32_t)); um->unmapped_IDs = Stack_new(); um->memory = Segments_new(1); assert(um->registers); assert(um->unmapped_IDs); assert(um->memory); registers_to_zero(um); um->instr_ctr = 0; }
IO_METHOD(IoFile, readLine) { /*doc File readLine Reads the next line of the file and returns it as a string without the return character. Returns Nil if the end of the file has been reached. */ //char *path = UTF8CSTRING(DATA(self)->path); // tmp for debugging IoFile_assertOpen(self, locals, m); if (feof(DATA(self)->stream) != 0) { clearerr(DATA(self)->stream); return IONIL(self); } else { UArray *ba = UArray_new(); int error; unsigned char didRead = UArray_readLineFromCStream_(ba, DATA(self)->stream); if (!didRead) { UArray_free(ba); return IONIL(self); } error = ferror(DATA(self)->stream); if (error != 0) { UArray_free(ba); clearerr(DATA(self)->stream); IoState_error_(IOSTATE, m, "error reading from file '%s'", UTF8CSTRING(DATA(self)->path)); return IONIL(self); } return IoSeq_newWithUArray_copy_(IOSTATE, ba, 0); /*return IoState_symbolWithUArray_copy_(IOSTATE, ba, 0);*/ } }
IoRegexMatches *IoRegexMatches_proto(void *state) { IoObject *self = IoObject_new(state); IoObject_tag_(self, IoRegexMatches_newTag(state)); IoObject_setDataPointer_(self, calloc(1, sizeof(IoRegexMatchesData))); DATA(self)->regex = IONIL(self); DATA(self)->string = IOSYMBOL(""); DATA(self)->captureArray = UArray_new(); UArray_setItemType_(DATA(self)->captureArray, CTYPE_uint32_t); IoState_registerProtoWithId_(state, self, protoId); { IoMethodTable methodTable[] = { {"setRegex", IoRegexMatches_setRegex}, {"regex", IoRegexMatches_regex}, {"setString", IoRegexMatches_setString}, {"string", IoRegexMatches_string}, {"setPosition", IoRegexMatches_setPosition}, {"position", IoRegexMatches_position}, {"setEndPosition", IoRegexMatches_setEndPosition}, {"endPosition", IoRegexMatches_endPosition}, {"next", IoRegexMatches_next}, {"anchored", IoRegexMatches_anchored}, {"allowEmptyMatches", IoRegexMatches_allowEmptyMatches}, {"disallowEmptyMatches", IoRegexMatches_disallowEmptyMatches}, {"allowsEmptyMatches", IoRegexMatches_allowsEmptyMatches}, {0, 0}, }; IoObject_addMethodTable_(self, methodTable); } return self; }