Alien-FreeImage

 view release on metacpan or  search on metacpan

src/Source/FreeImage/PluginPNM.cpp  view on Meta::CPAN

					break;

				case 24 :
					magic = 3;	// PPM file (RGB)
					break;

				default:
					return FALSE;	// Invalid bit depth
			}
			break;
		
		case FIT_UINT16:
			magic = 2;	// PGM file	(Greyscale)
			maxval = 65535;
			break;

		case FIT_RGB16:
			magic = 3;	// PPM file (RGB)
			maxval = 65535;
			break;

		default:
			return FALSE;
	}


	if (flags == PNM_SAVE_RAW)
		magic += 3;

	// Write the header info

	sprintf(buffer, "P%d\n%d %d\n", magic, width, height);
	io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);

	if (bpp != 1) {
		sprintf(buffer, "%d\n", maxval);
		io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
	}

	// Write the image data
	///////////////////////

	if(image_type == FIT_BITMAP) {
		switch(bpp)  {
			case 24 :            // 24-bit RGB, 3 bytes per pixel
			{
				if (flags == PNM_SAVE_RAW)  {
					for (y = 0; y < height; y++) {
						// write the scanline to disc
						BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);

						for (x = 0; x < width; x++) {
							io->write_proc(&bits[FI_RGBA_RED], 1, 1, handle);	// R
							io->write_proc(&bits[FI_RGBA_GREEN], 1, 1, handle);	// G
							io->write_proc(&bits[FI_RGBA_BLUE], 1, 1, handle);	// B

							bits += 3;
						}
					}
				} else {
					int length = 0;

					for (y = 0; y < height; y++) {
						// write the scanline to disc
						BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);
						
						for (x = 0; x < width; x++) {
							sprintf(buffer, "%3d %3d %3d ", bits[FI_RGBA_RED], bits[FI_RGBA_GREEN], bits[FI_RGBA_BLUE]);

							io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);

							length += 12;

							if(length > 58) {
								// No line should be longer than 70 characters
								sprintf(buffer, "\n");
								io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
								length = 0;
							}

							bits += 3;
						}					
					}

				}
			}
			break;

			case 8:		// 8-bit greyscale
			{
				if (flags == PNM_SAVE_RAW)  {
					for (y = 0; y < height; y++) {
						// write the scanline to disc
						BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);

						for (x = 0; x < width; x++) {
							io->write_proc(&bits[x], 1, 1, handle);
						}
					}
				} else {
					int length = 0;

					for (y = 0; y < height; y++) {
						// write the scanline to disc
						BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);

						for (x = 0; x < width; x++) {
							sprintf(buffer, "%3d ", bits[x]);

							io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);

							length += 4;

							if (length > 66) {
								// No line should be longer than 70 characters
								sprintf(buffer, "\n");
								io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
								length = 0;
							}
						}
					}
				}
			}
			break;

			case 1:		// 1-bit B & W
			{
				int color;

				if (flags == PNM_SAVE_RAW)  {
					for(y = 0; y < height; y++) {
						// write the scanline to disc
						BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);

						for(x = 0; x < (int)FreeImage_GetLine(dib); x++)
							io->write_proc(&bits[x], 1, 1, handle);
					}
				} else  {
					int length = 0;

					for (y = 0; y < height; y++) {
						// write the scanline to disc
						BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);

						for (x = 0; x < (int)FreeImage_GetLine(dib) * 8; x++)	{
							color = (bits[x>>3] & (0x80 >> (x & 0x07))) != 0;

							sprintf(buffer, "%c ", color ? '1':'0');

							io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);

							length += 2;

							if (length > 68) {
								// No line should be longer than 70 characters
								sprintf(buffer, "\n");
								io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
								length = 0;
							}
						}
					}
				}
			}
			
			break;
		}
	} // if(FIT_BITMAP)

	else if(image_type == FIT_UINT16) {		// 16-bit greyscale
		if (flags == PNM_SAVE_RAW)  {
			for (y = 0; y < height; y++) {
				// write the scanline to disc
				WORD *bits = (WORD*)FreeImage_GetScanLine(dib, height - 1 - y);

				for (x = 0; x < width; x++) {
					WriteWord(io, handle, bits[x]);
				}
			}
		} else {
			int length = 0;

			for (y = 0; y < height; y++) {
				// write the scanline to disc
				WORD *bits = (WORD*)FreeImage_GetScanLine(dib, height - 1 - y);

				for (x = 0; x < width; x++) {
					sprintf(buffer, "%5d ", bits[x]);

					io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);

					length += 6;

					if (length > 64) {
						// No line should be longer than 70 characters
						sprintf(buffer, "\n");
						io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
						length = 0;
					}
				}
			}
		}
	}

	else if(image_type == FIT_RGB16) {		// 48-bit RGB
		if (flags == PNM_SAVE_RAW)  {
			for (y = 0; y < height; y++) {
				// write the scanline to disc
				FIRGB16 *bits = (FIRGB16*)FreeImage_GetScanLine(dib, height - 1 - y);

				for (x = 0; x < width; x++) {
					WriteWord(io, handle, bits[x].red);		// R
					WriteWord(io, handle, bits[x].green);	// G
					WriteWord(io, handle, bits[x].blue);	// B
				}
			}
		} else {
			int length = 0;

			for (y = 0; y < height; y++) {
				// write the scanline to disc
				FIRGB16 *bits = (FIRGB16*)FreeImage_GetScanLine(dib, height - 1 - y);
				
				for (x = 0; x < width; x++) {
					sprintf(buffer, "%5d %5d %5d ", bits[x].red, bits[x].green, bits[x].blue);

					io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);

					length += 18;

					if(length > 52) {
						// No line should be longer than 70 characters
						sprintf(buffer, "\n");
						io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
						length = 0;
					}
				}					
			}

		}
	}

	return TRUE;
}

// ==========================================================
//   Init
// ==========================================================

void DLL_CALLCONV
InitPNM(Plugin *plugin, int format_id) {
	s_format_id = format_id;

	plugin->format_proc = Format;
	plugin->description_proc = Description;
	plugin->extension_proc = Extension;
	plugin->regexpr_proc = RegExpr;
	plugin->open_proc = NULL;
	plugin->close_proc = NULL;
	plugin->pagecount_proc = NULL;
	plugin->pagecapability_proc = NULL;
	plugin->load_proc = Load;
	plugin->save_proc = Save;
	plugin->validate_proc = Validate;
	plugin->mime_proc = MimeType;
	plugin->supports_export_bpp_proc = SupportsExportDepth;
	plugin->supports_export_type_proc = SupportsExportType;
	plugin->supports_icc_profiles_proc = NULL;
	plugin->supports_no_pixels_proc = SupportsNoPixels;
}



( run in 1.453 second using v1.01-cache-2.11-cpan-119454b85a5 )