/*! * pixReadMemGif() * * Input: data (const; gif-encoded) * size (of data) * Return: pix, or null on error * * Notes: * (1) Of course, we are cheating here -- writing the data out * to file and then reading it back in as a gif format. */ PIX * pixReadMemGif(const l_uint8 *cdata, size_t size) { char *tname; l_uint8 *data; PIX *pix; PROCNAME("pixReadMemGif"); if (!cdata) return (PIX *)ERROR_PTR("cdata not defined", procName, NULL); tname = genTempFilename("/tmp/", "junk_mem_gif.blah", 1); data = (l_uint8 *)cdata; /* we're really not going to change this */ arrayWrite(tname, "w", data, size); pix = pixRead(tname); FREE(tname); if (!pix) return (PIX *)ERROR_PTR("pix not read", procName, NULL); return pix; }
main(int argc, char **argv) { char buf[256]; size_t nbytes; l_int32 i, w, h, success, display; l_int32 format, bps, spp, iscmap, format2, w2, h2, bps2, spp2, iscmap2; l_uint8 *data; l_uint32 *data32, *data32r; BOX *box; FILE *fp; PIX *pixs, *pixt, *pixt2, *pixd; if (regTestSetup(argc, argv, &fp, &display, &success, NULL)) return 1; /* Test basic serialization/deserialization */ for (i = 0; i < nfiles; i++) { pixs = pixRead(filename[i]); /* Serialize to memory */ pixSerializeToMemory(pixs, &data32, &nbytes); /* Just for fun, write and read back from file */ arrayWrite("/tmp/array", "w", data32, nbytes); data32r = (l_uint32 *)arrayRead("/tmp/array", (l_int32 *)(&nbytes)); /* Deserialize */ pixd = pixDeserializeFromMemory(data32r, nbytes); regTestComparePix(fp, argv, pixs, pixd, i, &success); pixDestroy(&pixd); pixDestroy(&pixs); FREE(data32); FREE(data32r); } /* Test read/write fileio interface */ for (i = 0; i < nfiles; i++) { pixs = pixRead(filename[i]); pixGetDimensions(pixs, &w, &h, NULL); box = boxCreate(0, 0, L_MIN(150, w), L_MIN(150, h)); pixt = pixClipRectangle(pixs, box, NULL); boxDestroy(&box); snprintf(buf, sizeof(buf), "/tmp/pixs.%d", i); pixWrite(buf, pixt, IFF_SPIX); regTestCheckFile(fp, argv, buf, i, &success); pixt2 = pixRead(buf); regTestComparePix(fp, argv, pixt, pixt2, nfiles + i, &success); pixDestroy(&pixs); pixDestroy(&pixt); pixDestroy(&pixt2); } /* Test read header. Note that for rgb input, spp = 3, * but for 32 bpp spix, we set spp = 4. */ for (i = 0; i < nfiles; i++) { pixs = pixRead(filename[i]); pixWriteMem(&data, &nbytes, pixs, IFF_SPIX); pixReadHeader(filename[i], &format, &w, &h, &bps, &spp, &iscmap); pixReadHeaderMem(data, nbytes, &format2, &w2, &h2, &bps2, &spp2, &iscmap2); if (format2 != 16 || w != w2 || h != h2 || bps != bps2 || iscmap != iscmap2) { if (fp) fprintf(fp, "Failure comparing data"); else fprintf(stderr, "Failure comparing data"); success = FALSE; } pixDestroy(&pixs); FREE(data); } #if 0 /* Do timing */ for (i = 0; i < nfiles; i++) { pixs = pixRead(filename[i]); startTimer(); pixSerializeToMemory(pixs, &data32, &nbytes); pixd = pixDeserializeFromMemory(data32, nbytes); fprintf(stderr, "Time for %s: %7.3f sec\n", filename[i], stopTimer()); FREE(data32); pixDestroy(&pixs); pixDestroy(&pixd); } #endif regTestCleanup(argc, argv, fp, success, NULL); return 0; }
int main(){ int *a; char *b="Hello "; char *c= "World"; char *d; char *file1="e.bin"; char *file2="text.bin"; int size; int sizes[2]; int e[4][3]={{1,2,3},{4,5,6},{7,8,9},{10,11,12}}; int **f; int **g; //1 mark //write a function "allocate10" that allocates space for 10 integer using the parameter list //in other words the function is of VOID type, takes a parameter and allocates and assigns memory to that parameter //call the function to allocate and assign memory to a //use a loop in the function to assign a[0] to a[9] to integers 0 to 9 //print the values out one per line in the main program //To be clear - the memory is allocated and assigning values happens in the function //in the main function //free the memory in a allocate10(&a); for(int i=0;i<10;i++){ printf("%d\n",a[i]); } free(a); //1 mark //Write a function "joinStrings" takes as parameters 3 strings. It joins the first 2 together and puts the result in the third string //The function allocates memory for the third string using malloc //apply the function to strings b,c, and d. After the function is done d should have memory allocated to it and contain "Hello World" //the function should not assume the sizes of b or c - it needs to be general enough for any string //after calling the function using b,c,d as parameters print out d from the main function //free the memory in d joinStrings(b,c,&d); printf("%s\n",d); free(d); //1 mark //write a function "arrayWrite" that takes as parameters an array of the same type as e, the size of the first dimension, and a string variables, binaryFilename //the function "arrayWrite" writes the values of the the array (starting from array[0][0] and ending at array[size-1][2]) to the binaryFilename //apply the function to array e and file1 size=4; arrayWrite(e,size,file1); //1 mark //write a function "binaryIO" to take as a parameter two filenames //it opens the first file which is binary reads in sizeof(int) bytes at a time //it writes value and the value squared separated by a ',' one set of values per line i.e. //0,0 //1,1 //2,4 //etc. to a the second file // //run the function with parameters file1, file2 //so at the end of this there should be two new files binaryIO(file1,file2); //2 marks //malloc and assign memory for f as a pointer to pointers to integer style array of the same size as e //malloc and assign memory for g as a pointer to pointer to integer where you assign the pointers to a block of memory the size of e //write a function "arrayCopy" that that takes parameters of the same type as e, f, and g and a sizes array parameter //sizes is an array of the dimension sizes //use for loops to copy values of e to f and g inside the function //in main print out e, f and g 0 //in main free the memory for f //in main free the memory for g f=(int**) malloc(sizeof(int*)*4); for(int i=0;i<4;i++) f[i]=(int*) malloc(sizeof(int)*3); g=(int**) malloc(sizeof(int*)*4); g[0]=malloc(12*sizeof(int)); for(int i=1;i<4;i++) g[i]=g[i-1]+3; sizes[0]=4; sizes[1]=3; arrayCopy(e,f,g,sizes); for(int i=0;i<4;i++){ for(int j=0;j<3;j++){ printf("%d %d %d\n",e[i][j],f[i][j],g[i][j]); } } for(int i=0;i<4;i++) free(f[i]); free(f); f=0; free(g[0]); free(g); g=0; return 1; }