aa_context *aa_autoinit(__AA_CONST struct aa_hardware_params *params) { aa_context *context = NULL; int i = 0; char *t; while ((t = aa_getfirst(&aa_displayrecommended)) != NULL) { if (context == NULL) { for (i = 0; aa_drivers[i] != NULL; i++) { if (!strcmp(t, aa_drivers[i]->name) || !strcmp(t, aa_drivers[i]->shortname)) { context = aa_init(aa_drivers[i], params, NULL); break; } } if (aa_drivers[i] == NULL) printf("Driver %s unknown", t); free(t); } } i = 0; while (context == NULL) { if (aa_drivers[i] == NULL) return NULL; context = aa_init(aa_drivers[i], params, NULL); i++; } return (context); }
int main(int argc, char* argv[]) { if (argc < 3) { fprintf(stderr, "Usage: %s IN_FILE OUT_FILE", argv[0]); exit(1); } MagickWand* m_wand; // Initialize MagickWandGenesis(); m_wand = NewMagickWand(); // read image if (MagickReadImage(m_wand, argv[1]) == MagickFalse) { fprintf(stderr, "Cannot read image: %s\n", argv[1]); exit(1); } // resize MagickResizeImage(m_wand, WIDTH, HEIGHT, LanczosFilter, 1.0); // ready for using AAlib aa_context* c; aa_savedata save_data = { argv[2], &aa_text_format, NULL }; // Initialize AAlib c = aa_init(&save_d, &aa_defparams, (const void*) &save_data); if (c == NULL) { fprintf(stderr, "Cannot initialize AA-lib\n"); exit(1); } // record image data to AAlib image buffer PixelIterator* iter = NewPixelIterator(m_wand); PixelWand** pix; unsigned long num_wands; double h, s, l; int x, y; y = 0; while ((pix = PixelGetNextIteratorRow(iter, &num_wands)) != NULL) { for (x = 0; x < num_wands; ++x) { PixelGetHSL(pix[x], &h, &s, &l); aa_putpixel(c, x, y, 256*l); } y++; } // rendering ascii and print file aa_fastrender(c, 0, 0, aa_scrwidth(c), aa_scrheight(c)); aa_flush(c); // terminate aa_close(c); // finalize to finish if (m_wand) { m_wand = DestroyMagickWand(m_wand); } MagickWandTerminus(); return 0; }
status_t AalibTranslator::DerivedTranslate(BPositionIO *source, const translator_info *info, BMessage *ioExtension, uint32 outType, BPositionIO *target, int32 baseType) { if(baseType == 1 && outType == AALIB_TEXT_FORMAT) { BBitmap *originalbmp, *greyscalebmp; BRect bounds; int imgWidth; int imgHeight; int imgHalfWidth; int imgHalfHeight; aa_context *context; aa_renderparams *params; aa_palette palette; struct aa_hardware_params hwparams; // get the image originalbmp = BTranslationUtils::GetBitmap(source); if(originalbmp == NULL) { return B_ERROR; } // get the image size bounds = originalbmp->Bounds(); imgWidth = bounds.IntegerWidth()+1; imgHeight = bounds.IntegerHeight()+1; // convert the bitmap to greyscale greyscalebmp = new BBitmap(bounds, B_GRAY8); if(greyscalebmp->ImportBits(originalbmp) != B_OK) { return B_ERROR; } // get half the height and width, rounded up // aalib outputs half the height and width of the original if(imgWidth%2 == 1) imgHalfWidth = (imgWidth+1)/2; else imgHalfWidth = imgWidth/2; if(imgHeight%2 == 1) imgHalfHeight = (imgHeight+1)/2; else imgHalfHeight = imgHeight/2; // use some custom settings memcpy(&hwparams, &aa_defparams, sizeof(struct aa_hardware_params)); hwparams.font = NULL; // default font // output is half of original width and height hwparams.width = imgHalfWidth; hwparams.height = imgHalfHeight; // new aalib context // use mem_d (memory drive) as we will get the output ourselves context = aa_init(&mem_d, &hwparams, NULL); if(context == NULL) return B_ERROR; // we can't use memcpy, as the image width // might not be equal to the bytes per row /*memcpy(context->imagebuffer, greyscalebmp->Bits(), imgWidth*imgHeight);*/ // get the location of the bitmap bits, and the bytes per row unsigned char *bitsLocation = (unsigned char*)greyscalebmp->Bits(); int bytesPerRow = greyscalebmp->BytesPerRow(); for(int y=0; y<imgHeight; y++) { // for each row and column for(int x=0; x<imgWidth; x++) { // set the pixel // 255- is to invert the greyscale image aa_putpixel(context, x, y, 255-(bitsLocation[y*bytesPerRow+x])); } } // render the image params = aa_getrenderparams(); aa_render(context, params, 0, 0, imgWidth, imgHeight); for(int i=0; i<imgHalfHeight; i++) { // for each row if(i != 0) { // after first line, write newline target->Write("\n",1); } // output that line target->Write(context->textbuffer+i*imgHalfWidth,imgHalfWidth); } aa_close(context); free(originalbmp); delete greyscalebmp; delete originalbmp; return B_OK; } return B_NO_TRANSLATOR; }