示例#1
0
int main(){
    unsigned char buffer[BUF_SIZE] = {0};
    unsigned int picSize=0;
    unsigned int packNum=0;
    unsigned int packSize;
    unsigned short int checksum;
    unsigned int width, height;
    unsigned int i,j;
    unsigned char bytes[2];
    unsigned char image[160][120];

    int  fd = serialOpen("/dev/ttyUSB1", 115200);
    FILE* filefd = fopen("./image.ppm", "w");
    unsigned char* bufptr;

    sendShot(fd, RES_160X128);
    readImageDetails(fd, &picSize, &packNum);
    printf("size: %d\npackets count: %d\n", picSize, packNum);
    unsigned char* imgBuffer = (unsigned char*) malloc((picSize+1)*sizeof(unsigned char));
    readImage(fd, imgBuffer, picSize, packNum); 
    serialClose(fd);
    njInit();
    if (njDecode(imgBuffer, picSize)) {
        printf("Error decoding the input file.\n");
        return 1;
    }
    printf("size:\n%d %d\n", njGetWidth(), njGetHeight());
    fprintf(filefd, "P%d\n%d %d\n255\n", njIsColor() ? 6 : 5, njGetWidth(), njGetHeight());
    width = njGetWidth();
    height = njGetHeight();
    fwrite(njGetImage(), 1, njGetImageSize(), filefd);
    bufptr = njGetImage();
    for(i=0;i<width;i++){
        for(j=0;j<height;j++){
            image[i][j] = (char)((0.21 * (double)bufptr[(i+j*width)*3]) + 0.72 * (double)bufptr[(i+j*width)*3 + 1]  + 0.07 * (double)bufptr[(i+j*width)*3 + 2]);
        }
    }
    for(j=0;j<height;j++){
        for(i=0;i<width;i++){
            if(image[i][j]<63)
                printf("@");
            else if(image[i][j]<128)
                printf("*");
            else if(image[i][j]<191)
                printf("+");
            else
                printf("-");
        }
        printf("\n");
    }   
    fclose(filefd);
    njDone();
}
示例#2
0
int main(int argc, char* argv[]) {
    int size;
    char *buf;
    FILE *f;

    if (argc < 2) {
        printf("Usage: %s <input.jpg> [<output.ppm>]\n", argv[0]);
        return 2;
    }
    f = fopen(argv[1], "rb");
    if (!f) {
        printf("Error opening the input file.\n");
        return 1;
    }
    fseek(f, 0, SEEK_END);
    size = (int) ftell(f);
    buf = malloc(size);
    fseek(f, 0, SEEK_SET);
    size = (int) fread(buf, 1, size, f);
    fclose(f);

    njInit();
    if (njDecode(buf, size)) {
        printf("Error decoding the input file.\n");
        return 1;
    }

    f = fopen((argc > 2) ? argv[2] : (njIsColor() ? "nanojpeg_out.ppm" : "nanojpeg_out.pgm"), "wb");
    if (!f) {
        printf("Error opening the output file.\n");
        return 1;
    }
    fprintf(f, "P%d\n%d %d\n255\n", njIsColor() ? 6 : 5, njGetWidth(), njGetHeight());
    fwrite(njGetImage(), 1, njGetImageSize(), f);
    fclose(f);
    njDone();
    return 0;
}
示例#3
0
void mainCRTStartup(void) {
    char *argv[3], *buf = GetCommandLine();
    enum { S_WHITESPACE, S_PARAM, S_QUOTE } state = S_WHITESPACE;
    int argc, size, esize;
    HANDLE f;
    for (argc = 0;  (argc < 1022) && buf[argc];  ++argc)
        cmdline[argc] = buf[argc];
    cmdline[argc] = 0;
    for (buf = cmdline, argc = 0;  *buf;  ++buf) {
        switch (state) {
            case S_WHITESPACE:
                if (buf[0] == 32)
                    buf[0] = 0;
                else {
                    if (argc >= 3) ExitProcess(2);
                    if (buf[0] == 34) {
                        argv[argc++] = buf + 1;
                        state = S_QUOTE;
                    } else {
                        argv[argc++] = buf;
                        state = S_PARAM;
                    }
                }
                break;
            case S_PARAM:
                if (buf[0] == 32) {
                    buf[0] = 0;
                    state = S_WHITESPACE;
                } else if (buf[0] == 34) {
                    argv[argc - 1] = buf + 1;
                    state = S_QUOTE;
                }
                break;
            case S_QUOTE:
                if (buf[0] == 34) {
                    buf[0] = 0;
                    state = S_WHITESPACE;
                }
                break;
        }
    }
    if (argc < 2) ExitProcess(2);

    f = CreateFile(argv[1], GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
    if (f == INVALID_HANDLE_VALUE) ExitProcess(1);
    size = (int) SetFilePointer(f, 0, NULL, FILE_END);
    buf = LocalAlloc(LMEM_FIXED, size);
    if (!buf) ExitProcess(1);
    SetFilePointer(f, 0, NULL, FILE_BEGIN);
    ReadFile(f, buf, size, (LPDWORD) &esize, NULL);
    CloseHandle(f);

    njInit();
    if (njDecode(buf, esize)) ExitProcess(1);
    f = CreateFile((argc > 2) ? argv[2] : (njIsColor() ? "nanojpeg_out.ppm" : "nanojpeg_out.pgm"),
        GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
    if (f == INVALID_HANDLE_VALUE) ExitProcess(1);
    if (njIsColor()) header[1] = '6';
    putnum(&header[7], njGetWidth());
    putnum(&header[13], njGetHeight());
    WriteFile(f, header, 19, (LPDWORD) &esize, NULL);
    WriteFile(f, njGetImage(), njGetImageSize(), (LPDWORD) &esize, NULL);
    njDone();

    ExitProcess(0);
}