/// <summary> /// Converts the media in a valid format for this player and returns /// a valid resource, if there are any. /// </summary> /// <remarks> /// Checks, which resource belongs to this player, change the size /// or format, if needed, and creates a MediaResource. /// </remarks> /// <returns>MediaResource with the correct media or null.</returns> /// <param name="media">Media.</param> MediaResource ConvertMedia(Media media) { MediaResource res = null; // Are there any resources if (media.Resources.Count < 1) { return(null); } // Get the last good media resource that could be found foreach (MediaResource mr in media.Resources) { if (_mediaFormats.Contains(mr.Type) && mr.Type.IsImage() && media.Resources[0].Type.IsImage() && (mr == media.Resources[0] || mr.Directives.Contains("wfplayer") || mr.Filename.ToLower().Contains("wfplayer"))) { res = mr; } if (_mediaFormats.Contains(mr.Type) && mr.Type.IsSound() && media.Resources[0].Type.IsSound() && (mr == media.Resources[0] || mr.Directives.Contains("wfplayer") || mr.Filename.ToLower().Contains("wfplayer"))) { res = mr; } } if (res == null) { return(null); } // Create MemoryStream MediaResource result = new MediaResource(); result.Filename = res.Filename; result.Directives = res.Directives; result.Type = res.Type; result.Data = res.Data; // Now remove all resources, because we don't need them anymore media.Resources = null; return(result); }
/// <summary> /// Converts the media in a valid format for this player and returns /// a valid resource, if there are any. /// </summary> /// <remarks> /// Checks, which resource belongs to this player, change the size /// or format, if needed, and creates a MediaResource. /// </remarks> /// <returns>MediaResource with the correct media or null.</returns> /// <param name="media">Media.</param> MediaResource ConvertMedia(Media media) { MediaResource res = null; // Are there any resources if (media.Resources.Count < 1) return null; // Get the last good media resource that could be found foreach(MediaResource mr in media.Resources) { if (_mediaFormats.Contains(mr.Type) && mr.Type.IsImage() && media.Resources[0].Type.IsImage() && (mr == media.Resources[0] || mr.Directives.Contains("xmarksthespot") || mr.Filename.ToLower().Contains("xmarksthespot"))) res = mr; if (_mediaFormats.Contains(mr.Type) && mr.Type.IsSound() && media.Resources[0].Type.IsSound() && (mr == media.Resources[0] || mr.Directives.Contains("xmarksthespot") || mr.Filename.ToLower().Contains("xmarksthespot"))) res = mr; } if (res == null) return null; // Create MemoryStream MediaResource result = new MediaResource(); result.Filename = res.Filename; result.Directives = res.Directives; result.Type = res.Type; result.Data = res.Data; // Now remove all resources, because we don't need them anymore media.Resources = null; return result; }
/// <summary> /// Extracts the media resources from Lua. /// </summary> /// <returns>The media resources.</returns> /// <param name="resources">LuaTable with resources.</param> static List<MediaResource> ExtractMediaResources (LuaTable resources) { List<MediaResource> result = new List<MediaResource>(); // Do it for each entry of the Lua table foreach (KeyValuePair<LuaValue, LuaValue> pair in resources) { // Get table for this resource LuaTable resTable = (LuaTable)pair.Value; // Create a new media resource object MediaResource mediaResource = new MediaResource (); // Extract table entries mediaResource.Filename = resTable["Filename"].ToString(); string type = resTable["Type"].ToString(); if (String.IsNullOrEmpty(type) && !String.IsNullOrEmpty(mediaResource.Filename) && Path.HasExtension(mediaResource.Filename)) type = Path.GetExtension(mediaResource.Filename); switch(type.ToLower()) { case "bmp": mediaResource.Type = MediaType.BMP; break; case "jpg": mediaResource.Type = MediaType.JPG; break; case "png": mediaResource.Type = MediaType.PNG; break; case "gif": mediaResource.Type = MediaType.GIF; break; case "wav": mediaResource.Type = MediaType.WAV; break; case "mp3": mediaResource.Type = MediaType.MP3; break; case "fdl": mediaResource.Type = MediaType.FDL; break; case "snd": mediaResource.Type = MediaType.SND; break; case "ogg": mediaResource.Type = MediaType.OGG; break; case "swf": mediaResource.Type = MediaType.SWF; break; case "txt": mediaResource.Type = MediaType.TXT; break; } // Get directives for this media resource entry mediaResource.Directives = resTable ["Directives"] is LuaBoolean ? null : ExtractDirectives ((LuaTable)resTable ["Directives"]); // Save new media resource result.Add (mediaResource); } return result; }
/// <summary> /// Converts the media in a valid format for this player and returns /// a valid resource, if there are any. /// </summary> /// <remarks> /// Checks, which resource belongs to this player, change the size /// or format, if needed, and creates a MediaResource. /// </remarks> /// <returns>MediaResource with the correct media or null.</returns> /// <param name="media">Media.</param> MediaResource ConvertMedia(Media media) { MediaResource res = null; // Are there any resources if (media.Resources.Count < 1) { return(null); } // Get the last good media resource that could be found foreach (MediaResource mr in media.Resources) { if (_mediaFormats.Contains(mr.Type) && mr.Type.IsImage() && media.Resources[0].Type.IsImage() && (mr == media.Resources[0] || mr.Directives.Contains(_mediaSelector) || mr.Filename.ToLower().Contains(_mediaSelector))) { res = mr; } if (_mediaFormats.Contains(mr.Type) && mr.Type.IsSound() && media.Resources[0].Type.IsSound() && (mr == media.Resources[0] || mr.Directives.Contains(_mediaSelector) || mr.Filename.ToLower().Contains(_mediaSelector))) { res = mr; } } if (res == null) { return(null); } // Create MediaResurce MediaResource result = new MediaResource(); result.Filename = res.Filename; result.Directives = res.Directives; if (res.Type.IsImage()) { // Image Image imgLoader; Image img; using (MemoryStream ims = new MemoryStream(res.Data)) { imgLoader = Image.FromStream(ims); img = new Bitmap(imgLoader); // Do special things with the image (resize, bit depth, ...) if (!res.Directives.Contains("noresize") && img.Width > _maxImageWidth) { img = ResizeImage(img, _maxImageWidth); } // Garmin can only handle jpg using (MemoryStream oms = new MemoryStream()) { img.Save(oms, _jpegCodec, _encParams); result.Type = MediaType.JPG; result.Data = oms.ToArray(); } } result.Type = MediaType.JPG; } if (res.Type.IsSound()) { result.Type = res.Type; result.Data = res.Data; } // Now remove all resources, because we don't need them anymore media.Resources = null; return(result); }
/// <summary> /// Extracts the media resources from Lua. /// </summary> /// <returns>The media resources.</returns> /// <param name="resources">LuaTable with resources.</param> static List <MediaResource> ExtractMediaResources(LuaTable resources) { List <MediaResource> result = new List <MediaResource>(); // Do it for each entry of the Lua table foreach (KeyValuePair <LuaValue, LuaValue> pair in resources) { // Get table for this resource LuaTable resTable = (LuaTable)pair.Value; // Create a new media resource object MediaResource mediaResource = new MediaResource(); // Extract table entries mediaResource.Filename = resTable["Filename"].ToString(); string type = resTable["Type"].ToString(); if (String.IsNullOrEmpty(type) && !String.IsNullOrEmpty(mediaResource.Filename) && Path.HasExtension(mediaResource.Filename)) { type = Path.GetExtension(mediaResource.Filename); } switch (type.ToLower()) { case "bmp": mediaResource.Type = MediaType.BMP; break; case "jpg": mediaResource.Type = MediaType.JPG; break; case "png": mediaResource.Type = MediaType.PNG; break; case "gif": mediaResource.Type = MediaType.GIF; break; case "wav": mediaResource.Type = MediaType.WAV; break; case "mp3": mediaResource.Type = MediaType.MP3; break; case "fdl": mediaResource.Type = MediaType.FDL; break; case "snd": mediaResource.Type = MediaType.SND; break; case "ogg": mediaResource.Type = MediaType.OGG; break; case "swf": mediaResource.Type = MediaType.SWF; break; case "txt": mediaResource.Type = MediaType.TXT; break; } // Get directives for this media resource entry mediaResource.Directives = resTable ["Directives"] is LuaBoolean ? null : ExtractDirectives((LuaTable)resTable ["Directives"]); // Save new media resource result.Add(mediaResource); } return(result); }
/// <summary> /// Extract table entries for this resource entry of resources. /// </summary> /// <param name="expr">Expression, which is found in the chunk.</param> /// <returns>A media resource object with type, filename and directives. </returns> private MediaResource getMediaResource ( TableConstructorExpr expr ) { MediaResource result = new MediaResource (); foreach ( TableConstructorStringKeyExpr entry in expr.EntryList ) { switch ( entry.Key ) { case "Type": result.Type = (MediaFormat) Enum.Parse ( typeof ( MediaFormat ), getExpressionAsString ( entry.Value ), true ); break; case "Filename": result.Filename = getExpressionAsString ( entry.Value ); break; case "Directives": foreach ( StringExpr str in ( (TableConstructorExpr) entry.Value ).EntryList ) result.Directives.Add ( str.Value ); break; } } return result; }
/// <summary> /// Converts the media in a valid format for this player and returns /// a valid resource, if there are any. /// </summary> /// <remarks> /// Checks, which resource belongs to this player, change the size /// or format, if needed, and creates a MediaResource. /// </remarks> /// <returns>MediaResource with the correct media or null.</returns> /// <param name="media">Media.</param> MediaResource ConvertMedia(Media media) { MediaResource res = null; // Are there any resources if (media.Resources.Count < 1) return null; // Get the last good media resource that could be found foreach(MediaResource mr in media.Resources) { if (_mediaFormats.Contains(mr.Type) && mr.Type.IsImage() && media.Resources[0].Type.IsImage() && (mr == media.Resources[0] || mr.Directives.Contains("garmin") || mr.Filename.ToLower().Contains("garmin"))) res = mr; if (_mediaFormats.Contains(mr.Type) && mr.Type.IsSound() && media.Resources[0].Type.IsSound() && (mr == media.Resources[0] || mr.Type == MediaType.FDL || mr.Directives.Contains("garmin") || mr.Filename.ToLower().Contains("garmin"))) res = mr; } if (res == null) return null; // Create MemoryStream MediaResource result = new MediaResource(); result.Filename = res.Filename; result.Directives = res.Directives; if (res.Type.IsImage()) { // Image Image imgLoader; Image img; using(MemoryStream ims = new MemoryStream(res.Data)) { imgLoader = Image.FromStream(ims); img = new Bitmap(imgLoader); // Do special things with the image (resize, bit depth, ...) if (!res.Directives.Contains("noresize") && img.Width > 230) img = ResizeImage(img, 230); // Garmin can only handle jpg using(MemoryStream oms = new MemoryStream()) { img.Save(oms, _jpegCodec, _encParams); result.Data = oms.ToArray(); } } result.Type = MediaType.JPG; } else { // Sound if (res.Type == MediaType.FDL) { result.Type = MediaType.FDL; result.Data = res.Data; } else { result = null; } } // Now remove all resources, because we don't need them anymore media.Resources = null; return result; }
public MemoryStream Create(Cartridge cartridge, string username, long userId, string completionCode) { // Create memory stream to write gwc file MemoryStream stream = new MemoryStream(); BinaryWriter output = new BinaryWriter(stream); long savePosition; // Write gwc signature output.Write(new byte[7] { 0x02, 0x0a, 0x43, 0x41, 0x52, 0x54, 0x00 }); // Write number of images output.Write(GetShort((short)(cartridge.Medias.Count + 1))); // Write media table for (int i = 0; i <= cartridge.Medias.Count; i++) { output.Write(GetShort((short)i)); output.Write(GetInt((int)0)); } // Write size of header output.Write(GetInt(0)); // Save position, where header starts long startHeader = output.BaseStream.Position; // Write header // Write position output.Write(GetDouble((float)cartridge.Latitude)); output.Write(GetDouble((float)cartridge.Longitude)); output.Write(GetDouble((float)cartridge.Altitude)); // Write creation date // TODO: Replace with creation date string date = cartridge.CreateDate; output.Write(GetLong(0)); // Write poster and icon index output.Write(GetShort((short)cartridge.Poster)); output.Write(GetShort((short)cartridge.Icon)); // Write cartridge type output.Write(GetAscii(cartridge.Activity)); // Write player data output.Write(GetAscii(username)); output.Write(GetLong(userId)); // Write cartridge relevant information output.Write(GetAscii(cartridge.Name)); output.Write(GetAscii(cartridge.Id)); output.Write(GetAscii(cartridge.Description)); output.Write(GetAscii(cartridge.StartingLocationDescription)); output.Write(GetAscii(cartridge.Version)); output.Write(GetAscii(cartridge.Author)); output.Write(GetAscii(cartridge.Company)); output.Write(GetAscii(cartridge.TargetDevice)); // Write CompletionCode length output.Write(GetInt(completionCode.Length + 1)); // Write CompletionCode output.Write(GetAscii(completionCode)); // Save position for later writing savePosition = output.BaseStream.Position; // Goto header length position and save the length output.BaseStream.Position = startHeader - 4; output.Write(GetInt((int)(savePosition - startHeader))); output.BaseStream.Position = savePosition; // Write Lua binary code WriteToMediaTable(output, 0); output.Write(GetInt((int)cartridge.Chunk.Length)); output.Write(cartridge.Chunk); // Now save all media files for (short i = 0; i < cartridge.Medias.Count; i++) { // Write position for media table WriteToMediaTable(output, (short)(i + 1)); // Check media for right entry MediaResource mr = cartridge.Medias[i].Resource; // Save media if (mr == null) { // No valid media file is found in resource list output.Write((byte)0); } else { // Valid file found, so save type, length and bytes output.Write((byte)1); output.Write(GetInt((int)mr.Type)); output.Write(GetInt((int)mr.Data.Length)); output.Write(mr.Data); } } // Go to begining of memory stream stream.Position = 0; return(stream); }